Python gpt 3.5 turbo output running infinite

As instruction in the video in module 1 of AI training
it says to use while loop to get asking questions
but when I


used while loop and prompt the first question it provides infinite answer or it just repeats it infinitely

Hi @fadesam536

Can you please check your code if the output is printing in an infinite while loop? This could be the reason. Also you can copy paste the code here so it will help me to debug.

from boltiotai import openai
import os
import sys

question=input(“WHAT IS YOUR QUESTION: \n”)

while True:
openai.api_key = os.environ[‘OPENAI_API_KEY’]
if openai.api_key == “”:
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.chat.completions.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": question
    }])
output=response['choices'][0]['message']['content']
print(output)

Here is the code please tell me where did i gone wrong ???

thankyou

Hi @fadesam536

The infinite printing occurs because of the while True loop in your code. Each time the loop runs, it asks the OpenAI API the same question repeatedly.

Here is the updated code. Please try this and let us know if this solves the issue.


from boltiotai import openai
import os
import sys

while True:
    question = input("WHAT IS YOUR QUESTION (type 'exit' to quit): \n")
    if question.lower() == 'exit':
        break
    openai.api_key = os.environ['OPENAI_API_KEY']
    if openai.api_key == "":
        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.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{
            "role": "system",
            "content": "You are a helpful assistant."
        }, {
            "role": "user",
            "content": question
        }])
    output = response['choices'][0]['message']['content']
    print(output)

no it didnt resolve its showing same infinite answers with this code too

Hi @fadesam536

Please share your reply to support@boltiot.com and I will debug the issue.

hii @fadesam536

import openai

Ensure you have set the OPENAI_API_KEY as an environment variable

openai.api_key = ‘your-api-key-here’

Replace ‘question’ with the actual user input

question = “Who won the world series in 2020?”

response = openai.chat.completions.create(
model=“gpt-3.5-turbo”,
messages=[{
“role”: “system”,
“content”: “You are a helpful assistant.”
}, {
“role”: “user”,
“content”: question
}]
)

output = response[‘choices’][0][‘message’][‘content’]
print(output)