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

Can't identify my OPENAI_API_KEY in .env file #52

Open
aldkfjwe opened this issue Jun 30, 2024 · 12 comments
Open

Can't identify my OPENAI_API_KEY in .env file #52

aldkfjwe opened this issue Jun 30, 2024 · 12 comments
Assignees
Labels
bug Something isn't working

Comments

@aldkfjwe
Copy link

I'm running the sample code from Github page and found out it used my old api key that resulted in this error.

image

I then added my newer API key to the .env file in root file of the project: OPENAI_API_KEY = 'my new version of the key'. However, after I added this line, the code still used older version, instead of the key provided in environment file. Is there any file should I create to configure the API key?

@aldkfjwe aldkfjwe added the bug Something isn't working label Jun 30, 2024
@kyleroche
Copy link
Member

Can you post the code you're using?

Do you have a global env variable set? %env

@collindutter
Copy link
Member

@aldkfjwe Can you please print out os.envion['OPENAI_API_KEY'] somewhere in your program to verify that it's the key you're expecting?

@tolseee
Copy link

tolseee commented Jul 2, 2024

Same here

@collindutter
Copy link
Member

@tolseee can you please print out the environment variable to ensure it's the key you're expecting?

Griptape does not automatically load .env files -- you would need to use something like python-dotenv to do that.

@tolseee
Copy link

tolseee commented Jul 2, 2024

@tolseee can you please print out the environment variable to ensure it's the key you're expecting?

Griptape does not automatically load .env files -- you would need to use something like python-dotenv to do that.

I already did it
Requirement already satisfied: python-dotenv in c:\users\enpit\appdata\local\programs\python\python310\lib\site-packages (1.0.1)

But always get the error above on comfy
Error code: 401 - {'error': {'message': 'Incorrect API key provided: sk-proj-********************************************jP2D. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}

When I run a OpenAI check on terminal, it gives me this:

python check_api_key.py
The API key being used is: sk-pr...vwcYI

The API is right

It seems it doesn't load the right api
I've reinstalled the nodes but it haven't changed anything

@collindutter
Copy link
Member

@tolseee are you able to reproduce this issue outside of ComfyUI? That would help us pinpoint whether this is an issue with the integration or griptape.

@shhlife
Copy link
Collaborator

shhlife commented Jul 2, 2024

Hi @tolseee - in your .env, do you have spaces between OPENAI_API_KEY and the = sign and your key?

Does it look like this:

OPENAI_API_KEY=yourapikey

or

OPENAI_API_KEY = yourapikey

@shhlife shhlife self-assigned this Jul 2, 2024
@shhlife shhlife transferred this issue from griptape-ai/griptape Jul 2, 2024
@tolseee
Copy link

tolseee commented Jul 3, 2024

Hi @tolseee - in your .env, do you have spaces between OPENAI_API_KEY and the = sign and your key?

Does it look like this:

OPENAI_API_KEY=yourapikey

or

OPENAI_API_KEY = yourapikey

OPENAI_API_KEY=sk-proj-WKF[...]

@shhlife
Copy link
Collaborator

shhlife commented Jul 3, 2024

Hmm.. okay.. this is interesting. are you on windows or mac?

One thing to try.. in the shell where you're running comfyUI, try and run python by just executing:

python

This should put you in a shell that looks something like:

Python 3.11.9 (tags/v3.11.9:de54cf5, Apr  2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Then run:

import os
os.getenv("OPENAI_API_KEY")

This should show the default API key set for your environment. If it's the old one, then do:

from dotenv import load_dotenv
load_dotenv()

This will force python to get the keys from your .env file

then again do:

os.getenv("OPENAI_API_KEY")

Are they the same?

The other question.. is your .env located in the same directory you're running comfyUI from?

@tolseee
Copy link

tolseee commented Jul 3, 2024

os.getenv("OPENAI_API_KEY")

This is what I get

PS C:\SD[...]\ComfyUI_windows_portable\ComfyUI> python
Python 3.10.14 (main, Mar 20 2024, 14:08:13) [MSC v.1939 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import os
os.getenv("OPENAI_API_KEY")
'sk-proj-WKF[...]YvwcYI'
from dotenv import load_dotenv
load_dotenv()
True
os.getenv("OPENAI_API_KEY")
'sk-proj-WKF[...]YvwcYI'

The other question.. is your .env located in the same directory you're running comfyUI from?

Yes

Honestly I have no idea where the AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key
provided: sk-proj-********************************************jP2D. API is coming from

@shhlife
Copy link
Collaborator

shhlife commented Jul 3, 2024

Yeah, it's very strange!

Okay, here's another thing to try..

in your custom_nodes/ComfyUI-Griptape/ folder there should be a file called griptape_config.json.

If you open that file, you should see something that looks like:

{
    "env": {
        "OPENAI_API_KEY": "sk-****",
    }
}

is that the right api key?

If not, try deleting the griptape_config.json file and restarting comfyui

-Jason

@tolseee
Copy link

tolseee commented Jul 4, 2024

Yeah, it's very strange!

Okay, here's another thing to try..

in your custom_nodes/ComfyUI-Griptape/ folder there should be a file called griptape_config.json.

If you open that file, you should see something that looks like:

{
    "env": {
        "OPENAI_API_KEY": "sk-****",
    }
}

is that the right api key?

If not, try deleting the griptape_config.json file and restarting comfyui

-Jason

You got it.
It was pointing at the wrong API, deleted and now it's pointing to the right one.

Now I'm facing a new issue but I'll open a new thread to avoiding off topic posts :)

Thank'you so much!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants