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

我在尝试从ChatGPT的”openai.Completion.create”函数中提取”text”时需要一些帮助。

这是我用来生成”response”的函数:

#Have ChatGPT generate keywords from articledef 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  }}

我想从这个数据结构中提取”text”。

当我运行以下代码时:

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'

回答:

像这样只返回完成中的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'] # 修改此处

然后像这样打印keywords

keywords = generate_keywords(article)print(keywords)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注