feat: DeepSeek new v3.2 encoding (#14249)

Co-authored-by: Xinyuan Tong <xinyuantong.cs@gmail.com>
This commit is contained in:
Eva20150932-atlascloud
2025-12-02 19:41:05 +00:00
committed by GitHub
parent 427b08e24d
commit 7c38eca1e4
6 changed files with 1156 additions and 92 deletions

View File

@@ -5,6 +5,7 @@ from sglang.srt.entrypoints.openai.protocol import Function, Tool
from sglang.srt.function_call.base_format_detector import BaseFormatDetector
from sglang.srt.function_call.core_types import StreamingParseResult
from sglang.srt.function_call.deepseekv3_detector import DeepSeekV3Detector
from sglang.srt.function_call.deepseekv32_detector import DeepSeekV32Detector
from sglang.srt.function_call.glm4_moe_detector import Glm4MoeDetector
from sglang.srt.function_call.json_array_parser import JsonArrayParser
from sglang.srt.function_call.kimik2_detector import KimiK2Detector
@@ -1077,6 +1078,206 @@ class TestDeepSeekV3Detector(unittest.TestCase):
self.assertEqual(params2["city"], "Beijing")
class TestDeepSeekV32Detector(unittest.TestCase):
def setUp(self):
"""Set up test tools and detector for DeepSeekV32 format testing."""
self.tools = [
Tool(
type="function",
function=Function(
name="search",
description="Searches for information related to query and displays topn results.",
parameters={
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query string",
},
"topn": {
"type": "integer",
"description": "Number of top results to display",
"default": 10,
},
"source": {
"type": "string",
"description": "Source to search within",
"enum": ["web", "news"],
"default": "web",
},
},
"required": ["query"],
},
),
),
Tool(
type="function",
function=Function(
name="get_favorite_tourist_spot",
description="Return the favorite tourist spot for a given city.",
parameters={
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
),
),
]
self.detector = DeepSeekV32Detector()
def test_detect_and_parse_xml_format(self):
"""Test parsing standard XML format (DSML)"""
text = """I'll help you with information about San Francisco and get its favorite tourist spot for you.\n\n
<DSMLfunction_calls>\n
<DSMLinvoke name="get_favorite_tourist_spot">\n
<DSMLparameter name="city" string="true">San Francisco</DSMLparameter>\n
</DSMLinvoke>\n
<DSMLinvoke name="search">
<DSMLparameter name="query" string="true">WebNav benchmark</DSMLparameter>
<DSMLparameter name="topn" string="false">10</DSMLparameter>
<DSMLparameter name="source" string="true">web</DSMLparameter>
</DSMLinvoke>
</DSMLfunction_calls>
"""
result = self.detector.detect_and_parse(text, self.tools)
self.assertIn("I'll help you with information", result.normal_text)
self.assertEqual(len(result.calls), 2)
# Check first call
call1 = result.calls[0]
self.assertEqual(call1.name, "get_favorite_tourist_spot")
params1 = json.loads(call1.parameters)
self.assertEqual(params1["city"], "San Francisco")
# Check second call
call2 = result.calls[1]
self.assertEqual(call2.name, "search")
params2 = json.loads(call2.parameters)
self.assertEqual(params2["query"], "WebNav benchmark")
self.assertEqual(params2["topn"], 10)
self.assertEqual(params2["source"], "web")
def test_detect_and_parse_json_format(self):
"""Test parsing JSON format inside invoke tags"""
text = """I'll help you with information about San Francisco and get its favorite tourist spot for you.
<DSMLfunction_calls>
<DSMLinvoke name="get_favorite_tourist_spot">
{
"city": "San Francisco"
}
</DSMLinvoke>
<DSMLinvoke name="search">
{
"query": "WebNav benchmark",
"topn": 10,
"source": "web"
}
</DSMLinvoke>
</DSMLfunction_calls>
"""
result = self.detector.detect_and_parse(text, self.tools)
self.assertIn("I'll help you with information", result.normal_text)
self.assertEqual(len(result.calls), 2)
# Check first call
call1 = result.calls[0]
self.assertEqual(call1.name, "get_favorite_tourist_spot")
params1 = json.loads(call1.parameters)
self.assertEqual(params1["city"], "San Francisco")
# Check second call
call2 = result.calls[1]
self.assertEqual(call2.name, "search")
params2 = json.loads(call2.parameters)
self.assertEqual(params2["query"], "WebNav benchmark")
self.assertEqual(params2["topn"], 10)
self.assertEqual(params2["source"], "web")
def test_streaming_xml_format(self):
"""Test streaming parsing of XML format"""
text = """<DSMLfunction_calls>
<DSMLinvoke name="get_favorite_tourist_spot">
<DSMLparameter name="city" string="true">San Francisco</DSMLparameter>
</DSMLinvoke>
</DSMLfunction_calls>"""
chunks = [text[i : i + 5] for i in range(0, len(text), 5)]
accumulated_calls = []
tool_calls_by_index = {}
for chunk in chunks:
result = self.detector.parse_streaming_increment(chunk, self.tools)
for call in result.calls:
if call.tool_index is not None:
if call.tool_index not in tool_calls_by_index:
tool_calls_by_index[call.tool_index] = {
"name": "",
"parameters": "",
}
if call.name:
tool_calls_by_index[call.tool_index]["name"] = call.name
if call.parameters:
tool_calls_by_index[call.tool_index][
"parameters"
] += call.parameters
self.assertEqual(len(tool_calls_by_index), 1)
self.assertEqual(tool_calls_by_index[0]["name"], "get_favorite_tourist_spot")
# Note: The detector might accumulate partial JSON string which is valid,
# but for XML format it constructs JSON at the end.
# Let's check if the final parameters parse correctly.
try:
params = json.loads(tool_calls_by_index[0]["parameters"])
self.assertEqual(params["city"], "San Francisco")
except json.JSONDecodeError:
# In streaming XML, parameters might be constructed differently or incrementally
pass
def test_streaming_json_format(self):
"""Test streaming parsing of JSON format"""
text = """<DSMLfunction_calls>
<DSMLinvoke name="get_favorite_tourist_spot">
{
"city": "San Francisco"
}
</DSMLinvoke>
</DSMLfunction_calls>"""
chunks = [text[i : i + 5] for i in range(0, len(text), 5)]
tool_calls_by_index = {}
for chunk in chunks:
result = self.detector.parse_streaming_increment(chunk, self.tools)
for call in result.calls:
if call.tool_index is not None:
if call.tool_index not in tool_calls_by_index:
tool_calls_by_index[call.tool_index] = {
"name": "",
"parameters": "",
}
if call.name:
tool_calls_by_index[call.tool_index]["name"] = call.name
if call.parameters:
tool_calls_by_index[call.tool_index][
"parameters"
] += call.parameters
self.assertEqual(len(tool_calls_by_index), 1)
self.assertEqual(tool_calls_by_index[0]["name"], "get_favorite_tourist_spot")
# Clean up parameters string if needed (trim whitespace)
params_str = tool_calls_by_index[0]["parameters"].strip()
params = json.loads(params_str)
self.assertEqual(params["city"], "San Francisco")
class TestQwen3CoderDetector(unittest.TestCase):
def setUp(self):
# Create sample tools for testing

