import openai
import os
import sys
question = input(“Enter your question/instruction: \n”)
try:
openai.api_key = os.environ[‘OPENAI_API_KEY’]
except KeyError:
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
- Make an account or sign in
- Click “View API Keys” from the top right menu.
- 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)
This is what I see on the console:
Hi @niralinj24 , In the latest OpenAI package the response.choices
object type is changed
you can replace
output = response[‘choices’][0][‘message’][‘content’]
with the following:
output = response.choices[0].message.content
Below is the entire working code.Replace OPENAI_API_KEY with your key from the OpenAI Dashboard.
import openai
import os
import sys
question = input("Enter your question/instruction: \n")
try:
openai.api_key = os.environ['OPENAI_API_KEY']
except KeyError:
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
Make an account or sign in
Click "View API Keys" from the top right menu.
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)
If you still face any issues, please feel free to get back to us.
1 Like