OpenAI GPT-3 API错误:“属性错误:'builtin_function_or_method'对象没有属性'text'“

2izufjch  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(209)

我正在寻找一些帮助,从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'
tp5buhyn

tp5buhyn1#

只返回完成后的text,如下所示:

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['choices'][0]['text'] # Change this

然后像这样打印keywords

keywords = generate_keywords(article)
print(keywords)

相关问题