Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9a6b3c0

Browse files
authored
Update gptchat.php
1 parent 06b1941 commit 9a6b3c0

File tree

1 file changed

+24
-44
lines changed

1 file changed

+24
-44
lines changed

gptchat.php

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,42 @@
11
<?php
2-
// Set the content type of the response to JSON
3-
header('Content-Type: application/json');
4-
5-
// Allow cross-origin requests from any domain
2+
header('Content-Type: text/event-stream');
3+
header('Cache-Control: no-cache');
64
header('Access-Control-Allow-Origin: *');
7-
8-
// Allow the POST method for cross-origin requests
95
header('Access-Control-Allow-Methods: POST');
10-
11-
// Specify allowed headers for cross-origin requests, including Content-Type and Authorization
126
header('Access-Control-Allow-Headers: Content-Type, Authorization');
137

14-
// Include the configuration file
158
require_once 'config.php';
169

17-
// Check if the request method is POST
1810
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
19-
20-
// Decode the JSON input and store it in a variable
2111
$input = json_decode(file_get_contents('php://input'), true);
22-
23-
// URL-encode the message from the input
24-
$message = urlencode($input['message']);
25-
26-
// Initialize a new cURL session
12+
$message = $input['message'];
2713

2814
$ch = curl_init();
29-
// Change the API endpoint URL according to your needs
30-
// For example, use /v1/chat/completions for GPT-4, GPT-4-0314, GPT-4-32k, GPT-4-32k-0314, GPT-3.5-turbo, and GPT-3.5-turbo-0301 models
31-
// Use /v1/completions for Lingua models like text-davinci-003, text-davinci-002, text-curie-001, text-babbage-001, and text-ada-001
32-
// See the readme.md file for more information
33-
// if you dont want to use /v1/engines/ than you must remove model from CURLOPT_URL and must set it in CURLOPT_POSTFIELDS like "model" => gpt-3.5-turbo-16k,
34-
curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/engines/" . MODEL . "/completions");
35-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
36-
curl_setopt($ch, CURLOPT_POST, 1);
37-
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
38-
39-
"prompt" => $message,
40-
"max_tokens" => MAX_TOKENS,
41-
"temperature" => TEMPERATURE,
42-
"top_p" => TOP_P,
43-
"frequency_penalty" => FREQUENCY_PENALTY,
44-
"presence_penalty" => PRESENCE_PENALTY
45-
)));
46-
// Set the Content-Type and Authorization headers
47-
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
15+
curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/chat/completions");
16+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
17+
curl_setopt($ch, CURLOPT_POST, true);
18+
curl_setopt($ch, CURLOPT_HTTPHEADER, [
4819
"Content-Type: application/json",
4920
"Authorization: Bearer " . OPENAI_API_KEY
50-
));
51-
// Execute the cURL session and store the response
52-
$response = curl_exec($ch);
53-
// Output the response
54-
echo $response;
55-
// Close the cURL session
21+
]);
22+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
23+
"model" => MODEL,
24+
"messages" => [["role" => "user", "content" => $message]],
25+
"temperature" => TEMPERATURE,
26+
"max_tokens" => MAX_TOKENS,
27+
"stream" => true
28+
]));
29+
30+
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($curl, $data) {
31+
echo "data: " . $data . "\n\n";
32+
ob_flush();
33+
flush();
34+
return strlen($data);
35+
});
36+
37+
curl_exec($ch);
5638
curl_close($ch);
5739
} else {
58-
// Set the HTTP response code to 405 (Method Not Allowed) if the request method is not POST
5940
http_response_code(405);
60-
// Output an error message in JSON format
6141
echo json_encode(['error' => 'Method not allowed']);
6242
}

0 commit comments

Comments
 (0)