Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Observation: 100
Final Answer: 110
```

An action's arguments are read as a Python call of literals (a triple-quoted `code` argument arrives whole), else by a `name=value` pattern. Arguments neither reads bind as none, which a tool with a required argument refuses as a tool error. An `Action:` that spells no call gets the format hint. Neither is an episode error.
An action's arguments are read as a Python call of literals (a triple-quoted `code` argument arrives whole), else by a `name=value` pattern. Arguments neither reads bind as none, which a tool with a required argument refuses as a tool error. An `Action:` that spells no call gets the format hint. Neither is an episode error. One action runs per turn: the first, read up to the next `Thought:`, `Action:` or `Observation` line, so a second action or a made-up observation after it is ignored.

## Configuration

Expand Down
10 changes: 8 additions & 2 deletions halo/environments/protocols/react.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,21 @@
logger = logging.getLogger(__name__)

# The ReAct turn grammar: a Thought, then an Action or a Final Answer (the answer wins when both appear).
_THOUGHT_LABELS = "Thought|THOUGHT|Think|THINK|Reasoning|REASONING"
_THOUGHT_RE = re.compile(
r"(?:^|\n)\s*(?:Thought|THOUGHT|Think|THINK|Reasoning|REASONING)\s*:\s*(.+?)(?=\n\s*(?:Action|ACTION|Final|FINAL)|$)",
rf"(?:^|\n)\s*(?:{_THOUGHT_LABELS})\s*:\s*(.+?)(?=\n\s*(?:Action|ACTION|Final|FINAL)|$)",
re.IGNORECASE | re.DOTALL,
)
_FINAL_ANSWER_RE = re.compile(
r"(?:^|\n)\s*(?:Final Answer|FINAL ANSWER|Answer|ANSWER)\s*:\s*(.+?)$", re.IGNORECASE | re.DOTALL
)
# The first Action runs, up to a made-up Observation or the next Thought or Action: read on, the later call's arguments
# would bind to the first tool, or the Thought line would leave the call unparseable. The Thought and Action labels
# match case-sensitively, so a code line such as ``action: int = 3`` inside an argument does not end it.
_ACTION_RE = re.compile(
r"(?:^|\n)\s*(?:Action|ACTION)\s*:\s*(.+?)(?=\n\s*(?:Observation|OBSERVATION)|$)", re.IGNORECASE | re.DOTALL
r"(?:^|\n)\s*(?:Action|ACTION)\s*:\s*(.+?)"
rf"(?=\n\s*(?:Observation|OBSERVATION|(?-i:Action|ACTION|{_THOUGHT_LABELS})\s*:)|$)",
re.IGNORECASE | re.DOTALL,
)
# An action's spellings: ``tool_name(arg=...)``, ``tool_name: arg=...``, a bare ``tool_name``.
_CALL_ACTION_RE = re.compile(r"^(\w+)\s*\((.*)\)$", re.DOTALL)
Expand Down
35 changes: 35 additions & 0 deletions tests/cpu/environments/test_environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,41 @@ def test_parse_react_output_reads_an_escape_python_rejects_without_a_syntax_warn
assert not [w for w in caught if issubclass(w.category, SyntaxWarning)]


@pytest.mark.parametrize(
"second",
[
'Action: calculate(expression="10*10")',
'Action: python(code="print(5)")',
'ACTION: {"name": "python"}',
'Thought: next\nAction: calculate(expression="10*10")',
],
ids=["same-tool", "other-tool", "json", "thought-between"],
)
def test_parse_react_output_runs_the_first_of_two_actions_with_its_own_arguments(second):
"""A turn holding a second ``Action:`` runs its first, as one that runs on into a made-up ``Observation`` does;
read together, the first tool would run with the later call's arguments."""
step = parse_react_output(f'Thought: t\nAction: calculate(expression="1+1")\n{second}')
assert (step.action, step.action_args) == ("calculate", {"expression": "1+1"})


@pytest.mark.parametrize("label", ["Thought", "THOUGHT", "Think", "THINK", "Reasoning", "REASONING"])
def test_parse_react_output_ends_the_action_at_a_thought_line(label):
"""Read into the call, the Thought line leaves it unparseable and the turn gets the format hint."""
step = parse_react_output(f'Thought: t\nAction: calculate(expression="1+1")\n{label}: that should do it')
assert (step.action, step.action_args) == ("calculate", {"expression": "1+1"})


@pytest.mark.parametrize(
"line",
["action = x + 1", "action: int = x + 1", "thought: int = x + 1", "Action = x + 1"],
ids=["action-assignment", "action-annotation", "thought-annotation", "Action-assignment"],
)
def test_parse_react_output_keeps_a_code_line_that_starts_with_action_or_thought(line):
code = f"x = 1\n{line}\nprint(x)"
step = parse_react_output(f"Thought: t\nAction: python(code='''{code}''')")
assert step.action_args == {"code": code}


def test_parse_react_output_numeric_args():
text = """Thought: Calculate
Action: multiply(a=5, b=3.14)"""
Expand Down