我试图为每个在文本提示中循环的关键词至少获取一个响应,然而当我运行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")
回答:
以下是修复了两个问题的代码:
- 设置
outputText = choice.message.content
来获取响应的文本内容。 - 将
response
和print
代码块缩进,使其位于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")