Simplify archiving process (#469)

* simplify args

* Fix tests

* Black format
This commit is contained in:
Anton Osika
2023-07-02 16:43:13 +02:00
committed by GitHub
parent 60e0a7e1dd
commit 20ea0c126a
6 changed files with 40 additions and 45 deletions

View File

@@ -43,7 +43,7 @@ class AI:
chat = [] chat = []
for chunk in response: for chunk in response:
delta = chunk["choices"][0]["delta"] delta = chunk["choices"][0]["delta"] # type: ignore
msg = delta.get("content", "") msg = delta.get("content", "")
print(msg, end="") print(msg, end="")
chat.append(msg) chat.append(msg)

View File

@@ -1,6 +1,8 @@
import datetime
import shutil
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from typing import Optional
# This class represents a simple database that stores its data as files in a directory. # This class represents a simple database that stores its data as files in a directory.
@@ -49,3 +51,15 @@ class DBs:
input: DB input: DB
workspace: DB workspace: DB
archive: 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 []

View File

@@ -1,16 +1,14 @@
import json import json
import logging import logging
import os
from pathlib import Path from pathlib import Path
import typer import typer
from gpt_engineer import steps
from gpt_engineer.ai import AI, fallback_model from gpt_engineer.ai import AI, fallback_model
from gpt_engineer.collect import collect_learnings from gpt_engineer.collect import collect_learnings
from gpt_engineer.db import DB, DBs from gpt_engineer.db import DB, DBs, archive
from gpt_engineer.steps import STEPS from gpt_engineer.steps import STEPS, Config as StepsConfig
app = typer.Typer() app = typer.Typer()
@@ -20,17 +18,10 @@ def main(
project_path: str = typer.Argument("example", help="path"), project_path: str = typer.Argument("example", help="path"),
model: str = typer.Argument("gpt-4", help="model id string"), model: str = typer.Argument("gpt-4", help="model id string"),
temperature: float = 0.1, temperature: float = 0.1,
steps_config: steps.Config = typer.Option( steps_config: StepsConfig = typer.Option(
steps.Config.DEFAULT, "--steps", "-s", help="decide which steps to run" StepsConfig.DEFAULT, "--steps", "-s", help="decide which steps to run"
), ),
verbose: bool = typer.Option(False, "--verbose", "-v"), 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) logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO)
@@ -41,20 +32,26 @@ def main(
) )
input_path = Path(project_path).absolute() input_path = Path(project_path).absolute()
memory_path = input_path / f"{run_prefix}memory" memory_path = input_path / "memory"
workspace_path = input_path / f"{run_prefix}workspace" workspace_path = input_path / "workspace"
archive_path = input_path / f"{run_prefix}archive" archive_path = input_path / "archive"
initial_run = not os.path.exists(memory_path) and not os.path.exists(workspace_path)
dbs = DBs( dbs = DBs(
memory=DB(memory_path), memory=DB(memory_path),
logs=DB(memory_path / "logs"), logs=DB(memory_path / "logs"),
input=DB(input_path), input=DB(input_path),
workspace=DB(workspace_path), workspace=DB(workspace_path),
preprompts=DB(Path(__file__).parent / "preprompts"), 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] steps = STEPS[steps_config]
for step in steps: for step in steps:
messages = step(ai, dbs) messages = step(ai, dbs)

View File

@@ -1,8 +1,5 @@
import datetime
import json import json
import os
import re import re
import shutil
import subprocess import subprocess
from enum import Enum from enum import Enum
@@ -261,16 +258,6 @@ def fix_code(ai: AI, dbs: DBs):
return messages 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): def human_review(ai: AI, dbs: DBs):
review = human_input() review = human_input()
dbs.memory["review"] = review.to_json() # type: ignore dbs.memory["review"] = review.to_json() # type: ignore
@@ -293,17 +280,15 @@ class Config(str, Enum):
# Different configs of what steps to run # Different configs of what steps to run
STEPS = { STEPS = {
Config.DEFAULT: [ Config.DEFAULT: [
archive,
clarify, clarify,
gen_clarified_code, gen_clarified_code,
gen_entrypoint, gen_entrypoint,
execute_entrypoint, execute_entrypoint,
human_review, human_review,
], ],
Config.BENCHMARK: [archive, simple_gen, gen_entrypoint], Config.BENCHMARK: [simple_gen, gen_entrypoint],
Config.SIMPLE: [archive, simple_gen, gen_entrypoint, execute_entrypoint], Config.SIMPLE: [simple_gen, gen_entrypoint, execute_entrypoint],
Config.TDD: [ Config.TDD: [
archive,
gen_spec, gen_spec,
gen_unit_tests, gen_unit_tests,
gen_code, gen_code,
@@ -312,7 +297,6 @@ STEPS = {
human_review, human_review,
], ],
Config.TDD_PLUS: [ Config.TDD_PLUS: [
archive,
gen_spec, gen_spec,
gen_unit_tests, gen_unit_tests,
gen_code, gen_code,
@@ -322,7 +306,6 @@ STEPS = {
human_review, human_review,
], ],
Config.CLARIFY: [ Config.CLARIFY: [
archive,
clarify, clarify,
gen_clarified_code, gen_clarified_code,
gen_entrypoint, gen_entrypoint,
@@ -330,7 +313,6 @@ STEPS = {
human_review, human_review,
], ],
Config.RESPEC: [ Config.RESPEC: [
archive,
gen_spec, gen_spec,
respec, respec,
gen_unit_tests, gen_unit_tests,

View File

@@ -125,7 +125,10 @@ def insert_markdown_section(file_path, section_title, section_text, level):
if line_number != -1: if line_number != -1:
lines.insert(line_number, new_section) lines.insert(line_number, new_section)
else: 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 return
# Write the file # Write the file

View File

@@ -3,8 +3,7 @@ import os
from unittest.mock import MagicMock from unittest.mock import MagicMock
from gpt_engineer.db import DB, DBs from gpt_engineer.db import DB, DBs, archive
from gpt_engineer.steps import archive
def freeze_at(monkeypatch, time): def freeze_at(monkeypatch, time):
@@ -28,7 +27,7 @@ def test_archive(tmp_path, monkeypatch):
tmp_path, ["memory", "logs", "preprompts", "input", "workspace", "archive"] tmp_path, ["memory", "logs", "preprompts", "input", "workspace", "archive"]
) )
freeze_at(monkeypatch, datetime.datetime(2020, 12, 25, 17, 5, 55)) 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 / "memory")
assert not os.path.exists(tmp_path / "workspace") assert not os.path.exists(tmp_path / "workspace")
assert os.path.isdir(tmp_path / "archive" / "20201225_170555") 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"] tmp_path, ["memory", "logs", "preprompts", "input", "workspace", "archive"]
) )
freeze_at(monkeypatch, datetime.datetime(2022, 8, 14, 8, 5, 12)) 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 / "memory")
assert not os.path.exists(tmp_path / "workspace") assert not os.path.exists(tmp_path / "workspace")
assert os.path.isdir(tmp_path / "archive" / "20201225_170555") assert os.path.isdir(tmp_path / "archive" / "20201225_170555")