gpt4 book ai didi

python - OpenAI ChatGPT (GPT-3.5) API : How to implement a for loop with a list of questions in Python?

转载 作者:行者123 更新时间:2023-12-02 05:48:49 25 4
gpt4 key购买 nike

我一直在尝试运行一个 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 列表中看到第一个答案。为什么会这样呢?我该如何解决它?我想查看所有回复。

非常感谢您的帮助!

最佳答案

您需要打印输出

如果您运行 test.py,OpenAI API 将返回完成结果:

['The winner of the UEFA Champions League in 2017 was Real Madrid.','The 2014 FIFA World Cup was won by Germany.']

测试.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)

关于python - OpenAI ChatGPT (GPT-3.5) API : How to implement a for loop with a list of questions in Python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75743057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com