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