ChatGPT

‘text-davinci-002’, // Model name, choose appropriate one
‘prompt’ => $user_prompt, // User prompt
‘max_tokens’ => 150, // Maximum number of tokens in the response
‘temperature’ => 0.7, // Temperature parameter for controlling randomness of the response
‘n’ => 1 // Number of completions to generate
);

// Set up the cURL request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
‘Authorization: Bearer ‘ . $api_key
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);

// Check for errors
if(curl_errno($ch)) {
echo ‘Error:’ . curl_error($ch);
} else {
// Process the response
$responseData = json_decode($response, true);
// Output the generated completion
echo $responseData[‘choices’][0][‘text’];
}

// Close cURL session
curl_close($ch);
} else {
// Display form to input user prompt
echo ‘

‘;
echo ‘Enter your prompt: ‘;
echo ‘‘;
echo ‘

‘;
}

?>