Skip to content

Commit a28104f

Browse files
committed
improve and refator
1 parent 216e1ae commit a28104f

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

ChatGPT.php

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
// Simple ChatGPT Class that enables both text and image prompt
2-
// 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)
3-
//
4-
// Example:
5-
//
6-
// include_once('ChatGPT.php'); // include class from folder
7-
// $ai = new ChatGPT(); // initialize class object
8-
// echo $ai->generateImage('a cat on a post lamp'); // print the image URL
9-
// echo $ai->createTextRequest('what is the weather in Romania?') // print the text response
10-
// // https://github.com/Radulepy/PHP-ChatGPT
1+
<!-- Simple ChatGPT Class that enables both text and image prompt
2+
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)
113

12-
<?php
4+
Code Example:
5+
6+
include_once('ChatGPT.php'); // include class from folder
7+
$ai = new ChatGPT(); // initialize class object
8+
echo $ai->generateImage('a cat on a post lamp')['data'] ?? 'ERROR!'; // print the image URL or error text
9+
echo $ai->createTextRequest('what is the weather in Romania?')['data'] ?? 'ERROR!'; // print the text response or error text -->
1310

11+
12+
<?php
1413
class ChatGPT
1514
{
16-
17-
private $API_KEY = "enter_API_KEY_here";
15+
private $API_KEY = "ADD_YOUR_API_KEY_HERE";
1816
private $textURL = "https://api.openai.com/v1/completions";
19-
private $imageURL = "https://api.openai.com/v1/images/generations";
17+
private $imageURL = "https://api.openai.com/v1/images/generations";
2018

21-
public $curl; // create cURL object
22-
public $data = []; // data request array
19+
public $curl;
2320

2421
public function __construct()
2522
{
@@ -46,38 +43,59 @@ public function initialize($requestType = "text" || "image")
4643
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
4744
}
4845

49-
// returns text
50-
public function createTextRequest($prompt, $model = 'text-davinci-003', $temperature = 0.5, $maxTokens = 1000)
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)
5156
{
5257
curl_reset($this->curl);
5358
$this->initialize('text');
5459

55-
$this->data["model"] = $model;
56-
$this->data["prompt"] = $prompt;
57-
$this->data["temperature"] = $temperature;
58-
$this->data["max_tokens"] = $maxTokens;
60+
$data["model"] = $model;
61+
$data["prompt"] = $prompt;
62+
$data["temperature"] = $temperature;
63+
$data["max_tokens"] = $maxTokens;
5964

60-
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($this->data));
65+
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($data));
6166

6267
$response = curl_exec($this->curl);
6368
$response = json_decode($response, true);
64-
return $response['choices'][0]['text'] ?? -1; // return text or -1 if error
69+
70+
$output['data'] = $response['choices'][0]['text'] ?? null;
71+
$output['error'] = $response['error']['code'] ?? null;
72+
return $output;
6573
}
6674

67-
// returns URL with the image
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+
*/
6883
public function generateImage($prompt, $imageSize = '512x512', $numberOfImages = 1)
6984
{
7085
curl_reset($this->curl);
7186
$this->initialize('image');
7287

73-
$this->data["prompt"] = $prompt;
74-
$this->data["n"] = $numberOfImages;
75-
$this->data["size"] = $imageSize;
88+
$data["prompt"] = $prompt;
89+
$data["n"] = $numberOfImages;
90+
$data["size"] = $imageSize;
7691

77-
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($this->data));
92+
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($data));
7893

7994
$response = curl_exec($this->curl);
8095
$response = json_decode($response, true);
81-
return $response['data'][0]['url'] ?? -1; //return the first url or -1 if error
96+
97+
$output['data'] = $response['data'][0]['url'] ?? null;
98+
$output['error'] = $response['error']['code'] ?? null;
99+
return $output;
82100
}
83101
}

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
$curl = curl_init(); // create cURL session
44

5-
$API_KEY = "paste_your_API_here"; // login to https://beta.openai.com/account/api-keys and create an API KEY
5+
$API_KEY = "ADD_YOUR_API_KEY_HERE"; // login to https://beta.openai.com/account/api-keys and create an API KEY
66

77
$url = "https://api.openai.com/v1/completions";
88
curl_setopt($curl, CURLOPT_URL, $url);

0 commit comments

Comments
 (0)