[Refactor] Derive is_reasoning_end from the engine grammar (#56200)
Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
@@ -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