opt engineer2

This commit is contained in:
hongjiongteng 2024-06-20 10:34:20 +08:00
parent 942a6d61bb
commit 599eecf1f4
2 changed files with 15 additions and 9 deletions

View file

@ -1,3 +1,5 @@
import asyncio
from tenacity import retry, stop_after_attempt, wait_random_exponential
from metagpt.actions.action import Action
@ -18,16 +20,18 @@ class RewriteCode(Action):
name: str = "RewriteCode"
async def run(self, code_path: str, design_doc: str = "", task_doc: str = "", code_review_k_times: int = 2) -> str:
async def run(
self, code_path: str, design_doc_path: str = "", task_doc_path: str = "", code_review_k_times: int = 2
) -> str:
"""Reviews the provided code based on the accompanying design and task documentation, return the complete and correct code.
Read the code from `code_path`, and write the final code to `code_path`.
If there is no `design_doc` or `task_doc`, it will return and do nothing.
If there is no `design_doc_path` or `task_doc_path`, it will return and do nothing.
Args:
code_path (str): The file path of the code snippet to be reviewed. This should be a string containing the path to the source code file.
design_doc (str): The design document associated with the code. This should describe the system architecture, used in the code. It helps provide context for the review process.
task_doc (str): The task document describing what the code is intended to accomplish. This should outline the functional requirements or objectives of the code.
design_doc_path (str): The file path of the design document associated with the code. This should describe the system architecture, used in the code. It helps provide context for the review process.
task_doc_path (str): The file path of the task document describing what the code is intended to accomplish. This should outline the functional requirements or objectives of the code.
code_review_k_times (int, optional): The number of iterations for reviewing and potentially rewriting the code. Defaults to 2.
Returns:
@ -37,14 +41,16 @@ class RewriteCode(Action):
# Example of how to call the run method with a code snippet and documentation
await WriteCodeReview().run(
code_path="/tmp/game.js",
design_doc='{"Implementation approach":"We will implement the 2048 game..."}',
task_doc='{"Required packages":["No third-party dependencies required"],"..."}'
design_doc="/tmp/design_doc.json",
task_doc="/tmp/task_doc.json"
)
"""
if not design_doc or not task_doc:
if not design_doc_path or not design_doc_path:
return
code = await aread(code_path)
code, design_doc, task_doc = await asyncio.gather(
aread(code_path), aread(design_doc_path), aread(task_doc_path)
)
context = "\n".join(
[

View file

@ -10,7 +10,7 @@ EXTRA_INSTRUCTION = """
10. When provided system design, YOU MUST FOLLOW "Data structures and interfaces". DONT CHANGE ANY DESIGN. Do not use public member functions that do not exist in your design.
11. Write out EVERY CODE DETAIL, DON'T LEAVE TODO.
12. To modify code in a file, read the entire file, make changes, and use Editor.write instead of Editor.write_content to update the file with the complete code.
13. Revise task is to use RewriteCode.run to correct code, must pass the content of system design and project schedule instead of just file path of them.
13. Revise task is to use RewriteCode.run to correct code.
14. Only When provided system design, at the end of the plan, add a Revise task for each file; for example, if there are three files, add three Revise tasks.
"""