-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai.php
74 lines (68 loc) · 2.36 KB
/
openai.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
class OpenAI
{
private $apiKey;
private $apiBaseUrl = 'https://api.openai.com/';
public function connect($apiKey)
{
$this->apiKey = $apiKey;
}
public function generateText($prompt, $model, $numResponses = 1)
{
$url = $this->apiBaseUrl . 'v1/models/' . $model . '/completions';
$data = [
'prompt' => $prompt,
'max_tokens' => 256, // or 2048?
'top_p' => 1,
'n' => $numResponses,
'stop' => '.',
];
$response = $this->makeRequest($url, $data);
return $this->processGenerateTextResponse($response);
}
public function answerQuestion($question, $model)
{
$url = $this->apiBaseUrl . 'v1/models/' . $model . '/answer';
$data = [
'question' => $question,
];
$response = $this->makeRequest($url, $data);
return $this->processAnswerQuestionResponse($response);
}
private function makeRequest($url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $this->apiKey,
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
private function processGenerateTextResponse($response)
{
$responseArray = json_decode($response, true);
if (isset($responseArray['completions']) && is_array($responseArray['completions'])) {
$completions = [];
foreach ($responseArray['completions'] as $completion) {
$completions[] = $completion['text'];
}
return $completions;
}
return 'Error: ' . $responseArray['error'];
}
private function processAnswerQuestionResponse($response)
{
$responseArray = json_decode($response, true);
if (isset($responseArray['answer']) && is_string($responseArray['answer'])) {
return $responseArray['answer'];
}
return 'Error: ' . $responseArray['error'];
}
}
?>