|
1 | 1 | <?php
|
| 2 | +// Set the content type of the response to JSON |
2 | 3 | header('Content-Type: application/json');
|
| 4 | + |
| 5 | +// Allow cross-origin requests from any domain |
3 | 6 | header('Access-Control-Allow-Origin: *');
|
| 7 | + |
| 8 | +// Allow the POST method for cross-origin requests |
4 | 9 | header('Access-Control-Allow-Methods: POST');
|
| 10 | + |
| 11 | +// Specify allowed headers for cross-origin requests, including Content-Type and Authorization |
5 | 12 | header('Access-Control-Allow-Headers: Content-Type, Authorization');
|
6 | 13 |
|
| 14 | +// Include the configuration file |
7 | 15 | require_once 'config.php';
|
8 | 16 |
|
| 17 | +// Check if the request method is POST |
9 | 18 | if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
| 19 | + |
| 20 | + // Decode the JSON input and store it in a variable |
10 | 21 | $input = json_decode(file_get_contents('php://input'), true);
|
| 22 | + |
| 23 | + // URL-encode the message from the input |
11 | 24 | $message = urlencode($input['message']);
|
| 25 | + |
| 26 | + // Initialize a new cURL session |
12 | 27 |
|
13 | 28 | $ch = curl_init();
|
14 |
| -// Change API-Endpoint URL to your needs |
| 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 |
15 | 33 | curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/engines/" . MODEL . "/completions");
|
16 | 34 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
17 | 35 | curl_setopt($ch, CURLOPT_POST, 1);
|
|
23 | 41 | "frequency_penalty" => FREQUENCY_PENALTY,
|
24 | 42 | "presence_penalty" => PRESENCE_PENALTY
|
25 | 43 | )));
|
| 44 | + // Set the Content-Type and Authorization headers |
26 | 45 | curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
27 | 46 | "Content-Type: application/json",
|
28 | 47 | "Authorization: Bearer " . OPENAI_API_KEY
|
29 | 48 | ));
|
30 |
| - |
| 49 | +// Execute the cURL session and store the response |
31 | 50 | $response = curl_exec($ch);
|
| 51 | + // Output the response |
32 | 52 | echo $response;
|
33 |
| - |
| 53 | +// Close the cURL session |
34 | 54 | curl_close($ch);
|
35 | 55 | } else {
|
| 56 | + // Set the HTTP response code to 405 (Method Not Allowed) if the request method is not POST |
36 | 57 | http_response_code(405);
|
| 58 | + // Output an error message in JSON format |
37 | 59 | echo json_encode(['error' => 'Method not allowed']);
|
38 | 60 | }
|
0 commit comments