From 347c3cde5f30eedc946de96bdbb9660be907e3a3 Mon Sep 17 00:00:00 2001 From: Anton Osika Date: Sun, 2 Jul 2023 16:43:13 +0200 Subject: [PATCH] Simplify archiving process (#469) * simplify args * Fix tests * Black format --- gpt_engineer/ai.py | 2 +- gpt_engineer/db.py | 16 +++++++++++++++- gpt_engineer/main.py | 33 +++++++++++++++------------------ gpt_engineer/steps.py | 22 ++-------------------- scripts/benchmark.py | 5 ++++- tests/steps/test_archive.py | 7 +++---- 6 files changed, 40 insertions(+), 45 deletions(-) diff --git a/gpt_engineer/ai.py b/gpt_engineer/ai.py index ef9b65bb8e..195aaa673a 100644 --- a/gpt_engineer/ai.py +++ b/gpt_engineer/ai.py @@ -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) diff --git a/gpt_engineer/db.py b/gpt_engineer/db.py index 1f117b2fde..979be0beda 100644 --- a/gpt_engineer/db.py +++ b/gpt_engineer/db.py @@ -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. @@ -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 [] diff --git a/gpt_engineer/main.py b/gpt_engineer/main.py index 81c4c4c291..2312f4368b 100644 --- a/gpt_engineer/main.py +++ b/gpt_engineer/main.py @@ -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() @@ -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) @@ -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) diff --git a/gpt_engineer/steps.py b/gpt_engineer/steps.py index 438b162957..7a012f4b74 100644 --- a/gpt_engineer/steps.py +++ b/gpt_engineer/steps.py @@ -1,8 +1,5 @@ -import datetime import json -import os import re -import shutil import subprocess from enum import Enum @@ -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 @@ -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, @@ -312,7 +297,6 @@ class Config(str, Enum): human_review, ], Config.TDD_PLUS: [ - archive, gen_spec, gen_unit_tests, gen_code, @@ -322,7 +306,6 @@ class Config(str, Enum): human_review, ], Config.CLARIFY: [ - archive, clarify, gen_clarified_code, gen_entrypoint, @@ -330,7 +313,6 @@ class Config(str, Enum): human_review, ], Config.RESPEC: [ - archive, gen_spec, respec, gen_unit_tests, diff --git a/scripts/benchmark.py b/scripts/benchmark.py index a923f03116..d5f71e9e95 100644 --- a/scripts/benchmark.py +++ b/scripts/benchmark.py @@ -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 diff --git a/tests/steps/test_archive.py b/tests/steps/test_archive.py index 3211d3dd54..ec01f79678 100644 --- a/tests/steps/test_archive.py +++ b/tests/steps/test_archive.py @@ -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): @@ -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") @@ -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")