File size: 12,129 Bytes
00a0cd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
{{- bos_token -}}
{%- set preserve_thinking = preserve_thinking | default(false) -%}
{%- set enable_thinking = enable_thinking | default(true) -%}

{%- macro render_value(v) -%}
    {%- if v is string -%}
        {{- v -}}
    {%- elif v is boolean -%}
        {{- "true" if v else "false" -}}
    {%- elif v is none -%}
        {{- "null" -}}
    {%- elif v is mapping or (v is sequence and v is not string) -%}
        {{- v | tojson -}}
    {%- else -%}
        {{- v -}}
    {%- endif -%}
{%- endmacro -%}

{%- macro parse_content(content) -%}
    {%- if content is string -%}
        {{- content -}}
    {%- elif content is mapping and content.get("type") == "image" -%}
        {{- "<|vision_start|><|image|><|vision_end|>" -}}
    {%- elif content is mapping and content.get("type") == "video" -%}
        {{- "<|vision_start|><|video|><|vision_end|>" -}}
    {%- elif content is mapping and content.get("type") == "text" -%}
        {{- (content.get("text") or "") | string -}}
    {%- elif content is mapping -%}
        {{- content | tojson -}}
    {%- elif content is iterable -%}
        {%- set _ns = namespace(result="") -%}
        {%- for item in content -%}
            {%- if item is string -%}
                {%- set _ns.result = _ns.result + item -%}
            {%- elif item is mapping and item.get("type") == "image" -%}
                {%- set _ns.result = _ns.result + "<|vision_start|><|image|><|vision_end|>" -%}
            {%- elif item is mapping and item.get("type") == "video" -%}
                {%- set _ns.result = _ns.result + "<|vision_start|><|video|><|vision_end|>" -%}
            {%- elif item is mapping and item.get("type") == "text" -%}
                {%- set _ns.result = _ns.result + ((item.get("text") or "") | string) -%}
            {%- else -%}
                {%- set _ns.result = _ns.result + (item | tojson) -%}
            {%- endif -%}
        {%- endfor -%}
        {{- _ns.result -}}
    {%- elif content is not none -%}
        {{- content | tojson -}}
    {%- endif -%}
{%- endmacro -%}

{%- macro render_tool_calls(tool_calls) -%}
    {%- for tool_call in tool_calls -%}
        {%- if not loop.first -%}{{- "\n\n" -}}{%- endif -%}
        {%- set func = tool_call["function"] if "function" in tool_call else tool_call -%}
        {%- set func_args = func.get("arguments") -%}
        {%- if func_args is string and (func_args | trim) not in ["", "{}", "null"] -%}
            {{- raise_exception("Tool call arguments must be a mapping, got a JSON-encoded string: parse arguments with json.loads() before applying the chat template") -}}
        {%- endif -%}
        {{- "<tool_call>\n<function=" + func["name"] + ">\n" -}}
        {%- if func_args is mapping -%}
            {%- for arg_name, arg_value in func_args.items() -%}
                {{- "<parameter=" + arg_name + ">\n" + render_value(arg_value) + "\n</parameter>\n" -}}
            {%- endfor -%}
        {%- endif -%}
        {{- "</function>\n</tool_call>" -}}
    {%- endfor -%}
{%- endmacro -%}

{%- macro tool_to_json(tool) -%}
    {%- set fn = tool.function if tool.function is defined else tool -%}
    {%- set ns_t = namespace(first=true) -%}
    {{- "{" -}}
    {%- for k, v in fn.items() -%}
        {%- if k != "defer_loading" -%}
            {%- if not ns_t.first -%}{{- ", " -}}{%- endif -%}
            {%- set ns_t.first = false -%}
            {{- k | tojson -}}{{- ": " -}}{{- v | tojson -}}
        {%- endif -%}
    {%- endfor -%}
    {{- "}" -}}
{%- endmacro -%}

{%- macro is_tool_reference(item) -%}
    {{- "1" if item is mapping and item.get("type") == "tool_reference" else "" -}}
{%- endmacro -%}

