Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bkowshik opened this issue Jun 14, 2023 · 0 comments
Open

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

bkowshik opened this issue Jun 14, 2023 · 0 comments

Comments

@bkowshik
Copy link

bkowshik commented Jun 14, 2023

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"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant