Add chat_template.jinja, byte-exact with apply_chat_template (transformers + llama.cpp)

#66
by dimondev - opened
Files changed (1) hide show
  1. chat_template.jinja +174 -0
chat_template.jinja ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {#- Kimi K3 chat template (XTML) -- llama.cpp / minja variant.
2
+
3
+ Same format as chat_template.jinja, rewritten to the subset minja implements.
4
+ Use this one for GGUF and llama.cpp; use chat_template.jinja for transformers,
5
+ vLLM and SGLang.
6
+
7
+ Three constructs had to change, and they are why one file cannot serve both
8
+ engines:
9
+
10
+ * `sort(attribute='pos,off')` -- minja has no `sort` filter, so the
11
+ tool-result reorder selects by matched position with `range` instead.
12
+ * `tojson(sort_keys=true, separators=(',', ':'))` -- minja's `tojson` takes
13
+ only `value` and `indent`, so `cjson` below serialises sorted compact JSON
14
+ directly. It relies on minja's `dictsort` being a plain byte-order sort,
15
+ which is what `json.dumps(sort_keys=True)` does. transformers' `dictsort`
16
+ defaults to case-insensitive and minja rejects `case_sensitive=true`, so
17
+ this macro is correct here and would be subtly wrong under transformers.
18
+ * `replace` as a filter -- minja only exposes it as a string method.
19
+
20
+ Only literal text sits at column 0. Lines holding nothing but a block tag are
21
+ indented for reading; lstrip_blocks strips that indentation.
22
+ -#}
23
+ {%- macro attr(k, v) %} {{ k }}="{{ (v | string).replace('&', '&').replace('"', '"') }}"{% endmacro %}
24
+ {%- macro cjson(x) %}{% if x is mapping %}{{ '{' }}{% for k, v in x | dictsort %}{% if loop.index0 > 0 %},{% endif %}{{ k | tojson }}:{{ cjson(v) }}{% endfor %}{{ '}' }}{% elif x is string or x is boolean or x is number or x is none %}{{ x | tojson }}{% else %}[{% for item in x %}{% if loop.index0 > 0 %},{% endif %}{{ cjson(item) }}{% endfor %}]{% endif %}{% endmacro %}
25
+ {%- macro body(c) %}{% if c is string %}{{ c }}{% elif c is not none %}{% for part in c %}{% if part.type in ['image', 'image_url'] %}<|kimi_image_placeholder|>{% else %}{{ part.text }}{% endif %}{% endfor %}{% endif %}{% endmacro %}
26
+ {%- macro call_name(tc) %}{{ tc.function.name if 'function' in tc else tc.name }}{% endmacro %}
27
+ {%- macro xtml_type(v) %}{{ 'boolean' if v is boolean else 'null' if v is none else 'number' if v is number else 'string' if v is string else 'object' if v is mapping else 'array' }}{% endmacro %}
28
+ {%- set thinking = thinking if thinking is defined else true -%}
29
+ {%- set thinking_effort = thinking_effort if thinking_effort is defined else 'max' -%}
30
+ {%- if thinking and thinking_effort is not none and thinking_effort not in ['low', 'high', 'max'] -%}
31
+ {%- set _ = raise_exception('Unsupported thinking_effort=' ~ thinking_effort ~ '; supported values are [\'high\', \'low\', \'max\'].') -%}
32
+ {%- endif -%}
33
+ {#- Pass 1: render order and authoritative tool names. -#}
34
+ {%- set pre = namespace(plan=[], calls=none) -%}
35
+ {%- for m in messages -%}
36
+ {%- if m.role == 'tool' and not (loop.index0 == 0 or messages[loop.index0 - 1].role != 'tool') -%}
37
+ {#- already emitted as part of its run -#}
38
+ {%- elif m.role == 'tool' -%}
39
+ {%- set run = namespace(items=[], matched=true) -%}
40
+ {%- for m2 in messages[loop.index0:] -%}
41
+ {%- if m2.role != 'tool' -%}{%- break -%}{%- endif -%}
42
+ {%- set cid = m2.tool_call_id if 'tool_call_id' in m2 else (m2.id if 'id' in m2 else none) -%}
43
+ {%- set hit = namespace(pos=none, name=none) -%}
44
+ {%- if cid is not none and pre.calls -%}
45
+ {%- for tc in pre.calls -%}
46
+ {%- if hit.pos is none and 'id' in tc and tc.id is not none and (tc.id | string) == (cid | string) -%}
47
+ {%- set hit.pos = loop.index -%}
48
+ {%- set nm = call_name(tc) -%}
49
+ {%- if nm -%}{%- set hit.name = nm -%}{%- endif -%}
50
+ {%- endif -%}
51
+ {%- endfor -%}
52
+ {%- endif -%}
53
+ {%- if hit.pos is none -%}{%- set run.matched = false -%}{%- endif -%}
54
+ {%- set run.items = run.items + [{'m': m2, 'pos': hit.pos, 'tool': hit.name}] -%}
55
+ {%- endfor -%}
56
+ {%- if run.matched -%}
57
+ {#- Selection by position: run.items is already in arrival order, so
58
+ picking each position in turn is a stable sort by (pos, offset). -#}
59
+ {%- for p in range(1, (pre.calls | length) + 1) -%}
60
+ {%- for it in run.items -%}
61
+ {%- if it.pos == p -%}
62
+ {%- set pre.plan = pre.plan + [{'m': it.m, 'tool': it.tool}] -%}
63
+ {%- endif -%}
64
+ {%- endfor -%}
65
+ {%- endfor -%}
66
+ {%- else -%}
67
+ {%- for it in run.items -%}
68
+ {%- set pre.plan = pre.plan + [{'m': it.m, 'tool': none}] -%}
69
+ {%- endfor -%}
70
+ {%- endif -%}
71
+ {%- else -%}
72
+ {%- if m.role == 'assistant' -%}
73
+ {%- set pre.calls = m.tool_calls if m.tool_calls else none -%}
74
+ {%- endif -%}
75
+ {%- set pre.plan = pre.plan + [{'m': m, 'tool': none}] -%}
76
+ {%- endif -%}
77
+ {%- endfor -%}
78
+ {#- Pass 2: emit. -#}
79
+ {%- set ns = namespace(tool_index=0, tool_calls=none) -%}
80
+ {%- if tools -%}
81
+ <|open|>message{{ attr('role', 'system') }}{{ attr('type', 'tool-declare') }}<|sep|># Tools
82
+ Here are the available tools, described in JSONSchema.
83
+
84
+ ```json
85
+ {{ cjson(tools) }}
86
+ ```<|close|>message<|sep|><|end_of_msg|>
87
+ {%- endif -%}
88
+ {%- if thinking and thinking_effort in ['low', 'high', 'max'] -%}
89
+ <|open|>message{{ attr('role', 'system') }}{{ attr('type', 'thinking-effort') }}<|sep|>`thinking_effort` guides on how much to think in your thinking channel (not including the response channel), supported values include `low`, `medium`, `high`, and `max`.
90
+ Now the system is invoked with `thinking_effort={{ thinking_effort }}`.<|close|>message<|sep|><|end_of_msg|>
91
+ {%- endif -%}
92
+ {%- for item in pre.plan -%}
93
+ {%- set message = item.m -%}
94
+ {%- if message.role == 'user' -%}
95
+ <|open|>message{{ attr('role', 'user') }}{% if message.name %}{{ attr('name', message.name) }}{% endif %}<|sep|>{{ body(message.content) }}<|close|>message<|sep|><|end_of_msg|>
96
+ {%- elif message.role == 'system' and message.tools -%}
97
+ <|open|>message{{ attr('role', 'system') }}{{ attr('type', 'tool-declare') }}<|sep|>## New Tools Available
98
+ The system dynamically extends the toolset via lazy-loading.
99
+ You have access to all existing and extended tools.
100
+ Here are the specs for the extended tools.
101
+
102
+ ```json
103
+ {{ cjson(message.tools) }}
104
+ ```<|close|>message<|sep|><|end_of_msg|>
105
+ {%- elif message.role == 'system' -%}
106
+ <|open|>message{{ attr('role', 'system') }}{% if message.name %}{{ attr('name', message.name) }}{% endif %}<|sep|>{{ body(message.content) }}<|close|>message<|sep|><|end_of_msg|>
107
+ {%- elif message.role == 'tool' -%}
108
+ {%- set ns.tool_index = ns.tool_index + 1 -%}
109
+ {%- set found = namespace(name=item.tool) -%}
110
+ {%- if found.name is none -%}
111
+ {%- set found.name = message.tool if 'tool' in message else (message.name if 'name' in message else none) -%}
112
+ {%- endif -%}
113
+ {%- if found.name is none and ns.tool_calls and ns.tool_index <= ns.tool_calls | length -%}
114
+ {%- set found.name = call_name(ns.tool_calls[ns.tool_index - 1]) -%}
115
+ {%- endif -%}
116
+ {%- if found.name is none -%}
117
+ {%- set _ = raise_exception('Kimi K3 tool messages need a resolvable tool name: carry `tool`/`name`, or match a preceding assistant tool_call by order.') -%}
118
+ {%- endif -%}
119
+ <|open|>message{{ attr('role', 'tool') }}{{ attr('tool', found.name) }}{{ attr('index', ns.tool_index) }}<|sep|>{{ body(message.content) }}<|close|>message<|sep|><|end_of_msg|>
120
+ {%- elif message.role == 'assistant' -%}
121
+ {%- set ns.tool_calls = message.tool_calls if message.tool_calls else none -%}
122
+ {%- set ns.tool_index = 0 -%}
123
+ <|open|>message{{ attr('role', 'assistant') }}{% if message.name %}{{ attr('name', message.name) }}{% endif %}<|sep|>
124
+ {%- if thinking -%}
125
+ {%- set rc = message.reasoning_content or message.reasoning -%}
126
+ <|open|>think<|sep|>{% if rc is not none and rc | string | trim %}{{ rc }}{% endif %}<|close|>think<|sep|>
127
+ {%- endif -%}
128
+ <|open|>response<|sep|>{{ body(message.content) }}<|close|>response<|sep|>
129
+ {%- if message.tool_calls -%}
130
+ <|open|>tools<|sep|>
131
+ {%- for tc in message.tool_calls -%}
132
+ {%- set fn = tc.function if 'function' in tc else tc -%}
133
+ <|open|>call{{ attr('tool', fn.name) }}{{ attr('index', loop.index) }}<|sep|>
134
+ {%- set args = fn.arguments if 'arguments' in fn else {} -%}
135
+ {%- if args is string and args | trim -%}
136
+ <|open|>json{{ attr('type', 'object') }}<|sep|>{{ args }}<|close|>json<|sep|>
137
+ {%- elif args is mapping -%}
138
+ {%- for key, value in args.items() -%}
139
+ <|open|>argument{{ attr('key', key) }}{{ attr('type', xtml_type(value)) }}<|sep|>{{ value if value is string else (value | tojson) }}<|close|>argument<|sep|>
140
+ {%- endfor -%}
141
+ {%- endif -%}
142
+ <|close|>call<|sep|>
143
+ {%- endfor -%}
144
+ <|close|>tools<|sep|>
145
+ {%- endif -%}
146
+ <|close|>message<|sep|><|end_of_msg|>
147
+ {%- endif -%}
148
+ {%- endfor -%}
149
+ {%- if tool_choice is defined and tool_choice == 'required' -%}
150
+ <|open|>message{{ attr('role', 'system') }}{{ attr('type', 'tool-choice') }}<|sep|>The system is invoked with `tool_choice=required`.
151
+ You MUST call tools in the next message.<|close|>message<|sep|><|end_of_msg|>
152
+ {%- elif tool_choice is defined and tool_choice == 'none' -%}
153
+ <|open|>message{{ attr('role', 'system') }}{{ attr('type', 'tool-choice') }}<|sep|>The system is invoked with `tool_choice=none`.
154
+ You MUST NOT call any tools in the next message.<|close|>message<|sep|><|end_of_msg|>
155
+ {%- endif -%}
156
+ {%- set rf = response_format if response_format is defined else none -%}
157
+ {%- set rf_type = (rf.type if 'type' in rf else rf) if rf is mapping else rf -%}
158
+ {%- if rf_type == 'json_object' -%}
159
+ <|open|>message{{ attr('role', 'system') }}{{ attr('type', 'response-format') }}<|sep|>The system is invoked with `response_format=json_object`.
160
+ Your response must be raw JSON data without markdown code blocks (```json) or any additional formatting.<|close|>message<|sep|><|end_of_msg|>
161
+ {%- elif rf_type == 'json_schema' -%}
162
+ {%- set js = (rf.json_schema if 'json_schema' in rf else none) if rf is mapping else none -%}
163
+ {%- set derived = (js.schema if 'schema' in js else (js.json_schema if 'json_schema' in js else js)) if js is mapping else js -%}
164
+ {%- set schema = response_schema if response_schema is defined and response_schema is not none else derived -%}
165
+ <|open|>message{{ attr('role', 'system') }}{{ attr('type', 'response-format') }}<|sep|>The system is invoked with `response_format=json_schema`.
166
+ Your response must be raw JSON data without markdown code blocks (```json) or any additional formatting.
167
+ The JSON data must match the following schema:
168
+ ```json
169
+ {{ cjson(schema) }}
170
+ ```<|close|>message<|sep|><|end_of_msg|>
171
+ {%- endif -%}
172
+ {%- if add_generation_prompt -%}
173
+ <|open|>message{{ attr('role', 'assistant') }}<|sep|><|open|>{{ 'think' if thinking else 'response' }}<|sep|>
174
+ {%- endif -%}