gpt4 book ai didi

node.js - OPENAI API 完成不返回文本

转载 作者:行者123 更新时间:2023-12-02 22:44:23 25 4
gpt4 key购买 nike

我正在使用 node.js 并想使用 openai API

我刚刚从openai playground复制代码,它看起来像这样

export const askOpenAi = async () => {
const response = await openai.createCompletion("text-davinci-001", {
prompt: "\ninput: What is human life expectancy in the United States?\n",
temperature: 0,
max_tokens: 100,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
stop: ["\n", "\ninput:"],
});
return response.data;
}

openai 的返回数据是这样的

{
id: '~~~',
object: 'text_completion',
created: ~~~,
model: 'text-davinci:001',
choices: [ { text: '', index: 0, logprobs: null, finish_reason: 'stop' } ]
}

在 Playground 上,这段代码运行良好。

In the playground, this code works very well.

如何获得正确的响应?

最佳答案

应该是这样的:

export const askOpenAi = async () => {
const prompt = `input: What is human life expectancy in the United States?
output:`
const response = await openai.createCompletion("text-davinci-001", {
prompt: prompt,
temperature: 0,
max_tokens: 100,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
stop: ["input:"],
});
return response.data;
}

这里,首先,从 stop 数组中删除\n ,因为这样它会在每个换行符后停止完成(任何答案都可以在多行中)。其次,无需在输入前添加额外的\n:。其实没关系。

最后,请记住通过添加输出来提供一些关于您期望完成的线索:在提示的最后。

顺便说一句,这些类型的问题也可以通过 openAI 的新指令模式来完成。

const prompt = `Answer the following question:
What is human life expectancy in the United States?
{}`
const response = await openai.createCompletion("text-davinci-001", {
prompt: prompt,
temperature: .7,
max_tokens: 100,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
stop: ["{}"],
});

关于node.js - OPENAI API 完成不返回文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70893330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com