有没有更好的方法来提取和获取OpenAI的响应?

我试图为每个在文本提示中循环的关键词至少获取一个响应,然而当我运行Python代码生成响应时,我得到了以下错误:

PS C:\Users\...\Documents\Article-gen> & C:/Users/.../AppData/Local/Microsoft/WindowsApps/python3.11.exe c:/Users/.../Documents/Article-gen/createArticle.pyChatGPT API replies for Amazon (AMZN) stock:Traceback (most recent call last):  File "C:\Users\...\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\openai\openai_object.py", line 59, in __getattr__    return self[k]           ~~~~^^^KeyError: 'text'During handling of the above exception, another exception occurred:Traceback (most recent call last):  File "c:\Users\...\Documents\Article-gen\createArticle.py", line 29, in <module>    outputText = choice.text.strip()                 ^^^^^^^^^^^  File "C:\Users\...\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\openai\openai_object.py", line 61, in __getattr__    raise AttributeError(*err.args)AttributeError: text

我该如何修复这个问题,以便每个关键词至少能得到一个响应?这是我正在使用的Python代码:

import openaiimport os# Set OpenAI API keyopenai.api_key = ""# Then, you can call the "gpt-3.5-turbo" modelmodelEngine = "gpt-3.5-turbo"# set your input textinputText = "Write a 1,500 word that is highly speculative bullish article IN YOUR OWN WORDS on {} stock and why it went up, you must include how it could affect the stock price and future outcome of the business. Include subheadings in your own words and act like you know it all and be an authoritative expert on the topic. Now write."# Array of keywords to generate article onkeywords = ["Nio (NIO)", "Apple (AAPL)", "Microsoft (MSFT)", "Tesla (TSLA)", "Meta (META)", "Amazon (AMZN)"]# Switches and injects keywords into API requestfor keyword in keywords:    # set input text with the current keyword    inputSent = inputText.format(keyword)# Send an API request and get a response, note that the interface and parameters have changed compared to the old modelresponse = openai.ChatCompletion.create(   model=modelEngine,   messages=[{"role": "user", "content": inputSent }],   n = 1)print("ChatGPT API replies for", keyword, "stock:\n")for choice in response.choices:        outputText = choice.text.strip()        print(outputText)        print("------")print("\n")

回答:

以下是修复了两个问题的代码:

  1. 设置outputText = choice.message.content来获取响应的文本内容。
  2. responseprint代码块缩进,使其位于for keyword...循环内。

请注意,我在测试时为了简洁稍微调整了提示语。

import openaiimport os# Set OpenAI API keyopenai.api_key = ""# Then, you can call the "gpt-3.5-turbo" modelmodelEngine = "gpt-3.5-turbo"# set your input text# inputText = "Write a 1,500 word that is highly speculative bullish article IN YOUR OWN WORDS on {} stock and why it went up, you must include how it could affect the stock price and future outcome of the business. Include subheadings in your own words and act like you know it all and be an authoritative expert on the topic. Now write."inputText = "Write a 100 word tweet is highly speculative bullish article IN YOUR OWN WORDS on {} stock and why it went up, you must include how it could affect the stock price and future outcome of the business. Include subheadings in your own words and act like you know it all and be an authoritative expert on the topic. Now write."# Array of keywords to generate article onkeywords = ["Nio (NIO)", "Apple (AAPL)", "Microsoft (MSFT)", "Tesla (TSLA)", "Meta (META)", "Amazon (AMZN)"]# Switches and injects keywords into API requestfor keyword in keywords:    # set input text with the current keyword    inputSent = inputText.format(keyword)    # Send an API request and get a response, note that the interface and parameters have changed compared to the old model    response = openai.ChatCompletion.create(    model=modelEngine,    messages=[{"role": "user", "content": inputSent }],    n = 1    )    print("ChatGPT API replies for", keyword, "stock:\n")    for choice in response.choices:            #outputText = choice.text.strip()            outputText = choice.message.content            print(outputText)            print("------")    print("\n")

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中创建了一个多类分类项目。该项目可以对…

发表回复

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