4

I am just trying some simple functions in Python with OpenAI APIs but running into an error:

I have a valid API secret key which I am using.

Code:

>>> import os
>>> import openai
>>> openai.api_key = os.getenv("I have placed the key here")
>>> response = openai.Completion.create(model="text-davinci-003", prompt="Say this is a test", temperature=0, max_tokens=7)

Simple test

3
  • 3
    Please do not upload images of code/data/errors when asking a question.. And you've cut out the most important part of the traceback. Commented Feb 1, 2023 at 16:47
  • 4
    It looks like you pasted your actual API key, but os.getenv() expects the NAME of the ENV variable, not the value itself. If you're going to just paste the value of the key, I think you can just do: openai.api_key = "xxxxxxxxxx" and paste your key there. Commented Feb 1, 2023 at 16:48
  • 1
    @YevhenKuzmovych: Apologies. I will make sure to follow the standards. Thanks for pointing it out. Commented Feb 2, 2023 at 11:58

3 Answers 3

17

Option 1: OpenAI API key not set as an environment variable

Change this...

openai.api_key = os.getenv('sk-xxxxxxxxxxxxxxxxxxxx')

...to this.

openai.api_key = 'sk-xxxxxxxxxxxxxxxxxxxx'


Option 2: OpenAI API key set as an environment variable (recommended)

There are two ways to set the OpenAI API key as an environment variable:

  • using an .env file (easier, but don't forget to create a .gitignore file) or
  • using Windows Environment Variables.

Way 1: Using an .env file

Change this...

openai.api_key = os.getenv('sk-xxxxxxxxxxxxxxxxxxxx')

...to this...

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

Also, don't forget to use the python-dotenv package. Your final Python file should look as follows:

# main.py

import os
from dotenv import load_dotenv
from openai import OpenAI

# Load environment variables from the .env file
load_dotenv()

# Initialize OpenAI client with the API key from environment variables
client = OpenAI(
    api_key=os.getenv("OPENAI_API_KEY"),
)

It's crucial that you create a .gitignore file not to push the .env file to your GitHub/GitLab and leak your OpenAI API key!

# .gitignore

.env

Way 2: Using Windows Environment Variables (source)

STEP 1: Open System properties and select Advanced system settings

Screenshot 1

STEP 2: Select Environment Variables

Screenshot 2

STEP 3: Select New

STEP 4: Add your name/key value pair

Variable name: OPENAI_API_KEY

Variable value: sk-xxxxxxxxxxxxxxxxxxxx

STEP 5: Restart your computer (IMPORTANT!)

Your final Python file should look as follows:

# main.py

import os
from dotenv import load_dotenv
from openai import OpenAI

# Initialize OpenAI client
# It will automatically use your OpenAI API key set via Windows Environment Variables
client = OpenAI()
Sign up to request clarification or add additional context in comments.

3 Comments

Works like a charm. Yes, missed the stupid getenv thing. I am new to Python. More of a PS guy. Thanks a lot
it works thanks
5

For me the solution was to add a load_dotenv() call.

from dotenv import load_dotenv
import os

# load environment variables from .env file
load_dotenv()

# get the environment variable
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

Comments

0

Also make sure you have installed the load_dotenv into your virtual environment into your IDE (for example like Visual Studio code) though it may be present in your requirements.txt file

pip install python-dotenv

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.