[Refactor] Derive is_reasoning_end from the engine grammar (#56200)
Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
@@ -1616,3 +1616,36 @@ class TestCommaInStringValueRegression:
|
||||
assert result.tools_called is True
|
||||
args = json.loads(result.tool_calls[0].function.arguments)
|
||||
assert args["destination"] == "456 Oakwood Avenue, Rivermist, 83214"
|
||||
|
||||
|
||||
class TestGemma4IsReasoningEnd:
|
||||
@staticmethod
|
||||
def _parser(thinking: bool):
|
||||
return Gemma4Parser(
|
||||
_make_tokenizer(_PLAIN_ANSWER_TOKENS),
|
||||
chat_template_kwargs={"enable_thinking": thinking},
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("ids", "thinking", "ended"),
|
||||
[
|
||||
([CHANNEL_START_ID, 3000, CHANNEL_END_ID], True, True),
|
||||
([CHANNEL_START_ID, 3000, TOOL_CALL_START_ID], True, True),
|
||||
([CHANNEL_START_ID, 3000], True, False),
|
||||
([NEW_TURN_ID, 9100], True, False),
|
||||
([CHANNEL_END_ID, NEW_TURN_ID, 9100], True, False),
|
||||
([NEW_TURN_ID, 9100], False, True),
|
||||
# The bundled template's thinking-off prompt: closed channel.
|
||||
([NEW_TURN_ID, 9100, CHANNEL_START_ID, 3000, CHANNEL_END_ID], False, True),
|
||||
# A marker still wins with thinking off: the grammar honours it.
|
||||
([CHANNEL_START_ID, 3000], False, False),
|
||||
# A tool-call opener after the channel closed: still ended.
|
||||
(
|
||||
[CHANNEL_START_ID, 3000, CHANNEL_END_ID, TOOL_CALL_START_ID, 9200],
|
||||
True,
|
||||
True,
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_is_reasoning_end(self, ids, thinking, ended):
|
||||
assert self._parser(thinking).is_reasoning_end(ids) is ended
|
||||
|
||||
@@ -180,9 +180,8 @@ class TestIsReasoningEnd:
|
||||
"""Tool examples before the generation <think> must not end reasoning."""
|
||||
assert not parser.is_reasoning_end([_TOOL_CALL_ID, _TEXT_ID, _THINK_START_ID])
|
||||
|
||||
def test_paired_tool_call_not_end(self, parser):
|
||||
"""Paired <tool_call>...</tool_call> (from template) is NOT end."""
|
||||
assert not parser.is_reasoning_end(
|
||||
def test_completed_tool_call_after_open_think_is_end(self, parser):
|
||||
assert parser.is_reasoning_end(
|
||||
[_THINK_START_ID, 1, _TOOL_CALL_ID, 2, _TOOL_CALL_END_ID]
|
||||
)
|
||||
|
||||
@@ -196,6 +195,43 @@ class TestIsReasoningEnd:
|
||||
assert not parser.is_reasoning_end([])
|
||||
|
||||
|
||||
class TestIsReasoningEndDerivedFromGrammar:
|
||||
@pytest.mark.parametrize(
|
||||
("ids", "thinking", "ended"),
|
||||
[
|
||||
# Explicit close, and the implicit close on <tool_call>.
|
||||
([_THINK_START_ID, 1, _THINK_END_ID], True, True),
|
||||
([_THINK_START_ID, 1, _TOOL_CALL_ID], True, True),
|
||||
# Still inside the block.
|
||||
([_THINK_START_ID, 1], True, False),
|
||||
([_THINK_END_ID, _THINK_START_ID, 1], True, False),
|
||||
# Uncommitted: a bare turn, an empty sequence. Thinking on starts
|
||||
# in REASONING, so reasoning is expected.
|
||||
([_IM_END_ID, _IM_START_ID, 1], True, False),
|
||||
([], True, False),
|
||||
# Thinking off starts in CONTENT with no way into REASONING, so
|
||||
# a <think> the template left behind is inert, and a silent tail
|
||||
# is ended because no block is expected.
|
||||
([_THINK_START_ID, 1], False, True),
|
||||
([_THINK_END_ID, _THINK_START_ID, 1], False, True),
|
||||
([_IM_END_ID, _IM_START_ID, 1], False, True),
|
||||
([], False, True),
|
||||
],
|
||||
)
|
||||
def test_is_reasoning_end(self, mock_tokenizer, ids, thinking, ended):
|
||||
parser = Qwen3Parser(
|
||||
mock_tokenizer, chat_template_kwargs={"enable_thinking": thinking}
|
||||
)
|
||||
assert parser.is_reasoning_end(ids) is ended
|
||||
|
||||
@pytest.mark.parametrize("thinking", [True, False])
|
||||
def test_wait_for_reasoning_follows_thinking(self, mock_tokenizer, thinking):
|
||||
parser = Qwen3Parser(
|
||||
mock_tokenizer, chat_template_kwargs={"enable_thinking": thinking}
|
||||
)
|
||||
assert parser.parser_engine_config.wait_for_reasoning is thinking
|
||||
|
||||
|
||||
class TestIsReasoningEndTurnBoundaries:
|
||||
"""Reasoning markers from earlier turns must not mark the new turn's
|
||||
reasoning as finished (think-marker injection / interleaved-thinking
|
||||
|
||||
Reference in New Issue
Block a user