我在尝试将我的助手传递给OpenAI Assistants API时,遇到了以下错误:
message: ‘模型
my Model ID
不存在或您没有访问权限。’,type: ‘invalid_request_error’,param: null,code: ‘model_not_found’
代码:
try { const fetch = await import('node-fetch').then(mod => mod.default); const response = await fetch('https://api.openai.com/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}` }, body: JSON.stringify({ model: 'My model ID', messages: conversation }) });}
如果我将model
参数改为gpt-3.5-turbo
,它就能工作,我的应用可以与标准模型进行交互。
我知道API密钥是正确的,并且它拥有所有权限。我尝试传递给model
参数的助手ID也是正确的。为了测试目的,这两者都在同一个文件中定义。
回答:
问题
OpenAI Assistants API不使用Chat Completions API端点(即https://api.openai.com/v1/chat/completions
)。使用OpenAI的Assistants API与使用其他API(如Completions API或Chat Completions API)在根本上是不同的(即更复杂)。
解决方案
您不通过使用model
参数来传递助手。您通过使用assistant_id
参数来传递助手(见下面的步骤4)。
以下是您需要遵循的步骤,以从助手那里获得响应:
步骤1:创建助手
POST https://api.openai.com/v1/assistants
步骤2:创建线程
POST https://api.openai.com/v1/threads
步骤3:将用户的问题添加到线程中
POST https://api.openai.com/v1/threads/{thread_id}/messages
步骤4:运行助手
POST https://api.openai.com/v1/threads/{thread_id}/runs
GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}
步骤6:获取助手的回答
GET https://api.openai.com/v1/threads/{thread_id}/messages
此外,我还制作了一个关于如何使用Assistants API的YouTube教程,并在我的GitHub个人资料上发布了代码。