Facing issue with OPEN_API_KEY while writing your first AI code

I am facing an error while running the API key code. I even tried it after generating a new API key, but still, it is showing the same error showing

Hi @kritikapant03

Can you share screenshots of entire code with us? This will help us to debug the code better.

It seems like you haven’t stored the key in the ‘secrets’ section available on replit.

If that is the case then you click on the secrets section (present bottom right list of tools.) There you will find the option to add the key. You will then copy the key and name it as ‘OPEN_API_KEY’(or any other name you want but be sure to use the same name every else you want to use the key.)

Yes, please find it below

from boltiotai 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.

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”: “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?”
}])

print(response)

I had already stored it in ‘secrets’. Also, I did all the processes you mentioned here. I generated the key again to check for any issues but couldn’t get it.

Can you share a screenshot of the secrets section? Make sure that the API is’nt visible.

yes please the attached photo

H @kritikapant03

You are getting the issue because the API key variable is not matching in the code. You have used OPENAI_API_KEY variable in the code but in the secrets section you have written it as OPEN AI_API_KEY (This has an extra space after N) .

Please edit the API key variable in the secrets section to OPENAI_API_KEY so that it matches with code . Then run the code again.

Do try this and let us know. If you still face any issue, please feel free to get back to us.