You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure OpenAI资源未找到错误:NodeJS文本摘要问题求助

Azure OpenAI 文本摘要请求「Resource not found」问题解决

问题背景

已将有效Azure OpenAI API密钥与端点配置到.env文件,在NodeJS中实现文本摘要功能时,触发「OpenAI API Error: Resource not found」错误,原代码如下:

const prompt = `Provide a summary of the text: ${data}`;
const apiKey = process.env.AZURE_OPENAI_API_KEY;
const endpoint = process.env.AZURE_OPENAI_ENDPOINT;
const url = `${endpoint}/v1/chat/completions`;

const response = await axios.post(
  url,
  {
    model: "gpt-35-turbo",
    prompt: prompt,
    temperature: 0.3,
    max_tokens: 250,
    top_p: 1,
    frequency_penalty: 0,
    presence_penalty: 0
  },
  {
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${apiKey}`,
    },
  }
);
const summary = response.data.choices[0].text.trim();
return summary;

尝试过多个URL仍未解决:

  • const url = ${endpoint}/v1/completions;
  • const url = ${endpoint}/openai/deployments/MY_DEPLOYMENT_NAME/completions?api-version=2023-05-15;
  • const url = ${endpoint}/openai/deployments/MY_DEPLOYMENT_NAME/completions?api-version=2023-05-15-preview;

错误原因

  1. Azure OpenAI API路径必须包含部署名称和API版本,原生OpenAI的路径格式不适用
  2. gpt-35-turbo属于聊天模型,需调用chat/completions接口而非completions接口
  3. 聊天模型的请求参数需用messages数组而非prompt字段,返回结果的结构也不同

解决方案

修正后的完整代码

const apiKey = process.env.AZURE_OPENAI_API_KEY;
const endpoint = process.env.AZURE_OPENAI_ENDPOINT;
// 替换MY_DEPLOYMENT_NAME为你在Azure门户创建的gpt-35-turbo部署名称
// 选择Azure支持的API版本,推荐使用稳定版或最新预览版
const url = `${endpoint}/openai/deployments/MY_DEPLOYMENT_NAME/chat/completions?api-version=2024-02-15-preview`;

const response = await axios.post(
  url,
  {
    messages: [
      { role: "system", content: "你是一个专业的文本摘要助手,需要简洁准确地总结给定文本内容" },
      { role: "user", content: `Provide a summary of the text: ${data}` }
    ],
    temperature: 0.3,
    max_tokens: 250,
    top_p: 1,
    frequency_penalty: 0,
    presence_penalty: 0
  },
  {
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${apiKey}`,
    },
  }
);
// 聊天模型返回的是message.content而非text
const summary = response.data.choices[0].message.content.trim();
return summary;

关键修正点

  • URL格式:必须遵循{endpoint}/openai/deployments/{部署名称}/chat/completions?api-version={版本号}的结构
  • 请求参数:用messages数组定义对话上下文,system角色可设定助手行为,user角色传入待摘要文本
  • 结果解析:从response.data.choices[0].message.content提取摘要内容,而非原代码的text字段

验证步骤

  1. 确认.env中的AZURE_OPENAI_ENDPOINT是完整的HTTPS地址(例如https://your-resource-name.openai.azure.com/)
  2. 替换代码中的MY_DEPLOYMENT_NAME为Azure门户中实际创建的gpt-35-turbo部署名称
  3. 确认API版本与Azure OpenAI支持的版本匹配,可在Azure门户的部署详情页查看可用版本

内容的提问来源于stack exchange,提问作者ShehaniWageesha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 15:12:01