'gpt-4-1106-preview', 'messages' => [ ['role' => 'system', 'content' => $system_prompt], ['role' => 'user', 'content' => $user_message] ], 'temperature' => 0.7, 'max_tokens' => 1024, ]; // ✅ APIリクエスト $ch = curl_init('https://api.openai.com/v1/chat/completions'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key ]); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $response = curl_exec($ch); curl_close($ch); // ✅ レスポンスから内容を抽出 $response_data = json_decode($response, true); $reply = $response_data['choices'][0]['message']['content'] ?? '申し訳ありません、うまく回答できませんでした。'; // ✅ ログに保存 $log = "[" . date('Y-m-d H:i:s') . "]\nUser: " . $user_message . "\nBot: " . $reply . "\n\n"; file_put_contents(__DIR__ . '/chatbot_log.txt', $log, FILE_APPEND); // ✅ 出力 echo json_encode(['reply' => $reply]);