From be6c3b445554bf4e8278392ab9935cc4cd127afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BC=9F=E9=9F=AC?= Date: Mon, 12 Aug 2024 16:53:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4repair=5Fescape=5Ferror?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/roles/di/role_zero.py | 28 ++++++-------------------- metagpt/utils/repair_llm_raw_output.py | 24 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/metagpt/roles/di/role_zero.py b/metagpt/roles/di/role_zero.py index 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 Editor from metagpt.tools.tool_recommend import BM25ToolRecommender, ToolRecommender from metagpt.tools.tool_registry import register_tool from 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 math commands = 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.py index 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