forked from Radulepy/PHP-ChatGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatgpt.php
More file actions
78 lines (61 loc) · 2.56 KB
/
Copy pathchatgpt.php
File metadata and controls
78 lines (61 loc) · 2.56 KB
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
75
76
77
78
<?php
// using https://github.com/Radulepy/PHP-ChatGPT
class ChatGPT
{
private $API_KEY = "";
private $textURL = "https://api.openai.com/v1/completions";
private $imageURL = "https://api.openai.com/v1/images/generations";
public $curl; // create cURL object
public $data = []; // data request array
public function __construct()
{
$this->curl = curl_init();
}
public function initialize($requestType = "text" || "image")
{
$this->curl = curl_init();
if ($requestType === 'image')
curl_setopt($this->curl, CURLOPT_URL, $this->imageURL);
if ($requestType === 'text')
curl_setopt($this->curl, CURLOPT_URL, $this->textURL);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_POST, true);
// load api key from env file
$env = parse_ini_file('.env');
$this->API_KEY = $env['apikey'];
$headers = array(
"Content-Type: application/json",
"Authorization: Bearer $this->API_KEY"
);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
}
// returns text
public function createTextRequest($prompt, $model = 'text-davinci-003', $temperature = 0.5, $maxTokens = 1000)
{
curl_reset($this->curl);
$this->initialize('text');
$this->data["model"] = $model;
$this->data["prompt"] = $prompt;
$this->data["temperature"] = $temperature;
$this->data["max_tokens"] = $maxTokens;
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($this->data));
$response = curl_exec($this->curl);
$response = json_decode($response, true);
//return $response['choices'][0]['text'] ?? -1; // return text or -1 if error
return $response['choices'][0]['text'] ?? $response; // return text or -1 if error
}
// returns URL with the image
public function generateImage($prompt, $imageSize = '512x512', $numberOfImages = 1)
{
curl_reset($this->curl);
$this->initialize('image');
$this->data["prompt"] = $prompt;
$this->data["n"] = $numberOfImages;
$this->data["size"] = $imageSize;
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($this->data));
$response = curl_exec($this->curl);
$response = json_decode($response, true);
//return $response['data'][0]['url'] ?? -1; //return the first url or -1 if error
return $response['data'][0]['url'] ?? $response; //return the first url or -1 if error
}
}