有没有更好的方法来提取和获取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

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

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