diff --git a/README.md b/README.md index 7aab2f3..62dc241 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# GPT API Integration (in HTML/CSS with JS/PHP) +# GPT API Integration with stream (in HTML/CSS with JS/PHP) ###### Updated: 02.06.2024 modules/endpoints  diff --git a/config.php b/config.php index d0ce36a..072315b 100644 --- a/config.php +++ b/config.php @@ -4,7 +4,7 @@ // Define the OpenAI API key define('OPENAI_API_KEY', 'sk-.................................'); // Define the model to be used, for example: text-davinci-003 -define('MODEL', 'text-davinci-003'); +define('MODEL', 'gpt-4o-mini'); // Define the temperature setting for the model, a value between 0 and 1 (e.g., 0.9) define('TEMPERATURE', 0.9); // Define the maximum number of tokens to be generated by the model (e.g., 1000) diff --git a/gptchat.php b/gptchat.php index 19eae6a..09cc926 100644 --- a/gptchat.php +++ b/gptchat.php @@ -1,62 +1,42 @@ gpt-3.5-turbo-16k, - curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/engines/" . MODEL . "/completions"); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array( - - "prompt" => $message, - "max_tokens" => MAX_TOKENS, - "temperature" => TEMPERATURE, - "top_p" => TOP_P, - "frequency_penalty" => FREQUENCY_PENALTY, - "presence_penalty" => PRESENCE_PENALTY - ))); - // Set the Content-Type and Authorization headers - curl_setopt($ch, CURLOPT_HTTPHEADER, array( + curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/chat/completions"); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Content-Type: application/json", "Authorization: Bearer " . OPENAI_API_KEY - )); -// Execute the cURL session and store the response - $response = curl_exec($ch); - // Output the response - echo $response; -// Close the cURL session + ]); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ + "model" => MODEL, + "messages" => [["role" => "user", "content" => $message]], + "temperature" => TEMPERATURE, + "max_tokens" => MAX_TOKENS, + "stream" => true + ])); + + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($curl, $data) { + echo "data: " . $data . "\n\n"; + ob_flush(); + flush(); + return strlen($data); + }); + + curl_exec($ch); curl_close($ch); } else { - // Set the HTTP response code to 405 (Method Not Allowed) if the request method is not POST http_response_code(405); - // Output an error message in JSON format echo json_encode(['error' => 'Method not allowed']); } diff --git a/index.html b/index.html index 3c9d2cd..c7b3a57 100644 --- a/index.html +++ b/index.html @@ -3,7 +3,7 @@
-