如何在 Python 中获取 OpenAI 对象内的项目?

Kat*_*sto 6 python openai-api gpt-3

我想获取通过 G​​PT3 OpenAI 输出的数据结构中的文本。我正在使用Python。当我打印对象时,我得到:

<OpenAIObject text_completion id=cmpl-6F7ScZDu2UKKJGPXTiTPNKgfrikZ at 0x7f7648cacef0> JSON: {
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "text": "\nWhat was Malcolm X's original name?\nMalcolm X's original name was Malcolm Little.\n\nWhere was Malcolm X born?\nMalcolm X was born in Omaha, Nebraska.\n\nWhat was the profession of Malcolm X's father?\nMalcolm X's father was a Baptist minister.\n\nWhat did Malcolm X do after he stopped attending school?\nMalcolm X became involved in petty criminal activities."
    }
  ],
  "created": 1669061618,
  "id": "cmpl-6F7ScZDu2gJJHKZSPXTiTPNKgfrikZ",
  "model": "text-davinci-002",
  "object": "text_completion",
  "usage": {
    "completion_tokens": 86,
    "prompt_tokens": 1200,
    "total_tokens": 1286
  }
}
Run Code Online (Sandbox Code Playgroud)

我如何获得它的“文本”组件?例如,如果这个对象被称为:qa ...我可以输出

qa['choices']
Run Code Online (Sandbox Code Playgroud)

我得到了与上面相同的项目...但是添加一个.text或 ['text'] 到此并不会执行此操作,并且会出现错误。

但不确定如何隔离“文本”我已阅读文档,但找不到这个... https://beta.openai.com/docs/api-reference/files/delete?lang=python

小智 3

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

text = x['choices'][0]['text']
print(text)  # , everyone, and welcome to the first installment of the new opening
Run Code Online (Sandbox Code Playgroud)