Skip to content

Commit

Permalink
Merge branch 'main' into readme-tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
patillacode committed Jun 18, 2023
2 parents 4f86e7f + 3f86b2a commit e119f75
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 35 deletions.
9 changes: 8 additions & 1 deletion gpt_engineer/ai.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import logging

import openai

logger = logging.getLogger(__name__)


class AI:
def __init__(self, **kwargs):
Expand Down Expand Up @@ -36,6 +40,7 @@ def next(self, messages: list[dict[str, str]], prompt=None):
if prompt:
messages = messages + [{"role": "user", "content": prompt}]

logger.debug(f"Creating a new chat completion: {messages}")
response = openai.ChatCompletion.create(
messages=messages, stream=True, **self.kwargs
)
Expand All @@ -46,4 +51,6 @@ def next(self, messages: list[dict[str, str]], prompt=None):
msg = delta.get("content", "")
print(msg, end="")
chat.append(msg)
return messages + [{"role": "assistant", "content": "".join(chat)}]
messages = messages + [{"role": "assistant", "content": "".join(chat)}]
logger.debug(f"Chat completion finished: {messages}")
return messages
22 changes: 5 additions & 17 deletions gpt_engineer/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,29 @@


# 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()

# Create the directory if it doesn't exist.
self.path.mkdir(parents=True, exist_ok=True)

def __getitem__(self, key):
# 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:
with full_path.open("r", encoding="utf-8") as f:
return f.read()
else:
# If the file doesn't exist, raise an error.
raise FileNotFoundError(f"No such file: '{full_path}'")
raise KeyError(key)

def __setitem__(self, key, val):
# Combine the database directory with the provided file path.
full_path = self.path / key

# Create the directory tree if it doesn't exist.
full_path.parent.mkdir(parents=True, exist_ok=True)

# 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)
full_path.write_text(val, encoding="utf-8")
else:
# If val is neither a string nor bytes, raise an error.
raise TypeError("val must be either a str or bytes")
Expand Down
4 changes: 4 additions & 0 deletions gpt_engineer/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
import os
import pathlib
import shutil
Expand Down Expand Up @@ -26,7 +27,10 @@ def chat(
model: str = "gpt-4",
temperature: float = 0.1,
steps_config: str = "default",
verbose: bool = typer.Option(False, "--verbose", "-v"),
):
logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO)

app_dir = pathlib.Path(os.path.curdir)
input_path = pathlib.Path(app_dir / "projects" / project_path)
memory_path = input_path / (run_prefix + "memory")
Expand Down
19 changes: 16 additions & 3 deletions gpt_engineer/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ def gen_entrypoint(ai, dbs):
messages = ai.start(
system=(
"You will get information about a codebase that is currently on disk in "
f"the folder {dbs.workspace.path}.\n"
"the current folder.\n"
"From this you will answer with code blocks that includes all the necessary "
"Windows, MacOS, and Linux terminal commands to "
"unix 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"
Expand Down Expand Up @@ -193,10 +193,23 @@ def use_feedback(ai: AI, dbs: DBs):
return messages


def fix_code(ai: AI, dbs: DBs):
codemem = json.loads(dbs.logs[gen_code.__name__])[-1]["content"]
messages = [
ai.fsystem(setup_sys_prompt(dbs)),
ai.fuser(f"Instructions: {dbs.input['main_prompt']}"),
ai.fuser(codemem),
ai.fsystem(dbs.identity["fixer"]),
]
messages = ai.next(messages, "Please fix any errors in the code above.")
to_files(messages[-1]["content"], dbs.workspace)
return messages


# Different configs of what steps to run
STEPS = {
"default": [gen_spec, gen_unit_tests, gen_code, gen_entrypoint, execute_entrypoint],
"benchmark": [gen_spec, gen_unit_tests, gen_code, gen_entrypoint],
"benchmark": [gen_spec, gen_unit_tests, gen_code, fix_code, gen_entrypoint],
"simple": [simple_gen, gen_entrypoint, execute_entrypoint],
"clarify": [clarify, gen_clarified_code, gen_entrypoint, execute_entrypoint],
"respec": [
Expand Down
2 changes: 2 additions & 0 deletions identity/fix_code
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
You are a super smart developer. You have been tasked with fixing a program and making it work according to the best of your knowledge. There might be placeholders in the code you have to fill in.
You provide fully functioning, well formatted code with few comments, that works and has no bugs or placeholders.
16 changes: 11 additions & 5 deletions identity/generate
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
You will get instructions for code to write.
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.
You will write a very long answer. Make sure that every detail of the architecture is, in the end, implemented as code.
Make sure that every detail of the architecture is, in the end, implemented as code.

Think step by step and reason yourself to the right decisions to make sure we get it right.
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.
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 format and output the content, including ALL code, 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 code:

Then you will output the content of each file, with syntax below, including ALL code.

Syntax:
[FILENAME]
```[LANG]
[CODE]
```
Please note that the code should be fully functional. No placeholders.
Where [FILENAME] is the lowercase file name including the file extension,
[LANG] is the language for the code's language, and [CODE] is the code:

You will start with the "entrypoint" file, then go to the ones that are imported by that file, and so on.
Please note that the code should be fully functional. No placeholders.

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.
Include module dependency or package manager dependency definition file.
Before you finish, double check that all parts of the architecture is present in the files.
5 changes: 0 additions & 5 deletions identity/philosophy
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
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
Expand Down
12 changes: 8 additions & 4 deletions identity/use_qa
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
Please now remember the steps:

Think step by step and reason yourself to the right decisions to make sure we get it right.
First lay out the names of the core classes, functions, methods that will be necessary, As well as a quick comment on their purpose.
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 code:

Then you will output the content of each file, with syntax below, including ALL code.

Syntax:
[FILENAME]
```[LANG]
[CODE]
```
Where [FILENAME] is the lowercase file name including the file extension,
[LANG] is the language for the code's language, and [CODE] is the code:

Please note that the code should be fully functional. No placeholders.

You will start with the "entrypoint" file, then go to the ones that are imported by that file, and so on.
Expand Down

0 comments on commit e119f75

Please sign in to comment.