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

NodeJS开发的Discord Bot调用OpenAI GPT-3 API返回404错误求助

解决Discord机器人调用OpenAI API时的404错误

核心原因:过时的API端点

你代码中使用的https://api.openai.com/v1/engines/davinci-codex/completions属于已废弃的旧版端点,Davinci Codex引擎已停止服务,这是触发404错误的直接原因。

修复方案

方案1:使用通用补全端点(兼容GPT-3系列模型)

将请求URL替换为OpenAI当前支持的通用补全端点,并指定可用的GPT-3模型:

try {
  const response = await axios.post('https://api.openai.com/v1/completions', {
    model: 'text-davinci-003', // 替代Davinci Codex的有效模型
    prompt: msg.content,
    max_tokens: 100,
  }, {
    headers: {
      'Authorization': `Bearer ${OPENAI_TOKEN}`,
      'Content-Type': 'application/json',
    },
  });

  await interaction.followUp(`Resposta do GPT-3: ${response.data.choices[0].text.trim()}`);
} catch (error) {
  console.error(error.response?.data || error.message); // 打印更详细的错误信息
  await interaction.followUp('Desculpe, ocorreu um erro ao buscar a resposta.');
}

方案2:推荐使用聊天模型端点(GPT-3.5/4,更高效)

如果你的OpenAI账号有权限访问GPT-3.5-turbo或GPT-4,建议使用官方主推的聊天接口,性能和成本更优:

try {
  const response = await axios.post('https://api.openai.com/v1/chat/completions', {
    model: 'gpt-3.5-turbo',
    messages: [
      { role: 'user', content: msg.content }
    ],
    max_tokens: 100,
  }, {
    headers: {
      'Authorization': `Bearer ${OPENAI_TOKEN}`,
      'Content-Type': 'application/json',
    },
  });

  // 注意聊天接口的返回结构与补全接口不同
  await interaction.followUp(`Resposta do GPT: ${response.data.choices[0].message.content.trim()}`);
} catch (error) {
  console.error(error.response?.data || error.message);
  await interaction.followUp('Desculpe, ocorreu um erro ao buscar a resposta.');
}

额外检查事项

  • 确认OpenAI令牌具备访问指定模型的权限,部分模型需要额外申请或足够的付费额度
  • 在Discord开发者后台启用机器人的Message Content Intent,否则消息收集器可能无法正常工作
  • 捕获错误时打印error.response?.data,可以获取OpenAI返回的具体错误信息,便于排查

内容的提问来源于stack exchange,提问作者Phil Coulson xD

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 08:45:05