英文:
OpenAI ChatGPT (GPT-3.5) API: How to implement a for loop with a list of questions in Python?
问题
我一直在尝试运行一个for循环,通过OpenAI ChatCompletion API来遍历,但似乎无法使其工作 - 我感到困惑。我的目标是获得所有回复的列表。
基本上,我有一组句子;让我们称之为input_list
。以下是这是什么样子的示例
['Who won the Champions League in 2017?', 'Who won the World Cup in 2014?', ...]
这是我尝试遍历输入的方式:
output = []
for i in range(len(input_list)):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a chatbot."},
{"role": "user", "content": input_list[i]},
]
)
chat_response = response['choices'][0]['message']['content']
output.append(chat_response)
然而,当运行此代码时,回复似乎无法追加 - 我只看到output
列表中的第一个答案。为什么会这样?我该如何修复它?我想看到所有的回复。
非常感谢您的帮助!
英文:
I've been trying to run a for loop to run through the OpenAI ChatCompletion API, but I don't seem to make it work - I'm puzzled. My goal is to have a list of all the responses
Basically, I have a list of sentences; let's call this list input_list
. Here's an example of how this would look like
['Who won the Champions League in 2017?', 'Who won the World Cup in 2014?', ...]
And here's how I tried to loop through the input:
output = []
for i in range(len(input_list)):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a chatbot."},
{"role": "user", "content": input_list[i]},
]
)
chat_response = response['choices'][0]['message']['content']
output.append(chat_response)
When running this, however, the responses don't seem to append - I only ever see the very first answer in the output
list. Why is this the case? And how can I fix it? I would like to see all responses.
Many thanks in advance for your help!
答案1
得分: 2
你需要打印output
。
如果你运行test.py
,OpenAI API会返回一个完成的结果:
> ['2017年UEFA冠军联赛的获胜者是皇家马德里。', '2014年FIFA世界杯由德国获胜。']
test.py
import openai
import os
openai.api_key = os.getenv('OPENAI_API_KEY')
input_list = ['2017年谁赢得了冠军联赛?', '2014年谁赢得了世界杯?']
output = []
for i in range(len(input_list)):
response = openai.ChatCompletion.create(
model = 'gpt-3.5-turbo',
messages = [
{'role': 'system', 'content': '你是一个聊天机器人。'},
{'role': 'user', 'content': input_list[i]},
]
)
chat_response = response['choices'][0]['message']['content']
output.append(chat_response)
print(output)
英文:
You need to print the output
.
If you run test.py
the OpenAI API will return a completion:
> ['The winner of the UEFA Champions League in 2017 was Real Madrid.',
> 'The 2014 FIFA World Cup was won by Germany.']
test.py
import openai
import os
openai.api_key = os.getenv('OPENAI_API_KEY')
input_list = ['Who won the Champions League in 2017?', 'Who won the World Cup in 2014?']
output = []
for i in range(len(input_list)):
response = openai.ChatCompletion.create(
model = 'gpt-3.5-turbo',
messages = [
{'role': 'system', 'content': 'You are a chatbot.'},
{'role': 'user', 'content': input_list[i]},
]
)
chat_response = response['choices'][0]['message']['content']
output.append(chat_response)
print(output)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论