#I'm getting error ' No API key provided.' but my API key is correct

2 messages · Page 1 of 1 (latest)

shut monolith
#

I'm following the quickstart tutorial for PYTHON and using that repo: https://github.com/openai/openai-quickstart-python

I placed my API key within the variable OPENAI_API_KEY, and then run flash run, but when sending a text it shows that error on CLI:

openai.error.AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = <API-KEY>', or you can set the environment variable OPENAI_API_KEY=<API-KEY>). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = <PATH>'. You can generate API keys in the OpenAI web interface. See https://onboard.openai.com for details, or email support@openai.com if you have any questions.

the same API key works when doing the nodejs code tutorial.
Does anyone know what could be wrong?

hollow forge
# shut monolith I'm following the quickstart tutorial for PYTHON and using that repo: https://gi...

What you need to do is to put your environment variables in a file called .env. So you need to rename the file that you have been given (.env.example) to .env. Secondly, at the top of app.py, you need to add the following:

from dotenv import load_dotenv
load_dotenv()

You can remove the openai.api_key = os.getenv("OPENAI_API_KEY") line from app.py, and replace it with openai.api_key = os.environ["OPENAI_API_KEY"].

If it didn't work then let me know.