visual-chatgpt icon indicating copy to clipboard operation
visual-chatgpt copied to clipboard

Display can't find openai_api_key.

Open baipengfeifei opened this issue 1 year ago • 14 comments

Did not find openai_api_key, please add an environment variable OPENAI_API_KEY which contains it, or pass openai_api_key as a named parameter. (type=value_error) how to solve it?

baipengfeifei avatar Mar 13 '23 10:03 baipengfeifei

Did you run:

# prepare your private openAI private key
export OPENAI_API_KEY={Your_Private_Openai_Key}

If you did run it and restarted your pc/terminal or something then you might have to do it again

miitchel avatar Mar 13 '23 12:03 miitchel

Did you run:

# prepare your private openAI private key
export OPENAI_API_KEY={Your_Private_Openai_Key}

If you did run it and restarted your pc/terminal or something then you might have to do it again

If you run it on Windows, use set instead of export, and your openai api key can be found here.

Golevka2001 avatar Mar 13 '23 14:03 Golevka2001

I added this to the visual_chatgpt.py file (line 11): os.environ["OPENAI_API_KEY"] = "INSERT_YOUR_KEY_HERE" (replace with your key and save the file)

mikesoes avatar Mar 13 '23 19:03 mikesoes

i tried both “set OPENAI_API_KEY=sk-xxxx” and “set OPENAI_API_KEY={sk-xxxx}” still not work,could anyone help?

nagaki09 avatar Mar 14 '23 14:03 nagaki09

I have the same problem in PyCharm on Linux, and I have tried all the remedies above plus setting the environment variable in the IDE.

However, when I run the program using the same API key from the terminal, it instead complains about that the (newly generated) API key is Incorrect...

Moomindad avatar Mar 14 '23 16:03 Moomindad

i tried both “set OPENAI_API_KEY=sk-xxxx” and “set OPENAI_API_KEY={sk-xxxx}” still not work,could anyone help?

hello, try removing the {} : “set OPENAI_API_KEY=sk-xxxx”, works for me

ecureuilfourbe avatar Mar 14 '23 17:03 ecureuilfourbe

i tried both “set OPENAI_API_KEY=sk-xxxx” and “set OPENAI_API_KEY={sk-xxxx}” still not work,could anyone help?

I provide a direct solution: enter System and Environmental Variables, and write the key and value to both System and User

Golevka2001 avatar Mar 15 '23 02:03 Golevka2001

I have the same problem before and I solve it by deactivate and then activate the virtual env after set the env variable. Something like below:

conda env config vars set OPENAI_API_KEY=sky
conda deactivate env_name
conda activate env_name

Hope this help you.

Rain2022debug avatar Mar 15 '23 13:03 Rain2022debug

For testing you can edit visual_chatgpt.py file at line 2 after import os put: os.environ['OPENAI_API_KEY'] = 'your key'

JihadAKl avatar Mar 15 '23 16:03 JihadAKl

I added this to the visual_chatgpt.py file (line 11): os.environ["OPENAI_API_KEY"] = "INSERT_YOUR_KEY_HERE" (replace with your key and save the file)

It is work for me by adding that.

my computer system : Mac M1 using pipenv

wastu01 avatar Mar 15 '23 18:03 wastu01

I solved it by the fix below.

self.llm = OpenAI(temperature=0, openai_api_key="sk-xxx")

hjw21century avatar Mar 26 '23 12:03 hjw21century

If you are using windows.

  1. Create .env file in the same folder as your python program.
  2. OPENAI_API_KEY="xxx" in the .env file
  3. pip install python-dotenv In your code, declare
from dotenv import load_dotenv

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

sagarspatil avatar Apr 01 '23 14:04 sagarspatil

Context: I am reading my token from a config file and this did not work:

llm = OpenAI(openai_api_key=str(api_key_input))
llm = OpenAI(temperature=0.9)

But this did: llm = OpenAI(temperature=0.9, openai_api_key=str(api_key_input))

thanks @hjw21century

solomonakinyemi avatar Jul 02 '23 07:07 solomonakinyemi

If you are using -> Jupiter Try this

%env OPENAI_API_KEY="sk-*****"

amt0ny avatar Aug 21 '23 13:08 amt0ny