Skip to content

Commit b8f17f5

Browse files
authored
Support DeepSeek-V3.1 tool call (#23454)
Signed-off-by: Xu Wenqing <[email protected]>
1 parent d9a5520 commit b8f17f5

File tree

4 files changed

+468
-0
lines changed

4 files changed

+468
-0
lines changed

docs/features/tool_calling.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@ Supported models:
284284

285285
Flags: `--tool-call-parser deepseek_v3 --chat-template {see_above}`
286286

287+
### DeepSeek-V3.1 Models (`deepseek_v31`)
288+
289+
Supported models:
290+
291+
* `deepseek-ai/DeepSeek-V3.1` (use with <gh-file:examples/tool_chat_template_deepseekv31.jinja>)
292+
293+
Flags: `--tool-call-parser deepseek_v31 --chat-template {see_above}`
294+
287295
### Kimi-K2 Models (`kimi_k2`)
288296

289297
Supported models:
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{% if not add_generation_prompt is defined %}
2+
{% set add_generation_prompt = false %}
3+
{% endif %}
4+
{% if not thinking is defined %}
5+
{% set thinking = false %}
6+
{% endif %}
7+
{% set ns = namespace(is_first=false, is_tool=false, system_prompt='', is_first_sp=true, is_last_user=false) %}
8+
{%- for message in messages %}
9+
{%- if message['role'] == 'system' %}
10+
{%- if ns.is_first_sp %}
11+
{% set ns.system_prompt = ns.system_prompt + message['content'] %}
12+
{% set ns.is_first_sp = false %}
13+
{%- else %}
14+
{% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}
15+
{%- endif %}
16+
{%- endif %}
17+
{%- endfor %}
18+
19+
{% if tools is defined and tools is not none %}
20+
{% set tool_ns = namespace(text='## Tools\nYou have access to the following tools:\n') %}
21+
{% for tool in tools %}
22+
{% set tool_ns.text = tool_ns.text + '\n### ' + tool.function.name + '\nDescription: ' + tool.function.description + '\n\nParameters: ' + (tool.function.parameters | tojson) + '\n' %}
23+
{% endfor %}
24+
{% set tool_ns.text = tool_ns.text + "\nIMPORTANT: ALWAYS adhere to this exact format for tool use:\n<|tool▁calls▁begin|><|tool▁call▁begin|>tool_call_name<|tool▁sep|>tool_call_arguments<|tool▁call▁end|>{{additional_tool_calls}}<|tool▁calls▁end|>\n\nWhere:\n\n- `tool_call_name` must be an exact match to one of the available tools\n- `tool_call_arguments` must be valid JSON that strictly follows the tool's Parameters Schema\n- For multiple tool calls, chain them directly without separators or spaces\n" %}
25+
{% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
26+
{% endif %}
27+
28+
{{ bos_token }}{{ ns.system_prompt }}
29+
{%- for message in messages %}
30+
{%- if message['role'] == 'user' %}
31+
{%- set ns.is_tool = false -%}
32+
{%- set ns.is_first = false -%}
33+
{%- set ns.is_last_user = true -%}
34+
{{'<|User|>' + message['content']}}
35+
{%- endif %}
36+
{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
37+
{%- if ns.is_last_user %}
38+
{{'<|Assistant|></think>'}}
39+
{%- endif %}
40+
{%- set ns.is_last_user = false -%}
41+
{%- set ns.is_first = false %}
42+
{%- set ns.is_tool = false -%}
43+
{%- for tool in message['tool_calls'] %}
44+
{%- if not ns.is_first %}
45+
{%- if message['content'] is none %}
46+
{{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
47+
{%- else %}
48+
{{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
49+
{%- endif %}
50+
{%- set ns.is_first = true -%}
51+
{%- else %}
52+
{{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
53+
{%- endif %}
54+
{%- endfor %}
55+
{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
56+
{%- endif %}
57+
{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}
58+
{%- if ns.is_last_user %}
59+
{{'<|Assistant|>'}}
60+
{%- if message['prefix'] is defined and message['prefix'] and thinking %}
61+
{{'<think>'}}
62+
{%- else %}
63+
{{'</think>'}}
64+
{%- endif %}
65+
{%- endif %}
66+
{%- set ns.is_last_user = false -%}
67+
{%- if ns.is_tool %}
68+
{{message['content'] + '<|end▁of▁sentence|>'}}
69+
{%- set ns.is_tool = false -%}
70+
{%- else %}
71+
{%- set content = message['content'] -%}
72+
{%- if '</think>' in content %}
73+
{%- set content = content.split('</think>', 1)[1] -%}
74+
{%- endif %}
75+
{{content + '<|end▁of▁sentence|>'}}
76+
{%- endif %}
77+
{%- endif %}
78+
{%- if message['role'] == 'tool' %}
79+
{%- set ns.is_last_user = false -%}
80+
{%- set ns.is_tool = true -%}
81+
{{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
82+
{%- endif %}
83+
{%- endfor -%}
84+
{%- if add_generation_prompt and ns.is_last_user and not ns.is_tool %}
85+
{{'<|Assistant|>'}}
86+
{%- if not thinking %}
87+
{{'</think>'}}
88+
{%- else %}
89+
{{'<think>'}}
90+
{%- endif %}
91+
{% endif %}

vllm/entrypoints/openai/tool_parsers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from .abstract_tool_parser import ToolParser, ToolParserManager
55
from .deepseekv3_tool_parser import DeepSeekV3ToolParser
6+
from .deepseekv31_tool_parser import DeepSeekV31ToolParser
67
from .glm4_moe_tool_parser import Glm4MoeModelToolParser
78
from .granite_20b_fc_tool_parser import Granite20bFCToolParser
89
from .granite_tool_parser import GraniteToolParser
@@ -36,6 +37,7 @@
3637
"PythonicToolParser",
3738
"Phi4MiniJsonToolParser",
3839
"DeepSeekV3ToolParser",
40+
"DeepSeekV31ToolParser",
3941
"xLAMToolParser",
4042
"MinimaxToolParser",
4143
"KimiK2ToolParser",

0 commit comments

Comments
 (0)