OpenAI GPT-3 API 错误:“无效 URL (POST /v1/chat/completions)”

Das*_* CO 5 node.js openai-api gpt-3

这是我的代码片段:

const { Configuration, OpenAI, OpenAIApi } = require ("openai");
const configuration = new Configuration({
    apiKey: 'MY KEY'
})

const openai = new OpenAIApi(configuration)

async function start() {
    const response = await openai.createChatCompletion({
        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()
Run Code Online (Sandbox Code Playgroud)

当我跑步时:node index

我遇到这个问题:

data: {
      error: {
        message: 'Invalid URL (POST /v1/chat/completions)',
        type: 'invalid_request_error',
        param: null,
        code: null
      }
    }
  },
  isAxiosError: true,
  toJSON: [Function: toJSON]
}
Run Code Online (Sandbox Code Playgroud)

Node.js v18.15.0

我查遍了互联网并尝试了一些解决方案,但似乎没有任何效果。请帮忙!

当我在线查找这个问题时,通常其他人都会在他们的代码中附加一些链接。我是这方面的初学者,因此我们将不胜感激

Rok*_*nko 7

TL;DR:将 视为text-davinci-003GPT-3 模型(即Completions API)。请参阅选项 1 下的代码。

\n

介绍

\n

乍一看,作为过去几个月一直在使用 OpenAI API 的人,如果您阅读 OpenAI 官方文档,我认为答案是直接而简单的。好吧,我再次阅读了文档,现在我明白你为什么感到困惑了。

\n

困惑1

\n

您想使用该text-davinci-003模型。该模型最初来自 GPT-3 模型系列。但是,如果您查看OpenAI 模型概述并单击GPT-3,您将不会发现text-davinci-003列为 GPT-3 模型。这是出乎意料的。

\n

截图1

\n

困惑之二

\n

此外,它text-davinci-003被列为GPT-3.5型号。

\n

截图2

\n

困惑之三

\n

好像这还不够令人困惑,如果您查看OpenAI 模型端点兼容性,您会发现端点text-davinci-003下列出了该兼容性/v1/completions。此 API 端点用于 GPT-3 模型系列。

\n

截图3

\n
\n

等等,什么?

\n

\xc2\xa0text-davinci-003未列为 GPT-3 模型(即Completions API)。它被列为 GPT-3.5 模型(即Chat Completions API),但与 GPT-3 API 端点兼容。这没有任何意义。

\n
\n

测试

\n

可以text-davinci-003被视为 GPT-3 模型或 GPT-3.5 模型,或者两者兼而有之?我们来做个测试吧。

\n

注意:OpenAI NodeJS SDK于2023年8月16日发布v4,是对SDK的完全重写。下面的代码根据您当前拥有的版本而有所不同。请参阅迁移指南v3v4

\n
选项 1:将其text-davinci-003视为 GPT-3 模型 --> IT WORKS \xe2\x9c\x93
\n

如果将 视为text-davinci-003GPT-3 模型,则运行test-1.js,OpenAI 将返回以下完成结果:

\n
\n

这确实是一个考验

\n
\n

\xe2\x80\xa2 如果您有 OpenAI NodeJS SDK v3

\n

测试1.js

\n
const { Configuration, OpenAIApi } = require(\'openai\');\n\nconst configuration = new Configuration({\n  apiKey: process.env.OPENAI_API_KEY,\n});\n\nconst openai = new OpenAIApi(configuration);\n\nasync function getCompletionFromOpenAI() {\n  const completion = await openai.createCompletion({\n    model: \'text-davinci-003\',\n    prompt: \'Say this is a test\',\n    max_tokens: 7,\n    temperature: 0,\n  });\n\n  console.log(completion.data.choices[0].text);\n}\n\ngetCompletionFromOpenAI();\n
Run Code Online (Sandbox Code Playgroud)\n

\xe2\x80\xa2 如果您有 OpenAI NodeJS SDK v4

\n

测试1.js

\n
import OpenAI from \'openai\';\n\nconst openai = new OpenAI({\n  apiKey: process.env.OPENAI_API_KEY,\n});\n\nasync function getCompletionFromOpenAI() {\n  const completion = await openai.completions.create({\n    model: \'text-davinci-003\',\n    prompt: \'Say this is a test\',\n    max_tokens: 7,\n    temperature: 0,\n  });\n\n  console.log(completion.choices[0].text);\n}\n\ngetCompletionFromOpenAI();\n
Run Code Online (Sandbox Code Playgroud)\n
选项 2:将其text-davinci-003视为 GPT-3.5 模型 --> 它不起作用 \xe2\x9c\x97
\n

如果将 视为text-davinci-003GPT-3.5 模型,然后运行test-2.js​​,OpenAI 将返回以下错误:

\n
data: {\n  error: {\n    message: \'Invalid URL (POST /v1/chat/completions)\',\n    type: \'invalid_request_error\',\n    param: null,\n    code: null\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

\xe2\x80\xa2 如果您有 OpenAI NodeJS SDK v3

\n

测试2.js

\n
const { Configuration, OpenAIApi } = require(\'openai\');\n\nconst configuration = new Configuration({\n  apiKey: process.env.OPENAI_API_KEY,\n});\n\nconst openai = new OpenAIApi(configuration);\n\nasync function getChatCompletionFromOpenAI() {\n  const chatCompletion = await openai.createChatCompletion({\n    model: \'text-davinci-003\',\n    messages: [{ role: \'user\', content: \'Hello!\' }],\n    temperature: 0,\n  });\n\n  console.log(chatCompletion.data.choices[0].message.content);\n}\n\ngetChatCompletionFromOpenAI();\n
Run Code Online (Sandbox Code Playgroud)\n

\xe2\x80\xa2 如果您有 OpenAI NodeJS SDK v4

\n

测试2.js

\n
import OpenAI from \'openai\';\n\nconst openai = new OpenAI({\n  apiKey: process.env.OPENAI_API_KEY,\n});\n\nasync function getChatCompletionFromOpenAI() {\n  const chatCompletion = await openai.chat.completions.create({\n    model: \'gpt-3.5-turbo\',\n    messages: [{role: \'user\', content: \'Hello!\'}],\n    temperature: 0,\n  });\n\n  console.log(chatCompletion.choices[0].message.content);\n}\n\ngetChatCompletionFromOpenAI();\n
Run Code Online (Sandbox Code Playgroud)\n

结论

\n

将其text-davinci-003视为 GPT-3 模型。请参阅选项 1 下的代码。

\n