Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix repair_llm_raw_output ut due to import order problem #678

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions tests/metagpt/utils/test_repair_llm_raw_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
# -*- coding: utf-8 -*-
# @Desc : unittest of repair_llm_raw_output


from metagpt.config import CONFIG
from metagpt.utils.repair_llm_raw_output import (
RepairType,
extract_content_from_output,
repair_invalid_json,
repair_llm_raw_output,
retry_parse_json_text,
)

"""
CONFIG.repair_llm_output should be True before retry_parse_json_text imported.
so we move `from ... impot ...` into each `test_xx` to avoid `Module level import not at top of file` format warning.
"""
CONFIG.repair_llm_output = True


def test_repair_case_sensitivity():
from metagpt.utils.repair_llm_raw_output import repair_llm_raw_output

raw_output = """{
"Original requirements": "Write a 2048 game",
"search Information": "",
Expand All @@ -36,6 +34,8 @@ def test_repair_case_sensitivity():


def test_repair_special_character_missing():
from metagpt.utils.repair_llm_raw_output import repair_llm_raw_output

raw_output = """[CONTENT]
"Anything UNCLEAR": "No unclear requirements or information."
[CONTENT]"""
Expand Down Expand Up @@ -66,11 +66,12 @@ def test_repair_special_character_missing():
target_output = '[CONTENT] {"a": "b"} [/CONTENT]'

output = repair_llm_raw_output(output=raw_output, req_keys=["[/CONTENT]"])
print("output\n", output)
assert output == target_output


def test_required_key_pair_missing():
from metagpt.utils.repair_llm_raw_output import repair_llm_raw_output

raw_output = '[CONTENT] {"a": "b"}'
target_output = '[CONTENT] {"a": "b"}\n[/CONTENT]'

Expand Down Expand Up @@ -107,6 +108,8 @@ def test_required_key_pair_missing():


def test_repair_json_format():
from metagpt.utils.repair_llm_raw_output import RepairType, repair_llm_raw_output

raw_output = "{ xxx }]"
target_output = "{ xxx }"

Expand All @@ -127,6 +130,8 @@ def test_repair_json_format():


def test_repair_invalid_json():
from metagpt.utils.repair_llm_raw_output import repair_invalid_json

raw_output = """{
"key": "value"
},
Expand Down Expand Up @@ -169,6 +174,8 @@ def test_repair_invalid_json():


def test_retry_parse_json_text():
from metagpt.utils.repair_llm_raw_output import retry_parse_json_text

invalid_json_text = """{
"Original Requirements": "Create a 2048 game",
"Competitive Quadrant Chart": "quadrantChart\n\ttitle Reach and engagement of campaigns\n\t\tx-axis"
Expand Down Expand Up @@ -205,6 +212,7 @@ def test_extract_content_from_output():
xxx [CONTENT] xxx [CONTENT] xxxx [/CONTENT]
xxx [CONTENT] xxxx [/CONTENT] xxx [CONTENT][/CONTENT] xxx [CONTENT][/CONTENT] # target pair is the last one
"""
from metagpt.utils.repair_llm_raw_output import extract_content_from_output

output = (
'Sure! Here is the properly formatted JSON output based on the given context:\n\n[CONTENT]\n{\n"'
Expand Down
Loading