-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathChatGPTClass.php
51 lines (43 loc) · 1.53 KB
/
ChatGPTClass.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
class ChatGPT {
private $endpoint;
private $apiKey;
private $languageModel;
private $systemMessage;
public function __construct($apiKey, $languageModel, $systemMessage, $endpoint = 'https://api.openai.com/v1/') {
$this->endpoint = $endpoint;
$this->apiKey = $apiKey;
$this->languageModel = $languageModel;
$this->systemMessage = $systemMessage;
}
public function sendMessage($question, $sessionId = null) {
$headers = [
'Authorization: Bearer ' . $this->apiKey,
'Content-Type: application/json',
];
$data = [
'messages' => [
['role' => 'system', 'content' => $this->systemMessage],
['role' => 'user', 'content' => $question]
],
'model' => $this->languageModel,
];
if ($sessionId) {
$data['session_id'] = $sessionId;
}
$ch = curl_init($this->endpoint . 'chat/completions');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
throw new Exception("Curl request error: $error");
}
curl_close($ch);
return $response;
}
}
?>