这是我的代码片段:
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()
当我跑步时:
node index
我遇到这个问题:
data: {
error: {
message: 'Invalid URL (POST /v1/chat/completions)',
type: 'invalid_request_error',
param: null,
code: null
}
}
},
isAxiosError: true,
toJSON: [Function: toJSON]
}
Node.js v18.15.0
我浏览了整个互联网并尝试了一些解决方案,但似乎没有任何效果。请帮忙!
通常当我在网上查找这个问题时,其他人会在他们的代码上附加一些链接。这方面的初学者非常多,所以任何帮助将不胜感激
TL;DR 将
text-davinci-003
视为 GPT-3 模型。请参阅选项 1 下的代码。
乍一看,作为过去几个月一直在使用 OpenAI API 的人,我认为如果您阅读官方 OpenAI 文档,答案会很简单。好吧,我又看了一遍文档,现在我明白你为什么感到困惑了。
您想使用
text-davinci-003
模型。该模型最初来自 GPT-3 模型系列。但是,如果您查看 OpenAI 模型概述 并单击 GPT-3,您将不会发现 text-davinci-003
被列为 GPT-3 模型。这是出乎意料的。
此外,
text-davinci-003
被列为GPT-3.5模型。
好像这还不够混乱,如果您看一下 OpenAI 模型端点兼容性,您会发现
text-davinci-003
列在 /v1/completions
端点下。此 API 端点用于 GPT-3 模型系列。
text-davinci-003
未列为 GPT-3 模型。它被列为 GPT-3.5 模型,但与 GPT-3 API 端点兼容。这没有任何意义。
text-davinci-003
可以被视为 GPT-3 模型还是 GPT-3.5 模型,或者两者兼而有之?让我们做一个测试。
text-davinci-003
视为 GPT-3 模型 --> 它有效 ✓如果你把
text-davinci-003
当作GPT-3模型,那么运行test-1.js
,OpenAI会返回如下补全:
这确实是一个考验
test-1.js
const { Configuration, OpenAIApi } = require('openai');
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
async function getCompletionFromOpenAI() {
const completion = await openai.createCompletion({
model: 'text-davinci-003',
prompt: 'Say this is a test',
max_tokens: 7,
temperature: 0,
});
console.log(completion.data.choices[0].text);
}
getCompletionFromOpenAI();
text-davinci-003
视为 GPT-3.5 模型 --> 它不起作用 ✗如果把
text-davinci-003
当作GPT-3.5模型,那么运行test-2.js
,OpenAI会返回如下错误:
data: {
error: {
message: 'Invalid URL (POST /v1/chat/completions)',
type: 'invalid_request_error',
param: null,
code: null
}
}
test-2.js
const { Configuration, OpenAIApi } = require('openai');
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
async function getCompletionFromOpenAI() {
const completion = await openai.createChatCompletion({
model: 'text-davinci-003',
messages: [{ role: 'user', content: 'Hello!' }],
temperature: 0,
});
console.log(completion.data.choices[0].message.content);
}
getCompletionFromOpenAI();
将
text-davinci-003
视为GPT-3模型。请参阅选项 1 下的代码。
您正在混合 OpenAI API 的两种功能。
您可以根据提示创建一次性完成,他们称之为 Completions,或者您可以通过代理和用户之间的讨论创建完成,他们称之为 ChatCompletion。
看你要用哪一个参数不一样
第一种情况应该是
const response = await openai. createCompletion({
model:"text-davinci-003",
prompt: "Write a 90 word essay about Family Guy",
temperature: 0,
max_tokens: 1000
})
在另一种情况下,您需要指定消息和角色。
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{role: "user", content: "Hello world"}],
});
查看 API 文档以了解两种不同 API 之间的区别。