Skip to content

Commit 20ea0c1

Browse files
authored
Simplify archiving process (AntonOsika#469)
* simplify args * Fix tests * Black format
1 parent 60e0a7e commit 20ea0c1

File tree

6 files changed

+40
-45
lines changed

6 files changed

+40
-45
lines changed

gpt_engineer/ai.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def next(self, messages: list[dict[str, str]], prompt=None):
4343

4444
chat = []
4545
for chunk in response:
46-
delta = chunk["choices"][0]["delta"]
46+
delta = chunk["choices"][0]["delta"] # type: ignore
4747
msg = delta.get("content", "")
4848
print(msg, end="")
4949
chat.append(msg)

gpt_engineer/db.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import datetime
2+
import shutil
3+
14
from dataclasses import dataclass
25
from pathlib import Path
3-
from typing import Optional
46

57

68
# This class represents a simple database that stores its data as files in a directory.
@@ -49,3 +51,15 @@ class DBs:
4951
input: DB
5052
workspace: DB
5153
archive: DB
54+
55+
56+
def archive(dbs: DBs):
57+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
58+
shutil.move(
59+
str(dbs.memory.path), str(dbs.archive.path / timestamp / dbs.memory.path.name)
60+
)
61+
shutil.move(
62+
str(dbs.workspace.path),
63+
str(dbs.archive.path / timestamp / dbs.workspace.path.name),
64+
)
65+
return []

gpt_engineer/main.py

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import json
22
import logging
3-
import os
43

54
from pathlib import Path
65

76
import typer
87

9-
from gpt_engineer import steps
108
from gpt_engineer.ai import AI, fallback_model
119
from gpt_engineer.collect import collect_learnings
12-
from gpt_engineer.db import DB, DBs
13-
from gpt_engineer.steps import STEPS
10+
from gpt_engineer.db import DB, DBs, archive
11+
from gpt_engineer.steps import STEPS, Config as StepsConfig
1412

1513
app = typer.Typer()
1614

@@ -20,17 +18,10 @@ def main(
2018
project_path: str = typer.Argument("example", help="path"),
2119
model: str = typer.Argument("gpt-4", help="model id string"),
2220
temperature: float = 0.1,
23-
steps_config: steps.Config = typer.Option(
24-
steps.Config.DEFAULT, "--steps", "-s", help="decide which steps to run"
21+
steps_config: StepsConfig = typer.Option(
22+
StepsConfig.DEFAULT, "--steps", "-s", help="decide which steps to run"
2523
),
2624
verbose: bool = typer.Option(False, "--verbose", "-v"),
27-
run_prefix: str = typer.Option(
28-
"",
29-
help=(
30-
"run prefix, if you want to run multiple variants of the same project and "
31-
"later compare them"
32-
),
33-
),
3425
):
3526
logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO)
3627

@@ -41,20 +32,26 @@ def main(
4132
)
4233

4334
input_path = Path(project_path).absolute()
44-
memory_path = input_path / f"{run_prefix}memory"
45-
workspace_path = input_path / f"{run_prefix}workspace"
46-
archive_path = input_path / f"{run_prefix}archive"
35+
memory_path = input_path / "memory"
36+
workspace_path = input_path / "workspace"
37+
archive_path = input_path / "archive"
4738

48-
initial_run = not os.path.exists(memory_path) and not os.path.exists(workspace_path)
4939
dbs = DBs(
5040
memory=DB(memory_path),
5141
logs=DB(memory_path / "logs"),
5242
input=DB(input_path),
5343
workspace=DB(workspace_path),
5444
preprompts=DB(Path(__file__).parent / "preprompts"),
55-
archive=None if initial_run else DB(archive_path),
45+
archive=DB(archive_path),
5646
)
5747

48+
if steps_config not in [
49+
StepsConfig.EXECUTE_ONLY,
50+
StepsConfig.USE_FEEDBACK,
51+
StepsConfig.EVALUATE,
52+
]:
53+
archive(dbs)
54+
5855
steps = STEPS[steps_config]
5956
for step in steps:
6057
messages = step(ai, dbs)

gpt_engineer/steps.py

+2-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import datetime
21
import json
3-
import os
42
import re
5-
import shutil
63
import subprocess
74

85
from enum import Enum
@@ -261,16 +258,6 @@ def fix_code(ai: AI, dbs: DBs):
261258
return messages
262259

263260

264-
def archive(ai: AI, dbs: DBs):
265-
os.makedirs(dbs.archive.path, exist_ok=True)
266-
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
267-
shutil.move(dbs.memory.path, dbs.archive.path / timestamp / dbs.memory.path.name)
268-
shutil.move(
269-
dbs.workspace.path, dbs.archive.path / timestamp / dbs.workspace.path.name
270-
)
271-
return []
272-
273-
274261
def human_review(ai: AI, dbs: DBs):
275262
review = human_input()
276263
dbs.memory["review"] = review.to_json() # type: ignore
@@ -293,17 +280,15 @@ class Config(str, Enum):
293280
# Different configs of what steps to run
294281
STEPS = {
295282
Config.DEFAULT: [
296-
archive,
297283
clarify,
298284
gen_clarified_code,
299285
gen_entrypoint,
300286
execute_entrypoint,
301287
human_review,
302288
],
303-
Config.BENCHMARK: [archive, simple_gen, gen_entrypoint],
304-
Config.SIMPLE: [archive, simple_gen, gen_entrypoint, execute_entrypoint],
289+
Config.BENCHMARK: [simple_gen, gen_entrypoint],
290+
Config.SIMPLE: [simple_gen, gen_entrypoint, execute_entrypoint],
305291
Config.TDD: [
306-
archive,
307292
gen_spec,
308293
gen_unit_tests,
309294
gen_code,
@@ -312,7 +297,6 @@ class Config(str, Enum):
312297
human_review,
313298
],
314299
Config.TDD_PLUS: [
315-
archive,
316300
gen_spec,
317301
gen_unit_tests,
318302
gen_code,
@@ -322,15 +306,13 @@ class Config(str, Enum):
322306
human_review,
323307
],
324308
Config.CLARIFY: [
325-
archive,
326309
clarify,
327310
gen_clarified_code,
328311
gen_entrypoint,
329312
execute_entrypoint,
330313
human_review,
331314
],
332315
Config.RESPEC: [
333-
archive,
334316
gen_spec,
335317
respec,
336318
gen_unit_tests,

scripts/benchmark.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ def insert_markdown_section(file_path, section_title, section_text, level):
125125
if line_number != -1:
126126
lines.insert(line_number, new_section)
127127
else:
128-
print(f"Markdown file was of unexpected format. No section of level {level} found. Did not write results.")
128+
print(
129+
f"Markdown file was of unexpected format. No section of level {level} found. "
130+
"Did not write results."
131+
)
129132
return
130133

131134
# Write the file

tests/steps/test_archive.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
from unittest.mock import MagicMock
55

6-
from gpt_engineer.db import DB, DBs
7-
from gpt_engineer.steps import archive
6+
from gpt_engineer.db import DB, DBs, archive
87

98

109
def freeze_at(monkeypatch, time):
@@ -28,7 +27,7 @@ def test_archive(tmp_path, monkeypatch):
2827
tmp_path, ["memory", "logs", "preprompts", "input", "workspace", "archive"]
2928
)
3029
freeze_at(monkeypatch, datetime.datetime(2020, 12, 25, 17, 5, 55))
31-
archive(None, dbs)
30+
archive(dbs)
3231
assert not os.path.exists(tmp_path / "memory")
3332
assert not os.path.exists(tmp_path / "workspace")
3433
assert os.path.isdir(tmp_path / "archive" / "20201225_170555")
@@ -37,7 +36,7 @@ def test_archive(tmp_path, monkeypatch):
3736
tmp_path, ["memory", "logs", "preprompts", "input", "workspace", "archive"]
3837
)
3938
freeze_at(monkeypatch, datetime.datetime(2022, 8, 14, 8, 5, 12))
40-
archive(None, dbs)
39+
archive(dbs)
4140
assert not os.path.exists(tmp_path / "memory")
4241
assert not os.path.exists(tmp_path / "workspace")
4342
assert os.path.isdir(tmp_path / "archive" / "20201225_170555")

0 commit comments

Comments
 (0)