mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-04-27 09:46:24 +02:00
调整repair_escape_error函数位置
This commit is contained in:
parent
c870167fe4
commit
be6c3b4455
2 changed files with 30 additions and 22 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue