'OpenAI API key is not configured.']; } $url = 'https://api.openai.com/v1/chat/completions'; $data = [ 'model' => 'gpt-3.5-turbo', 'messages' => [ ['role' => 'system', 'content' => 'You are a helpful assistant.'], ['role' => 'user', 'content' => $message] ], 'max_tokens' => 150 ]; $headers = [ 'Content-Type: application/json', 'Authorization: Bearer ' . self::$apiKey ]; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); if ($error) { return ['error' => 'API call failed: ' . $error]; } if ($httpcode !== 200) { return ['error' => 'API returned status ' . $httpcode . ': ' . $response]; } $decodedResponse = json_decode($response, true); if (isset($decodedResponse['choices'][0]['message']['content'])) { return ['reply' => $decodedResponse['choices'][0]['message']['content']]; } elseif (isset($decodedResponse['error'])) { return ['error' => $decodedResponse['error']['message']]; } return ['error' => 'Unexpected API response format.']; } } OpenAIService::init();