Skip to content

Commit 02658c2

Browse files
authored
Add DeepSeek-R1-0528 function call chat template (vllm-project#18874)
Signed-off-by: 许文卿 <[email protected]>
1 parent 01dc9a7 commit 02658c2

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

docs/features/tool_calling.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,11 @@ Flags: `--tool-call-parser hermes`
238238
### DeepSeek-V3 Models (`deepseek_v3`)
239239

240240
Supported models:
241-
* `deepseek-ai/DeepSeek-V3-0324`
242241

243-
Flags: `--tool-call-parser deepseek_v3 --chat-template examples/tool_chat_template_deepseekv3.jinja`
242+
* `deepseek-ai/DeepSeek-V3-0324` (use with <gh-file:examples/tool_chat_template_deepseekv3.jinja>)
243+
* `deepseek-ai/DeepSeek-R1-0528` (use with <gh-file:examples/tool_chat_template_deepseekr1.jinja>)
244+
245+
Flags: `--tool-call-parser deepseek_v3 --chat-template {see_above}`
244246

245247
### Models with Pythonic Tool Calls (`pythonic`)
246248

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{% if not add_generation_prompt is defined %}
2+
{% set add_generation_prompt = false %}
3+
{% endif %}
4+
{% set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='', is_first_sp=true, is_last_user=false) %}
5+
{%- for message in messages %}
6+
{%- if message['role'] == 'system' %}
7+
{%- if ns.is_first_sp %}
8+
{% set ns.system_prompt = ns.system_prompt + message['content'] %}
9+
{% set ns.is_first_sp = false %}
10+
{%- else %}
11+
{% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}
12+
{%- endif %}
13+
{%- endif %}
14+
{%- endfor %}
15+
16+
{#- Adapted from https://github.com/sgl-project/sglang/blob/main/examples/chat_template/tool_chat_template_deepseekr1.jinja #}
17+
{% if tools is defined and tools is not none %}
18+
{% set tool_ns = namespace(text='You are a helpful assistant with tool calling capabilities. '
19+
'When a tool call is needed, you MUST use the following format to issue the call:\n'
20+
'<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>FUNCTION_NAME\n'
21+
'```json\n{"param1": "value1", "param2": "value2"}\n```<|tool▁call▁end|><|tool▁calls▁end|>\n\n'
22+
'Make sure the JSON is valid.'
23+
'## Tools\n\n### Function\n\nYou have the following functions available:\n\n') %}
24+
{% for tool in tools %}
25+
{% set tool_ns.text = tool_ns.text + '\n```json\n' + (tool | tojson) + '\n```\n' %}
26+
{% endfor %}
27+
{% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
28+
{% endif %}
29+
30+
{{ bos_token }}
31+
{{ ns.system_prompt }}
32+
{%- for message in messages %}
33+
{% set content = message['content'] %}
34+
{%- if message['role'] == 'user' %}
35+
{%- set ns.is_tool = false -%}
36+
{%- set ns.is_first = false -%}
37+
{%- set ns.is_last_user = true -%}
38+
{{'<|User|>' + content + '<|Assistant|>'}}
39+
{%- endif %}
40+
{%- if message['role'] == 'assistant' %}
41+
{% if '</think>' in content %}
42+
{% set content = content.split('</think>')[-1] %}
43+
{% endif %}
44+
{% endif %}
45+
{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
46+
{%- set ns.is_last_user = false -%}
47+
{%- if ns.is_tool %}
48+
{{'<|tool▁outputs▁end|>'}}
49+
{%- endif %}
50+
{%- set ns.is_first = false %}
51+
{%- set ns.is_tool = false -%}
52+
{%- set ns.is_output_first = true %}
53+
{%- for tool in message['tool_calls'] %}
54+
{%- if not ns.is_first %}
55+
{%- if content is none %}
56+
{{'<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments']|tojson + '\n' + '```' + '<|tool▁call▁end|>'}}
57+
{%- else %}
58+
{{content + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments']|tojson + '\n' + '```' + '<|tool▁call▁end|>'}}
59+
{%- endif %}
60+
{%- set ns.is_first = true -%}
61+
{%- else %}
62+
{{'\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments']|tojson + '\n' + '```' + '<|tool▁call▁end|>'}}
63+
{%- endif %}
64+
{%- endfor %}
65+
{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
66+
{%- endif %}
67+
{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none)%}
68+
{%- set ns.is_last_user = false -%}
69+
{%- if ns.is_tool %}
70+
{{'<|tool▁outputs▁end|>' + content + '<|end▁of▁sentence|>'}}
71+
{%- set ns.is_tool = false -%}
72+
{%- else %}
73+
{{content + '<|end▁of▁sentence|>'}}
74+
{%- endif %}
75+
{%- endif %}
76+
{%- if message['role'] == 'tool' %}
77+
{%- set ns.is_last_user = false -%}
78+
{%- set ns.is_tool = true -%}
79+
{%- if ns.is_output_first %}
80+
{{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
81+
{%- set ns.is_output_first = false %}
82+
{%- else %}
83+
{{'\n<|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
84+
{%- endif %}
85+
{%- endif %}
86+
{%- endfor -%}
87+
{% if ns.is_tool %}
88+
{{'<|tool▁outputs▁end|>'}}
89+
{% endif %}
90+
{% if add_generation_prompt and not ns.is_last_user and not ns.is_tool %}
91+
{{'<|Assistant|>'}}
92+
{% endif %}

0 commit comments

Comments
 (0)