ChatGPT-3 如何获取请求响应中字符串的内容?

xmakbtuz  于 2023-03-03  发布在  其他
关注(0)|答案(3)|浏览(208)

我正在编写一个基于GPT-2的Web应用程序,但效果不佳,所以我决定改用官方的OpenAI GPT-3。

response = openai.Completion.create(
  engine="davinci",
  prompt="Hello",
  temperature=0.7,
  max_tokens=64,
  top_p=1,
  frequency_penalty=0,
  presence_penalty=0
)

当我打印响应时,我得到了这个:

{
  "choices": [
    {
      "finish_reason": "length",
      "index": 0,
      "logprobs": null,
      "text": ", everyone, and welcome to the first installment of the new opening"
    }
  ],
  "created": 1624033807,
  "id": "cmpl-3CBfb8yZAFEUIVXfZO90m77dgd9V4",
  "model": "davinci:2020-05-03",
  "object": "text_completion"
}

但是我只想打印文本,那么我怎么做才能打印响应列表中的“文本”值呢,提前谢谢你,祝你有美好的一天。

ffscu2ro

ffscu2ro1#

使用按键的dict索引和按索引的列表索引

x = {"choices": [{"finish_reason": "length",
                  "text": ", everyone, and welcome to the first installment of the new opening"}], }

text = x['choices'][0]['text']
print(text)  # , everyone, and welcome to the first installment of the new opening
ssgvzors

ssgvzors2#

您可以尝试打印(响应["选择"][0]["文本"])
希望这个有用。

bvuwiixz

bvuwiixz3#

我认为GPT-3响应结构已经改变,作为参考,响应对象看起来如下:

const response = await openai.createCompletion({
    model: "text-davinci-002",
    prompt: "Say this is a test",
    temperature: 0,
    max_tokens: 6,
});

// the response looks like the following
{
  status: 200,
  statusText: 'OK',
  headers: {
  },
  config: {
  },
  request: <ref *1> ClientRequest {
  },
  data: {
    id: 'cmpl-5zzyzqvh4Hmi5yyNL2LMI9ADkLBU0',
    object: 'text_completion',
    created: 1665457953,
    model: 'text-davinci-002',
    choices: [ [Object] ],
    usage: { prompt_tokens: 5, completion_tokens: 6, total_tokens: 11 }
  }
}

// choices can be accessed like this
var { choices } = { ...response.data }

相关问题