【问题标题】:How to get th content of a string inside a request response?如何在请求响应中获取字符串的内容?
【发布时间】:2021-09-03 09:33:04
【问题描述】:

我正在编写一个基于 GPT-2 的 webapp,但效果不佳,所以我决定改用官方的 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"
}

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

【问题讨论】:

    标签: python python-requests openai gpt-3


    【解决方案1】:

    使用字典键索引,列表索引索引

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 2018-06-24
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      相关资源
      最近更新 更多