Skip to content

Commit 780c361

Browse files
authored
Update gptchat.php
1 parent 17dea11 commit 780c361

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

gptchat.php

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
<?php
2+
// Set the content type of the response to JSON
23
header('Content-Type: application/json');
4+
5+
// Allow cross-origin requests from any domain
36
header('Access-Control-Allow-Origin: *');
7+
8+
// Allow the POST method for cross-origin requests
49
header('Access-Control-Allow-Methods: POST');
10+
11+
// Specify allowed headers for cross-origin requests, including Content-Type and Authorization
512
header('Access-Control-Allow-Headers: Content-Type, Authorization');
613

14+
// Include the configuration file
715
require_once 'config.php';
816

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

1328
$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
1533
curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/engines/" . MODEL . "/completions");
1634
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1735
curl_setopt($ch, CURLOPT_POST, 1);
@@ -23,16 +41,20 @@
2341
"frequency_penalty" => FREQUENCY_PENALTY,
2442
"presence_penalty" => PRESENCE_PENALTY
2543
)));
44+
// Set the Content-Type and Authorization headers
2645
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
2746
"Content-Type: application/json",
2847
"Authorization: Bearer " . OPENAI_API_KEY
2948
));
30-
49+
// Execute the cURL session and store the response
3150
$response = curl_exec($ch);
51+
// Output the response
3252
echo $response;
33-
53+
// Close the cURL session
3454
curl_close($ch);
3555
} else {
56+
// Set the HTTP response code to 405 (Method Not Allowed) if the request method is not POST
3657
http_response_code(405);
58+
// Output an error message in JSON format
3759
echo json_encode(['error' => 'Method not allowed']);
3860
}

0 commit comments

Comments
 (0)