Unable to run the code in replit (openai python turbo3.5)

As i’m Taking the input if AI Code in replit , it is showing error i couldn’t find the can anyone please help me to solve this :
import openai
import os
import sys

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”: “Where was it played?”
}])
output=response[‘choices’][0][‘message’][‘content’]

print(response)

as in the tutorial given we have to use the code to run , it is showing error in this case

and also tell me that how can we get the line by line in the run (console)tab were as in my replit it is showing like a paragraph :upside_down_face:

Hi @indoorubhargavi

In the latest OpenAI package the response.choices object type is changed . Kindly make the following changes in your code.

response = openai.chat.completions.create

and

return response.choices[0].message.content

Also please remove the part in the code which is not necessary for eg.: “make an account or sign in…”
This must solve your problem. We will be updating the same in the training content so that other students don’t face this issue. If you still face any issues please feel free to get back to us.

HI @sneharsh.kerkar
can u please tell me the error i did in this code as i follow your instruction but i couldn’t make it right


can u please help me to do correct with this code

Hi @indoorubhargavi

Your code is correct. Only thing there is an indentation error on line 36. You can simply fix this by:

  • Come on line no. 35 at reponse.choices[0].content. Go few spaces back till the previous line and press enter. This will automatically indent the code correctly.

If this doesn’t solve your issue. Please share the entire code here. I will debug it and share the working code with you.

hi ,
can u please tell me how to align the below output in console lines in each line as it coming in the paragraph form how can i edit this option in replit.

Hi @indoorubhargavi

To get the output on a new line, you can store the response in an output variable and then print the output variable. Below is an example:

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

This will print a clean output on the console. Please try this and let us know if you are facing any issue.

2 Likes

In line 36, you wrote “response.choices[0].message.content” but have not assigned it to any variable. Also, you are passing the entire “response” as the parameter into the print() function.
So, you might want to assign the value in line 36 to a variable and pass that variable as the parameter to the print() function.
The final code will contain these lines:
line 36 output_variable = response.choices[0].message.content
line 37 print(output_variable)