{%- macro referenced_tool_to_json(name) -%}
    {%- set _ns = namespace(found=none) -%}
    {%- for tool in tools or [] -%}
        {%- set fn = tool.function if tool.function is defined else tool -%}
        {%- if fn.name == name -%}
            {%- set _ns.found = fn -%}
        {%- endif -%}
    {%- endfor -%}
    {%- if _ns.found is none -%}
        {{- raise_exception("Tool reference '" + name | string + "' not found in available tools. Every tool referenced by a tool_reference must be in `tools` on every request.") -}}
    {%- endif -%}
    {{- tool_to_json(_ns.found) -}}
{%- endmacro -%}

{%- macro render_tool_response(content) -%}
    {%- if content is string and "<tool_response>" in content -%}
        {{- content -}}
    {%- else -%}
    {{- "<tool_response>\n" -}}
    {%- if content is string -%}
        {{- content -}}
    {%- elif content is mapping -%}
        {{- parse_content(content) -}}
    {%- elif content is iterable -%}
        {%- for item in content -%}
            {%- if not loop.first -%}{{- "\n" -}}{%- endif -%}
            {%- if is_tool_reference(item) -%}
                {%- if loop.first or not is_tool_reference(loop.previtem) -%}
                    {{- "<additional_tools>\n" -}}
                {%- endif -%}
                {{- referenced_tool_to_json(item.name) -}}
                {%- if loop.last or not is_tool_reference(loop.nextitem) -%}
                    {{- "\n</additional_tools>" -}}
                {%- endif -%}
            {%- elif item is mapping and item.result is defined and item.result is mapping and item.result.tools is defined -%}
                {{- "<additional_tools>\n" -}}
                {%- set _tl = item.result.tools -%}
                {%- if _tl is string -%}
                    {{- _tl -}}
                {%- elif _tl is mapping -%}
                    {{- tool_to_json(_tl) -}}
                {%- else -%}
                    {%- for t in _tl -%}
                        {%- if not loop.first -%}{{- "\n" -}}{%- endif -%}
                        {{- tool_to_json(t) -}}
                    {%- endfor -%}
                {%- endif -%}
                {{- "\n</additional_tools>" -}}
            {%- else -%}
                {{- parse_content(item) -}}
            {%- endif -%}
        {%- endfor -%}
    {%- elif content is not none -%}
        {{- content | tojson -}}
    {%- endif -%}
    {{- "\n</tool_response>" -}}
    {%- endif -%}
{%- endmacro -%}

