griptape-ai / ComfyUI-Griptape

A suite of Griptape nodes for ComfyUI
Apache License 2.0
101 stars 11 forks source link

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

Closed aldkfjwe closed 2 months ago

aldkfjwe commented 3 months ago

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?

kyleroche commented 3 months ago

Can you post the code you're using?

Do you have a global env variable set? %env

collindutter commented 3 months ago

@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 commented 3 months ago

Same here

collindutter commented 3 months ago

@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 commented 3 months ago

@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 commented 3 months ago

@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 commented 3 months ago

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
tolseee commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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!!

shhlife commented 2 months ago

my pleasure!