Skip to content

Commit 2515953

Browse files
sfbemerkBenjamin Merkel
andauthored
Fix DeepSeek-R1-0528 chat template (#20717)
Signed-off-by: Benjamin Merkel <benjamin.merkel@tngtech.com> Co-authored-by: Benjamin Merkel <benjamin.merkel@tngtech.com>
1 parent 4bed167 commit 2515953

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

examples/tool_chat_template_deepseekr1.jinja

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}
1212
{%- endif %}
1313
{%- endif %}
14-
{%- endfor %}
14+
{%- endfor -%}
1515

1616
{#- Adapted from https://github.com/sgl-project/sglang/blob/main/examples/chat_template/tool_chat_template_deepseekr1.jinja #}
1717
{% if tools is defined and tools is not none %}
@@ -27,8 +27,8 @@
2727
{% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
2828
{% endif %}
2929

30-
{{ bos_token }}
31-
{{ ns.system_prompt }}
30+
{{- bos_token }}
31+
{{- ns.system_prompt }}
3232
{%- for message in messages %}
3333
{% set content = message['content'] %}
3434
{%- if message['role'] == 'user' %}
@@ -45,48 +45,48 @@
4545
{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
4646
{%- set ns.is_last_user = false -%}
4747
{%- if ns.is_tool %}
48-
{{'<|tool▁outputs▁end|>'}}
48+
{{- '<|tool▁outputs▁end|>'}}
4949
{%- endif %}
5050
{%- set ns.is_first = false %}
5151
{%- set ns.is_tool = false -%}
5252
{%- set ns.is_output_first = true %}
5353
{%- for tool in message['tool_calls'] %}
5454
{%- if not ns.is_first %}
5555
{%- 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|>'}}
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|>'}}
5757
{%- 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|>'}}
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|>'}}
5959
{%- endif %}
6060
{%- set ns.is_first = true -%}
6161
{%- else %}
62-
{{'\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments']|tojson + '\n' + '```' + '<|tool▁call▁end|>'}}
62+
{{- '\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments']|tojson + '\n' + '```' + '<|tool▁call▁end|>'}}
6363
{%- endif %}
6464
{%- endfor %}
65-
{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
65+
{{- '<|tool▁calls▁end|><|end▁of▁sentence|>'}}
6666
{%- endif %}
6767
{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none)%}
6868
{%- set ns.is_last_user = false -%}
6969
{%- if ns.is_tool %}
70-
{{'<|tool▁outputs▁end|>' + content + '<|end▁of▁sentence|>'}}
70+
{{- '<|tool▁outputs▁end|>' + content + '<|end▁of▁sentence|>'}}
7171
{%- set ns.is_tool = false -%}
7272
{%- else %}
73-
{{content + '<|end▁of▁sentence|>'}}
73+
{{- content + '<|end▁of▁sentence|>'}}
7474
{%- endif %}
7575
{%- endif %}
7676
{%- if message['role'] == 'tool' %}
7777
{%- set ns.is_last_user = false -%}
7878
{%- set ns.is_tool = true -%}
7979
{%- if ns.is_output_first %}
80-
{{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
80+
{{- '<|tool▁outputs▁begin|><|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
8181
{%- set ns.is_output_first = false %}
8282
{%- else %}
83-
{{'\n<|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
83+
{{- '\n<|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
8484
{%- endif %}
8585
{%- endif %}
8686
{%- endfor -%}
8787
{% if ns.is_tool %}
88-
{{'<|tool▁outputs▁end|>'}}
89-
{% endif %}
88+
{{- '<|tool▁outputs▁end|>'}}
89+
{%- endif %}
9090
{% if add_generation_prompt and not ns.is_last_user and not ns.is_tool %}
91-
{{'<|Assistant|>'}}
92-
{% endif %}
91+
{{- '<|Assistant|>'}}
92+
{%- endif %}

0 commit comments

Comments
 (0)