Skip to content

Commit 67b0e18

Browse files
committed
Incorporated review feedback
1 parent 42350db commit 67b0e18

File tree

10 files changed

+61
-28
lines changed

10 files changed

+61
-28
lines changed

Acknowledgements.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# We thank the following people for inspiration
2+
3+
| Person | Content | File(s) | Source |
4+
|----|---|---|---|
5+
| Paul Gauthier | The prompt for the `improve code` step is strongly based on Paul's prompt in Aider | /preprompts/improve.txt | https://github.com/paul-gauthier/aider/blob/main/aider/coders/editblock_coder.py

docs/intro/chat_parsing.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ Here are the functions defined in the `chat_to_files.py` module:
88

99
`parse_chat(chat)`: This function takes the chat as an argument and returns a list of tuples. Each tuple contains a filename and the corresponding file content. The function uses regular expressions to extract the filenames and the file contents from the chat. The filenames are cleaned up to remove any non-allowed characters.
1010

11-
`to_files(chat, dbs)`: This function takes the chat and the DBs as arguments. DBs contains the workspace and memory path. The function first saves the entire chat as a text file in the memory path. Then it calls the parse_chat function to parse the chat and get the files. Each file is then saved to the workspace.
11+
`to_files_and_memory(chat, dbs)`: This function takes the chat and the DBs as arguments. DBs contains the workspace and memory path. The function first saves the entire chat as a text file in the memory path. Then it calls the to_files function to write each file to the workspace.
12+
13+
`to_files(chat, db)`: This function takes the chat and workspace DB as arguments. It calls the parse_chat function to parse the chat and get the files. Each file is then saved to the workspace.
1214

1315
<br>
1416

docs/intro/quick_overview.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ This module contains two main functions:
1515

1616
`parse_chat(chat)`: This function takes a chat conversation and extracts all the code blocks and preceding filenames. It returns a list of tuples, where each tuple contains a filename and the corresponding code block.
1717

18-
`to_files(chat, dbs)`: This function takes the chat and the DBs as arguments. DBs contains the workspace and memory path. The function first saves the entire chat as a text file in the memory path. Then it calls the parse_chat function to parse the chat and get the files. Each file is then saved to the workspace.
18+
`to_files_and_memory(chat, dbs)`: This function takes the chat and the DBs as arguments. DBs contains the workspace and memory path. The function first saves the entire chat as a text file in the memory path. Then it calls the to_files function to write each file to the workspace.
19+
20+
`to_files(chat, db)`: This function takes the chat and workspace DB as arguments. It calls the parse_chat function to parse the chat and get the files. Each file is then saved to the workspace.
1921

2022
<br>
2123

gpt_engineer/chat_to_files.py

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
2-
from pathlib import Path
32
import re
3+
import logging
44

55
from dataclasses import dataclass
66
from typing import List, Tuple
@@ -9,6 +9,9 @@
99
from gpt_engineer.file_selector import FILE_LIST_NAME
1010

1111

