🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
update: SWE Agent
seeker816a6eadde46
diff --git a/metagpt/roles/di/swe.py b/metagpt/roles/di/swe_agent.pysimilarity index 92%rename from metagpt/roles/di/swe.pyrename to metagpt/roles/di/swe_agent.pyindex dde686e6a..e86b50a9d 100644--- a/metagpt/roles/di/swe.py+++ b/metagpt/roles/di/swe_agent.py@@ -14,9 +14,9 @@ from metagpt.tools.libs.terminal import Bashfrom metagpt.tools.swe_agent_commands.swe_agent_utils import extract_patch-class SWE(RoleZero):- name: str = "SweAgent"- profile: str = "Software Engineer"+class SWEAgent(RoleZero):+ name: str = "Swen"+ profile: str = "Issue Solver"goal: str = "Resolve GitHub issue"_bash_window_size: int = 100_system_msg: str = SWE_AGENT_SYSTEM_TEMPLATE@@ -26,12 +26,14 @@ class SWE(RoleZero):terminal: Bash = Field(default_factory=Bash, exclude=True)output_diff: str = ""max_react_loop: int = 40+ run_eval: bool = Falseasync def _think(self) -> bool:self._set_system_msg()self._format_instruction()res = await super()._think()- await self._handle_action()+ if self.run_eval:+ await self._parse_commands_for_eval()return resdef _set_system_msg(self):@@ -61,7 +63,7 @@ class SWE(RoleZero):return self.instruction- async def _handle_action(self):+ async def _parse_commands_for_eval(self):"""Handles actions based on parsed commands.diff --git a/metagpt/tools/libs/terminal.py b/metagpt/tools/libs/terminal.pyindex 3270b0623..bcf039a5e 100644--- a/metagpt/tools/libs/terminal.py+++ b/metagpt/tools/libs/terminal.py@@ -2,7 +2,7 @@ import subprocessimport threadingfrom queue import Queue-from metagpt.const import SWE_SETUP_PATH, SWE_WORKSPACE_ROOT+from metagpt.const import DEFAULT_WORKSPACE_ROOT, SWE_SETUP_PATHfrom metagpt.tools.tool_registry import register_toolfrom metagpt.utils.report import END_MARKER_VALUE, TerminalReporter@@ -142,7 +142,7 @@ class Bash(Terminal):def __init__(self):"""init"""super().__init__()- self.run_command(f"cd {SWE_WORKSPACE_ROOT}")+ self.run_command(f"cd {DEFAULT_WORKSPACE_ROOT}")self.run_command(f"source {SWE_SETUP_PATH}")def run(self, cmd) -> str:diff --git a/metagpt/tools/swe_agent_commands/__init__.py b/metagpt/tools/swe_agent_commands/__init__.pyindex 42e92a12d..c0d3e2a60 100644--- a/metagpt/tools/swe_agent_commands/__init__.py+++ b/metagpt/tools/swe_agent_commands/__init__.py@@ -1,5 +1,5 @@"""-This tool is originally developed by the team behind the princeton-nlp/SWE-agent repository.+This folder is borrowed from princeton-nlp/SWE-agentYou can find the original repository here:https://github.com/princeton-nlp/SWE-agent/tree/main/config/commandsWe are using a modified version from OpenDevin:diff --git a/tests/metagpt/roles/di/run_swe.py b/tests/metagpt/roles/di/run_sweagent_for_benchmark.pysimilarity index 96%rename from tests/metagpt/roles/di/run_swe.pyrename to tests/metagpt/roles/di/run_sweagent_for_benchmark.pyindex 9965107db..cdd8df5d7 100644--- a/tests/metagpt/roles/di/run_swe.py+++ b/tests/metagpt/roles/di/run_sweagent_for_benchmark.py@@ -5,7 +5,7 @@ from datetime import datetimefrom metagpt.config2 import configfrom metagpt.const import DEFAULT_WORKSPACE_ROOT, METAGPT_ROOTfrom metagpt.logs import logger-from metagpt.roles.di.swe import SWE+from metagpt.roles.di.swe_agent import SWEAgentfrom metagpt.tools.libs.terminal import Terminalfrom metagpt.tools.swe_agent_commands.swe_agent_utils import load_hf_dataset@@ -74,13 +74,14 @@ async def run(instance, swe_result_dir):)logger.info(f"**** Starting to run {instance['instance_id']}****")- swe_agent = SWE()+ swe_agent = SWEAgent()+ swe_agent.run_eval = Trueawait swe_agent.run(user_requirement_and_issue)save_predictions(swe_agent, instance, swe_result_dir)logger.info(f"**** Finished running {instance['instance_id']}****")-def save_predictions(swe_agent: SWE, instance, swe_result_dir):+def save_predictions(swe_agent: SWEAgent, instance, swe_result_dir):output_file = swe_result_dir / "all_preds.jsonl"instance["model_name_or_path"] = swe_agent.config.llm.modelinstance["model_patch"] = swe_agent.output_diffdiff --git a/tests/metagpt/roles/di/run_swe_new.py b/tests/metagpt/roles/di/run_sweagent_open_source_issue.pysimilarity index 53%rename from tests/metagpt/roles/di/run_swe_new.pyrename to tests/metagpt/roles/di/run_sweagent_open_source_issue.pyindex 8ed1e9107..ec87dd7e2 100644--- a/tests/metagpt/roles/di/run_swe_new.py+++ b/tests/metagpt/roles/di/run_sweagent_open_source_issue.py@@ -1,41 +1,44 @@import asynciofrom metagpt.logs import logger-from metagpt.roles.di.swe import SWE+from metagpt.roles.di.swe_agent import SWEAgentFIX_ISSUE1 = """Write a fix for this issue: https://github.com/langchain-ai/langchain/issues/20453,-you can fix it on this repo https://github.com/garylin2099/langchain,+you can fix it on this repo https://github.com/garylin2099/langchain"""-# + "checkout a branch named test-fix, commit your changes, push, and create a PR to the master branch of https://github.com/iorisa/langchain"-+# + "checkout a branch named test-fix, commit your changes, push,+# and create a PR to the master branch of https://github.com/iorisa/langchain"+# """FIX_ISSUE2 = """Write a fix for this issue https://github.com/geekan/MetaGPT/issues/1275.-You can fix it on the v0.8-release branch of this repo https://github.com/garylin2099/MetaGPT,+You can fix it on the v0.8-release branch of this repo https://github.com/garylin2099/MetaGPT"""-# + "during fixing, checkout a branch named test-fix-1275, commit your changes, push, and create a PR to the v0.8-release branch of https://github.com/garylin2099/MetaGPT"+# + "during fixing, checkout a branch named test-fix-1275, commit your changes, push,+# and create a PR to the v0.8-release branch of https://github.com/garylin2099/MetaGPT"FIX_ISSUE3 = """Write a fix for this issue https://github.com/geekan/MetaGPT/issues/1262.-You can fix it on this repo https://github.com/garylin2099/MetaGPT,-during fixing, checkout a branch named test-fix-1262, commit your changes, push, and create a PR to https://github.com/garylin2099/MetaGPT+You can fix it on this repo https://github.com/garylin2099/MetaGPT"""+# during fixing, checkout a branch named test-fix-1262, commit your changes, push,+# and create a PR to https://github.com/garylin2099/MetaGPT+# """FIX_ISSUE_SIMPLE = """Write a fix for this issue: https://github.com/mannaandpoem/simple_calculator/issues/1,-you can fix it on this repo https://github.com/garylin2099/simple_calculator,-checkout a branch named test, commit your changes, push, and create a PR to the master branch of original repo.+you can fix it on this repo https://github.com/garylin2099/simple_calculator"""+# checkout a branch named test, commit your changes, push, and create a PR to the master branch of original repo.+# """++NO_ENV_TIP = """+Because the environment is not available, you DO NOT need to run and modify any existing test case files or+add new test case files to ensure that the bug is fixed.+"""if __name__ == "__main__":- swe_agent = SWE()+ swe_agent = SWEAgent()logger.info("**** Starting run ****")- user_requirement_and_issue = (- # FIX_ISSUE1- # FIX_ISSUE2- # FIX_ISSUE3- FIX_ISSUE_SIMPLE- + """Because the environment is not available, you DO NOT need to run and modify any existing test case files or add new test case files to ensure that the bug is fixed."""- )+ user_requirement_and_issue = FIX_ISSUE1 + NO_ENV_TIPasyncio.run(swe_agent.run(user_requirement_and_issue))logger.info("**** Finished running ****")- logger.info(f"Patch: {swe_agent.output_diff}")