🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
调整repair_escape_error函数位置
黄伟韬be6c3b445554
diff --git a/metagpt/roles/di/role_zero.py b/metagpt/roles/di/role_zero.pyindex 032bf8101..960dfa805 100644--- a/metagpt/roles/di/role_zero.py+++ b/metagpt/roles/di/role_zero.py@@ -35,7 +35,11 @@ from metagpt.tools.libs.editor import Editorfrom metagpt.tools.tool_recommend import BM25ToolRecommender, ToolRecommenderfrom metagpt.tools.tool_registry import register_toolfrom metagpt.utils.common import CodeParser, any_to_str-from metagpt.utils.repair_llm_raw_output import RepairType, repair_llm_raw_output+from metagpt.utils.repair_llm_raw_output import (+ RepairType,+ repair_escape_error,+ repair_llm_raw_output,+)from metagpt.utils.report import ThoughtReporter@@ -326,7 +330,7 @@ class RoleZero(Role):except json.JSONDecodeError:# repair escape error of code and mathcommands = CodeParser.parse_code(block=None, lang="json", text=self.command_rsp)- new_command = self.repair_escape_error(commands)+ new_command = repair_escape_error(commands)commands = json.loads(repair_llm_raw_output(output=new_command, req_keys=[None], repair_type=RepairType.JSON))@@ -342,26 +346,6 @@ class RoleZero(Role):commands = commands["commands"] if "commands" in commands else [commands]return commands, True- def repair_escape_error(self, commands):- """Repaires escape errors in command responses"""- escape_repair_map = {- "\a": "\\\\a",- "\b": "\\\\b",- "\f": "\\\\f",- "\r": "\\\\r",- "\t": "\\\\t",- "\v": "\\\\v",- }- new_command = ""- for index, ch in enumerate(commands):- if ch == "\\" and index + 1 < len(commands):- if commands[index + 1] not in ["n", '"', " "]:- new_command += "\\"- elif ch in escape_repair_map:- ch = escape_repair_map[ch]- new_command += ch- return commands-async def _run_commands(self, commands) -> str:outputs = []for cmd in commands:diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.pyindex 17e095c5f..fc27448eb 100644--- a/metagpt/utils/repair_llm_raw_output.py+++ b/metagpt/utils/repair_llm_raw_output.py@@ -347,3 +347,27 @@ def extract_state_value_from_output(content: str) -> str:matches = list(set(matches))state = matches[0] if len(matches) > 0 else "-1"return state+++def repair_escape_error(commands):+ """+ Repaires escape errors in command responses.+ When role-zero parses a command, the command may contain unknown escape characters.+ """+ escape_repair_map = {+ "\a": "\\\\a",+ "\b": "\\\\b",+ "\f": "\\\\f",+ "\r": "\\\\r",+ "\t": "\\\\t",+ "\v": "\\\\v",+ }+ new_command = ""+ for index, ch in enumerate(commands):+ if ch == "\\" and index + 1 < len(commands):+ if commands[index + 1] not in ["n", '"', " "]:+ new_command += "\\"+ elif ch in escape_repair_map:+ ch = escape_repair_map[ch]+ new_command += ch+ return new_command