Skip to content

Commit

Permalink
Simplify archiving process (AntonOsika#469)
Browse files Browse the repository at this point in the history
* simplify args

* Fix tests

* Black format
  • Loading branch information
AntonOsika authored and 70ziko committed Oct 25, 2023
1 parent 3115f1e commit 347c3cd
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 45 deletions.
2 changes: 1 addition & 1 deletion gpt_engineer/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def next(self, messages: list[dict[str, str]], prompt=None):

chat = []
for chunk in response:
delta = chunk["choices"][0]["delta"]
delta = chunk["choices"][0]["delta"] # type: ignore
msg = delta.get("content", "")
print(msg, end="")
chat.append(msg)
Expand Down
16 changes: 15 additions & 1 deletion gpt_engineer/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datetime
import shutil

from dataclasses import dataclass
from pathlib import Path
from typing import Optional


# This class represents a simple database that stores its data as files in a directory.
Expand Down Expand Up @@ -49,3 +51,15 @@ class DBs:
input: DB
workspace: DB
archive: DB


def archive(dbs: DBs):
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
shutil.move(
str(dbs.memory.path), str(dbs.archive.path / timestamp / dbs.memory.path.name)
)
shutil.move(
str(dbs.workspace.path),
str(dbs.archive.path / timestamp / dbs.workspace.path.name),
)
return []
33 changes: 15 additions & 18 deletions gpt_engineer/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import json
import logging
import os

from pathlib import Path

import typer

from gpt_engineer import steps
from gpt_engineer.ai import AI, fallback_model
from gpt_engineer.collect import collect_learnings
from gpt_engineer.db import DB, DBs
from gpt_engineer.steps import STEPS
from gpt_engineer.db import DB, DBs, archive
from gpt_engineer.steps import STEPS, Config as StepsConfig

app = typer.Typer()

Expand All @@ -20,17 +18,10 @@ def main(
project_path: str = typer.Argument("example", help="path"),
model: str = typer.Argument("gpt-4", help="model id string"),
temperature: float = 0.1,
steps_config: steps.Config = typer.Option(
steps.Config.DEFAULT, "--steps", "-s", help="decide which steps to run"
steps_config: StepsConfig = typer.Option(
StepsConfig.DEFAULT, "--steps", "-s", help="decide which steps to run"
),
verbose: bool = typer.Option(False, "--verbose", "-v"),
run_prefix: str = typer.Option(
"",
help=(
"run prefix, if you want to run multiple variants of the same project and "
"later compare them"
),
),
):
logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO)

Expand All @@ -41,20 +32,26 @@ def main(
)

input_path = Path(project_path).absolute()
memory_path = input_path / f"{run_prefix}memory"
workspace_path = input_path / f"{run_prefix}workspace"
archive_path = input_path / f"{run_prefix}archive"
memory_path = input_path / "memory"
workspace_path = input_path / "workspace"
archive_path = input_path / "archive"

initial_run = not os.path.exists(memory_path) and not os.path.exists(workspace_path)
dbs = DBs(
memory=DB(memory_path),
logs=DB(memory_path / "logs"),
input=DB(input_path),
workspace=DB(workspace_path),
preprompts=DB(Path(__file__).parent / "preprompts"),
archive=None if initial_run else DB(archive_path),
archive=DB(archive_path),
)

if steps_config not in [
StepsConfig.EXECUTE_ONLY,
StepsConfig.USE_FEEDBACK,
StepsConfig.EVALUATE,
]:
archive(dbs)

steps = STEPS[steps_config]
for step in steps:
messages = step(ai, dbs)
Expand Down
22 changes: 2 additions & 20 deletions gpt_engineer/steps.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import datetime
import json
import os
import re
import shutil
import subprocess

from enum import Enum
Expand Down Expand Up @@ -261,16 +258,6 @@ def fix_code(ai: AI, dbs: DBs):
return messages


def archive(ai: AI, dbs: DBs):
os.makedirs(dbs.archive.path, exist_ok=True)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
shutil.move(dbs.memory.path, dbs.archive.path / timestamp / dbs.memory.path.name)
shutil.move(
dbs.workspace.path, dbs.archive.path / timestamp / dbs.workspace.path.name
)
return []


def human_review(ai: AI, dbs: DBs):
review = human_input()
dbs.memory["review"] = review.to_json() # type: ignore
Expand All @@ -293,17 +280,15 @@ class Config(str, Enum):
# Different configs of what steps to run
STEPS = {
Config.DEFAULT: [
archive,
clarify,
gen_clarified_code,
gen_entrypoint,
execute_entrypoint,
human_review,
],
Config.BENCHMARK: [archive, simple_gen, gen_entrypoint],
Config.SIMPLE: [archive, simple_gen, gen_entrypoint, execute_entrypoint],
Config.BENCHMARK: [simple_gen, gen_entrypoint],
Config.SIMPLE: [simple_gen, gen_entrypoint, execute_entrypoint],
Config.TDD: [
archive,
gen_spec,
gen_unit_tests,
gen_code,
Expand All @@ -312,7 +297,6 @@ class Config(str, Enum):
human_review,
],
Config.TDD_PLUS: [
archive,
gen_spec,
gen_unit_tests,
gen_code,
Expand All @@ -322,15 +306,13 @@ class Config(str, Enum):
human_review,
],
Config.CLARIFY: [
archive,
clarify,
gen_clarified_code,
gen_entrypoint,
execute_entrypoint,
human_review,
],
Config.RESPEC: [
archive,
gen_spec,
respec,
gen_unit_tests,
Expand Down
5 changes: 4 additions & 1 deletion scripts/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ def insert_markdown_section(file_path, section_title, section_text, level):
if line_number != -1:
lines.insert(line_number, new_section)
else:
print(f"Markdown file was of unexpected format. No section of level {level} found. Did not write results.")
print(
f"Markdown file was of unexpected format. No section of level {level} found. "
"Did not write results."
)
return

# Write the file
Expand Down
7 changes: 3 additions & 4 deletions tests/steps/test_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

from unittest.mock import MagicMock

from gpt_engineer.db import DB, DBs
from gpt_engineer.steps import archive
from gpt_engineer.db import DB, DBs, archive


def freeze_at(monkeypatch, time):
Expand All @@ -28,7 +27,7 @@ def test_archive(tmp_path, monkeypatch):
tmp_path, ["memory", "logs", "preprompts", "input", "workspace", "archive"]
)
freeze_at(monkeypatch, datetime.datetime(2020, 12, 25, 17, 5, 55))
archive(None, dbs)
archive(dbs)
assert not os.path.exists(tmp_path / "memory")
assert not os.path.exists(tmp_path / "workspace")
assert os.path.isdir(tmp_path / "archive" / "20201225_170555")
Expand All @@ -37,7 +36,7 @@ def test_archive(tmp_path, monkeypatch):
tmp_path, ["memory", "logs", "preprompts", "input", "workspace", "archive"]
)
freeze_at(monkeypatch, datetime.datetime(2022, 8, 14, 8, 5, 12))
archive(None, dbs)
archive(dbs)
assert not os.path.exists(tmp_path / "memory")
assert not os.path.exists(tmp_path / "workspace")
assert os.path.isdir(tmp_path / "archive" / "20201225_170555")
Expand Down

0 comments on commit 347c3cd

Please sign in to comment.