gpt4 book ai didi

python - 如何在 python 中获取 OpenAIobject 中的项目?

转载 作者:行者123 更新时间:2023-12-02 05:46:30 27 4
gpt4 key购买 nike

我想获取此数据结构中通过 GPT3 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
}
}

我如何获得它的“文本”组件?例如,如果调用此对象:qa ...我可以输出

qa['choices']

我得到了与上面相同的项目...但是向其中添加 .text 或 ['text'] 并不能做到这一点。并得到一个错误

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

最佳答案

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

关于python - 如何在 python 中获取 OpenAIobject 中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74524530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com