12+
logger = logging.getLogger(__name__)
13+
14+
1215
def parse_chat(chat) -> List[Tuple[str, str]]:
1316
"""
1417
Extracts all code blocks from a chat and returns them
@@ -56,7 +59,22 @@ def parse_chat(chat) -> List[Tuple[str, str]]:
5659
return files
5760

5861

59-
def to_files(chat: str, dbs: DBs):
62+
def to_files_and_memory(chat: str, dbs: DBs):
63+
"""
64+
Save chat to memory, and parse chat to extracted file and save them to the workspace.
65+
66+
Parameters
67+
----------
68+
chat : str
69+
The chat to parse.
70+
dbs : DBs
71+
The databases that include the memory and workspace database
72+
"""
73+
dbs.memory["all_output.txt"] = chat
74+
to_files(chat, dbs.workspace)
75+
76+
77+
def to_files(chat: str, workspace: DB):
6078
"""
6179
Parse the chat and add all extracted files to the workspace.
6280
@@ -67,11 +85,9 @@ def to_files(chat: str, dbs: DBs):
6785
workspace : DB
6886
The database containing the workspace.
6987
"""
70-
dbs.memory["all_output.txt"] = chat
71-
7288
files = parse_chat(chat)
7389
for file_name, file_content in files:
74-
dbs.workspace[file_name] = file_content
90+
workspace[file_name] = file_content
7591

7692

7793
def overwrite_files(chat: str, dbs: DBs) -> None:
@@ -218,10 +234,18 @@ def parse_all_edits(txt):
218234

219235
def apply_edits(edits: List[Edit], workspace: DB):
220236
for edit in edits:
221-
filename = edit.filename.replace("workspace/", "")
237+
filename = edit.filename
222238
if edit.before == "":
239+
if workspace.get(filename) is not None:
240+
logger.warn(
241+
f"The edit to be applied wants to create a new file `{filename}`, but that already exists. The file will be overwritten. See `.gpteng/memory` for previous version."
242+
)
223243
workspace[filename] = edit.after # new file
224244
else:
245+
if workspace[filename].count(edit.before) > 1:
246+
logger.warn(
247+
f"While applying an edit to `{filename}`, the code block to be replaced was found multiple times. All instances will be replaced."
248+
)
225249
workspace[filename] = workspace[filename].replace(
226250
edit.before, edit.after
227251
) # existing file

gpt_engineer/main.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def main(
8080

8181
if lite_mode:
8282
assert not improve_mode, "Lite mode cannot improve code"
83+
if steps_config == StepsConfig.DEFAULT:
84+
steps_config = StepsConfig.LITE
8385

8486
if improve_mode:
8587
assert (
@@ -97,7 +99,7 @@ def main(
9799

98100
project_path = os.path.abspath(
99101
project_path
100-
) # resolve the string to a valid path (eg a/b/../c to a/c)
102+
) # resolve the string to a valid path (eg "a/b/../c" to "a/c")
101103
path = Path(project_path).absolute()
102104
print("Running gpt-engineer in", path, "\n")
103105

gpt_engineer/preprompts/ht.txt

-2
This file was deleted.

gpt_engineer/preprompts/improve

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Here is an example reponse:
2525
---
2626
PLANNING:
2727
We need to change ... because ..., therefore I will add the line `a=a+1` to the function `add_one`.
28-
Also, in the class `DB`, we need to update the ...
28+
Also, in the class `DB`, we need to update the ...
2929

3030
OUTPUT:
3131
```python

gpt_engineer/steps.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
format_file_to_input,
1414
get_code_strings,
1515
overwrite_files_with_edits,
16-
to_files,
16+
to_files_and_memory,
1717
)
1818
from gpt_engineer.db import DBs
1919
from gpt_engineer.file_selector import FILE_LIST_NAME, ask_for_files
@@ -62,14 +62,14 @@ def lite_gen(ai: AI, dbs: DBs) -> List[Message]:
6262
messages = ai.start(
6363
dbs.input["prompt"], dbs.preprompts["file_format"], step_name=curr_fn()
6464
)
65-
to_files(messages[-1].content.strip(), dbs)
65+
to_files_and_memory(messages[-1].content.strip(), dbs)
6666
return messages
6767

6868

6969
def simple_gen(ai: AI, dbs: DBs) -> List[Message]:
7070
"""Run the AI on the default prompts and save the results"""
7171
messages = ai.start(setup_sys_prompt(dbs), dbs.input["prompt"], step_name=curr_fn())
72-
to_files(messages[-1].content.strip(), dbs)
72+
to_files_and_memory(messages[-1].content.strip(), dbs)
7373
return messages
7474

7575

@@ -130,7 +130,7 @@ def gen_clarified_code(ai: AI, dbs: DBs) -> List[dict]:
130130
step_name=curr_fn(),
131131
)
132132

133-
to_files(messages[-1].content.strip(), dbs)
133+
to_files_and_memory(messages[-1].content.strip(), dbs)
134134
return messages
135135

136136

@@ -211,7 +211,7 @@ def use_feedback(ai: AI, dbs: DBs):
211211
]
212212
if dbs.input["feedback"]:
213213
messages = ai.next(messages, dbs.input["feedback"], step_name=curr_fn())
214-
to_files(messages[-1].content.strip(), dbs)
214+
to_files_and_memory(messages[-1].content.strip(), dbs)
215215
return messages
216216
else:
217217
print(

scripts/rerun_edited_message_logs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import typer
77

88
from gpt_engineer.ai import AI
9-
from gpt_engineer.chat_to_files import to_files
9+
from gpt_engineer.chat_to_files import to_files_and_memory
1010

1111
app = typer.Typer()
1212

@@ -30,7 +30,7 @@ def main(
3030

3131
if out_path:
3232
# ToDo: to_files expects a DBs as 2nd argument. But do we need this entire file?
33-
to_files(messages[-1]["content"], out_path)
33+
to_files_and_memory(messages[-1]["content"], out_path)
3434
with open(pathlib.Path(out_path) / "all_output.txt", "w") as f:
3535
json.dump(messages[-1]["content"], f)
3636

tests/test_chat_to_files.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dataclasses import dataclass
22
import textwrap
33

4-
from gpt_engineer.chat_to_files import to_files
4+
from gpt_engineer.chat_to_files import to_files_and_memory
55

66

77
@dataclass
@@ -15,7 +15,7 @@ class DummyDBs:
1515
project_metadata = {}
1616

1717

18-
def test_to_files():
18+
def test_to_files_and_memory():
1919
chat = textwrap.dedent(
2020
"""
2121
This is a sample program.
@@ -34,7 +34,7 @@ def add(a, b):
3434
)
3535

