如何通过PHP调用OpenAI Assistant获取响应?端点错误修正求助
修正OpenAI Assistant API的PHP调用代码
你遇到的错误是因为使用了错误的API端点,OpenAI Assistant API不支持直接通过/v1/assistants/{assistant_id}/completions发送请求,必须通过**线程(Thread)**完成交互,正确流程如下:
正确交互流程
- 创建一个新的线程(Thread)
- 向该线程添加用户消息
- 触发助手在该线程上运行
- 轮询运行状态,直到完成后提取助手的响应消息
修正后的完整PHP代码
<?php $api_key = '你的OpenAI API密钥'; // 替换为你的实际API密钥 $assistant_id = 'asst_abc'; // 替换为你的实际助手ID // 步骤1:创建线程 $thread_url = 'https://api.openai.com/v1/threads'; $headers = [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key, 'OpenAI-Beta: assistants=v1', ]; $ch = curl_init($thread_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([])); // 创建线程不需要额外参数 $thread_response = curl_exec($ch); $thread_data = json_decode($thread_response, true); curl_close($ch); if (isset($thread_data['id'])) { $thread_id = $thread_data['id']; // 步骤2:向线程添加用户消息 $message_url = 'https://api.openai.com/v1/threads/' . $thread_id . '/messages'; $message_data = [ 'role' => 'user', 'content' => 'Hello' ]; $ch = curl_init($message_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($message_data)); $message_response = curl_exec($ch); curl_close($ch); // 步骤3:触发助手运行 $run_url = 'https://api.openai.com/v1/threads/' . $thread_id . '/runs'; $run_data = [ 'assistant_id' => $assistant_id ]; $ch = curl_init($run_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($run_data)); $run_response = curl_exec($ch); $run_data = json_decode($run_response, true); curl_close($ch); if (isset($run_data['id'])) { $run_id = $run_data['id']; $run_status_url = 'https://api.openai.com/v1/threads/' . $thread_id . '/runs/' . $run_id; // 步骤4:轮询运行状态,直到完成 do { $ch = curl_init($run_status_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $status_response = curl_exec($ch); $status_data = json_decode($status_response, true); curl_close($ch); $status = $status_data['status']; if ($status === 'completed') { break; } sleep(1); // 等待1秒后再次查询 } while (in_array($status, ['queued', 'in_progress'])); // 获取线程中的所有消息,提取助手的响应 $messages_url = 'https://api.openai.com/v1/threads/' . $thread_id . '/messages'; $ch = curl_init($messages_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $messages_response = curl_exec($ch); $messages_data = json_decode($messages_response, true); curl_close($ch); // 输出助手的最新响应 foreach ($messages_data['data'] as $message) { if ($message['role'] === 'assistant') { echo '助手响应:' . $message['content'][0]['text']['value'] . PHP_EOL; break; // 只取最新的助手响应 } } } else { echo '创建运行失败:' . $run_response; } } else { echo '创建线程失败:' . $thread_response; } ?>
代码说明
- 替换代码中的
你的OpenAI API密钥和asst_abc为实际值 - 助手运行是异步的,所以需要通过轮询状态来等待结果
- 最后从线程的消息列表中筛选出
assistant角色的消息,即为助手的响应
内容的提问来源于stack exchange,提问作者vijay kumar
相关产品推荐
相关产品推荐

