├── ChatGPT.php
├── README.md
└── index.php
/ChatGPT.php:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 | curl = curl_init();
24 | }
25 |
26 | public function initialize($requestType = "text" || "image")
27 | {
28 | $this->curl = curl_init();
29 |
30 | if ($requestType === 'image')
31 | curl_setopt($this->curl, CURLOPT_URL, $this->imageURL);
32 | if ($requestType === 'text')
33 | curl_setopt($this->curl, CURLOPT_URL, $this->textURL);
34 |
35 | curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
36 | curl_setopt($this->curl, CURLOPT_POST, true);
37 |
38 | $headers = array(
39 | "Content-Type: application/json",
40 | "Authorization: Bearer $this->API_KEY"
41 | );
42 |
43 | curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
44 | }
45 |
46 | /**
47 | * Generates a text response based on the given prompt using the specified parameters.
48 | *
49 | * @param string $prompt The prompt for generating the text response.
50 | * @param string $model The GPT-3 model to use for text generation.
51 | * @param float $temperature The temperature parameter for controlling randomness (default: 0.7).
52 | * @param int $maxTokens The maximum number of tokens in the generated text (default: 1000).
53 | * @return array An array containing 'data' and 'error' keys, representing the generated text and any errors.
54 | */
55 | public function createTextRequest($prompt, $model = 'text-davinci-003', $temperature = 0.7, $maxTokens = 1000)
56 | {
57 | curl_reset($this->curl);
58 | $this->initialize('text');
59 |
60 | $data["model"] = $model;
61 | $data["prompt"] = $prompt;
62 | $data["temperature"] = $temperature;
63 | $data["max_tokens"] = $maxTokens;
64 |
65 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($data));
66 |
67 | $response = curl_exec($this->curl);
68 | $response = json_decode($response, true);
69 |
70 | $output['data'] = $response['choices'][0]['text'] ?? null;
71 | $output['error'] = $response['error']['code'] ?? null;
72 | return $output;
73 | }
74 |
75 | /**
76 | * Generates an image URL based on the given prompt and parameters.
77 | *
78 | * @param string $prompt The prompt for generating the image URL.
79 | * @param string $imageSize The desired image size (default: '512x512').
80 | * @param int $numberOfImages The number of images to generate (default: 1).
81 | * @return array An array containing ['data'] and ['error'] keys, representing the generated image URL and any errors.
82 | */
83 | public function generateImage($prompt, $imageSize = '512x512', $numberOfImages = 1)
84 | {
85 | curl_reset($this->curl);
86 | $this->initialize('image');
87 |
88 | $data["prompt"] = $prompt;
89 | $data["n"] = $numberOfImages;
90 | $data["size"] = $imageSize;
91 |
92 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($data));
93 |
94 | $response = curl_exec($this->curl);
95 | $response = json_decode($response, true);
96 |
97 | $output['data'] = $response['data'][0]['url'] ?? null;
98 | $output['error'] = $response['error']['code'] ?? null;
99 | return $output;
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PHP-ChatGPT (OpenAI) connection
2 | The `index.php` is a simple .php file to connect to OpenAi's API and prompt a text request using `cUrl()`.
3 | OPTIONAL: You can use the `ChatGPT.php` class to create both **image** and **text** request with the help of OpenAi's API. More details in comments.
4 |
5 | *This is the simplest and beginner-friendly method to connect to ChatGPT API without any need of library*
6 |
7 | ## API_KEY:
8 | Generate an API KEY from https://beta.openai.com/account/api-keys and paste into the code.
9 |
10 |
11 | ## Optional Parameters
12 | Modify the prompt to fit your own needs.
13 | You can add optional parameters to the class object function calls:
14 | * `createTextRequest("What's the best job?", 'text-davinci-003', 0.9, 300) // prompt, model, temperature, max_tokens`
15 | * `generateImage("Frog on a frozen lake", '1080x1080', 2) // prompt, imageSize, numberOfImages`
16 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 | "text-davinci-003", // choose your designated model
21 | "prompt" => "What's the weather in Vienna today? only metrics", // choose your prompt (what you ask the AI)
22 | "temperature" => 0.5, // temperature = creativity (higher value => greater creativity in your promts)
23 | "max_tokens" => 100 // max amount of tokens to use per request
24 | );
25 | curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
26 |
27 | $response = curl_exec($curl); // execute cURL
28 | $response = json_decode($response, true); // extract json from response
29 |
30 | $generated_text = $response['choices'][0]['text']; // extract first response from json
31 |
32 | echo $generated_text; // output response
33 |
34 | curl_close($curl); // close cURL session
35 |
--------------------------------------------------------------------------------