[Frontend] Add Streaming Parser Engine and new MinimaxM2 Parser (#45701)
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
This commit is contained in:
@@ -127,12 +127,14 @@ class IncrementalLexer:
|
||||
|
||||
def flush(self) -> list[LexToken]:
|
||||
tokens: list[LexToken] = []
|
||||
if self.buffer:
|
||||
tokens.extend(self._drain(final=True))
|
||||
if self.buffer:
|
||||
tokens.append(LexToken(self.content_terminal, self.buffer))
|
||||
self.buffer = ""
|
||||
return tokens
|
||||
|
||||
def _drain(self) -> list[LexToken]:
|
||||
def _drain(self, *, final: bool = False) -> list[LexToken]:
|
||||
tokens: list[LexToken] = []
|
||||
first_chars = self._literal_first_chars
|
||||
content_terminal = self.content_terminal
|
||||
@@ -161,11 +163,22 @@ class IncrementalLexer:
|
||||
):
|
||||
best_match = (name, lit, len(lit))
|
||||
|
||||
if self.buffer in prefix_set:
|
||||
# If the current buffer is both a complete literal and the prefix
|
||||
# of a longer literal, wait for the next chunk. For example,
|
||||
# "<invoke name=" should not be emitted before the next chunk
|
||||
# proves whether this is the quoted form '<invoke name="'.
|
||||
if self.buffer in prefix_set and not final:
|
||||
if best_match is not None:
|
||||
tokens.append(LexToken(best_match[0], best_match[1]))
|
||||
self.buffer = self.buffer[best_match[2] :]
|
||||
continue
|
||||
longer_match = False
|
||||
for lit, _ in literals_by_first.get(first, ()):
|
||||
if len(lit) > best_match[2] and lit.startswith(self.buffer):
|
||||
longer_match = True
|
||||
break
|
||||
if not longer_match:
|
||||
tokens.append(LexToken(best_match[0], best_match[1]))
|
||||
self.buffer = self.buffer[best_match[2] :]
|
||||
continue
|
||||
break
|
||||
else:
|
||||
break
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ from vllm.parser.engine.streaming_parser_engine import StreamingParserEngine
|
||||
from vllm.tool_parsers.utils import (
|
||||
coerce_to_schema_type,
|
||||
extract_types_from_schema,
|
||||
find_tool_name,
|
||||
find_tool_properties,
|
||||
)
|
||||
|
||||
@@ -319,15 +320,24 @@ class ParserEngine(Parser):
|
||||
return json.dumps(args, ensure_ascii=False)
|
||||
return args_json
|
||||
|
||||
def _is_valid_tool_name(self, name: str) -> bool:
|
||||
if not self.parser_engine_config.validate_tool_names:
|
||||
return True
|
||||
if not self._tools:
|
||||
return True
|
||||
return find_tool_name(self._tools, name)
|
||||
|
||||
# ── Private helpers ─────────────────────────────────────────────
|
||||
|
||||
def _check_skip_tool_parsing(
|
||||
self,
|
||||
request: ChatCompletionRequest | ResponsesRequest,
|
||||
) -> None:
|
||||
tools = getattr(request, "tools", None)
|
||||
if tools:
|
||||
self._tools = tools
|
||||
if not self.skip_tool_parsing:
|
||||
tool_choice = getattr(request, "tool_choice", None)
|
||||
tools = getattr(request, "tools", None)
|
||||
if tool_choice == "none" and tools:
|
||||
self.skip_tool_parsing = True
|
||||
|
||||
@@ -467,6 +477,7 @@ class ParserEngine(Parser):
|
||||
output, this method starts the parser engine in ``CONTENT`` state
|
||||
so it can parse content that has already had reasoning stripped.
|
||||
"""
|
||||
self._check_skip_tool_parsing(request)
|
||||
_, parsed_content, tool_call_info = self._single_pass_parse(
|
||||
content,
|
||||
[],
|
||||
@@ -586,6 +597,7 @@ class ParserEngine(Parser):
|
||||
enable_auto_tools: bool = False,
|
||||
model_output_token_ids: Sequence[int] = (),
|
||||
) -> tuple[str | None, str | None, list[FunctionCall] | None]:
|
||||
self._check_skip_tool_parsing(request)
|
||||
reasoning, content, tool_call_info = self._single_pass_parse(
|
||||
model_output,
|
||||
model_output_token_ids,
|
||||
@@ -705,7 +717,7 @@ class ParserEngine(Parser):
|
||||
deltas: list[DeltaToolCall],
|
||||
name: str | None,
|
||||
) -> None:
|
||||
if not name:
|
||||
if not name or not self._is_valid_tool_name(name):
|
||||
return
|
||||
slot = self._tool_slots[idx]
|
||||
slot.name = name
|
||||
@@ -762,7 +774,7 @@ class ParserEngine(Parser):
|
||||
|
||||
if not slot.name_sent:
|
||||
name = slot.name or self._try_extract_name(idx)
|
||||
if name:
|
||||
if name and self._is_valid_tool_name(name):
|
||||
slot.name = name
|
||||
slot.name_sent = True
|
||||
self._ensure_tool_id(slot, name)
|
||||
@@ -934,7 +946,7 @@ class ParserEngine(Parser):
|
||||
else:
|
||||
args_json = "{}"
|
||||
|
||||
if name:
|
||||
if name and self._is_valid_tool_name(name):
|
||||
self._ensure_tool_id(slot, name)
|
||||
args_json = self._fix_arg_types(args_json, name)
|
||||
tool_calls.append(
|
||||
|
||||
@@ -99,6 +99,9 @@ class ParserEngineConfig:
|
||||
# .strip() content text when tool calls are present.
|
||||
strip_content_whitespace_with_tools: bool = True
|
||||
|
||||
# Reject tool calls whose names are absent from the request tools.
|
||||
validate_tool_names: bool = False
|
||||
|
||||
drop_tokens: frozenset[str] = field(default_factory=frozenset)
|
||||
|
||||
@cached_property
|
||||
|
||||
@@ -9,9 +9,15 @@ names so that :class:`ReasoningParserManager` and
|
||||
|
||||
from vllm.parser.engine.adapters import make_adapters
|
||||
from vllm.parser.gemma4 import Gemma4Parser
|
||||
from vllm.parser.minimax_m2 import MinimaxM2Parser
|
||||
from vllm.parser.nemotron_v3 import NemotronV3Parser
|
||||
from vllm.parser.qwen3 import Qwen3Parser
|
||||
|
||||
(
|
||||
MinimaxM2ParserReasoningAdapter,
|
||||
MinimaxM2ParserToolAdapter,
|
||||
) = make_adapters(MinimaxM2Parser)
|
||||
|
||||
(
|
||||
Gemma4ParserReasoningAdapter,
|
||||
Gemma4ParserToolAdapter,
|
||||
|
||||
Reference in New Issue
Block a user