Rocketknight1 HF Staff commited on
Commit
9267142
·
verified ·
1 Parent(s): 314113f

Sync the chat template with the 16 October encoding.py, make it load in llama.cpp and transformers.js, and add a response template

Browse files

Updates the chat template to follow the 16 October `encoding.py` (as #74 does) and to load in llama.cpp and transformers.js, and adds a response template. OpenAI-style `reasoning_effort` values are now accepted (`medium` maps to 75 rather than 62). Supersedes #74.

Files changed (2) hide show
  1. chat_template.jinja +59 -22
  2. tokenizer_config.json +43 -1
chat_template.jinja CHANGED
@@ -1,12 +1,20 @@
1
  {#- DeepSeek-V4.1 chat template, ported from encoding/encoding.py in the model repo.
2
- Extra kwargs: thinking_mode ("thinking" | "chat", default "thinking"; enable_thinking=true/false is accepted
3
- as an alias), drop_thinking (default true), reasoning_effort (int in [1, 100] or "low"/"medium"/"high"/"max",
4
- default "high"). Assistant reasoning is read from reasoning_content, or from thinking as an alias. -#}
 
 
5
  {%- set IMG = "<|deepseek_image|>" -%}
6
  {%- set DSML = "|DSML|" -%}
7
 
8
  {%- if thinking_mode is not defined -%}
9
- {%- set thinking_mode = "chat" if (enable_thinking is defined and enable_thinking is not none and not enable_thinking) else "thinking" -%}
 
 
 
 
 
 
10
  {%- endif -%}
11
  {%- if thinking_mode not in ["chat", "thinking"] -%}
12
  {{- raise_exception("Invalid thinking_mode `" ~ thinking_mode ~ "`, should be \"chat\" or \"thinking\"") -}}
@@ -16,13 +24,13 @@
16
  {%- if tools is not defined -%}{%- set tools = none -%}{%- endif -%}
17
 
18
  {%- if reasoning_effort is not defined or reasoning_effort is none -%}{%- set reasoning_effort = "high" -%}{%- endif -%}
19
- {%- set effort_map = {"low": 50, "medium": 62, "high": 75, "max": 100} -%}
20
  {%- if reasoning_effort is string and reasoning_effort in effort_map -%}
21
  {%- set budget = effort_map[reasoning_effort] -%}
22
- {%- elif reasoning_effort is integer and 1 <= reasoning_effort <= 100 -%}
23
  {%- set budget = reasoning_effort -%}
24
  {%- else -%}
25
- {{- raise_exception("Invalid reasoning effort: " ~ reasoning_effort ~ ", should be int within [1,100] or one of \"low\", \"medium\", \"high\", \"max\"") -}}
26
  {%- endif -%}
27
 
28
  {#- Text of text/image blocks joined by blank lines; other block types are dropped. -#}
@@ -58,13 +66,25 @@
58
  {%- for tool in tool_list -%}
59
  {%- set fn = tool.function if tool.function is defined else tool -%}
60
  {%- set ns = tool.namespace if tool.namespace is defined and tool.namespace is not none else fn.get("namespace") -%}
61
- {%- set schema = namespace(d={}) -%}
62
- {%- for k, v in fn.items() if k != "namespace" -%}{%- set schema.d = dict(schema.d, **{k: v}) -%}{%- endfor -%}
63
- {%- set schema.d = dict(schema.d, name=qualified_name(fn.name, ns)) -%}
64
- {%- if ns is mapping and ns.description -%}
65
- {%- set schema.d = dict(schema.d, description=ns.description ~ "\n" ~ (fn.description or "")) -%}
 
 
 
 
 
 
 
 
 
 
 
 
66
  {%- endif -%}
67
- {%- set ts.schemas = ts.schemas + [schema.d | tojson] -%}
68
  {%- endfor -%}
69
  {{- "## Tools\n\nYou have access to a set of tools to help answer the user's question. You can invoke tools by writing a \"<|DSML| calls>\" block like the following:\n\n<|DSML| calls>\n<|DSML| invoke name=\"$TOOL_NAME\">\n<|DSML| parameter name=\"$PARAMETER_NAME\" string=\"true|false\">$PARAMETER_VALUE</|DSML| parameter>\n...\n</|DSML| invoke>\n<|DSML| invoke name=\"$TOOL_NAME2\">\n...\n</|DSML| invoke>\n</|DSML| calls>\n\nString parameters should be specified as is and set `string=\"true\"`. For all other types (numbers, booleans, arrays, objects), pass the value in JSON format and set `string=\"false\"`.\n\nIf thinking_mode is enabled (triggered by <think>), you MUST output your complete reasoning inside <think>...</think> BEFORE any tool calls or final response.\n\nOtherwise, output directly after </think> with tool calls or final response.\n\n### Available Tool Schemas\n\n" -}}
70
  {{- ts.schemas | join("\n") -}}
@@ -72,9 +92,16 @@
72
  {%- endmacro -%}
73
 
74
  {#- 1. Merge tool results (and consecutive user turns) into user messages made of content blocks. -#}
 
 
75
  {%- set mns = namespace(list=[]) -%}
76
  {%- for msg in messages -%}
77
  {%- set content = msg.get("content") -%}
 
 
 
 
 
78
  {%- set is_blocks = content is iterable and content is not string and content is not mapping -%}
79
  {%- if content is string and IMG in content -%}
80
  {{- raise_exception("Message content contains image special token " ~ IMG ~ ". Images should be provided as image content blocks.") -}}
@@ -85,18 +112,18 @@
85
  {%- endif -%}
86
  {%- set prev = mns.list[-1] if mns.list else none -%}
87
  {%- if msg.role == "tool" -%}
88
- {%- set tblock = {"type": "tool_result", "tool_use_id": msg.get("tool_call_id", ""), "content": joined_text(content) if is_blocks else msg.get("content", "")} -%}
89
  {%- if prev is not none and prev.role == "user" -%}
90
- {%- set mns.list = mns.list[:-1] + [dict(prev, content_blocks=prev.content_blocks + [tblock])] -%}
91
  {%- else -%}
92
  {%- set mns.list = mns.list + [{"role": "user", "content_blocks": [tblock]}] -%}
93
  {%- endif -%}
94
  {%- elif msg.role == "user" -%}
95
  {%- set blocks = content if is_blocks else [{"type": "text", "text": content or ""}] -%}
96
  {%- if prev is not none and prev.role == "user" and prev.get("task") is none -%}
97
- {%- set mns.list = mns.list[:-1] + [dict(prev, content_blocks=prev.content_blocks + blocks)] -%}
98
  {%- else -%}
99
- {%- set mns.list = mns.list + [dict(msg, content_blocks=blocks)] -%}
100
  {%- endif -%}
101
  {%- else -%}
102
  {%- set mns.list = mns.list + [msg] -%}
@@ -106,7 +133,7 @@
106
  {#- Top-level tools attach to a leading system message, or to a new empty one. -#}
107
  {%- if tools -%}
108
  {%- if mns.list and mns.list[0].role == "system" -%}
109
- {%- if not mns.list[0].get("tools") -%}{%- set mns.list = [dict(mns.list[0], tools=tools)] + mns.list[1:] -%}{%- endif -%}
110
  {%- else -%}
111
  {%- set mns.list = [{"role": "system", "content": "", "tools": tools}] + mns.list -%}
112
  {%- endif -%}
@@ -128,7 +155,7 @@
128
  {%- for tc_id in sns.ids -%}
129
  {%- if tc_id and tc_id == b.get("tool_use_id", "") -%}{%- set order.idx = loop.index0 -%}{%- endif -%}
130
  {%- endfor -%}
131
- {%- set tb.blocks = tb.blocks + [dict(b, _order=order.idx)] -%}
132
  {%- endfor -%}
133
  {%- if tb.blocks | length > 1 -%}
134
  {%- set sorted_blocks = tb.blocks | sort(attribute="_order") -%}
@@ -141,7 +168,7 @@
141
  {%- set rb.blocks = rb.blocks + [b] -%}
142
  {%- endif -%}
143
  {%- endfor -%}
144
- {%- set sns.list = sns.list + [dict(m, content_blocks=rb.blocks)] -%}
145
  {%- else -%}
146
  {%- set sns.list = sns.list + [m] -%}
147
  {%- endif -%}
@@ -151,10 +178,19 @@
151
  {%- endfor -%}
152
  {%- set msgs = sns.list -%}
153
 
154
- {#- Mid-conversation system messages count as user turns. Tools anywhere disable drop_thinking. -#}
 
155
  {%- set state = namespace(last_user=-1, has_tools=false) -%}
156
  {%- for m in msgs -%}
157
- {%- if m.role == "user" or (m.role == "system" and loop.index0 > 0) -%}{%- set state.last_user = loop.index0 -%}{%- endif -%}
 
 
 
 
 
 
 
 
158
  {%- if m.get("tools") -%}{%- set state.has_tools = true -%}{%- endif -%}
159
  {%- endfor -%}
160
  {%- set effective_drop = drop_thinking and not state.has_tools -%}
@@ -222,6 +258,7 @@
222
  {%- for tc in m.tool_calls -%}
223
  {%- set fn = tc.function -%}
224
  {%- set params = namespace(lines=[]) -%}
 
225
  {%- set args = fn.arguments if fn.arguments is mapping else {"arguments": fn.arguments} -%}
226
  {%- for k, v in args.items() -%}
227
  {%- set params.lines = params.lines + ["<" ~ DSML ~ " parameter name=\"" ~ k ~ "\" string=\"" ~ ("true" if v is string else "false") ~ "\">" ~ (v if v is string else v | tojson) ~ "</" ~ DSML ~ " parameter>"] -%}
 
1
  {#- DeepSeek-V4.1 chat template, ported from encoding/encoding.py in the model repo.
2
+ Extra kwargs: thinking_mode ("thinking" | "chat", default "thinking"; thinking=true/false and enable_thinking=true/false
3
+ are accepted as aliases), drop_thinking (default true), reasoning_effort (int in [1, 100] or "low"/"high"/"max",
4
+ default "high"; the OpenAI values "none"/"minimal"/"medium"/"xhigh" map like DeepSeek's Chat Completions adapter, and
5
+ "none" turns thinking off unless it is set explicitly). Assistant reasoning is read from reasoning_content, or from
6
+ thinking as an alias. Tool call arguments must be dicts. -#}
7
  {%- set IMG = "<|deepseek_image|>" -%}
8
  {%- set DSML = "|DSML|" -%}
9
 
10
  {%- if thinking_mode is not defined -%}
11
+ {%- if thinking is defined and thinking is not none -%}
12
+ {%- set thinking_mode = "thinking" if thinking else "chat" -%}
13
+ {%- elif enable_thinking is defined and enable_thinking is not none -%}
14
+ {%- set thinking_mode = "thinking" if enable_thinking else "chat" -%}
15
+ {%- else -%}
16
+ {%- set thinking_mode = "chat" if (reasoning_effort is defined and reasoning_effort == "none") else "thinking" -%}
17
+ {%- endif -%}
18
  {%- endif -%}
19
  {%- if thinking_mode not in ["chat", "thinking"] -%}
20
  {{- raise_exception("Invalid thinking_mode `" ~ thinking_mode ~ "`, should be \"chat\" or \"thinking\"") -}}
 
24
  {%- if tools is not defined -%}{%- set tools = none -%}{%- endif -%}
25
 
26
  {%- if reasoning_effort is not defined or reasoning_effort is none -%}{%- set reasoning_effort = "high" -%}{%- endif -%}
27
+ {%- set effort_map = {"none": 75, "minimal": 50, "low": 50, "medium": 75, "high": 75, "xhigh": 75, "max": 100} -%}
28
  {%- if reasoning_effort is string and reasoning_effort in effort_map -%}
29
  {%- set budget = effort_map[reasoning_effort] -%}
30
+ {%- elif reasoning_effort is integer and 1 <= reasoning_effort and reasoning_effort <= 100 -%}
31
  {%- set budget = reasoning_effort -%}
32
  {%- else -%}
33
+ {{- raise_exception("Invalid reasoning effort: " ~ reasoning_effort ~ ", should be int within [1,100] or one of \"low\", \"high\", \"max\"") -}}
34
  {%- endif -%}
35
 
36
  {#- Text of text/image blocks joined by blank lines; other block types are dropped. -#}
 
66
  {%- for tool in tool_list -%}
67
  {%- set fn = tool.function if tool.function is defined else tool -%}
68
  {%- set ns = tool.namespace if tool.namespace is defined and tool.namespace is not none else fn.get("namespace") -%}
69
+ {%- set qname = qualified_name(fn.name, ns) -%}
70
+ {%- set ns_description = ns.description ~ "\n" ~ (fn.description or "") if ns is mapping and ns.description else none -%}
71
+ {#- JSON built field by field (same separators as json.dumps) instead of with the dict builtin, which llama.cpp lacks -#}
72
+ {%- set schema = namespace(fields=[], has_name=false, has_description=false) -%}
73
+ {%- for k, v in fn.items() if k != "namespace" -%}
74
+ {%- if k == "name" -%}
75
+ {%- set value = qname -%}{%- set schema.has_name = true -%}
76
+ {%- elif k == "description" and ns_description is not none -%}
77
+ {%- set value = ns_description -%}{%- set schema.has_description = true -%}
78
+ {%- else -%}
79
+ {%- set value = v -%}
80
+ {%- endif -%}
81
+ {%- set schema.fields = schema.fields + [(k | tojson) ~ ": " ~ (value | tojson)] -%}
82
+ {%- endfor -%}
83
+ {%- if not schema.has_name -%}{%- set schema.fields = schema.fields + ['"name": ' ~ (qname | tojson)] -%}{%- endif -%}
84
+ {%- if ns_description is not none and not schema.has_description -%}
85
+ {%- set schema.fields = schema.fields + ['"description": ' ~ (ns_description | tojson)] -%}
86
  {%- endif -%}
87
+ {%- set ts.schemas = ts.schemas + ["{" ~ (schema.fields | join(", ")) ~ "}"] -%}
88
  {%- endfor -%}
89
  {{- "## Tools\n\nYou have access to a set of tools to help answer the user's question. You can invoke tools by writing a \"<|DSML| calls>\" block like the following:\n\n<|DSML| calls>\n<|DSML| invoke name=\"$TOOL_NAME\">\n<|DSML| parameter name=\"$PARAMETER_NAME\" string=\"true|false\">$PARAMETER_VALUE</|DSML| parameter>\n...\n</|DSML| invoke>\n<|DSML| invoke name=\"$TOOL_NAME2\">\n...\n</|DSML| invoke>\n</|DSML| calls>\n\nString parameters should be specified as is and set `string=\"true\"`. For all other types (numbers, booleans, arrays, objects), pass the value in JSON format and set `string=\"false\"`.\n\nIf thinking_mode is enabled (triggered by <think>), you MUST output your complete reasoning inside <think>...</think> BEFORE any tool calls or final response.\n\nOtherwise, output directly after </think> with tool calls or final response.\n\n### Available Tool Schemas\n\n" -}}
90
  {{- ts.schemas | join("\n") -}}
 
92
  {%- endmacro -%}
93
 
94
  {#- 1. Merge tool results (and consecutive user turns) into user messages made of content blocks. -#}
95
+ {#- Copies of user/system messages list the fields read below explicitly (rather than copying them with the dict builtin, which llama.cpp
96
+ lacks); absent fields become none, which every later .get() treats the same as missing. -#}
97
  {%- set mns = namespace(list=[]) -%}
98
  {%- for msg in messages -%}
99
  {%- set content = msg.get("content") -%}
100
+ {%- if msg.role == "user" and msg.get("content_blocks") is not none -%}
101
+ {%- set content = msg.content_blocks -%}
102
+ {%- elif msg.role == "tool" and msg.get("content_blocks") and content is not string -%}
103
+ {%- set content = msg.content_blocks -%}
104
+ {%- endif -%}
105
  {%- set is_blocks = content is iterable and content is not string and content is not mapping -%}
106
  {%- if content is string and IMG in content -%}
107
  {{- raise_exception("Message content contains image special token " ~ IMG ~ ". Images should be provided as image content blocks.") -}}
 
112
  {%- endif -%}
113
  {%- set prev = mns.list[-1] if mns.list else none -%}
114
  {%- if msg.role == "tool" -%}
115
+ {%- set tblock = {"type": "tool_result", "tool_use_id": msg.get("tool_call_id", ""), "content": joined_text(content) if is_blocks else (content or "")} -%}
116
  {%- if prev is not none and prev.role == "user" -%}
117
+ {%- set mns.list = mns.list[:-1] + [{"role": prev.role, "content": prev.get("content"), "content_blocks": prev.content_blocks + [tblock], "task": prev.get("task"), "tools": prev.get("tools"), "response_format": prev.get("response_format"), "wo_eos": prev.get("wo_eos")}] -%}
118
  {%- else -%}
119
  {%- set mns.list = mns.list + [{"role": "user", "content_blocks": [tblock]}] -%}
120
  {%- endif -%}
121
  {%- elif msg.role == "user" -%}
122
  {%- set blocks = content if is_blocks else [{"type": "text", "text": content or ""}] -%}
123
  {%- if prev is not none and prev.role == "user" and prev.get("task") is none -%}
124
+ {%- set mns.list = mns.list[:-1] + [{"role": prev.role, "content": prev.get("content"), "content_blocks": prev.content_blocks + blocks, "task": prev.get("task"), "tools": prev.get("tools"), "response_format": prev.get("response_format"), "wo_eos": prev.get("wo_eos")}] -%}
125
  {%- else -%}
126
+ {%- set mns.list = mns.list + [{"role": msg.role, "content": msg.get("content"), "content_blocks": blocks, "task": msg.get("task"), "tools": msg.get("tools"), "response_format": msg.get("response_format"), "wo_eos": msg.get("wo_eos")}] -%}
127
  {%- endif -%}
128
  {%- else -%}
129
  {%- set mns.list = mns.list + [msg] -%}
 
133
  {#- Top-level tools attach to a leading system message, or to a new empty one. -#}
134
  {%- if tools -%}
135
  {%- if mns.list and mns.list[0].role == "system" -%}
136
+ {%- if not mns.list[0].get("tools") -%}{%- set mns.list = [{"role": mns.list[0].role, "content": mns.list[0].get("content"), "content_blocks": mns.list[0].get("content_blocks"), "task": mns.list[0].get("task"), "tools": tools, "response_format": mns.list[0].get("response_format"), "wo_eos": mns.list[0].get("wo_eos")}] + mns.list[1:] -%}{%- endif -%}
137
  {%- else -%}
138
  {%- set mns.list = [{"role": "system", "content": "", "tools": tools}] + mns.list -%}
139
  {%- endif -%}
 
155
  {%- for tc_id in sns.ids -%}
156
  {%- if tc_id and tc_id == b.get("tool_use_id", "") -%}{%- set order.idx = loop.index0 -%}{%- endif -%}
157
  {%- endfor -%}
158
+ {%- set tb.blocks = tb.blocks + [{"type": b.type, "tool_use_id": b.get("tool_use_id", ""), "content": b.content, "_order": order.idx}] -%}
159
  {%- endfor -%}
160
  {%- if tb.blocks | length > 1 -%}
161
  {%- set sorted_blocks = tb.blocks | sort(attribute="_order") -%}
 
168
  {%- set rb.blocks = rb.blocks + [b] -%}
169
  {%- endif -%}
170
  {%- endfor -%}
171
+ {%- set sns.list = sns.list + [{"role": m.role, "content": m.get("content"), "content_blocks": rb.blocks, "task": m.get("task"), "tools": m.get("tools"), "response_format": m.get("response_format"), "wo_eos": m.get("wo_eos")}] -%}
172
  {%- else -%}
173
  {%- set sns.list = sns.list + [m] -%}
174
  {%- endif -%}
 
178
  {%- endfor -%}
179
  {%- set msgs = sns.list -%}
180
 
181
+ {#- Mid-conversation system messages count as user turns; user messages holding only tool results do not.
182
+ Tools anywhere disable drop_thinking. -#}
183
  {%- set state = namespace(last_user=-1, has_tools=false) -%}
184
  {%- for m in msgs -%}
185
+ {%- set starts_turn = (m.role == "system" and loop.index0 > 0) -%}
186
+ {%- if m.role == "user" -%}
187
+ {%- set turn = namespace(found=not m.content_blocks) -%}
188
+ {%- for b in m.content_blocks or [] -%}
189
+ {%- if b.type in ["text", "image", "image_url"] -%}{%- set turn.found = true -%}{%- endif -%}
190
+ {%- endfor -%}
191
+ {%- set starts_turn = turn.found -%}
192
+ {%- endif -%}
193
+ {%- if starts_turn -%}{%- set state.last_user = loop.index0 -%}{%- endif -%}
194
  {%- if m.get("tools") -%}{%- set state.has_tools = true -%}{%- endif -%}
195
  {%- endfor -%}
196
  {%- set effective_drop = drop_thinking and not state.has_tools -%}
 
258
  {%- for tc in m.tool_calls -%}
259
  {%- set fn = tc.function -%}
260
  {%- set params = namespace(lines=[]) -%}
261
+ {%- if fn.arguments is string -%}{{- raise_exception("Tool call arguments must be a dict, not a JSON string") -}}{%- endif -%}
262
  {%- set args = fn.arguments if fn.arguments is mapping else {"arguments": fn.arguments} -%}
263
  {%- for k, v in args.items() -%}
264
  {%- set params.lines = params.lines + ["<" ~ DSML ~ " parameter name=\"" ~ k ~ "\" string=\"" ~ ("true" if v is string else "false") ~ "\">" ~ (v if v is string else v | tojson) ~ "</" ~ DSML ~ " parameter>"] -%}
tokenizer_config.json CHANGED
@@ -30,5 +30,47 @@
30
  },
31
  "sp_model_kwargs": {},
32
  "unk_token": null,
33
- "tokenizer_class": "PreTrainedTokenizerFast"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  }
 
30
  },
31
  "sp_model_kwargs": {},
32
  "unk_token": null,
33
+ "tokenizer_class": "PreTrainedTokenizerFast",
34
+ "response_template": {
35
+ "defaults": {
36
+ "role": "assistant"
37
+ },
38
+ "start_anchor": [
39
+ "<|Assistant|></think>",
40
+ "<|Assistant|>"
41
+ ],
42
+ "fields": {
43
+ "reasoning_content": {
44
+ "open": "<think>",
45
+ "close": "</think>",
46
+ "content": "text"
47
+ },
48
+ "tool_calls": {
49
+ "open_pattern": "<|DSML| invoke name=\"(?P<name>[^\"]+)\">",
50
+ "close": "</|DSML| invoke>",
51
+ "repeats": true,
52
+ "content": "xml-inline",
53
+ "content_args": {
54
+ "tag_pattern": "<|DSML| parameter name=\"(?P<key>[^\"]+)\" string=\"(?:true|false)\">(?P<value>.*?)</|DSML| parameter>"
55
+ },
56
+ "transform": {
57
+ "type": "function",
58
+ "function": {
59
+ "name": "{name}",
60
+ "arguments": "{content}"
61
+ }
62
+ }
63
+ },
64
+ "content": {
65
+ "close": [
66
+ "<|end▁of▁sentence|>",
67
+ "<|DSML| calls>",
68
+ "</|DSML| calls>"
69
+ ],
70
+ "content": "text",
71
+ "repeats": true,
72
+ "join": ""
73
+ }
74
+ }
75
+ }
76
  }