-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai.php
51 lines (45 loc) · 1.47 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
<?php
include 'http.class.php';
class openai {
const APPKEY = '';
private $_http;
public function __construct()
{
$this->_http = new http();
}
public function chatCompletions($prompt, $functions = '', $model = '')
{
$model = isset($model) && !empty($model) ? $model : 'gpt-3.5-turbo';
$body = [
'model' => $model,
'messages' => $prompt,
'temperature' => 0.8,
];
if (isset($functions) && !empty($functions)) {
$body['functions'] = $functions;
$body['function_call'] = 'auto';
}
$requestParam = json_encode($body);
$url = 'https://api.openai.com/v1/chat/completions';
$this->_http->setHeader('Content-Type', 'application/json');
$this->_http->setHeader('Authorization', 'Bearer ' .self::APPKEY);
$retStr = $this->_http->setUrl($url)->setData($requestParam)->request('post', true);
#var_dump($this->_http->getHttpInfo());
return $retStr;
}
public function embeddings($input, $model = '')
{
$model = isset($model) && !empty($model) ? $model : 'text-embedding-ada-002';
$body = [
'model' => $model,
'input' => $input
];
$requestParam = json_encode($body);
$url = 'https://api.openai.com/v1/embeddings';
$this->_http->setHeader('Content-Type', 'application/json');
$this->_http->setHeader('Authorization', 'Bearer ' .self::APPKEY);
$retStr = $this->_http->setUrl($url)->setData($requestParam)->request('post', true);
#var_dump($this->_http->getHttpInfo());
return $retStr;
}
}