我正在寻找一些帮助,从ChatGPT的“openai.Completion.create”函数中提取“text”。
这是我用来生成“响应”的函数:
#Have ChatGPT generate keywords from article
def generate_keywords(article):
response = openai.Completion.create(
model="text-davinci-003",
prompt=article,
temperature=0.7,
max_tokens=60,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=1
)
return response
#---
这里的“article”是我给ChatGPT输入的文本。
“response”在打印时,提供了以下输出:
{
"choices": [
{
"finish_reason": "length",
"index": 0,
"logprobs": null,
**"text": ", Iraq 2008. Image by Flickr User VABusDriverNow, I can\u2019t speak for all of us, but I know that after the war we were still running. Running from our pasts, guilt, shame, fear, and the unexplainable anger that comes with being a"**
}
],
"created": 1680666103,
"id": "cmpl-71oJjQfWtHlTbcVsyfi7zzJRktzVT",
"model": "text-davinci-003",
"object": "text_completion",
"usage": {
"completion_tokens": 60,
"prompt_tokens": 1090,
"total_tokens": 1150
}
}
我想从这个数据结构中提取“文本”。
当我运行这个:
keywords = generate_keywords(article)
print(keywords.values.text)
但我得到了这个:
File "/Users/wolf/Development/OpenAI/generate_medium_story_image/generate_AI_image.py", line 63, in <module>
print(keywords.values.text)
^^^^^^^^^^^^^^^^^^^^
AttributeError: 'builtin_function_or_method' object has no attribute 'text'
1条答案
按热度按时间tp5buhyn1#
只返回完成后的
text
,如下所示:然后像这样打印
keywords
: