gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:45:05 26 4
gpt4 key购买 nike

我在 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/

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