gpt-engineer
gpt-engineer copied to clipboard
Code Duplication in 'gpt-engineer/steps.py'
Description The code contains sections with similar logic or repeated patterns, such as the gen_spec, gen_unit_tests, and gen_code functions. This duplication violates the DRY (Don't Repeat Yourself) principle, which advocates for avoiding redundant code and promoting code reuse.
Here's an overview of the shared logic:
- All three functions start with a similar sequence of messages being initialized, which includes the system prompt and instructions.
- They make use of the ai.next() method to generate AI responses based on the provided messages.
- They retrieve or assign values to the dbs.memory dictionary using specific keys.
- They save the generated content to the workspace using the to_files() function.
Based on this analysis, we can extract the shared logic into a separate function or utility. Here's an example of how it can be refactored:
def perform_ai_task(ai: AI, dbs: DBs, messages: List[Dict[str, str]], output_key: str) -> List[Dict[str, str]]:
"""
Perform an AI task by generating responses based on the given messages.
Save the output to the workspace using the specified output_key.
Return the updated messages.
"""
messages = messages + [ai.fassistant(dbs.workspace[output_key])]
messages = ai.next(messages, dbs.identity["use_qa"])
dbs.workspace[output_key] = messages[-1]["content"]
to_files(dbs.workspace[output_key], dbs.workspace)
return messages
The updated gen_spec(), gen_unit_tests() and gen_code() functions:
def gen_spec(ai: AI, dbs: DBs):
messages = [
ai.fsystem(f"Instructions: {dbs.input['main_prompt']}"),
]
return perform_ai_task(ai, dbs, messages, "specification")
def gen_unit_tests(ai: AI, dbs: DBs):
messages = [
ai.fsystem(f"Instructions: {dbs.input['main_prompt']}"),
ai.fsystem(f"Specification:\n\n{dbs.memory['specification']}"),
]
return perform_ai_task(ai, dbs, messages, "unit_tests")
def gen_code(ai: AI, dbs: DBs):
messages = [
ai.fsystem(f"Instructions: {dbs.input['main_prompt']}"),
ai.fsystem(f"Specification:\n\n{dbs.memory['specification']}"),
ai.fsystem(f"Unit tests:\n\n{dbs.memory['unit_tests']}"),
]
return perform_ai_task(ai, dbs, messages, "code")
Shall I submit a PR for this?