I am not able to generate any output while using the code as described in Module 1 - Taking input for the AI code . Instead its giving some error . My code is written like this
import openai
import os
import sys
question=input(“What is your question? \n”)
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
- 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”,“assistant”,“content”:“The Los Angeles Dodgers won the World Series in 2020.”},
{ “role”: “user”,“content”: question}
]
)
output=response[‘choices’][0][‘message’][‘content’]
print(output,“\n”)
Hi @bikiran.bordoloi.201
In the latest OpenAI package the, response.choices
object type is changed .Make sure that you have entered your Openai API key in the Repl.it Secrets tag. Kindly make the following changes in your code.
response = openai.chat.completions.create
and
return response.choices[0].message.content
This must solve your problem. We have also made the necessary changes in our Training content as well so that other students don’t face this issue.If you still face any issues please feel free to get back to us.
hi @bikiran.bordoloi.201
In this code please check your secret key and your billing cycle of openAi, your 5$ which is given to new user by openAI is used. that’s why you need a new secret key to add a project, for creating a secret key follow steps:
- create a new google account
- use that account on open ai website
- add new api key and copy it on your project
I’m having the same problem. But it’s still not running. Can you please verify the required code again ?
Hi @dasdikshita763
The code provided in the in training is up to date and is working. Please check your code structure and API key correctly. You could also share screenshot of your code so that we can guide you accordingly.
Hi @bikiran.bordoloi.201
Apologies for delayed response. I have rectified your code and there were some syntax and indentation errors in the errors. Here is the working code of your project. Please do give this a try and let us know.
import openai
import os
import sys
question=input("What is your question? \n")
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": "Where was it played?"
}])
output=response.choices[0].message.content
print(output)
If you still face any issues, please fell free to get back to us.