DLAI Course - trouble loading OpenAI API key

Hello! Newbie here. I am taking ChatGPT Prompt Engineering for Developers, and am unable to load the OpenAI API key to follow along with the the course. I am using Google Colab for Jupyter.

I loaded all the packages and dont get any errors when running this code:

import openai

import os

from dotenv import load_dotenv, find_dotenv

_ = load_dotenv(find_dotenv())

openai.api_key = os.getenv(‘OPENAI_API_KEY’)

However, nothing gets loaded into the openai.api_key



I highly recommend that you go through the course in the notebook provided with the course. This notebook has already an API Key provided. You just need to execute the cell. You DONT need to enter any API Key in the notebook of the course.

If you still want to do it in a separate notebook, you only have to enter your API Key like this:

openai.api_key = <YOUR_API_KEY>

That’s it!

Please let me know how it goes!

1 Like

I’m curious to know more about how the built-in api key has been obfuscated / transformed as I
would like to implement this in my own shared notebooks.

If i print the api key from os it gives me a much longer string not beginning with ‘sk-…’

Does os.getenv() do this natively or is the public notebook doing something extra?

+1 for @Juan_Olano ‘s suggestion to just use the string of your own key. But can you elaborate on the process you followed; did you setenv with your API key before calling getenv to retrieve it? In all the several threads describing this problem, no one mentions that step. Just, well, curious. Cheers

If you do print(os.getenv(‘OPENAI_API_KEY’)) in the last cell you’ll be able to see the api key being used.

Keep in mind that this is a ‘special’ api key that will only work on the notebooks of this course, so don’t expect it to work on your own notebooks or anywhere outside the course.

Yes, this api key looks different from those given to us normally. And again, this is a special api key.

Never deploy code to public where your api key is accessibly to others. This is the same as giving a blank check to internet.

My take on this would be the following:

If working within the course’s notebooks, we don’t have to provide an api key.

For other environments you can do several things:

  1. Enter the api key directly in the openai.api_key variable.
  2. Set the api key in your OS env and just call it, as is done in the notebooks of the course.
  3. Store it in a file somewhere only you have access to, and retrieve it from there. This includes retrieving it by reading the file directly or by calling it via some api, etc.

Just be sure that nobody else can access your api key.

Lets hear other learners with their thoughts and comments :slight_smile:

1 Like

Yes, this is why I asked. I wondered if there is a best practice method for implementing this in a jupyter notebook using encryption. The magic method %env displays the api key as hidden so I wondered if an extra step was being used. It sounds like the openai API has special keys for special users. Which is one of many reasons why they should remove “open” from their company name :slight_smile:

to better understand this, you could try inserting a test for the presence of the environment variable like this…

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

print('OPENAI_API_KEY' in os.environ)

if it outputs False then you know OPENAI_API_KEY hasn’t been set in the environment and you can’t use the os.getenv() method to retrieve it. Instead, use one of the steps provided in a reply above.

or do this…

os.environ['OPENAI_API_KEY'] = 'sk-my-openai-api-key-string-here'
print('OPENAI_API_KEY' in os.environ)

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

I generated my own key at OpenAI API. Just login, generate your key and copy it into your python code thats running locally on your terminal.

openai.api_key = ‘sk-whateverThePlatformGeneratedForYourAPIKEY’

Absolutely, I have followed these steps and I was able to do most of the exercises in my own Jupyter notebook

I am working in my local environment using Jupyter.

I solved the loading problem with the statement
openai.api_key = os.getenv('OPENAI_API_KEY') by defining the API key in .env file in the same folder. FIle content format is OPENAI_API_KEY=‘sk-my-openai-api-key-string-here’

However, Using my key I used to get AuthenticationError initially and in few days of trial and error the same code/API key started working. I assume its a delay to enable a key at openai end.

1 Like

Just to clarify what a line "_ = load_dotenv(find_dotenv())"does. It looks .env file and check ‘OPENAI_API_KEY’ in .env file in the same directory as notebook. “" is the convention for Python, if assignment is not needed. If "” is replaced with some variable, such as key_chek and print(key_check), it is returned the value True. So the line check key exist and the next line load the key.
As everyone, it is recommended to run the notebook in the Coursera environment, as everything including key is included. If one, wants to run the notebook locally, you have to get your own API key from OpenAI and create and write the key at .env file you create. .env file can not be created from a simple file creation, but can be created using some IDE such as Visual Studio Code or PyCharm.

I see a notebook in the frame to the left of the video frame. But, when I select a cell and run, nothing seems to happen, but for the count of the cell to increase.
What am I missing?
Thank you

That means the kernel has completed running that cell.