[Model][Frontend] Support DeepSeek-V4.1-Flash in Rust and Python frontends (#56208)
Signed-off-by: Bugen Zhao <i@bugenzhao.com> Co-authored-by: Isotr0py <Isotr0py@outlook.com>
This commit is contained in:
@@ -70,11 +70,17 @@ _PARTIAL_PARAM_RE = re.compile(
|
||||
)
|
||||
|
||||
|
||||
def _dsml_arg_converter(raw_args: str, partial: bool) -> str:
|
||||
def _dsml_arg_converter(
|
||||
raw_args: str,
|
||||
partial: bool,
|
||||
*,
|
||||
param_re: re.Pattern = _PARAM_RE,
|
||||
partial_param_re: re.Pattern = _PARTIAL_PARAM_RE,
|
||||
) -> str:
|
||||
params: dict[str, object] = {}
|
||||
|
||||
last_end = 0
|
||||
for m in _PARAM_RE.finditer(raw_args):
|
||||
for m in param_re.finditer(raw_args):
|
||||
name, is_str, value = m.group(1), m.group(2), m.group(3)
|
||||
if is_str == "true":
|
||||
params[name] = value
|
||||
@@ -86,7 +92,7 @@ def _dsml_arg_converter(raw_args: str, partial: bool) -> str:
|
||||
last_end = m.end()
|
||||
|
||||
if partial:
|
||||
pm = _PARTIAL_PARAM_RE.search(raw_args, last_end)
|
||||
pm = partial_param_re.search(raw_args, last_end)
|
||||
if pm:
|
||||
name, is_str, value = pm.group(1), pm.group(2), pm.group(3)
|
||||
if is_str == "true":
|
||||
@@ -229,6 +235,8 @@ def deepseek_v4_config(thinking: bool = False) -> ParserEngineConfig:
|
||||
|
||||
|
||||
class DeepSeekV4Parser(ParserEngine):
|
||||
parser_config = staticmethod(deepseek_v4_config)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
tokenizer: TokenizerLike,
|
||||
@@ -245,13 +253,15 @@ class DeepSeekV4Parser(ParserEngine):
|
||||
super().__init__(
|
||||
tokenizer,
|
||||
tools,
|
||||
parser_engine_config=deepseek_v4_config(thinking=thinking),
|
||||
parser_engine_config=self.parser_config(thinking=thinking),
|
||||
**kwargs,
|
||||
)
|
||||
self._arg_converter = self._convert_args
|
||||
|
||||
def _convert_args(self, raw_args: str, partial: bool) -> str:
|
||||
result = _dsml_arg_converter(raw_args, partial)
|
||||
converter = self.parser_engine_config.arg_converter
|
||||
assert converter is not None
|
||||
result = converter(raw_args, partial)
|
||||
if not self._tools:
|
||||
return result
|
||||
func_name = next((s.name for s in self._tool_slots if s.args == raw_args), None)
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""DeepSeek V4.1 reasoning and spaced DSML tool calls."""
|
||||
|
||||
import functools
|
||||
from dataclasses import replace
|
||||
|
||||
import regex as re
|
||||
|
||||
from vllm.parser.deepseek_v4 import (
|
||||
DeepSeekV4Parser,
|
||||
_dsml_arg_converter,
|
||||
deepseek_v4_config,
|
||||
)
|
||||
from vllm.parser.engine.parser_engine_config import ParserEngineConfig
|
||||
|
||||
DSML_TOOL_START = "<|DSML| calls>"
|
||||
DSML_TOOL_END = "</|DSML| calls>"
|
||||
DSML_INVOKE_PREFIX = '<|DSML| invoke name="'
|
||||
DSML_INVOKE_END = "</|DSML| invoke>"
|
||||
DSML_PARAM_START = "<|DSML| parameter"
|
||||
DSML_PARAM_CLOSE = "</|DSML| parameter>"
|
||||
|
||||
_PARAM_RE = re.compile(
|
||||
r'<|DSML| parameter\s+name="([^"]+)"\s+string="(true|false)">'
|
||||
r"(.*?)"
|
||||
r"(?:</|DSML| parameter>|(?=<|DSML| parameter\s+name=))",
|
||||
re.DOTALL,
|
||||
)
|
||||
_PARTIAL_PARAM_RE = re.compile(
|
||||
r'<|DSML| parameter\s+name="([^"]+)"\s+string="(true|false)">'
|
||||
r"(.*)$",
|
||||
re.DOTALL,
|
||||
)
|
||||
|
||||
|
||||
@functools.cache
|
||||
def deepseek_v41_config(thinking: bool = False) -> ParserEngineConfig:
|
||||
config = deepseek_v4_config(thinking=thinking)
|
||||
terminal_overrides = {
|
||||
"TOOL_START": DSML_TOOL_START,
|
||||
"TOOL_END": DSML_TOOL_END,
|
||||
"INVOKE_PREFIX": DSML_INVOKE_PREFIX,
|
||||
"INVOKE_END": DSML_INVOKE_END,
|
||||
"PARAM_START": DSML_PARAM_START,
|
||||
"PARAM_CLOSE": DSML_PARAM_CLOSE,
|
||||
}
|
||||
return replace(
|
||||
config,
|
||||
name="deepseek_v41",
|
||||
terminals={**config.terminals, **terminal_overrides},
|
||||
token_id_terminals={
|
||||
key: terminal_overrides.get(key, value)
|
||||
for key, value in config.token_id_terminals.items()
|
||||
},
|
||||
arg_converter=functools.partial(
|
||||
_dsml_arg_converter,
|
||||
param_re=_PARAM_RE,
|
||||
partial_param_re=_PARTIAL_PARAM_RE,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class DeepSeekV41Parser(DeepSeekV4Parser):
|
||||
parser_config = staticmethod(deepseek_v41_config)
|
||||
@@ -9,6 +9,7 @@ names so that :class:`ReasoningParserManager` and
|
||||
|
||||
from vllm.parser.deepseek_v4 import DeepSeekV4Parser
|
||||
from vllm.parser.deepseek_v32 import DeepSeekV32Parser
|
||||
from vllm.parser.deepseek_v41 import DeepSeekV41Parser
|
||||
from vllm.parser.engine.adapters import make_adapters
|
||||
from vllm.parser.gemma4 import Gemma4Parser
|
||||
from vllm.parser.glm47_moe import Glm47MoeParser
|
||||
@@ -30,6 +31,11 @@ from vllm.parser.seed_oss import SeedOssParser
|
||||
DeepSeekV4ParserToolAdapter,
|
||||
) = make_adapters(DeepSeekV4Parser)
|
||||
|
||||
(
|
||||
DeepSeekV41ParserReasoningAdapter,
|
||||
DeepSeekV41ParserToolAdapter,
|
||||
) = make_adapters(DeepSeekV41Parser)
|
||||
|
||||
(
|
||||
MinimaxM2ParserReasoningAdapter,
|
||||
MinimaxM2ParserToolAdapter,
|
||||
|
||||
Reference in New Issue
Block a user