-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathChatGPT.php
101 lines (80 loc) · 3.74 KB
/
ChatGPT.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!-- Simple ChatGPT Class that enables both text and image prompt
to use this class in another file just import it and call one of the 2 functions createTextRequest() or generateImage() with your prompt (or options)
Code Example:
include_once('ChatGPT.php'); // include class from folder
$ai = new ChatGPT(); // initialize class object
echo $ai->generateImage('a cat on a post lamp')['data'] ?? 'ERROR!'; // print the image URL or error text
echo $ai->createTextRequest('what is the weather in Romania?')['data'] ?? 'ERROR!'; // print the text response or error text -->
<?php
class ChatGPT
{
private $API_KEY = "ADD_YOUR_API_KEY_HERE";
private $textURL = "https://api.openai.com/v1/completions";
private $imageURL = "https://api.openai.com/v1/images/generations";
public $curl;
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);
$headers = array(
"Content-Type: application/json",
"Authorization: Bearer $this->API_KEY"
);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
}
/**
* Generates a text response based on the given prompt using the specified parameters.
*
* @param string $prompt The prompt for generating the text response.
* @param string $model The GPT-3 model to use for text generation.
* @param float $temperature The temperature parameter for controlling randomness (default: 0.7).
* @param int $maxTokens The maximum number of tokens in the generated text (default: 1000).
* @return array An array containing 'data' and 'error' keys, representing the generated text and any errors.
*/
public function createTextRequest($prompt, $model = 'text-davinci-003', $temperature = 0.7, $maxTokens = 1000)
{
curl_reset($this->curl);
$this->initialize('text');
$data["model"] = $model;
$data["prompt"] = $prompt;
$data["temperature"] = $temperature;
$data["max_tokens"] = $maxTokens;
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($this->curl);
$response = json_decode($response, true);
$output['data'] = $response['choices'][0]['text'] ?? null;
$output['error'] = $response['error']['code'] ?? null;
return $output;
}
/**
* Generates an image URL based on the given prompt and parameters.
*
* @param string $prompt The prompt for generating the image URL.
* @param string $imageSize The desired image size (default: '512x512').
* @param int $numberOfImages The number of images to generate (default: 1).
* @return array An array containing ['data'] and ['error'] keys, representing the generated image URL and any errors.
*/
public function generateImage($prompt, $imageSize = '512x512', $numberOfImages = 1)
{
curl_reset($this->curl);
$this->initialize('image');
$data["prompt"] = $prompt;
$data["n"] = $numberOfImages;
$data["size"] = $imageSize;
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($this->curl);
$response = json_decode($response, true);
$output['data'] = $response['data'][0]['url'] ?? null;
$output['error'] = $response['error']['code'] ?? null;
return $output;
}
}