OPENAI API 完成不返回文本

CWY*_*YOO 16 node.js openai-api

我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)

openai的返回数据是这样的

{
  id: '~~~',
  object: 'text_completion',
  created: ~~~,
  model: 'text-davinci:001',
  choices: [ { text: '', index: 0, logprobs: null, finish_reason: 'stop' } ]
}
Run Code Online (Sandbox Code Playgroud)

在操场上,这段代码运行得很好。

在操场上,这段代码运行得很好。

我怎样才能得到正确的回应?

小智 9

它应该是这样的:

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;
}
Run Code Online (Sandbox Code Playgroud)

在这里,首先,从停止数组中删除 \n,因为这样它将在每个换行符之后停止完成(任何答案都可能位于多行中)。其次,不需要在输入前添加额外的\n:。其实这并不重要。

最后,请记住通过在提示符的最后添加“output:”来提供有关您期望的完成情况的一些线索。

顺便说一句,这些类型的提问完成也可以通过 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: ["{}"],
});
Run Code Online (Sandbox Code Playgroud)