|
1 | 1 | <?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'); |
6 | 4 | header('Access-Control-Allow-Origin: *');
|
7 |
| - |
8 |
| -// Allow the POST method for cross-origin requests |
9 | 5 | header('Access-Control-Allow-Methods: POST');
|
10 |
| - |
11 |
| -// Specify allowed headers for cross-origin requests, including Content-Type and Authorization |
12 | 6 | header('Access-Control-Allow-Headers: Content-Type, Authorization');
|
13 | 7 |
|
14 |
| -// Include the configuration file |
15 | 8 | require_once 'config.php';
|
16 | 9 |
|
17 |
| -// Check if the request method is POST |
18 | 10 | if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
19 |
| - |
20 |
| - // Decode the JSON input and store it in a variable |
21 | 11 | $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']; |
27 | 13 |
|
28 | 14 | $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, [ |
48 | 19 | "Content-Type: application/json",
|
49 | 20 | "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); |
56 | 38 | curl_close($ch);
|
57 | 39 | } else {
|
58 |
| - // Set the HTTP response code to 405 (Method Not Allowed) if the request method is not POST |
59 | 40 | http_response_code(405);
|
60 |
| - // Output an error message in JSON format |
61 | 41 | echo json_encode(['error' => 'Method not allowed']);
|
62 | 42 | }
|
0 commit comments