From b97563726f1c0507ed723da7378928f2be6c7dd2 Mon Sep 17 00:00:00 2001 From: Enzo Martin Date: Sun, 18 Jun 2023 10:05:45 +0200 Subject: [PATCH 01/15] Add cleanup & move `projects` to their own directory - Add optional argument to clean and delete the working directories of the project before running the prompt - Add `.gitignore` entry to ignore all possible projects - Update readme --- .gitignore | 3 +++ README.md | 5 +++-- gpt_engineer/main.py | 17 ++++++++++++----- {example => projects/example}/main_prompt | 0 4 files changed, 18 insertions(+), 7 deletions(-) rename {example => projects/example}/main_prompt (100%) diff --git a/.gitignore b/.gitignore index 1c41f42..5a39df8 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,6 @@ archive # any log file *log.txt + +# ignore all project files +projects \ No newline at end of file diff --git a/README.md b/README.md index 40899bb..6fff452 100644 --- a/README.md +++ b/README.md @@ -24,12 +24,13 @@ GPT Engineer is made to be easy to adapt, extend, and make your agent learn how - `export OPENAI_API_KEY=[your api key]` with a key that has GPT4 access **Run**: -- Create a new empty folder with a `main_prompt` file (or copy the example folder `cp -r example/ my-new-project`) +- Create a new empty folder with a `main_prompt` file in the `projects` folder (or copy the example folder `cp -r projects/example/ projects/my-new-project`) - Fill in the `main_prompt` in your new folder - Run `python -m gpt_engineer.main my-new-project` + - Optionally pass in `true` to delete the working files before running **Results**: -- Check the generated files in my-new-project/workspace +- Check the generated files in projects/my-new-project/workspace ### Limitations Implementing additional chain of thought prompting, e.g. [Reflexion](https://github.com/noahshinn024/reflexion), should be able to make it more reliable and not miss requested functionality in the main prompt. diff --git a/gpt_engineer/main.py b/gpt_engineer/main.py index c03ed7f..1aeabc2 100644 --- a/gpt_engineer/main.py +++ b/gpt_engineer/main.py @@ -2,8 +2,9 @@ import os import json import pathlib import typer +import shutil + -from gpt_engineer.chat_to_files import to_files from gpt_engineer.ai import AI from gpt_engineer.steps import STEPS from gpt_engineer.db import DB, DBs @@ -14,7 +15,8 @@ app = typer.Typer() @app.command() def chat( - project_path: str = typer.Argument(str(pathlib.Path(os.path.curdir) / "example"), help="path"), + project_path: str = typer.Argument("example", help="path"), + delete_existing: str = typer.Argument(None, help="delete existing files"), run_prefix: str = typer.Option( "", help="run prefix, if you want to run multiple variants of the same project and later compare them", @@ -24,9 +26,14 @@ def chat( steps_config: str = "default", ): app_dir = pathlib.Path(os.path.curdir) - input_path = project_path - memory_path = pathlib.Path(project_path) / (run_prefix + "memory") - workspace_path = pathlib.Path(project_path) / (run_prefix + "workspace") + input_path = pathlib.Path(app_dir / "projects" / project_path) + memory_path = input_path / (run_prefix + "memory") + workspace_path = input_path / (run_prefix + "workspace") + + if delete_existing == 'true': + # Delete files and subdirectories in paths + shutil.rmtree(memory_path, ignore_errors=True) + shutil.rmtree(workspace_path, ignore_errors=True) ai = AI( model=model, diff --git a/example/main_prompt b/projects/example/main_prompt similarity index 100% rename from example/main_prompt rename to projects/example/main_prompt From 50c505c4594d6a2084f13487743f672a16ad29e3 Mon Sep 17 00:00:00 2001 From: Enzo Martin Date: Sun, 18 Jun 2023 10:05:59 +0200 Subject: [PATCH 02/15] Add support for directory creation and binary files - Use the `Path` module instead of `os` - Add ability to create any amount of missing directories for a given file - Add ability to save both text and binary files to save images (or other file types) later --- gpt_engineer/db.py | 47 +++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/gpt_engineer/db.py b/gpt_engineer/db.py index 01fa929..6088226 100644 --- a/gpt_engineer/db.py +++ b/gpt_engineer/db.py @@ -1,35 +1,52 @@ from dataclasses import dataclass -import os from pathlib import Path - +# This class represents a simple database that stores its data as files in a directory. +# It supports both text and binary files, and can handle directory structures. class DB: - """A simple key-value store, where keys are filenames and values are file contents.""" - def __init__(self, path): + # Convert the path string to a Path object and get its absolute path. self.path = Path(path).absolute() - os.makedirs(self.path, exist_ok=True) + + # Create the directory if it doesn't exist. + self.path.mkdir(parents=True, exist_ok=True) def __getitem__(self, key): - with open(self.path / key, encoding='utf-8') as f: - return f.read() + # Combine the database directory with the provided file path. + full_path = self.path / key + + # Check if the file exists before trying to open it. + if full_path.is_file(): + # Open the file in text mode and return its content. + with full_path.open('r') as f: + return f.read() + else: + # If the file doesn't exist, raise an error. + raise FileNotFoundError(f"No such file: '{full_path}'") def __setitem__(self, key, val): - Path(self.path / key).absolute().parent.mkdir(parents=True, exist_ok=True) + # Combine the database directory with the provided file path. + full_path = self.path / key - with open(self.path / key, 'w', encoding='utf-8') as f: - f.write(val) + # Create the directory tree if it doesn't exist. + full_path.parent.mkdir(parents=True, exist_ok=True) - def __contains__(self, key): - return (self.path / key).exists() + # Write the data to the file. If val is a string, it's written as text. + # If val is bytes, it's written as binary data. + if isinstance(val, str): + full_path.write_text(val) + elif isinstance(val, bytes): + full_path.write_bytes(val) + else: + # If val is neither a string nor bytes, raise an error. + raise TypeError("val must be either a str or bytes") +# dataclass for all dbs: @dataclass class DBs: - """A dataclass for all dbs""" - memory: DB logs: DB identity: DB input: DB - workspace: DB + workspace: DB \ No newline at end of file From 9c24119f915d2d1d5709faa2fd0da90b0d015e88 Mon Sep 17 00:00:00 2001 From: Enzo Martin Date: Sun, 18 Jun 2023 10:06:17 +0200 Subject: [PATCH 03/15] Generate instructions for all platforms - Update prompt to create instructions for all 3 major OS platforms - Fix small typo --- gpt_engineer/steps.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gpt_engineer/steps.py b/gpt_engineer/steps.py index 60fea23..7f18dee 100644 --- a/gpt_engineer/steps.py +++ b/gpt_engineer/steps.py @@ -97,8 +97,8 @@ def run_clarified(ai: AI, dbs: DBs): def execute_workspace(ai: AI, dbs: DBs): messages = ai.start( system=( - f"You will get infomation about a codebase that is currently on disk in the folder {dbs.workspace.path}.\n" - "From this you will answer with one code block that includes all the necessary macos terminal commands to " + f"You will get information about a codebase that is currently on disk in the folder {dbs.workspace.path}.\n" + "From this you will answer with code blocks that includes all the necessary Windows, MacOS, and Linux terminal commands to " "a) install dependencies " "b) run the necessary parts of the codebase to try it.\n" "Do not explain the code, just give the commands.\n" From e29e7bec2fd8a441529d401379d02abf5bd8a825 Mon Sep 17 00:00:00 2001 From: Enzo Martin Date: Sun, 18 Jun 2023 10:06:28 +0200 Subject: [PATCH 04/15] Enhance philosophy to include supporting documents - Create instructions for running/compiling the project - Create any package manager files --- identity/philosophy | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/identity/philosophy b/identity/philosophy index 96a12ad..34299f8 100644 --- a/identity/philosophy +++ b/identity/philosophy @@ -1,4 +1,11 @@ -You almost always put different classes in different files +You almost always put different classes in different files. +You always add a comment briefly describing the purpose of the function definition. +You try to add comments explaining very complex bits of logic. +You always follow the best practices for the requested languages in terms of describing the code written as a defined package/project. + +For Python, you always create an appropriate requirements.txt file. +For NodeJS, you always create an appropriate package.json file. +If relevant, you create and explain the steps or script necessary to compile and run the project. Python toolbelt preferences: - pytest From e7df947b9891304a7a625ba9d70ca9db0eb58341 Mon Sep 17 00:00:00 2001 From: Enzo Martin Date: Sun, 18 Jun 2023 10:06:36 +0200 Subject: [PATCH 05/15] Add support for directory paths in filenames and improve code splitting - Enforce an explicit markdown code block format - Add a token to split the output to clearly detect when the code blocks start - Save all non-code output to a `README.md` file - Update RegEx to extract and strip text more reliably and clean up the output - Update the identify prompts appropriately --- gpt_engineer/chat_to_files.py | 35 ++++++++++++++++++++++++----------- identity/setup | 21 ++++++++++++--------- identity/use_qa | 21 ++++++++++++--------- 3 files changed, 48 insertions(+), 29 deletions(-) diff --git a/gpt_engineer/chat_to_files.py b/gpt_engineer/chat_to_files.py index 9c8a9e6..2175bb3 100644 --- a/gpt_engineer/chat_to_files.py +++ b/gpt_engineer/chat_to_files.py @@ -1,28 +1,41 @@ import re -from typing import List, Tuple -from gpt_engineer.db import DB +def parse_chat(chat):# -> List[Tuple[str, str]]: + # Split the chat into sections by the '*CODEBLOCKSBELOW*' token + split_chat = chat.split('*CODEBLOCKSBELOW*') -def parse_chat(chat) -> List[Tuple[str, str]]: - # Get all ``` blocks - regex = r"```(.*?)```" + # Check if the '*CODEBLOCKSBELOW*' token was found + is_token_found = len(split_chat) > 1 - matches = re.finditer(regex, chat, re.DOTALL) + # If the '*CODEBLOCKSBELOW*' token is found, use the first part as README and second part as code blocks. + # Otherwise, treat README as optional and proceed with empty README and the entire chat as code blocks + readme = split_chat[0].strip() if is_token_found else 'No readme' + code_blocks = split_chat[1] if is_token_found else chat + + # Get all ``` blocks and preceding filenames + regex = r"\[(.*?)\]\s*```.*?\n(.*?)```" + matches = re.finditer(regex, code_blocks, re.DOTALL) files = [] for match in matches: - path = match.group(1).split("\n")[0] + # Strip the filename of any non-allowed characters and convert / to \ + path = re.sub(r'[<>"|?*]', '', match.group(1)) + # Get the code - code = match.group(1).split("\n")[1:] - code = "\n".join(code) + code = match.group(2) + # Add the file to the list files.append((path, code)) + # Add README to the list + files.append(('README.txt', readme)) + + # Return the files return files -def to_files(chat: str, workspace: DB): - workspace["all_output.txt"] = chat +def to_files(chat, workspace): + workspace['all_output.txt'] = chat files = parse_chat(chat) for file_name, file_content in files: diff --git a/identity/setup b/identity/setup index 4917945..4e3f2bc 100644 --- a/identity/setup +++ b/identity/setup @@ -1,15 +1,18 @@ You will get instructions for code to write. -You will write a very long answer. Make sure that every detail of the architecture is, in the end, implemented as code. +Following best practices and formatting for a README.md file, you will write a very long answer, make sure to provide the instructions on how to run the code. +Make sure that every detail of the architecture is, in the end, implemented as code. You will first lay out the names of the core classes, functions, methods that will be necessary, As well as a quick comment on their purpose. -Then you will output the content of each file, with syntax below. -(You will start with the "entrypoint" file, then go to the ones that are imported by that file, and so on.) +Before you start outputting the code, you will output a seperator in the form of a line containing "*CODEBLOCKSBELOW*" +Make sure to create any appropriate module dependency or package manager dependency definition file. +Then you will reformat and output the content of each file strictly following a markdown code block format, where the following tokens should be replaced such that [FILENAME] is the lowercase file name including the file extension, [LANG] is the markup code block language for the code's language, and [CODE] is the comments and code: +[FILENAME] +```[LANG] +[CODE] +``` + +You will start with the "entrypoint" file, then go to the ones that are imported by that file, and so on. +Follow a language and framework appropriate best practice file naming convention. Make sure that files contain all imports, types etc. Make sure that code in different files are compatible with each other. Ensure to implement all code, if you are unsure, write a plausible implementation. Before you finish, double check that all parts of the architecture is present in the files. - -File syntax: - -```file.py/ts/html -[ADD YOUR CODE HERE] -``` diff --git a/identity/use_qa b/identity/use_qa index 9aee050..47a5b3b 100644 --- a/identity/use_qa +++ b/identity/use_qa @@ -1,13 +1,16 @@ Please now remember the steps: First lay out the names of the core classes, functions, methods that will be necessary, As well as a quick comment on their purpose. -Then output the content of each file, with syntax below. -(You will start with the "entrypoint" file, then go to the ones that are imported by that file, and so on.) -Make sure that files contain all imports, types, variables etc. The code should be fully functional. If anything is unclear, just make assumptions. Make sure that code in different files are compatible with each other. +Make sure to provide instructions for running the code. +Before you start outputting the code, you will output a seperator in the form of a line containing "*CODEBLOCKSBELOW*" +Make sure to create any appropriate module dependency or package manager dependency definition file. +Then you will reformat and output the content of each file strictly following a markdown code block format, where the following tokens should be replaced such that [FILENAME] is the lowercase file name including the file extension, [LANG] is the markup code block language for the code's language, and [CODE] is the comments and code: +[FILENAME] +```[LANG] +[CODE] +``` + +You will start with the "entrypoint" file, then go to the ones that are imported by that file, and so on. +Follow a language and framework appropriate best practice file naming convention. +Make sure that files contain all imports, types etc. The code should be fully functional. Make sure that code in different files are compatible with each other. Before you finish, double check that all parts of the architecture is present in the files. - -File syntax: - -```filename.py/ts/html -[ADD YOUR CODE HERE] -``` \ No newline at end of file From 8b3862d94de3770e3e09383f12ed6c108125b9aa Mon Sep 17 00:00:00 2001 From: Enzo Martin Date: Sun, 18 Jun 2023 10:34:35 +0200 Subject: [PATCH 06/15] Fix linting --- gpt_engineer/chat_to_files.py | 22 ++++++++++++---------- gpt_engineer/db.py | 5 +++-- gpt_engineer/main.py | 10 +++++----- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/gpt_engineer/chat_to_files.py b/gpt_engineer/chat_to_files.py index 2175bb3..ec6b755 100644 --- a/gpt_engineer/chat_to_files.py +++ b/gpt_engineer/chat_to_files.py @@ -1,15 +1,17 @@ import re -def parse_chat(chat):# -> List[Tuple[str, str]]: - # Split the chat into sections by the '*CODEBLOCKSBELOW*' token - split_chat = chat.split('*CODEBLOCKSBELOW*') - # Check if the '*CODEBLOCKSBELOW*' token was found +def parse_chat(chat): # -> List[Tuple[str, str]]: + # Split the chat into sections by the "*CODEBLOCKSBELOW*" token + split_chat = chat.split("*CODEBLOCKSBELOW*") + + # Check if the "*CODEBLOCKSBELOW*" token was found is_token_found = len(split_chat) > 1 - # If the '*CODEBLOCKSBELOW*' token is found, use the first part as README and second part as code blocks. - # Otherwise, treat README as optional and proceed with empty README and the entire chat as code blocks - readme = split_chat[0].strip() if is_token_found else 'No readme' + # If the "*CODEBLOCKSBELOW*" token is found, use the first part as README + # and second part as code blocks. Otherwise, treat README as optional and + # proceed with empty README and the entire chat as code blocks + readme = split_chat[0].strip() if is_token_found else "No readme" code_blocks = split_chat[1] if is_token_found else chat # Get all ``` blocks and preceding filenames @@ -19,7 +21,7 @@ def parse_chat(chat):# -> List[Tuple[str, str]]: files = [] for match in matches: # Strip the filename of any non-allowed characters and convert / to \ - path = re.sub(r'[<>"|?*]', '', match.group(1)) + path = re.sub(r'[<>"|?*]', "", match.group(1)) # Get the code code = match.group(2) @@ -28,14 +30,14 @@ def parse_chat(chat):# -> List[Tuple[str, str]]: files.append((path, code)) # Add README to the list - files.append(('README.txt', readme)) + files.append(("README.txt", readme)) # Return the files return files def to_files(chat, workspace): - workspace['all_output.txt'] = chat + workspace["all_output.txt"] = chat files = parse_chat(chat) for file_name, file_content in files: diff --git a/gpt_engineer/db.py b/gpt_engineer/db.py index 6088226..b1e52ff 100644 --- a/gpt_engineer/db.py +++ b/gpt_engineer/db.py @@ -1,6 +1,7 @@ from dataclasses import dataclass from pathlib import Path + # This class represents a simple database that stores its data as files in a directory. # It supports both text and binary files, and can handle directory structures. class DB: @@ -18,7 +19,7 @@ class DB: # Check if the file exists before trying to open it. if full_path.is_file(): # Open the file in text mode and return its content. - with full_path.open('r') as f: + with full_path.open("r") as f: return f.read() else: # If the file doesn't exist, raise an error. @@ -49,4 +50,4 @@ class DBs: logs: DB identity: DB input: DB - workspace: DB \ No newline at end of file + workspace: DB diff --git a/gpt_engineer/main.py b/gpt_engineer/main.py index 1aeabc2..e0dff9c 100644 --- a/gpt_engineer/main.py +++ b/gpt_engineer/main.py @@ -1,14 +1,13 @@ -import os import json +import os import pathlib -import typer import shutil +import typer from gpt_engineer.ai import AI -from gpt_engineer.steps import STEPS from gpt_engineer.db import DB, DBs - +from gpt_engineer.steps import STEPS app = typer.Typer() @@ -30,7 +29,7 @@ def chat( memory_path = input_path / (run_prefix + "memory") workspace_path = input_path / (run_prefix + "workspace") - if delete_existing == 'true': + if delete_existing == "true": # Delete files and subdirectories in paths shutil.rmtree(memory_path, ignore_errors=True) shutil.rmtree(workspace_path, ignore_errors=True) @@ -52,5 +51,6 @@ def chat( messages = step(ai, dbs) dbs.logs[step.__name__] = json.dumps(messages) + if __name__ == "__main__": app() From c77b07a8460a7ca9495952b57b6b027713132ebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20Thom=C3=A9?= Date: Sun, 18 Jun 2023 14:32:35 +0200 Subject: [PATCH 07/15] Create ci.yaml --- .github/workflows/ci.yaml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .github/workflows/ci.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..1c7f02b --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,33 @@ +on: + pull_request: + branches: + - main + push: + branches: + - main + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: + - "3.8" + - "3.9" + - "3.10" + steps: + - uses: actions/checkout@v3 + + - uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + cache: pip + + - name: Install package + run: pip install -e . + + - name: Install test runner + run: pip install pytest pytest-cov + + - name: Run unit tests + run: pytest --cov=gpt_engineer From 084ce1759b703e398745fff7c6ab9abfbdb10933 Mon Sep 17 00:00:00 2001 From: Patilla Code Date: Sun, 18 Jun 2023 14:46:03 +0200 Subject: [PATCH 08/15] make pre commit pass in the whole codebase (#149) --- gpt_engineer/ai.py | 12 +++++++----- gpt_engineer/main.py | 5 ++++- gpt_engineer/steps.py | 34 ++++++++++++++++++++++----------- scripts/benchmark.py | 38 +++++++++++++++++++------------------ scripts/clean_benchmarks.py | 28 +++++++++++---------------- tests/test_db.py | 18 +++++++++--------- 6 files changed, 74 insertions(+), 61 deletions(-) diff --git a/gpt_engineer/ai.py b/gpt_engineer/ai.py index 619bda0..0714869 100644 --- a/gpt_engineer/ai.py +++ b/gpt_engineer/ai.py @@ -8,10 +8,12 @@ class AI: try: openai.Model.retrieve("gpt-4") except openai.error.InvalidRequestError: - print("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") - self.kwargs['model'] = "gpt-3.5-turbo" + print( + "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" + ) + self.kwargs["model"] = "gpt-3.5-turbo" def start(self, system, user): messages = [ @@ -26,10 +28,10 @@ class AI: def fuser(self, msg): return {"role": "user", "content": msg} + def fassistant(self, msg): return {"role": "assistant", "content": msg} - def next(self, messages: list[dict[str, str]], prompt=None): if prompt: messages = messages + [{"role": "user", "content": prompt}] diff --git a/gpt_engineer/main.py b/gpt_engineer/main.py index e0dff9c..09faa46 100644 --- a/gpt_engineer/main.py +++ b/gpt_engineer/main.py @@ -18,7 +18,10 @@ def chat( delete_existing: str = typer.Argument(None, help="delete existing files"), run_prefix: str = typer.Option( "", - help="run prefix, if you want to run multiple variants of the same project and later compare them", + help=( + "run prefix, if you want to run multiple variants of the same project and " + "later compare them", + ), ), model: str = "gpt-4", temperature: float = 0.1, diff --git a/gpt_engineer/steps.py b/gpt_engineer/steps.py index 49fd2e3..9f88528 100644 --- a/gpt_engineer/steps.py +++ b/gpt_engineer/steps.py @@ -2,9 +2,8 @@ import json import subprocess from gpt_engineer.ai import AI -from gpt_engineer.chat_to_files import to_files +from gpt_engineer.chat_to_files import parse_chat, to_files from gpt_engineer.db import DBs -from gpt_engineer.chat_to_files import parse_chat def setup_sys_prompt(dbs): @@ -54,7 +53,8 @@ def clarify(ai: AI, dbs: DBs): def gen_spec(ai: AI, dbs: DBs): """ - Generate a spec from the main prompt + clarifications and save the results to the workspace + Generate a spec from the main prompt + clarifications and save the results to + the workspace """ messages = [ ai.fsystem(setup_sys_prompt(dbs)), @@ -67,6 +67,7 @@ def gen_spec(ai: AI, dbs: DBs): return messages + def respec(ai: AI, dbs: DBs): messages = dbs.logs[gen_spec.__name__] messages += [ai.fsystem(dbs.identity["respec"])] @@ -75,10 +76,13 @@ def respec(ai: AI, dbs: DBs): messages = ai.next( messages, ( - 'Based on the conversation so far, please reiterate the specification for the program. ' - 'If there are things that can be improved, please incorporate the improvements. ' - "If you are satisfied with the specification, just write out the specification word by word again." - ) + "Based on the conversation so far, please reiterate the specification for " + "the program. " + "If there are things that can be improved, please incorporate the " + "improvements. " + "If you are satisfied with the specification, just write out the " + "specification word by word again." + ), ) dbs.memory["specification"] = messages[-1]["content"] @@ -116,6 +120,7 @@ def gen_clarified_code(ai: AI, dbs: DBs): to_files(messages[-1]["content"], dbs.workspace) return messages + def gen_code(ai: AI, dbs: DBs): # get the messages from previous step @@ -157,8 +162,10 @@ def execute_entrypoint(ai, dbs): def gen_entrypoint(ai, dbs): messages = ai.start( system=( - f"You will get information about a codebase that is currently on disk in the folder {dbs.workspace.path}.\n" - "From this you will answer with code blocks that includes all the necessary Windows, MacOS, and Linux terminal commands to " + "You will get information about a codebase that is currently on disk in " + f"the folder {dbs.workspace.path}.\n" + "From this you will answer with code blocks that includes all the necessary " + "Windows, MacOS, and Linux terminal commands to " "a) install dependencies " "b) run all necessary parts of the codebase (in parallell if necessary).\n" "Do not install globally. Do not use sudo.\n" @@ -170,11 +177,16 @@ def gen_entrypoint(ai, dbs): blocks = parse_chat(messages[-1]["content"]) for lang, _ in blocks: - assert lang in ["", "bash", "sh"], "Generated entrypoint command that was not bash" + assert lang in [ + "", + "bash", + "sh", + ], "Generated entrypoint command that was not bash" dbs.workspace["run.sh"] = "\n".join(block for lang, block in blocks) return messages + def use_feedback(ai: AI, dbs: DBs): messages = [ ai.fsystem(setup_sys_prompt(dbs)), @@ -182,7 +194,7 @@ def use_feedback(ai: AI, dbs: DBs): ai.fassistant(dbs.workspace["all_output.txt"]), ai.fsystem(dbs.identity["use_feedback"]), ] - messages = ai.next(messages, dbs.memory['feedback']) + messages = ai.next(messages, dbs.memory["feedback"]) to_files(messages[-1]["content"], dbs.workspace) return messages diff --git a/scripts/benchmark.py b/scripts/benchmark.py index 8e267a3..8a00fe8 100644 --- a/scripts/benchmark.py +++ b/scripts/benchmark.py @@ -2,45 +2,47 @@ # for each folder, run the benchmark import os -import sys import subprocess -import time -import datetime -import shutil -import argparse -import json -from pathlib import Path -from typer import run + from itertools import islice +from pathlib import Path + +from typer import run + def main( n_benchmarks: int | None = None, ): processes = [] files = [] - path = Path('benchmark') + path = Path("benchmark") if n_benchmarks: benchmarks = islice(path.iterdir(), n_benchmarks) for folder in benchmarks: if os.path.isdir(folder): - print('Running benchmark for {}'.format(folder)) + print("Running benchmark for {}".format(folder)) - log_path = folder / 'log.txt' - log_file = open(log_path, 'w') - processes.append(subprocess.Popen(['python', '-m', 'gpt_engineer.main', folder], stdout=log_file, stderr=log_file, bufsize=0)) + log_path = folder / "log.txt" + log_file = open(log_path, "w") + processes.append( + subprocess.Popen( + ["python", "-m", "gpt_engineer.main", folder], + stdout=log_file, + stderr=log_file, + bufsize=0, + ) + ) files.append(log_file) - print('You can stream the log file by running: tail -f {}'.format(log_path)) + print("You can stream the log file by running: tail -f {}".format(log_path)) for process, file in zip(processes, files): process.wait() - print('process finished with code', process.returncode) + print("process finished with code", process.returncode) file.close() -if __name__ == '__main__': +if __name__ == "__main__": run(main) - - diff --git a/scripts/clean_benchmarks.py b/scripts/clean_benchmarks.py index 21cb536..b90faa2 100644 --- a/scripts/clean_benchmarks.py +++ b/scripts/clean_benchmarks.py @@ -2,26 +2,21 @@ # for each folder, run the benchmark import os -import sys -import subprocess -import time -import datetime import shutil -import argparse -import json -from pathlib import Path -from typer import run -from itertools import islice -def main( -): - benchmarks = Path('benchmark') +from pathlib import Path + +from typer import run + + +def main(): + benchmarks = Path("benchmark") for benchmark in benchmarks.iterdir(): if benchmark.is_dir(): - print(f'Cleaning {benchmark}') + print(f"Cleaning {benchmark}") for path in benchmark.iterdir(): - if path.name == 'main_prompt': + if path.name == "main_prompt": continue # Get filename of Path object @@ -32,7 +27,6 @@ def main( # delete the file os.remove(path) -if __name__ == '__main__': - run(main) - +if __name__ == "__main__": + run(main) diff --git a/tests/test_db.py b/tests/test_db.py index 0843a21..1fef131 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -3,12 +3,12 @@ from gpt_engineer.db import DB def test_db(): # use /tmp for testing - db = DB('/tmp/test_db') - db['test'] = 'test' - assert db['test'] == 'test' - db['test'] = 'test2' - assert db['test'] == 'test2' - db['test2'] = 'test2' - assert db['test2'] == 'test2' - assert db['test'] == 'test2' - print('test_db passed') + db = DB("/tmp/test_db") + db["test"] = "test" + assert db["test"] == "test" + db["test"] = "test2" + assert db["test"] == "test2" + db["test2"] = "test2" + assert db["test2"] == "test2" + assert db["test"] == "test2" + print("test_db passed") From 695bfb4a922e71ef7b80a871bf0035d0dbc61130 Mon Sep 17 00:00:00 2001 From: Jeb <130810521+jebarpg@users.noreply.github.com> Date: Sun, 18 Jun 2023 05:52:25 -0700 Subject: [PATCH 09/15] Added CODE_OF_CONDUCT.md to the .github directory (#147) --- .github/CODE_OF_CONDUCT.md | 131 +++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 .github/CODE_OF_CONDUCT.md diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..337a651 --- /dev/null +++ b/.github/CODE_OF_CONDUCT.md @@ -0,0 +1,131 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity or expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, caste, color, religion, or sexual +identity and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall + community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or advances of + any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email address, + without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting using an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +anton.osika@gmail.com. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of reporters of incidents. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series of +actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or permanent +ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within the +community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.1, available at +[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]. + +Community Impact Guidelines were inspired by +[Mozilla's code of conduct enforcement ladder][Mozilla CoC]. + +For answers to common questions about this code of conduct, see the FAQ at +[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at +[https://www.contributor-covenant.org/translations][translations]. + +[homepage]: https://www.contributor-covenant.org +[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html +[Mozilla CoC]: https://github.com/mozilla/diversity +[FAQ]: https://www.contributor-covenant.org/faq +[translations]: https://www.contributor-covenant.org/translations \ No newline at end of file From 2134592411b61c8574bbc87b324528b81943d213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20Thom=C3=A9?= Date: Sun, 18 Jun 2023 14:46:50 +0200 Subject: [PATCH 10/15] Ignore my-new-project/ --- .gitignore | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 7e41317..32c67cc 100644 --- a/.gitignore +++ b/.gitignore @@ -39,5 +39,6 @@ archive *log.txt todo -# ignore all project files -projects \ No newline at end of file +# Ignore GPT Engineer files +projects +my-new-project/ From bac7af55abcb5255bb230e9626c9654c1ffc729a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20Thom=C3=A9?= Date: Sun, 18 Jun 2023 14:47:32 +0200 Subject: [PATCH 11/15] execute_workspace -> gen_entrypoint; execute_entrypoint Clarify semantics that `execute` steps doesn't communicate with the API. --- gpt_engineer/steps.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/gpt_engineer/steps.py b/gpt_engineer/steps.py index 9f88528..882db29 100644 --- a/gpt_engineer/steps.py +++ b/gpt_engineer/steps.py @@ -135,12 +135,6 @@ def gen_code(ai: AI, dbs: DBs): return messages -def execute_workspace(ai: AI, dbs: DBs): - messages = gen_entrypoint(ai, dbs) - execute_entrypoint(ai, dbs) - return messages - - def execute_entrypoint(ai, dbs): command = dbs.workspace["run.sh"] @@ -201,11 +195,11 @@ def use_feedback(ai: AI, dbs: DBs): # Different configs of what steps to run STEPS = { - "default": [gen_spec, gen_unit_tests, gen_code, execute_workspace], + "default": [gen_spec, gen_unit_tests, gen_code, gen_entrypoint, execute_entrypoint], "benchmark": [gen_spec, gen_unit_tests, gen_code, gen_entrypoint], - "simple": [simple_gen, execute_workspace], - "clarify": [clarify, gen_clarified_code, execute_workspace], - "respec": [gen_spec, respec, gen_unit_tests, gen_code, execute_workspace], + "simple": [simple_gen, gen_entrypoint, execute_entrypoint], + "clarify": [clarify, gen_clarified_code, gen_entrypoint, execute_entrypoint], + "respec": [gen_spec, respec, gen_unit_tests, gen_code, gen_entrypoint, execute_entrypoint], "execute_only": [execute_entrypoint], "use_feedback": [use_feedback], } From 57a8700825e4a0ed28f15b70635a74ccbd913ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Ahlb=C3=A4ck?= Date: Sun, 18 Jun 2023 14:54:04 +0200 Subject: [PATCH 12/15] fix to_files --- gpt_engineer/chat_to_files.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gpt_engineer/chat_to_files.py b/gpt_engineer/chat_to_files.py index ec6b755..9a7f8de 100644 --- a/gpt_engineer/chat_to_files.py +++ b/gpt_engineer/chat_to_files.py @@ -1,6 +1,5 @@ import re - def parse_chat(chat): # -> List[Tuple[str, str]]: # Split the chat into sections by the "*CODEBLOCKSBELOW*" token split_chat = chat.split("*CODEBLOCKSBELOW*") @@ -15,7 +14,7 @@ def parse_chat(chat): # -> List[Tuple[str, str]]: code_blocks = split_chat[1] if is_token_found else chat # Get all ``` blocks and preceding filenames - regex = r"\[(.*?)\]\s*```.*?\n(.*?)```" + regex = r"(\S+?)\n```\S+\n(.+?)```" matches = re.finditer(regex, code_blocks, re.DOTALL) files = [] From 742b56804e9ded872cbc5590e0c0464018f3a956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20Thom=C3=A9?= Date: Sun, 18 Jun 2023 15:02:40 +0200 Subject: [PATCH 13/15] Create test_ai.py --- tests/test_ai.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tests/test_ai.py diff --git a/tests/test_ai.py b/tests/test_ai.py new file mode 100644 index 0000000..2241d3d --- /dev/null +++ b/tests/test_ai.py @@ -0,0 +1,6 @@ +from gpt_engineer.ai import AI + + +def test_ai(): + AI() + # TODO Assert that methods behave and not only constructor. From 502756bb58a9ce4d674ef0ca1c827b0d3991df14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20Thom=C3=A9?= Date: Sun, 18 Jun 2023 15:04:30 +0200 Subject: [PATCH 14/15] `black` --- gpt_engineer/chat_to_files.py | 1 + gpt_engineer/steps.py | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/gpt_engineer/chat_to_files.py b/gpt_engineer/chat_to_files.py index 9a7f8de..758dac1 100644 --- a/gpt_engineer/chat_to_files.py +++ b/gpt_engineer/chat_to_files.py @@ -1,5 +1,6 @@ import re + def parse_chat(chat): # -> List[Tuple[str, str]]: # Split the chat into sections by the "*CODEBLOCKSBELOW*" token split_chat = chat.split("*CODEBLOCKSBELOW*") diff --git a/gpt_engineer/steps.py b/gpt_engineer/steps.py index 882db29..c368f34 100644 --- a/gpt_engineer/steps.py +++ b/gpt_engineer/steps.py @@ -199,7 +199,14 @@ STEPS = { "benchmark": [gen_spec, gen_unit_tests, gen_code, gen_entrypoint], "simple": [simple_gen, gen_entrypoint, execute_entrypoint], "clarify": [clarify, gen_clarified_code, gen_entrypoint, execute_entrypoint], - "respec": [gen_spec, respec, gen_unit_tests, gen_code, gen_entrypoint, execute_entrypoint], + "respec": [ + gen_spec, + respec, + gen_unit_tests, + gen_code, + gen_entrypoint, + execute_entrypoint, + ], "execute_only": [execute_entrypoint], "use_feedback": [use_feedback], } From d3d1c9e5aab530c0cf5869254a4830320dfd6e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20Thom=C3=A9?= Date: Sun, 18 Jun 2023 15:08:25 +0200 Subject: [PATCH 15/15] Mark test as failed because it requires OpenAI API access currently --- tests/test_ai.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_ai.py b/tests/test_ai.py index 2241d3d..acc017e 100644 --- a/tests/test_ai.py +++ b/tests/test_ai.py @@ -1,6 +1,9 @@ +import pytest + from gpt_engineer.ai import AI +@pytest.mark.xfail(reason="Constructor assumes API access") def test_ai(): AI() # TODO Assert that methods behave and not only constructor.