0

I am trying to use OpenAI's API to play with some of the examples they have. However, when I go to load my API key, I get errors. I created a ".env" file and did:

OPENAI_API_KEY=XYZ-123

and then in Python I have the following:

import os
import openai
openai.api_key_path = ".env"

openai.api_key = os.getenv("OPENAI_API_KEY")
print(openai.Model.list())

Every time it tells me my API key is malformed. I can also remove the 3rd line and I get the same error that it is malformed but I copied it directly into the .env file from the website. Also, if I set the key directly in Python, it seems to work just fine:

openai.api_key = "XYZ-123"

But for security, I would prefer I don't see the key in my Python code. Any suggestions on how to resolve this?

logankilpatrick
  • 13,148
  • 7
  • 44
  • 125
  • 1) Relevant docs? 2) `"./.env"`? 3) Wrap env value in quotes? – JBallin Sep 26 '22 at 20:41
  • Docs are here: https://beta.openai.com/docs/api-reference/authentication and none of those suggestions did the trick sadly. – logankilpatrick Sep 26 '22 at 20:58
  • When I search that page for `api_key_path` nothing comes up. Link to the relevant source code maybe? – JBallin Sep 26 '22 at 23:32
  • Maybe we can just rely on setting `api_key` using `os.getenv` instead of `api_key_path` (remove that), as that's what docs suggest. 1) Have you verified that it's reading the env var properly, by printing it or something? 2) Have you tried setting `openai.organization = "YOUR_ORG_ID"` before setting `openai.api_key`? 3) Can you share more info about the error itself? – JBallin Sep 26 '22 at 23:34

4 Answers4

2

I suggest using dotenv for this purpose:

pip install python-dotenv

Usage example:

import dotenv

config = dotenv.dotenv_values(".env")
openai.api_key = config['OPENAI_API_KEY']

It is pretty flexible and works well whenever storing secrets in .env files comes up. Don't forget to add them to .gitignore or use .env.local (ignored by default)!

Lodinn
  • 462
  • 2
  • 9
0

Create a .properties document and add only your API key in this document without any quotation marks or anything. The API key should be the only text in the document. Pass the path for this document in for the value of openai.api_key_path and it should work.

Remember that the value expects the path from the root directory. If you make it in the root directory, just pass in ".properties". If you make it in a sub directory called backend, for example, pass in "backend/.properties".

Hope this helps.

hunter_l
  • 11
  • 1
0

Setting the openai.api_key_path did not seem to work for me. Once I deleted its value, my code started working.

Does not work

This probably searches the path first even if the api_key value is set, and then throws an error.

openai.api_key_path = '.env'

openai.api_key = os.getenv("OPENAI_API_KEY")

Works

Initialize the api_key_path to None again.

openai.api_key_path = None

openai.api_key = os.getenv("OPENAI_API_KEY")
Alex Ishida
  • 1,021
  • 8
  • 9
0

I believe your .env file OpenAI key needs to be in the format:

OPENAI_API_KEY="XYZ-123"
bujian
  • 152
  • 8