3636
dbs = DummyDBs()
37-
to_files(chat, dbs)
37+
to_files_and_memory(chat, dbs)
3838

3939
assert dbs.memory["all_output.txt"] == chat
4040

@@ -66,7 +66,7 @@ def add(a, b):
6666
"""
6767
)
6868
dbs = DummyDBs()
69-
to_files(chat, dbs)
69+
to_files_and_memory(chat, dbs)
7070

7171
assert dbs.memory["all_output.txt"] == chat
7272

@@ -93,7 +93,7 @@ def test_files_with_brackets_in_name():
9393
)
9494

9595
dbs = DummyDBs()
96-
to_files(chat, dbs)
96+
to_files_and_memory(chat, dbs)
9797

9898
assert dbs.memory["all_output.txt"] == chat
9999

@@ -119,7 +119,7 @@ def test_files_with_file_colon():
119119
)
120120

121121
dbs = DummyDBs()
122-
to_files(chat, dbs)
122+
to_files_and_memory(chat, dbs)
123123

124124
assert dbs.memory["all_output.txt"] == chat
125125

@@ -145,7 +145,7 @@ def test_files_with_back_tick():
145145
)
146146

147147
dbs = DummyDBs()
148-
to_files(chat, dbs)
148+
to_files_and_memory(chat, dbs)
149149

150150
assert dbs.memory["all_output.txt"] == chat
151151

@@ -172,7 +172,7 @@ def test_files_with_newline_between():
172172
)
173173

174174
dbs = DummyDBs()
175-
to_files(chat, dbs)
175+
to_files_and_memory(chat, dbs)
176176

177177
assert dbs.memory["all_output.txt"] == chat
178178

@@ -199,7 +199,7 @@ def test_files_with_newline_between_header():
199199
)
200200

201201
dbs = DummyDBs()
202-
to_files(chat, dbs)
202+
to_files_and_memory(chat, dbs)
203203

204204
assert dbs.memory["all_output.txt"] == chat
205205

0 commit comments

Comments
 (0)