首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在请求响应中获取字符串的内容?

如何在请求响应中获取字符串的内容?
EN

Stack Overflow用户
提问于 2021-06-18 16:34:08
回答 3查看 1.2K关注 0票数 0

我正在编写一个基于GPT-2的was应用程序,但是它不是很好,所以我决定切换到正式的OpenAI GPT-3。所以我提出这个要求:

代码语言:javascript
复制
response = openai.Completion.create(
  engine="davinci",
  prompt="Hello",
  temperature=0.7,
  max_tokens=64,
  top_p=1,
  frequency_penalty=0,
  presence_penalty=0
)

当我打印回复时,我得到的是:

代码语言:javascript
复制
{
  "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"
}

但是我只想打印文本,那么如何在响应列表中打印" text“值呢?先谢谢你,祝你今天愉快。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-06-18 16:36:38

按键使用dict索引,按索引使用列表索引

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2022-06-05 18:00:00

您可以尝试打印(响应“选择”“文本”)

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2022-10-11 03:42:50

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

代码语言:javascript
复制
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 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68038662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档