Skip to content

Commit

Permalink
.env setup for OpenAI API key (AntonOsika#499)
Browse files Browse the repository at this point in the history
* .env setup for OpenAI API key

* Support for pip install with .env file

* Instuctions for .env file and pip install

* Further handling of missing env var for API key

* dotenv compliance with rudder-sdk

* Update __init__.py

* Update gpt_engineer/main.py

---------

Co-authored-by: Anton Osika <anton.osika@gmail.com>
  • Loading branch information
OriAshkenazi and AntonOsika authored Aug 13, 2023
1 parent 38ef734 commit 5d6542e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### OpenAI Setup ###

# OPENAI_API_KEY=Your personal OpenAI API key from https://platform.openai.com/account/api-keys
OPENAI_API_KEY=$key
14 changes: 10 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,35 @@ create-venv:
python -m venv venv

#Defines a target named upgrade-pip. This target will upgrade pip to the latest version.
upgrade-pip:
upgrade-pip: load-env
@echo -e "$(COLOR_CYAN)Upgrading pip...$(COLOR_RESET)" && \
source venv/bin/activate && \
pip install --upgrade pip >> /dev/null

#Defines a target named install-dependencies. This target will install the dependencies.
install-dependencies:
install-dependencies: load-env
@echo -e "$(COLOR_CYAN)Installing dependencies...$(COLOR_RESET)" && \
source venv/bin/activate && \
pip install -e . >> /dev/null

#Defines a target named install-pre-commit. This target will install the pre-commit hooks.
install-pre-commit:
install-pre-commit: load-env
@echo -e "$(COLOR_CYAN)Installing pre-commit hooks...$(COLOR_RESET)" && \
source venv/bin/activate && \
pre-commit install

#Defines a target named load-env. This target will load the environment variables from the .env file.
load-env:
@echo -e "$(COLOR_CYAN)Loading environment variables...$(COLOR_RESET)" && \
source venv/bin/activate && \
python -m dotenv load

#Defines a target named farewell. This target will print a farewell message.
farewell:
@echo -e "$(COLOR_GREEN)All done!$(COLOR_RESET)"

#Defines a target named run. This target will run GPT Engineer on the folder with the given name, name was defined earlier in the Makefile.
run:
run: load-env
@echo -e "$(COLOR_CYAN)Running GPT Engineer on $(COLOR_GREEN)$(name)$(COLOR_CYAN) folder...$(COLOR_RESET)" && \
source venv/bin/activate && \
gpt-engineer projects/$(name)
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ For **development**:
- (or: `make install && source venv/bin/activate` for a venv)

**API Key**

With an OpenAI API key run:

Either just:
- `export OPENAI_API_KEY=[your api key]`

Or:
- Create a copy of `.env.template` named `.env`
- Add your OPENAI_API_KEY in .env

Check the [Windows README](./WINDOWS_README.md) for windows usage.

**Running**
Expand Down
1 change: 1 addition & 0 deletions gpt_engineer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

17 changes: 17 additions & 0 deletions gpt_engineer/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
import os
from pathlib import Path

from pathlib import Path

import typer
from dotenv import load_dotenv

from gpt_engineer.ai import AI, fallback_model
from gpt_engineer.collect import collect_learnings
Expand All @@ -12,6 +15,18 @@

app = typer.Typer()

def load_env_if_needed():
"""
Check if API KEY env variable exist
If it doesn’t, call load_dotenv
"""
if os.getenv("OPENAI_API_KEY") is None:
load_dotenv()
# After attempting to load from .env, check again
if os.getenv("OPENAI_API_KEY") is None:
raise ValueError("Cannot run the program without OPENAI_API_KEY environment variable.\nPlease set it in your environment or in a .env file.")



@app.command()
def main(
Expand All @@ -25,6 +40,8 @@ def main(
):
logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO)

load_env_if_needed()

model = fallback_model(model)
ai = AI(
model_name=model,
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies = [
'dataclasses-json == 0.5.7',
'tiktoken >=0.0.4',
'tabulate == 0.9.0',
'python-dotenv >= 0.21.0',
'langchain >=0.0.240',
]

Expand Down

0 comments on commit 5d6542e

Please sign in to comment.