Merge branch 'main' into readme-tweaks

This commit is contained in:
PatillaCode
2023-06-18 16:28:12 +02:00
8 changed files with 54 additions and 35 deletions

View File

@@ -1,5 +1,9 @@
import logging
import openai
logger = logging.getLogger(__name__)
class AI:
def __init__(self, **kwargs):
@@ -36,6 +40,7 @@ class AI:
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
)
@@ -46,4 +51,6 @@ class AI:
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

View File

@@ -3,41 +3,29 @@ 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()
# 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")

View File

@@ -1,4 +1,5 @@
import json
import logging
import os
import pathlib
import shutil
@@ -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")

View File

@@ -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"
@@ -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": [

2
identity/fix_code Normal file
View File

@@ -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.

View File

@@ -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.

View File

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

View File

@@ -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.