OpenAI ChatGPT (GPT-3.5) API:如何在Python中使用问题列表实现for循环?

huangapple go评论69阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年3月15日 17:54:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定