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 =
“role”: “user”,
“content”: q

print (output)

But i am getting the following error in the output:

What am I doing wrong? Kindly help.

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