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

调用OpenAI GPT-3 API报错:Invalid URL (POST /v1/chat/completions)求助

解决OpenAI API调用时的"Invalid URL (POST /v1/chat/completions)"错误

你遇到的问题核心是模型与API接口不匹配,下面是两种可行的解决办法:

方法一:切换为聊天模型(推荐)

createChatCompletion(或v4版本的chat.completions.create)是专门给GPT-3.5/4系列聊天模型设计的接口,必须搭配gpt-3.5-turbo这类模型,同时参数要换成messages而非prompt:

如果你用的是openai v4版本:

const { OpenAI } = require("openai");

const openai = new OpenAI({
  apiKey: 'MY KEY'
});

async function start() {
  const response = await openai.chat.completions.create({
    model: "gpt-3.5-turbo",
    messages: [
      { role: "user", content: "Write a 90 word essay about Family Guy" }
    ],
    temperature: 0,
    max_tokens: 1000
  });

  console.log(response.choices[0].message.content);
}

start();

如果你用的是openai v3版本:

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: 'MY KEY'
});

const openai = new OpenAIApi(configuration);

async function start() {
  const response = await openai.createChatCompletion({
    model: "gpt-3.5-turbo",
    messages: [
      { role: "user", content: "Write a 90 word essay about Family Guy" }
    ],
    temperature: 0,
    max_tokens: 1000
  });

  console.log(response.data.choices[0].message.content);
}

start();

方法二:继续使用text-davinci-003模型

如果不想更换模型,就改用文本补全接口createCompletion,这才是text-davinci-003对应的官方接口:

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: 'MY KEY'
});

const openai = new OpenAIApi(configuration);

async function start() {
  const response = await openai.createCompletion({
    model: "text-davinci-003",
    prompt: "Write a 90 word essay about Family Guy",
    temperature: 0,
    max_tokens: 1000
  });

  console.log(response.data.choices[0].text);
}

start();

错误原因说明

  • text-davinci-003属于文本补全模型,对应的API端点是/v1/completions;而你调用的createChatCompletion是聊天模型专属接口,对应/v1/chat/completions,两者完全不兼容,因此触发URL无效错误。
  • 额外注意openai库的版本差异:v4版本简化了调用逻辑,移除了Configuration和OpenAIApi类,直接通过OpenAI实例调用方法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 09:02:49