Merge branch 'mgx_ops' into add_swe_agent_ablilities_to_engineer2

This commit is contained in:
黄伟韬 2024-09-05 20:04:57 +08:00
commit 166eb2db79
8 changed files with 38 additions and 37 deletions

View file

@ -16,6 +16,7 @@ from metagpt.prompts.di.engineer2 import (
from metagpt.roles.di.role_zero import RoleZero
from metagpt.schema import Message, UserMessage
from metagpt.strategy.experience_retriever import ENGINEER_EXAMPLE
from metagpt.tools.libs.cr import CodeReview
from metagpt.tools.libs.git import git_create_pull
from metagpt.tools.libs.terminal import Terminal
from metagpt.tools.tool_registry import register_tool
@ -40,6 +41,7 @@ class Engineer2(RoleZero):
"git_create_pull",
"SearchEnhancedQA",
"Engineer2",
"CodeReview",
]
# SWE Agent parameter
run_eval: bool = False
@ -64,11 +66,15 @@ class Engineer2(RoleZero):
self.cmd_prompt_current_state = CURRENT_STATE.format(**state).strip()
def _update_tool_execution(self):
# validate = ValidateAndRewriteCode()
cr = CodeReview()
self.tool_execution_map.update(
{
"Terminal.run_command": self.terminal.run_command,
"git_create_pull": git_create_pull,
"Engineer2.write_new_code": self.write_new_code,
"CodeReview.review": cr.review,
"CodeReview.fix": cr.fix,
# "ValidateAndRewriteCode.run": validate.run,
# "ValidateAndRewriteCode": validate.run,
}

View file

@ -89,7 +89,7 @@ class RoleZero(Role):
# Others
command_rsp: str = "" # the raw string containing the commands
commands: list[dict] = [] # commands to be executed
memory_k: int = 100 # number of memories (messages) to use as historical context
memory_k: int = 200 # number of memories (messages) to use as historical context
use_fixed_sop: bool = False
requirements_constraints: str = "" # the constraints in user requirements
use_summary: bool = True # whether to summarize at the end

View file

@ -11,7 +11,7 @@ from metagpt.prompts.di.swe_agent import (
from metagpt.roles.di.role_zero import RoleZero
from metagpt.schema import Message
from metagpt.tools.libs.git import git_create_pull
from metagpt.tools.libs.terminal import Terminal
from metagpt.tools.libs.terminal import Bash
class SWEAgent(RoleZero):
@ -19,8 +19,13 @@ class SWEAgent(RoleZero):
profile: str = "Issue Solver"
goal: str = "Resolve GitHub issue or bug in any existing codebase"
_instruction: str = NEXT_STEP_TEMPLATE
tools: list[str] = ["Browser:goto,scroll", "RoleZero", "git_create_pull", "Editor", "Terminal"]
terminal: Terminal = Field(default_factory=Terminal, exclude=True)
tools: list[str] = [
"Bash",
"Browser:goto,scroll",
"RoleZero",
"git_create_pull",
]
terminal: Bash = Field(default_factory=Bash, exclude=True)
output_diff: str = ""
max_react_loop: int = 40
run_eval: bool = False
@ -33,29 +38,14 @@ class SWEAgent(RoleZero):
def _update_tool_execution(self):
self.tool_execution_map.update(
{
"Terminal.run_command": self.eval_terminal_run if self.run_eval else self.terminal.run_command,
"Bash.run": self.terminal.run,
"git_create_pull": git_create_pull,
}
)
async def eval_terminal_run(self, cmd):
"""change command pull/push/commit to end."""
if any([cmd_key_word in cmd for cmd_key_word in ["pull", "push", "commit"]]):
# Observe that SWEAgent tries to submit the repo after fixing the bug.
# Set self.rc.todo to None and use git -diff to record the change.
logger.info("SWEAgent use cmd:{cmd}")
logger.info("finish current task")
# stop the sweagent
self._set_state(-1)
command_output = "Current test case is finished."
else:
command_output = await self.terminal.run_command(cmd)
return command_output
async def _format_instruction(self):
"""
Formats the instruction message for the SWE agent.
Runs the "state" command in the terminal, parses its output as JSON,
and uses it to format the `_instruction` template.
"""
@ -66,16 +56,14 @@ class SWEAgent(RoleZero):
async def _act(self) -> Message:
message = await super()._act()
if self.run_eval:
await self._parse_commands_for_eval()
self._parse_commands_for_eval()
return message
async def _parse_commands_for_eval(self):
"""
Handles actions based on parsed commands.
Parses commands, checks for a "submit" action, and generates a patch using `git diff`.
Stores the cleaned patch in `output_diff`. Logs any exceptions.
This function is specifically added for SWE bench evaluation.
"""
# If todo switches to None, it indicates that this is the final round of reactions, and the Swe-Agent will stop. Use git diff to store any changes made.
@ -88,7 +76,6 @@ class SWEAgent(RoleZero):
logger.info(f"Diff output: \n{clear_diff}")
if clear_diff:
self.output_diff = clear_diff
except Exception as e:
logger.error(f"Error during submission: {e}")