Traceback error when trying to get only answer as output (in OpenAI GPT-3.5 Turbo)

I am taking the Artificial Intelligence training from Bolt. As part of it I was trying to write a code that returned only the output message to a question and not the model and other specifications. I was asked to use this code: output=response[‘choices’][0][‘message’][‘content’] to define the output variable with only the answer. This is my code:
import openai
import os
import sys

q=input(“What is your question/instruction? \n”)
openai.api_key = os.environ[‘OPENAI_API_KEY’]

response = openai.chat.completions.create(
model=“gpt-3.5-turbo”,
messages=[{
“role”: “user”,
“content”: q
}])

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

But i am getting the following error in the output:
image

What am I doing wrong? Kindly help.

@huzaifa.bohori
Can you replace the output line in the same format as given below and check if you are able to get the output

output = response.choices[0].message.content

Thank you so much! It worked

1 Like