'davinci', // You can choose from different GPT-3 engines 'messages' => array( array( 'role' => 'system', 'content' => 'You are a helpful assistant.' ), array( 'role' => 'user', 'content' => $message ) ) ); $url = 'https://api.openai.com/v1/chat/completions'; $url = 'https://api.openai.com/v1/engines/davinci/completions'; $headers = array( 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); $decoded_response = json_decode($response, true); $message = $decoded_response['choices'][0]['message']['content']; return $message; } // Example usage if ($_SERVER['REQUEST_METHOD'] === 'POST') { $user_input = $_POST['user_input']; $response = chatWithGPT($user_input, $api_key); echo json_encode(array('message' => $response)); } ?>