modify code based on feedback of action_node.py and repair_llm_raw_output.py, add code in test_repair_llm_raw_output.py

This commit is contained in:
huzixia 2024-01-27 18:06:52 +08:00
parent 2361c7e8aa
commit 11f70ca9b1
3 changed files with 28 additions and 12 deletions

View file

@ -23,10 +23,7 @@ from metagpt.utils.common import OutputParser, general_after_log
TAG = "CONTENT"
LANGUAGE_CONSTRAINT = "Language: Please use the same language as Human INPUT."
FORMAT_CONSTRAINT = (f"Format: output wrapped inside [{TAG}][/{TAG}] like format example, nothing else. "
f"Delete comments in json")
# Delete comments in json
# If you don't want JSONDecodeError to occur, you can add Delete comments in json after FORMAT_CONSTRAINT
FORMAT_CONSTRAINT = f"Format: output wrapped inside [{TAG}][/{TAG}] like format example, nothing else."
SIMPLE_TEMPLATE = """

View file

@ -137,11 +137,10 @@ def repair_json_format(output: str) -> str:
elif output.startswith("{") and output.endswith("]"):
output = output[:-1] + "}"
# remove comments in output json str, after json value content, maybe start with #, maybe start with //
# remove comments in output json string
arr = output.split("\n")
new_arr = []
for line in arr:
# look for # or // comments and make sure they are not inside the string value
new_line = remove_comments_from_line(line)
new_arr.append(new_line)
output = "\n".join(new_arr)
@ -214,12 +213,6 @@ def repair_invalid_json(output: str, error: str) -> str:
new_line = line.replace("}", "")
elif line.endswith("},") and output.endswith("},"):
new_line = line[:-1]
# remove comments in output json str, after json value content, maybe start with #, maybe start with //
elif rline[col_no] == "#" or rline[col_no] == "/":
new_line = rline[:col_no]
# check the next line and remove the comments
for i in range(line_no + 1, len(arr)):
arr[i] = remove_comments_from_line(arr[i])
elif (rline[col_no] in ["'", '"']) and (line.startswith('"') or line.startswith("'")) and "," not in line:
# problem, `"""` or `'''` without `,`
new_line = f",{line}"

View file

@ -141,6 +141,32 @@ def test_repair_json_format():
output = repair_llm_raw_output(output=raw_output, req_keys=[None], repair_type=RepairType.JSON)
assert output == target_output
raw_output = """
{
"Language": "en_us", // define language
"Programming Language": "Python" # define code language
}
"""
target_output = """{
"Language": "en_us",
"Programming Language": "Python"
}"""
output = repair_llm_raw_output(output=raw_output, req_keys=[None], repair_type=RepairType.JSON)
assert output == target_output
raw_output = """
{
"Language": "#en_us#", // define language
"Programming Language": "//Python # Code // Language//" # define code language
}
"""
target_output = """{
"Language": "#en_us#",
"Programming Language": "//Python # Code // Language//"
}"""
output = repair_llm_raw_output(output=raw_output, req_keys=[None], repair_type=RepairType.JSON)
assert output == target_output
def test_repair_invalid_json():
from metagpt.utils.repair_llm_raw_output import repair_invalid_json