OpenAI API助手无法调用关联向量存储的问题求助
问题原因
你当前调用的是chat/completions接口,这是直接调用GPT-4o模型的接口,完全不会关联你在OpenAI控制台创建的、绑定了向量存储的助手。只有使用Assistants API的完整流程(线程-消息-运行),才能让助手调用已关联的向量存储资源。
修正后的PHP代码
<?php $api_key = 'MY-API-KEY'; $assistant_id = 'MY-ASSISTANT-ID'; // 获取POST请求的用户消息 $data = json_decode(file_get_contents('php://input'), true); $user_message = isset($data['message']) ? $data['message'] : ''; error_log("用户消息: " . $user_message); // 第一步:创建线程 $thread_payload = json_encode([]); $ch = curl_init('https://api.openai.com/v1/threads'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $thread_payload); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key, 'OpenAI-Beta: assistants=v2' ]); $thread_response = curl_exec($ch); if (curl_errno($ch)) { $error = '创建线程失败: ' . curl_error($ch); error_log($error); echo json_encode(['reply' => $error]); exit; } curl_close($ch); $thread_data = json_decode($thread_response, true); $thread_id = $thread_data['id'] ?? ''; if (!$thread_id) { $error = '无法获取线程ID'; error_log($error); echo json_encode(['reply' => $error]); exit; } error_log("线程ID: " . $thread_id); // 第二步:向线程添加用户消息 $message_payload = json_encode([ 'role' => 'user', 'content' => $user_message ]); $ch = curl_init("https://api.openai.com/v1/threads/{$thread_id}/messages"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $message_payload); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key, 'OpenAI-Beta: assistants=v2' ]); $message_response = curl_exec($ch); if (curl_errno($ch)) { $error = '添加消息失败: ' . curl_error($ch); error_log($error); echo json_encode(['reply' => $error]); exit; } curl_close($ch); error_log("消息添加结果: " . $message_response); // 第三步:运行线程,触发助手调用向量存储 $run_payload = json_encode([ 'assistant_id' => $assistant_id ]); $ch = curl_init("https://api.openai.com/v1/threads/{$thread_id}/runs"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $run_payload); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key, 'OpenAI-Beta: assistants=v2' ]); $run_response = curl_exec($ch); if (curl_errno($ch)) { $error = '启动运行失败: ' . curl_error($ch); error_log($error); echo json_encode(['reply' => $error]); exit; } curl_close($ch); $run_data = json_decode($run_response, true); $run_id = $run_data['id'] ?? ''; if (!$run_id) { $error = '无法获取运行ID'; error_log($error); echo json_encode(['reply' => $error]); exit; } error_log("运行ID: " . $run_id); // 第四步:轮询运行状态,直到完成 do { $ch = curl_init("https://api.openai.com/v1/threads/{$thread_id}/runs/{$run_id}"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer ' . $api_key, 'OpenAI-Beta: assistants=v2' ]); $run_status_response = curl_exec($ch); curl_close($ch); $run_status_data = json_decode($run_status_response, true); $status = $run_status_data['status'] ?? ''; error_log("当前运行状态: " . $status); if ($status === 'completed') { break; } elseif (in_array($status, ['failed', 'cancelled', 'expired'])) { $error = '运行失败,状态: ' . $status; error_log($error); echo json_encode(['reply' => $error]); exit; } // 等待1秒后再次查询 sleep(1); } while (true); // 第五步:获取线程中的所有消息,提取助手回复 $ch = curl_init("https://api.openai.com/v1/threads/{$thread_id}/messages?order=asc"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer ' . $api_key, 'OpenAI-Beta: assistants=v2' ]); $messages_response = curl_exec($ch); curl_close($ch); $messages_data = json_decode($messages_response, true); $assistant_reply = '无助手回复'; foreach ($messages_data['data'] as $message) { if ($message['role'] === 'assistant') { // 提取文本内容(忽略附件等其他格式) foreach ($message['content'] as $content) { if ($content['type'] === 'text') { $assistant_reply = $content['text']['value']; break; } } break; } } error_log("助手回复: " . $assistant_reply); echo json_encode(['reply' => $assistant_reply]); ?>
关键说明
- 必须携带
OpenAI-Beta: assistants=v2请求头,启用Assistants API v2版本 - 标准流程为:创建线程→添加用户消息→启动运行→轮询运行状态→获取助手回复,只有这套流程会触发助手调用已关联的向量存储
- 轮询状态是因为助手需要时间检索向量存储并生成回复,无法立即返回结果
内容的提问来源于stack exchange,提问作者objelland
相关产品推荐
相关产品推荐

