OPENAI API Completion not returning text

题意:使用 OpenAI 的 API(特别是其文本补全功能,如 GPT 系列模型)时,它没有返回你期望的文本结果

问题背景:

I am using node.js and want to use openai API

我正在使用 Node.js,并且想要使用 OpenAI API

I just copied the code from openai playground and it looks like this

我刚从 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's Returning data look like this        OpenAI 的返回数据看起来像这样

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

In the playground, this code works very well.        在后台代码运行正常

How can I get right response?        我怎样才能得到正确的响应?

问题解决:

It should be something like this:        它应该是这样的:

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;
}

Here, first of all, remove the \n from stop array, because then it will stop the completion after every newline (any answer could be in multiple lines). secondly, no need to add extra \n before input:. it doesnt matter actually.

首先,从停止数组(stop array)中移除换行符(\n),因为这样会导致在每个换行符之后都停止补全(任何答案都可能是多行的)。其次,实际上在input:之前不需要添加额外的换行符,这并不重要。

Finally, remember to give some clue about the completion you're expecting by adding output: at the last of your prompt.

最后,请记得通过在提示的最后添加“output:”来给出一些关于你期望的补全结果的线索

Btw, these type of question ask completion can be achieved through openAI's new instruct mode as well.

顺便说一句,这类要求补全的问题也可以通过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: ["{}"],
});

 

  • 19
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

营赢盈英

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值