Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions metagpt/utils/repair_llm_raw_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,9 @@ def extract_state_value_from_output(content: str) -> str:
"""
content = content.strip() # deal the output cases like " 0", "0\n" and so on.
pattern = (
r"(?<!-)[0-9]" # TODO find the number using a more proper method not just extract from content using pattern
r"-?\d+" # extract the first (possibly multi-digit or negative) integer from the output
)
matches = re.findall(pattern, content, re.DOTALL)
matches = list(set(matches))
state = matches[0] if len(matches) > 0 else "-1"
return state

Expand Down
12 changes: 12 additions & 0 deletions tests/metagpt/utils/test_repair_llm_raw_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,15 @@ class Game{\n +int score\n +list<tile> tiles\n +function move_
assert output.startswith('{\n"Implementation approach"') and output.endswith(
'"Anything UNCLEAR": "The requirement is clear to me."\n}'
)


def test_extract_state_value_from_output():
from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output

# single-digit states and the "-1" no-op state are returned as-is
assert extract_state_value_from_output("0") == "0"
assert extract_state_value_from_output(" 3\n") == "3"
assert extract_state_value_from_output("-1") == "-1"
# multi-digit state indices (>= 10) must be kept whole, not split into digits
assert extract_state_value_from_output("10") == "10"
assert extract_state_value_from_output("The next state is 12.") == "12"
Loading