{%- set ns = namespace(system_prompt="", last_user_index=-1) -%}
{%- if messages and messages[0]["role"] == "system" -%}
    {%- if messages[0].get("content") -%}
        {%- set ns.system_prompt = parse_content(messages[0]["content"]) -%}
    {%- endif -%}
    {%- set messages = messages[1:] -%}
{%- endif -%}
{%- if tools -%}
    {%- set tools_ns = namespace(lines=[]) -%}
    {%- for tool in tools -%}
        {%- set fn = tool.function if tool.function is defined else tool -%}
        {%- set _dyn = (fn.defer_loading is defined and fn.defer_loading) or (tool.defer_loading is defined and tool.defer_loading) -%}
        {%- if not _dyn -%}
            {%- set tools_ns.lines = tools_ns.lines + [tool_to_json(fn)] -%}
        {%- endif -%}
    {%- endfor -%}
    {%- if tools_ns.lines -%}
        {%- set ns.system_prompt = ns.system_prompt + ("\n\n" if ns.system_prompt else "") + "<tools>\n" + (tools_ns.lines | join("\n")) + "\n</tools>" -%}
    {%- endif -%}
{%- endif -%}
{%- if ns.system_prompt -%}
    {{- "<|im_start|>system\n" + ns.system_prompt + "<|im_end|>\n" -}}
{%- endif -%}
{%- for message in messages -%}
    {%- if message["role"] == "user" -%}
        {%- set ns.last_user_index = loop.index0 -%}
    {%- endif -%}
{%- endfor -%}
{%- set carry = namespace(pending=[]) -%}
{%- for message in messages -%}
    {%- set _next = messages[loop.index0 + 1] if not loop.last else none -%}
    {%- set _merge = message.role == "tool" and not message.get("additional_tools")
        and _next is not none and _next.role == "tool" and not _next.get("additional_tools")
        and message.get("tool_call_id") and _next.get("tool_call_id") == message.get("tool_call_id") -%}
    {%- set _items = [] -%}
    {%- if message.role == "tool" and not message.get("additional_tools") -%}
        {%- set _c = message.get("content") -%}
        {%- if _c is string -%}
            {%- set _items = [{"type": "text", "text": _c}] if _c else [] -%}
        {%- elif _c is mapping -%}
            {%- set _items = [_c] -%}
        {%- elif _c is iterable -%}
            {%- set _items = _c | list -%}
        {%- elif _c is not none -%}
            {%- set _items = [_c] -%}
        {%- endif -%}
    {%- endif -%}
    {%- if _merge -%}
        {%- set carry.pending = carry.pending + _items -%}
    {%- else -%}
    {{- "<|im_start|>" + message.role + "\n" -}}
    {%- if message.role == "assistant" -%}
        {%- generation -%}
        {%- set thinking = message.thinking if message.thinking is defined else message.reasoning if message.reasoning is defined else message.reasoning_content -%}
        {%- set keep_thinking = preserve_thinking or loop.index0 > ns.last_user_index -%}
        {%- if thinking is defined and keep_thinking -%}
            {{- "<think>" + (thinking or "") + "</think>" -}}
        {%- elif keep_thinking and "</think>" not in (message.get("content") if message.get("content") is string else "") -%}
            {{- "<think></think>" -}}
        {%- endif -%}
        {%- set _cfm_tag = "CONTINUE_FINAL_MESSAGE_TAG " -%}
        {%- set _has_cfm = false -%}
        {%- set content = "" -%}
        {%- if message.get("content") -%}
            {%- set content = parse_content(message.content) -%}
        {%- endif -%}
        {%- if not keep_thinking -%}
            {%- if "</think>" in content -%}
                {%- set content = content.split("</think>")[-1] | trim -%}
            {%- endif -%}
            {%- set content = "<think></think>" + content -%}
        {%- endif -%}
        {%- if content.endswith(_cfm_tag) -%}
            {%- set _has_cfm = true -%}
            {%- set _trunc_len = (content | length) - (_cfm_tag | length) -%}
            {%- set content = content[:_trunc_len] -%}
        {%- endif -%}
        {{- content -}}
        {%- if message.tool_calls -%}
            {{- render_tool_calls(message.tool_calls) -}}
        {%- endif -%}
        {%- if _has_cfm -%}
            {{- _cfm_tag -}}
        {%- endif -%}
        {{- "<|im_end|>\n" -}}
        {%- endgeneration -%}
    {%- elif message.role == "tool" -%}
        {%- if message.get("additional_tools") -%}
            {%- set _at = message["additional_tools"] -%}
            {%- if _at is mapping or (_at is sequence and _at is not string) -%}
                {%- set _src = _at -%}
            {%- else -%}
                {%- set _src = message.get("content") -%}
            {%- endif -%}
            {%- if _src is string and "<tool_response>" in _src -%}
                {{- _src -}}
            {%- elif _src is string and "<additional_tools>" in _src -%}
                {{- "<tool_response>\n" + _src + "\n</tool_response>" -}}
            {%- elif _src -%}
                {{- "<tool_response>\n<additional_tools>\n" -}}
                {%- if _src is string -%}
                    {{- _src -}}
                {%- elif _src is mapping -%}
                    {{- tool_to_json(_src) -}}
                {%- else -%}
                    {%- for t in _src -%}
                        {%- if not loop.first -%}{{- "\n" -}}{%- endif -%}
                        {{- tool_to_json(t) -}}
                    {%- endfor -%}
                {%- endif -%}
                {{- "\n</additional_tools>\n</tool_response>" -}}
            {%- endif -%}
        {%- elif carry.pending -%}
            {{- render_tool_response(carry.pending + _items) -}}
            {%- set carry.pending = [] -%}
        {%- else -%}
            {{- render_tool_response("" if message.get("content") is none else message.get("content")) -}}
        {%- endif -%}
        {{- "<|im_end|>\n" -}}
    {%- else %}
        {%- if message.get("content") -%}
            {{- parse_content(message["content"]) -}}
        {%- endif -%}
        {{- "<|im_end|>\n" -}}
    {%- endif %}
    {%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
    {{- "<|im_start|>assistant\n" -}}
    {%- if enable_thinking -%}
        {{- "<think>" -}}
    {%- else -%}
        {{- "<think></think>" -}}
    {%- endif -%}
{%- endif -%}