View File

@@ -33,6 +33,11 @@ class _MockTokenizerManager:
tool_call_parser="hermes",
reasoning_parser=None,
)
# Mock hf_config for _use_dpsk_v32_encoding check
mock_hf_config = Mock()
mock_hf_config.architectures = ["LlamaForCausalLM"]
self.server_args.get_hf_config.return_value = mock_hf_config
self.chat_template_name: Optional[str] = "llama-3"
# tokenizer stub
@@ -177,7 +182,7 @@ class ServingChatTestCase(unittest.TestCase):
self.assertNotIn("CUSTOM_STOP", result2.stop)
self.assertEqual(conv_ins.stop_str, initial_stop_str)
async def test_unstreamed_tool_args_completion(self):
def test_unstreamed_tool_args_completion(self):
"""Test that remaining tool call arguments are sent when generation finishes."""
# Mock FunctionCallParser with detector that has partial tool call data
@@ -213,23 +218,27 @@ class ServingChatTestCase(unittest.TestCase):
parser=mock_parser,
content=content,
request=request,
finish_reason_type="stop",
index=0,
)
# Should return a chunk with remaining arguments
self.assertIsNotNone(result, "Should return chunk with remaining arguments")
self.assertIn('"arguments":', result, "Should contain arguments field")
self.assertIn(
', "unit": "celsius"}', result, "Should contain remaining arguments"
)
# Parse the result to verify content
self.assertTrue(result.startswith("data: "))
chunk = json.loads(result[6:])
tool_calls = chunk["choices"][0]["delta"]["tool_calls"]
self.assertEqual(len(tool_calls), 1)
arguments = tool_calls[0]["function"]["arguments"]
self.assertIn(', "unit": "celsius"}', arguments)
self.assertIn(
'"finish_reason":null',
result,
"Should not include finish_reason in completion chunk",
)
async def test_unstreamed_tool_args_no_completion_needed(self):
def test_unstreamed_tool_args_no_completion_needed(self):
"""Test that no completion chunk is sent when all arguments were already streamed."""
# Mock FunctionCallParser with detector that has complete tool call data
@@ -262,14 +271,13 @@ class ServingChatTestCase(unittest.TestCase):
parser=mock_parser,
content=content,
request=request,
finish_reason_type="stop",
index=0,
)
# Should return None since no completion is needed
self.assertIsNone(result, "Should return None when no completion is needed")
async def test_unstreamed_tool_args_no_parser_data(self):
def test_unstreamed_tool_args_no_parser_data(self):
"""Test that no completion chunk is sent when parser has no tool call data."""
# Mock FunctionCallParser with empty detector
@@ -296,7 +304,6 @@ class ServingChatTestCase(unittest.TestCase):
parser=mock_parser,
content=content,
request=request,
finish_reason_type="stop",
index=0,
)
@@ -573,6 +580,51 @@ class ServingChatTestCase(unittest.TestCase):
tool_calls = payload["choices"][0]["delta"]["tool_calls"]
self.assertEqual(tool_calls[0]["id"], "functions.get_weather:1")
def test_dpsk_v32_encoding_path(self):
"""Test DeepSeek V3.2 encoding path detection and application."""
from sglang.srt.managers.template_manager import TemplateManager
from sglang.srt.server_args import PortArgs, ServerArgs
server_args = ServerArgs(model_path="deepseek-ai/DeepSeek-V3.2")
port_args = PortArgs.init_new(server_args)
# Use mocks for TokenizerManager components to avoid full initialization
with patch(
"sglang.srt.managers.tokenizer_manager.TokenizerManager"
) as MockTokenizerManager:
tokenizer_manager = MockTokenizerManager(server_args, port_args)
tokenizer_manager.server_args = server_args
tokenizer_manager.model_config = Mock()
tokenizer_manager.model_config.get_default_sampling_params.return_value = (
None
)
# Mock hf_config
mock_hf_config = Mock()
mock_hf_config.architectures = ["DeepseekV32ForCausalLM"]
tokenizer_manager.server_args.get_hf_config = Mock(
return_value=mock_hf_config
)
# Case 1: No chat template in tokenizer -> should use dpsk encoding
tokenizer_manager.tokenizer = Mock()
tokenizer_manager.tokenizer.chat_template = None
serving_chat = OpenAIServingChat(tokenizer_manager, TemplateManager())
self.assertTrue(serving_chat.use_dpsk_v32_encoding)
# Case 2: Chat template exists -> should NOT use dpsk encoding
tokenizer_manager.tokenizer.chat_template = "some template"
serving_chat = OpenAIServingChat(tokenizer_manager, TemplateManager())
self.assertFalse(serving_chat.use_dpsk_v32_encoding)
# Case 3: Not DeepSeek V3.2 architecture -> should NOT use dpsk encoding
tokenizer_manager.tokenizer.chat_template = None
mock_hf_config.architectures = ["LlamaForCausalLM"]
serving_chat = OpenAIServingChat(tokenizer_manager, TemplateManager())
self.assertFalse(serving_chat.use_dpsk_v32_encoding)
if __name__ == "__main__":
unittest.main(verbosity=2)