gpt-engineer icon indicating copy to clipboard operation
gpt-engineer copied to clipboard

OpenAI API Key set issue.

Open ChatTeach opened this issue 2 years ago • 9 comments

OpenAI API Key Set Issue

I use window 10, and i set my openai api key in main.py file.

app = typer.Typer()

openai.api_key = 'sk-yl.......',

@app.command() def main(

But i get this issue on run time...

openai.error.AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = <API-KEY>', or you can set the environment variable OPENAI_API_KEY=<API-KEY>). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = <PATH>'. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.

???

ChatTeach avatar Jun 22 '23 04:06 ChatTeach

same issue

EdwardDali avatar Jun 22 '23 08:06 EdwardDali

I am also getting the same issue with setting the Openai API Key in Windows 10.

kram254 avatar Jun 22 '23 08:06 kram254

put it in "ai.py"

import openai

logger = logging.getLogger(name)

openai.api_key = "sk-Vw..........................."

mattjohnpowell avatar Jun 22 '23 08:06 mattjohnpowell

Thanks for your suggestion.

but its not work for me...

ChatTeach avatar Jun 23 '23 02:06 ChatTeach

The reason why we are not seeing anyone that is working on this repo confront this issue is because they already know why we are getting the error and it has nothing to do with the program.

Looking at the code after getting this error from what I can tell is that you have to have a GPT-4 API.

You have to request access to GTP-4 API. I have requested access three times and have yet to be approved. Without it, I am 90% sure you can't run the program.

As an alternative, I believe it is called Chat2Code, which is the same as this but actually has a UI already. I cannot claim the quality of it vs. GPT-engineer since I havent used GPT-engineer either with this issue, but at least that program works. You can also select which version of GPT you want to use.

TiltdImage avatar Jun 24 '23 00:06 TiltdImage

Update:

The KeyError has to do with the missing file "main_prompt" that's been changed to "prompt" The quick workaround is to duplicate the original file called "prompt" with your instructions within it, then rename the duplicate file to "main_prompt. You'll need both for gpt-enginner to work until they can update code errors. This change happened recently and the file names were changed but all of the code wasn't updated completely to reflect the change to "prompt"

Previous Report before discovering the issue:

I'm having this issue on MacOS: Would it be as simple as changing init parameter from GPT4 to GPT3.5 in ai.py ? Otherwise, I can't get it to complete. It hits OpenAI for sure, but they won't allow it to proceed.

Also, I'm using an older key that I minted for Auto-GPT that was never used before today - if I use a newer key that I just minted/generated, it flat out rejects before even getting started. Seems they've changed the parameters of the keys more recently minted. Any ideas? I'd love to get this going and begin using it... lot's of projects I want to play with!

File "/Users/anthonytoigo/Desktop/gpt-engineer/gpt-engineer/gpt_engineer/db.py", line 21, in getitem raise KeyError(key)

KeyError: 'main_prompt'

(venv) (base) Anthony-Toigos-Computer:gpt-engineer anthonytoigo$ gpt-engineer projects/web-scraping/ INFO:openai:error_code=model_not_found error_message="The model 'gpt-4' does not exist" error_param=model error_type=invalid_request_error message='OpenAI API error received' stream_error=False Model gpt-4 not available for provided API key. Reverting to gpt-3.5-turbo. Sign up for the GPT-4 wait list here: https://openai.com/waitlist/gpt-4-api Traceback (most recent call last):

File "/Users/anthonytoigo/Desktop/gpt-engineer/gpt-engineer/venv/bin/gpt-engineer", line 8, in sys.exit(app()) ^^^^^

File "/Users/anthonytoigo/Desktop/gpt-engineer/gpt-engineer/gpt_engineer/main.py", line 62, in main messages = step(ai, dbs) ^^^^^^^^^^^^^

File "/Users/anthonytoigo/Desktop/gpt-engineer/gpt-engineer/gpt_engineer/steps.py", line 51, in clarify user_input = get_prompt(dbs) ^^^^^^^^^^^^^^^

File "/Users/anthonytoigo/Desktop/gpt-engineer/gpt-engineer/gpt_engineer/steps.py", line 36, in get_prompt return dbs.input.get("prompt", dbs.input["main_prompt"]) ~~~~~~~~~^^^^^^^^^^^^^^^

File "/Users/anthonytoigo/Desktop/gpt-engineer/gpt-engineer/gpt_engineer/db.py", line 21, in getitem raise KeyError(key)

KeyError: 'main_prompt'

attoigo avatar Jun 25 '23 04:06 attoigo

The reason why we are not seeing anyone that is working on this repo confront this issue is because they already know why we are getting the error and it has nothing to do with the program.

Looking at the code after getting this error from what I can tell is that you have to have a GPT-4 API.

You have to request access to GTP-4 API. I have requested access three times and have yet to be approved. Without it, I am 90% sure you can't run the program.

As an alternative, I believe it is called Chat2Code, which is the same as this but actually has a UI already. I cannot claim the quality of it vs. GPT-engineer since I havent used GPT-engineer either with this issue, but at least that program works. You can also select which version of GPT you want to use.

Issue is with file name changed to "prompt" from "main_prompt" when the code specifies/references both. Not all the code was updated to reflect the change.

A quick workaround until there's a code update is to use both files with the same identical prompt within them.

I also believed it was an OpenAI issue, but apparently not, although when I tried to use a more recently generated key, it flat out was rejected. That could be an issue for some who have only recently signed up for OpenAI API key.

I hope this helps!

attoigo avatar Jun 25 '23 05:06 attoigo

Alternative code to replace original in "steps.py" Seems to be working now. Also, "requirements.txt file missing from install download.

def get_prompt(dbs): """While we migrate we have this fallback getter""" assert ( Image 6-25-23 at 2 41 PM Image 6-25-23 at 2 36 PM

    "prompt" in dbs.input or "main_prompt" in dbs.input
), "Please put your prompt in the file `prompt` or `main_prompt` in the project directory"

if "prompt" not in dbs.input:
    print(
        colored("Please put the prompt in the file `prompt`, if `prompt` is not available, `main_prompt` will be used", "red")
    )
    print()

return dbs.input.get("prompt", dbs.input.get("main_prompt"))

attoigo avatar Jun 25 '23 07:06 attoigo

put it in "ai.py"

import openai

logger = logging.getLogger(name)

openai.api_key = "sk-Vw..........................."

I used this suggestion and changed everywhere from GPT-4 to gpt-3.5-turbo All works fine, thank you mattjohnpowell

imageen avatar Jun 28 '23 09:06 imageen

openai.api_key =

man.... who would have thought that the variable name in script should be called openai.api_key while the export suggestion in the readme identifies the variable name as OPENAI_API_KEY ?

CynicalWilson avatar Jun 29 '23 20:06 CynicalWilson

Has any of these been added to the main repo code yet? I am still not getting mine to work.

TiltdImage avatar Jun 29 '23 22:06 TiltdImage

Not sure if relevant but perhaps this works. It does for me (I do not have acces to GPT-4 and use the fallback model, and I'm on a windows machine as well). I have my API key set in the '.env' file (in the root of the project folder) with a line stating OPENAI_API_KEY=XYZ. I have changed the code in ai.py with these 4 lines of code (insert at line 6);

import dotenv, os
if not os.getenv("OPENAI_API_KEY") and os.path.exists(".env"):
    dotenv.load_dotenv()
    openai.api_key = os.getenv("OPENAI_API_KEY")

RemkoDelsman avatar Jun 30 '23 15:06 RemkoDelsman

Hello, kindly has anyone managed to get a solution workaround for this; all the recommendations above and still failed for me.

kram254 avatar Jul 05 '23 06:07 kram254

Hello, kindly has anyone managed to get a solution workaround for this; all the recommendations above and still failed for me.

Hi kram254

apart from the above I did this in steps.py (I added this- you need to add path to prompt):

def get_prompt(dbs: DBs) -> str: """Retrieve the prompt from the 'prompt' file in the project directory.""" prompt_file = "path to your prompt in your CURRENT project" #change every time you execute a new prompt

try:
    with open(prompt_file, "r") as file:
        prompt = file.read().strip()
        return prompt
except FileNotFoundError:
    raise FileNotFoundError(
        f"Could not find the 'prompt' file at {prompt_file}. Please make sure the file exists."
    )

(instead of this):

def get_prompt(dbs: DBs) -> str: """While we migrate we have this fallback getter""" assert ( "prompt" in dbs.input or "main_prompt" in dbs.input ), "Please put your prompt in the file prompt in the project directory"

if "prompt" not in dbs.input:
    print(
        colored("Please put the prompt in the file `prompt`, not `main_prompt", "red")
    )
    print()
    return dbs.input["main_prompt"]

return dbs.input["prompt"]

in ai.py (plus .env in root directory with OPENAI_API_KEY=xxx)

load_dotenv(find_dotenv()) if load_dotenv(find_dotenv()): print("The .env file has been loaded.") else: print("Could not load the .env file.") openai.api_key = os.getenv("OPENAI_API_KEY") logger = logging.getLogger(name)

in main.py : I changed model type esp.gpt-4 or gpt-3.5-turbo (if you don't have subscription)

@app.command() def main( project_path: str = typer.Argument("example", help="path"), delete_existing: bool = typer.Argument(False, help="delete existing files"), model: str = typer.Argument("gpt-3.5-turbo", help="model id string"), temperature: float = 0.1, steps_config: StepsConfig = typer.Option( StepsConfig.DEFAULT, "--steps", "-s", help="decide which steps to run" ), verbose: bool = typer.Option(False, "--verbose", "-v"), ):

in app.py

I changed to gpt-3.5-turbo:

class AI: def init(self, model="gpt-3.5-turbo", temperature=0.1): ...

imageen avatar Jul 05 '23 08:07 imageen

Hello bro, thank you so much for your help but still did not work for me still getting the same Openai key error: If it's okay with you kindly could you sent me the Zip for your gpt-engineer without your openai_key so that I can see where I am going wrong to my email [email protected]. Please Kindly

kram254 avatar Jul 05 '23 10:07 kram254

if you have dotenv, you need to do pip install dotenv, as it was not initailly in requirements.txt; also I do have acess to GPT-4 api although I don't have subscription and use here gpt-3.5-turbo, did you get access via waiting list to GPT-4?

imageen avatar Jul 05 '23 11:07 imageen

Not yet, but I have access to GPT-3.5.

kram254 avatar Jul 05 '23 14:07 kram254

"gpt-3.5-turbo" I have this in my settings

imageen avatar Jul 05 '23 21:07 imageen

Did you check readme?:)

AntonOsika avatar Jul 08 '23 15:07 AntonOsika

README_WINDOWS

AntonOsika avatar Jul 08 '23 15:07 AntonOsika

For those of you who are using mac with homebrew:

  1. Navigate to the Directory: cd /opt/homebrew/lib/python3.11/site-packages/gpt_engineer/
  2. Open the Directory in Finder: open .
  3. In the Finder window, create a new file named .env (with no name before the dot, only the extension).
  4. Open the .env file and add: OPENAI_API_KEY=<YOUR_API_KEY_HERE>
  5. Save & run

GershonHerczeg avatar Sep 18 '23 16:09 GershonHerczeg