[Frontend] Add Streaming Parser Engine and new MinimaxM2 Parser (#45701)
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
This commit is contained in:
@@ -0,0 +1,264 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.parser.engine.conftest import make_mock_tokenizer
|
||||
from tests.parser.engine.streaming_helpers import (
|
||||
collect_function_name,
|
||||
collect_tool_arguments,
|
||||
simulate_tool_streaming,
|
||||
)
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
ChatCompletionToolsParam,
|
||||
FunctionDefinition,
|
||||
)
|
||||
from vllm.parser.minimax_m2 import (
|
||||
THINK_END,
|
||||
TOOL_CALL_END,
|
||||
TOOL_CALL_START,
|
||||
MinimaxM2Parser,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_tokenizer():
|
||||
return make_mock_tokenizer(
|
||||
{
|
||||
THINK_END: 99,
|
||||
TOOL_CALL_START: 100,
|
||||
TOOL_CALL_END: 101,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def parser(mock_tokenizer):
|
||||
return MinimaxM2Parser(mock_tokenizer)
|
||||
|
||||
|
||||
def make_tools(*names: str):
|
||||
return [
|
||||
ChatCompletionToolsParam(
|
||||
function=FunctionDefinition(
|
||||
name=name,
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
},
|
||||
),
|
||||
)
|
||||
for name in names
|
||||
]
|
||||
|
||||
|
||||
class TestNonStreaming:
|
||||
def test_no_tool_calls(self, parser, mock_request):
|
||||
result = parser.extract_tool_calls(
|
||||
"</think>This is a regular response without tool calls.",
|
||||
mock_request,
|
||||
)
|
||||
assert result.tools_called is False
|
||||
assert result.tool_calls == []
|
||||
assert result.content == "This is a regular response without tool calls."
|
||||
|
||||
def test_single_tool_call(self, parser, mock_request):
|
||||
result = parser.extract_tool_calls(
|
||||
'<minimax:tool_call><invoke name="get_weather">'
|
||||
'<parameter name="city">Seattle</parameter>'
|
||||
"</invoke></minimax:tool_call>",
|
||||
mock_request,
|
||||
)
|
||||
|
||||
assert result.tools_called is True
|
||||
assert len(result.tool_calls) == 1
|
||||
assert result.tool_calls[0].function.name == "get_weather"
|
||||
assert json.loads(result.tool_calls[0].function.arguments) == {
|
||||
"city": "Seattle",
|
||||
}
|
||||
|
||||
def test_multiple_invokes(self, parser, mock_request):
|
||||
result = parser.extract_tool_calls(
|
||||
"<minimax:tool_call>"
|
||||
'<invoke name="search"><parameter name="q">OpenAI</parameter></invoke>'
|
||||
'<invoke name="search"><parameter name="q">vLLM</parameter></invoke>'
|
||||
"</minimax:tool_call>",
|
||||
mock_request,
|
||||
)
|
||||
|
||||
assert result.tools_called is True
|
||||
assert [tc.function.name for tc in result.tool_calls] == ["search", "search"]
|
||||
assert json.loads(result.tool_calls[0].function.arguments) == {"q": "OpenAI"}
|
||||
assert json.loads(result.tool_calls[1].function.arguments) == {"q": "vLLM"}
|
||||
|
||||
def test_schema_type_coercion(self, mock_tokenizer, mock_request):
|
||||
tools = [
|
||||
ChatCompletionToolsParam(
|
||||
function=FunctionDefinition(
|
||||
name="forecast",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"days": {"type": "integer"},
|
||||
"include_hourly": {"type": "boolean"},
|
||||
},
|
||||
},
|
||||
),
|
||||
)
|
||||
]
|
||||
parser = MinimaxM2Parser(mock_tokenizer, tools=tools)
|
||||
mock_request.tools = tools
|
||||
|
||||
result = parser.extract_tool_calls(
|
||||
'<minimax:tool_call><invoke name="forecast">'
|
||||
'<parameter name="days">5</parameter>'
|
||||
'<parameter name="include_hourly">true</parameter>'
|
||||
"</invoke></minimax:tool_call>",
|
||||
mock_request,
|
||||
)
|
||||
|
||||
assert json.loads(result.tool_calls[0].function.arguments) == {
|
||||
"days": 5,
|
||||
"include_hourly": True,
|
||||
}
|
||||
|
||||
def test_invalid_tool_name_is_rejected(self, mock_tokenizer, mock_request):
|
||||
tools = make_tools("search")
|
||||
parser = MinimaxM2Parser(mock_tokenizer)
|
||||
mock_request.tools = tools
|
||||
|
||||
result = parser.extract_tool_calls(
|
||||
'<minimax:tool_call><invoke name="img_gen">'
|
||||
'<parameter name="prompt">a cat</parameter>'
|
||||
"</invoke></minimax:tool_call>",
|
||||
mock_request,
|
||||
)
|
||||
|
||||
assert result.tools_called is False
|
||||
assert result.tool_calls == []
|
||||
|
||||
def test_mixed_tool_names_only_return_valid(self, mock_tokenizer, mock_request):
|
||||
tools = make_tools("search")
|
||||
parser = MinimaxM2Parser(mock_tokenizer)
|
||||
mock_request.tools = tools
|
||||
|
||||
result = parser.extract_tool_calls(
|
||||
"<minimax:tool_call>"
|
||||
'<invoke name="img_gen"><parameter name="prompt">cat</parameter></invoke>'
|
||||
'<invoke name="search"><parameter name="query">news</parameter></invoke>'
|
||||
"</minimax:tool_call>",
|
||||
mock_request,
|
||||
)
|
||||
|
||||
assert result.tools_called is True
|
||||
assert [tc.function.name for tc in result.tool_calls] == ["search"]
|
||||
assert json.loads(result.tool_calls[0].function.arguments) == {
|
||||
"query": "news",
|
||||
}
|
||||
|
||||
|
||||
class TestStreaming:
|
||||
def test_streaming_single_tool_call(self, parser, mock_request):
|
||||
results = simulate_tool_streaming(
|
||||
parser,
|
||||
mock_request,
|
||||
[
|
||||
"<minimax:tool_call>",
|
||||
'<invoke name="get_weather">',
|
||||
'<parameter name="city">Seattle</parameter>',
|
||||
"</invoke></minimax:tool_call>",
|
||||
],
|
||||
)
|
||||
|
||||
assert collect_function_name(results) == "get_weather"
|
||||
assert json.loads(collect_tool_arguments(results)) == {
|
||||
"city": "Seattle",
|
||||
}
|
||||
|
||||
def test_streaming_multiple_invokes(self, parser, mock_request):
|
||||
results = simulate_tool_streaming(
|
||||
parser,
|
||||
mock_request,
|
||||
[
|
||||
"<minimax:tool_call>",
|
||||
'<invoke name="a"><parameter name="x">1</parameter></invoke>',
|
||||
'<invoke name="b"><parameter name="y">2</parameter></invoke>',
|
||||
"</minimax:tool_call>",
|
||||
],
|
||||
)
|
||||
|
||||
tool_names = [
|
||||
tc.function.name
|
||||
for delta, _ in results
|
||||
if delta and delta.tool_calls
|
||||
for tc in delta.tool_calls
|
||||
if tc.function and tc.function.name
|
||||
]
|
||||
assert tool_names == ["a", "b"]
|
||||
|
||||
def test_streaming_invoke_prefix_split_before_quote(self, parser, mock_request):
|
||||
results = simulate_tool_streaming(
|
||||
parser,
|
||||
mock_request,
|
||||
[
|
||||
"<minimax:tool_call>",
|
||||
"<invoke name=",
|
||||
'"get_weather">',
|
||||
'<parameter name="city">Seattle</parameter>',
|
||||
"</invoke></minimax:tool_call>",
|
||||
],
|
||||
)
|
||||
|
||||
assert collect_function_name(results) == "get_weather"
|
||||
assert json.loads(collect_tool_arguments(results)) == {
|
||||
"city": "Seattle",
|
||||
}
|
||||
|
||||
def test_streaming_invalid_tool_name_is_rejected(
|
||||
self, mock_tokenizer, mock_request
|
||||
):
|
||||
tools = make_tools("search")
|
||||
parser = MinimaxM2Parser(mock_tokenizer)
|
||||
mock_request.tools = tools
|
||||
|
||||
results = simulate_tool_streaming(
|
||||
parser,
|
||||
mock_request,
|
||||
[
|
||||
"<minimax:tool_call>",
|
||||
'<invoke name="img_gen">',
|
||||
'<parameter name="prompt">cat</parameter>',
|
||||
"</invoke></minimax:tool_call>",
|
||||
],
|
||||
)
|
||||
|
||||
assert collect_function_name(results) is None
|
||||
assert collect_tool_arguments(results) == ""
|
||||
|
||||
|
||||
class TestReasoning:
|
||||
def test_extract_reasoning_without_start_token(self, parser, mock_request):
|
||||
reasoning, content = parser.extract_reasoning(
|
||||
"This is reasoning</think>This is content",
|
||||
mock_request,
|
||||
)
|
||||
|
||||
assert reasoning == "This is reasoning"
|
||||
assert content == "This is content"
|
||||
|
||||
def test_extract_reasoning_without_end_token(self, parser, mock_request):
|
||||
reasoning, content = parser.extract_reasoning(
|
||||
"This is still reasoning",
|
||||
mock_request,
|
||||
)
|
||||
|
||||
assert reasoning == "This is still reasoning"
|
||||
assert content is None
|
||||
|
||||
def test_extract_content_ids_without_end_token(self, parser):
|
||||
assert parser.extract_content_ids([1, 2, 3]) == []
|
||||
|
||||
def test_extract_content_ids_after_end_token(self, parser):
|
||||
assert parser.extract_content_ids([1, 99, 2, 3]) == [2, 3]
|
||||
@@ -30,6 +30,7 @@ from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
)
|
||||
from vllm.parser.engine.registered_adapters import (
|
||||
Gemma4Parser,
|
||||
MinimaxM2Parser,
|
||||
NemotronV3Parser,
|
||||
Qwen3Parser,
|
||||
)
|
||||
@@ -393,6 +394,79 @@ def _build_qwen3(
|
||||
return sample
|
||||
|
||||
|
||||
# ── MiniMax M2 (XML invoke format, starts in REASONING) ──────────────
|
||||
|
||||
_MINIMAX_M2_VOCAB: dict[str, int] = {
|
||||
"<think>": 50,
|
||||
"</think>": 51,
|
||||
"<minimax:tool_call>": 60,
|
||||
"</minimax:tool_call>": 61,
|
||||
}
|
||||
|
||||
|
||||
def _minimax_m2_arg_value(value: Any) -> str:
|
||||
if isinstance(value, bool):
|
||||
return "true" if value else "false"
|
||||
if isinstance(value, (int, float)):
|
||||
return str(value)
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
return json.dumps(value, ensure_ascii=False)
|
||||
|
||||
|
||||
def _minimax_m2_tool_segments(tool_calls: list[ToolCallSpec]) -> list[tuple[str, bool]]:
|
||||
segs: list[tuple[str, bool]] = [("<minimax:tool_call>", True)]
|
||||
for tc in tool_calls:
|
||||
segs.append((f'<invoke name="{tc.name}">', False))
|
||||
for key, value in tc.arguments.items():
|
||||
segs.append(
|
||||
(
|
||||
f'<parameter name="{key}">'
|
||||
f"{_minimax_m2_arg_value(value)}"
|
||||
"</parameter>",
|
||||
False,
|
||||
)
|
||||
)
|
||||
segs.append(("</invoke>", False))
|
||||
segs.append(("</minimax:tool_call>", True))
|
||||
return segs
|
||||
|
||||
|
||||
def _minimax_m2_segments(scenario: Scenario) -> list[tuple[str, bool]]:
|
||||
segs: list[tuple[str, bool]] = []
|
||||
if scenario.reasoning is not None:
|
||||
segs.append((scenario.reasoning, False))
|
||||
if scenario.content is not None or scenario.tool_calls:
|
||||
segs.append(("</think>", True))
|
||||
if scenario.content is not None:
|
||||
segs.append((scenario.content, False))
|
||||
if scenario.tool_calls:
|
||||
segs.extend(_minimax_m2_tool_segments(scenario.tool_calls))
|
||||
return segs
|
||||
|
||||
|
||||
def _build_minimax_m2(scenario: Scenario, validate: bool = True) -> Sample:
|
||||
expected_reasoning: str | None
|
||||
if scenario.reasoning is not None:
|
||||
expected_reasoning = scenario.reasoning.rstrip()
|
||||
else:
|
||||
expected_reasoning = ""
|
||||
|
||||
sample = _make_sample(
|
||||
sample_id=f"minimax_m2-{scenario.id}",
|
||||
description=scenario.description,
|
||||
vocab=_MINIMAX_M2_VOCAB,
|
||||
segments=_minimax_m2_segments(scenario),
|
||||
expected_reasoning=expected_reasoning,
|
||||
expected_content=_qwen3_expected_content(scenario),
|
||||
expected_tool_calls=_expected_tc(scenario),
|
||||
tools=_expected_tools(scenario),
|
||||
)
|
||||
if validate:
|
||||
_validate_sample(sample, MinimaxM2Parser)
|
||||
return sample
|
||||
|
||||
|
||||
# ── Gemma4 (channel reasoning, custom arg format) ────────────────────
|
||||
|
||||
_GEMMA4_VOCAB: dict[str, int] = {
|
||||
@@ -502,6 +576,7 @@ def _build_nemotron_v3(scenario: Scenario, validate: bool = True) -> Sample:
|
||||
_BUILDERS: dict[str, Any] = {
|
||||
"qwen3": _build_qwen3,
|
||||
"gemma4": _build_gemma4,
|
||||
"minimax_m2": _build_minimax_m2,
|
||||
"nemotron_v3": _build_nemotron_v3,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user