python - 聊天 GPT3.5-turbo API 不打印聊天响应。没有代码错误

标签 python openai-api chatgpt-api

我在 Repl 中构建了一个基本的聊天导师 API,但在运行时没有收到聊天响应。 我的 key 在 OpenAI 中设置正确,但设置为个人 - 这是一个问题吗?

我没有代码错误,因此我不确定如果代码有问题,会出现什么问题。

import os
my_secret = os.environ['OPENAI_API_KEY']
import openai

import sys
  
try:
  openai.api_key = os.environ['OPENAI_API_KEY']
except KeyError:
  sys.stderr.write("""
  You haven't set up your API key yet.
  
  If you don't have an API key yet, visit:
  
  https://platform.openai.com/signup

  1. Make an account or sign in
  2. Click "View API Keys" from the top right menu.
  3. Click "Create new secret key"

  Then, open the Secrets Tool and add OPENAI_API_KEY as a secret.
  """)
  exit(1)

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
)

print(response)

topic = input("I'm your new Latin tutor. What would you like to learn about?\n> ".strip())

messages = [{"role": "system", "content": f"I want to do some interactive instruction. I want you to start explaining the concept of Latin language to me at a 10th grade level. Then stop, give me a multiple choice quiz, grade the quiz, and resume the explanation. If it get the quiz wrong, reduce the grade level by 3 for the explanation and laguage you use, making the language simpler. Otherwise increase it to make the language harder. Then, quiz me again and repeat the process. Do not talk about changing the grade level. Don't give away to answer to the quiz before the user has a chance to respond. Stop after you've asked each question to wait for the user to answer."}]

while True:

  response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=messages
  )

first = False
while True:
  if first:
    question = input("> ")
    messages.append({"role": "user", "content": question})

    first = True

    response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=messages
    )

    content = response['choices'][0]['messages']['content'].strip()

    print(f"{content}")
    messages.append({"role": "assistant", "content": content})

聊天提出问题:

你想了解什么?

但不回答任何格式的问题,例如:

I want to learn about Latin

What is a language?

最佳答案

您的系统消息似乎很长。目前已知的模型不太关注系统消息( https://community.openai.com/t/system-message-how-to-force-chatgpt-api-to-follow-it/82775/3 )。

尝试将说明放在用户消息本身中,并使用更少的字数。

关于python - 聊天 GPT3.5-turbo API 不打印聊天响应。没有代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75721401/

相关文章:

openai-api - 我如何使用 openai 的 gpt 3 来查找坏词的替代拼写?

openai-api - Chatgpt api url 问题 : Chatgpt3. 5 错误

python - OpenAI API错误: "The model ' curie:ft-personal-2023-09-15-06-05-0 1' does not exist" when trying to delete a fine-tuned model

python - pandas:根据其他列乘以列

javascript - 从 tableau 工作簿下载所有过滤器

python - 如何在backtrader中查看fed数据?

python - 使用 Openai 在 Google 表格中获取完整文章

python - Spark DataFrame 方法 `toPandas` 实际上在做什么?

xml - 制作我自己的 mujoco 环境的最佳方法是什么?

python - 有没有办法可以处理 Langchain QA 检索中的上下文和一般问题?