1

I built a basic chat tutor API in Repl but I am getting no chat response when running. My secret key is set up correctly in OpenAI, but set to personal - is this an issue?

I have no code errors so I am unsure what's going wrong if there is an issue with code.

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})

Chat asks question:

What do you want to learn about?

But does not respond to questions in any format e.g:

I want to learn about Latin

What is a language?

1 Answers1

0

Your system message seems quite long. The current model is known to not pay much attention to the system message (https://community.openai.com/t/system-message-how-to-force-chatgpt-api-to-follow-it/82775/3).

Try putting the instructions in the user message itself, and in fewer words.

dhruv
  • 1