使用Azure OpenAI服务时出现404 NotFoundError问题求助
Azure OpenAI JavaScript库404错误解决指南
问题背景
团队通过大学账号在Microsoft Azure获取了OpenAI额度,使用JavaScript版OpenAI库时,已配置Azure提供的密钥和端点,但调用接口时返回404资源未找到错误。
错误信息
NotFoundError: 404 Resource not found at APIError.generate (file:///home/teo/social_stories_creator/node_modules/openai/error.mjs:48:20) at OpenAI.makeStatusError (file:///home/teo/social_stories_creator/node_modules/openai/core.mjs:244:25) at OpenAI.makeRequest (file:///home/teo/social_stories_creator/node_modules/openai/core.mjs:283:30) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async main (file:///home/teo/social_stories_creator/test.mjs:9:22) { status: 404, headers: { 'apim-request-id': 'e04bf750-6d8c-478e-b6d5-967bbbc44b62', 'content-length': '56', 'content-type': 'application/json', date: 'Sat, 18 Nov 2023 10:14:24 GMT', 'strict-transport-security': 'max-age=31536000; includeSubDomains; preload', 'x-content-type-options': 'nosniff' }, error: { code: '404', message: 'Resource not found' }, code: '404', param: undefined, type: undefined } Node.js v21.1.0
测试代码
import OpenAI from 'openai'; const openai = new OpenAI({ apiKey: 'OUR_KEY', baseURL: 'OUR_ENDPOINT', }); async function main() { const completion = await openai.chat.completions.create({ messages: [{ role: 'system', content: 'You are a helpful assistant.' }], model: 'gpt-3.5-turbo', }); console.log(completion.choices[0]); } main();
解决步骤
修正baseURL格式
Azure OpenAI的baseURL必须包含资源名称、部署名称和API版本后缀,正确格式为:https://{资源名称}.openai.azure.com/openai/deployments/{部署名称}/v1
多数404错误源于遗漏部署名称或v1后缀,导致请求路径无效。匹配模型部署名称
代码中model参数需填写Azure门户中实际部署的模型名称,而非通用模型ID。例如Azure部署时通常将gpt-3.5-turbo命名为gpt-35-turbo(不允许使用点号),需严格对应。验证端点与密钥有效性
登录Azure门户确认:- 端点地址复制正确,无多余空格或字符
- 使用的密钥是当前资源的有效密钥(可尝试切换另一个密钥)
- 资源所在区域支持调用的模型类型
更新OpenAI库版本
旧版本库可能对Azure端点适配存在问题,执行npm update openai升级至最新版,确保兼容性。
内容的提问来源于stack exchange,提问作者Teo7
相关产品推荐
相关产品推荐

