wandb / edu

Educational materials on deep learning by Weights & Biases
http://wandb.ai
GNU General Public License v2.0
562 stars 257 forks source link

API key set as an environment variable is not recognised by openai #108

Open bkowshik opened 1 year ago

bkowshik commented 1 year ago

NOTE: I have opened the notebook on Google Colab by clicking on the icon at the top of the Jupyter notebook.

Expected outcome

Since, the API token is pasted into the prompt, the expected behaviour is for the API calls to work.

if os.getenv("OPENAI_API_KEY") is None:
  if any(['VSCODE' in x for x in os.environ.keys()]):
    print('Please enter password in the VS Code prompt at the top of your VS Code window!')
  os.environ["OPENAI_API_KEY"] = getpass("Paste your OpenAI key from: https://platform.openai.com/account/api-keys\n")

assert os.getenv("OPENAI_API_KEY", "").startswith("sk-"), "This doesn't look like a valid OpenAI API key"
print("OpenAI API key configured")

Paste your OpenAI key from: https://platform.openai.com/account/api-keys
··········
OpenAI API key configured

As additional verification, I checked the value of the environment variable manually using the code snippet below:

print(os.environ["OPENAI_API_KEY"])

Actual outcome

Instead, I get the following error; the openai package for some reason is not able to read the token.

---------------------------------------------------------------------------
AuthenticationError                       Traceback (most recent call last)
[<ipython-input-8-6552200a9f15>](https://0znpwkodc2fc-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20230612-060131-RC00_539609191#) in <cell line: 1>()
      1 for temp in [0, 0.5, 1, 1.5, 2]:
----> 2   pprint(f'TEMP: {temp}, GENERATION: {generate_with_temperature(temp)}')

6 frames
[/usr/local/lib/python3.10/dist-packages/openai/util.py](https://0znpwkodc2fc-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20230612-060131-RC00_539609191#) in default_api_key()
    184         return openai.api_key
    185     else:
--> 186         raise openai.error.AuthenticationError(
    187             "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://platform.openai.com/account/api-keys for details."
    188         )

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://platform.openai.com/account/api-keys for details.

Temporary solution

The AuthenticationError provided an alternative to set API. Using this, the authentication was successful. 🎉

openai.api_key = os.environ["OPENAI_API_KEY"]