File size: 15,524 Bytes
909f6f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
{{ bos_token }}{%- macro format_parameters(properties, required, filter_keys=false) -%}
    {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
    {%- set ns = namespace(found_first=false) -%}
    {%- for key, value in properties | dictsort -%}
        {%- set add_comma = false -%}
        {%- if not filter_keys or key not in standard_keys -%}
            {%- if ns.found_first %},{% endif -%}
            {%- set ns.found_first = true -%}
            {{ key }}:{
            {%- if value['description'] -%}
                description:<|"|>{{ value['description'] }}<|"|>
                {%- set add_comma = true -%}
            {%- endif -%}
            {%- if value['type'] | upper == 'STRING' -%}
                {%- if value['enum'] -%}
                    {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                    enum:{{ format_argument(value['enum']) }}
                {%- endif -%}
            {%- elif value['type'] | upper == 'ARRAY' -%}
                {%- if value['items'] is mapping and value['items'] -%}
                    {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                    items:{
                    {%- set ns_items = namespace(found_first=false) -%}
                    {%- for item_key, item_value in value['items'] | dictsort -%}
                        {%- if item_value is not none -%}
                            {%- if ns_items.found_first %},{% endif -%}
                            {%- set ns_items.found_first = true -%}
                            {%- if item_key == 'properties' -%}
                                properties:{
                                {%- if item_value is mapping -%}
                                    {{- format_parameters(item_value, value['items']['required'] | default([])) -}}
                                {%- endif -%}
                                }
                            {%- elif item_key == 'required' -%}
                                required:[
                                {%- for req_item in item_value -%}
                                    <|"|>{{- req_item -}}<|"|>
                                    {%- if not loop.last %},{% endif -%}
                                {%- endfor -%}
                                ]
                            {%- elif item_key == 'type' -%}
                                {%- if item_value is string -%}
                                    type:{{ format_argument(item_value | upper) }}
                                {%- else -%}
                                    type:{{ format_argument(item_value | map('upper') | list) }}
                                {%- endif -%}
                            {%- else -%}
                                {{ item_key }}:{{ format_argument(item_value) }}
                            {%- endif -%}
                        {%- endif -%}
                    {%- endfor -%}
                    }
                {%- endif -%}
            {%- endif -%}
            {%- if value['nullable'] %}
                {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                nullable:true
            {%- endif -%}
            {%- if value['type'] | upper == 'OBJECT' -%}
                {%- if value['properties'] is defined and value['properties'] is mapping -%}
                    {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                    properties:{
                    {{- format_parameters(value['properties'], value['required'] | default([])) -}}
                    }
                {%- elif value is mapping -%}
                    {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                    properties:{
                    {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
                    }
                {%- endif -%}
                {%- if value['required'] -%}
                    {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                    required:[
                    {%- for item in value['required'] | default([]) -%}
                        <|"|>{{- item -}}<|"|>
                        {%- if not loop.last %},{% endif -%}
                    {%- endfor -%}
                    ]
                {%- endif -%}
            {%- endif -%}
            {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
            type:<|"|>{{ value['type'] | upper }}<|"|>}
        {%- endif -%}
    {%- endfor -%}
{%- endmacro -%}
{%- macro format_function_declaration(tool_data) -%}
    declaration:{{- tool_data['function']['name'] -}}{description:<|"|>{{- tool_data['function']['description'] -}}<|"|>
    {%- set params = tool_data['function']['parameters'] -%}
    {%- if params -%}
        ,parameters:{
        {%- if params['properties'] -%}
            properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
        {%- endif -%}
        {%- if params['required'] -%}
            required:[
            {%- for item in params['required'] -%}
                <|"|>{{- item -}}<|"|>
                {{- ',' if not loop.last -}}
            {%- endfor -%}
            ],
        {%- endif -%}
        {%- if params['type'] -%}
            type:<|"|>{{- params['type'] | upper -}}<|"|>}
        {%- endif -%}
    {%- endif -%}
    {%- if 'response' in tool_data['function'] -%}
        {%- set response_declaration = tool_data['function']['response'] -%}
        ,response:{
        {%- if response_declaration['description'] -%}
            description:<|"|>{{- response_declaration['description'] -}}<|"|>,
        {%- endif -%}
        {%- if response_declaration['type'] | upper == 'OBJECT' -%}
            type:<|"|>{{- response_declaration['type'] | upper -}}<|"|>}
        {%- endif -%}
    {%- endif -%}
    }
{%- endmacro -%}
{%- macro format_argument(argument, escape_keys=True) -%}
    {%- if argument is none -%}
        {{- 'null' -}}
    {%- elif argument is string -%}
        {{- '<|"|>' + argument + '<|"|>' -}}
    {%- elif argument is boolean -%}
        {{- 'true' if argument else 'false' -}}
    {%- elif argument is mapping -%}
        {{- '{' -}}
        {%- set ns = namespace(found_first=false) -%}
        {%- for key, value in argument | dictsort -%}
            {%- if ns.found_first %},{% endif -%}
            {%- set ns.found_first = true -%}
            {%- if escape_keys -%}
                {{- '<|"|>' + key + '<|"|>' -}}
            {%- else -%}
                {{- key -}}
            {%- endif -%}
            :{{- format_argument(value, escape_keys=escape_keys) -}}
        {%- endfor -%}
        {{- '}' -}}
    {%- elif argument is sequence -%}
        {{- '[' -}}
        {%- for item in argument -%}
            {{- format_argument(item, escape_keys=escape_keys) -}}
            {%- if not loop.last %},{% endif -%}
        {%- endfor -%}
        {{- ']' -}}
    {%- else -%}
        {{- argument -}}
    {%- endif -%}
{%- endmacro -%}
{%- macro strip_thinking(text) -%}
    {%- set ns = namespace(result='') -%}
    {%- for part in text.split('<channel|>') -%}
        {%- if '<|channel>' in part -%}
            {%- set ns.result = ns.result + part.split('<|channel>')[0] -%}
        {%- else -%}
            {%- set ns.result = ns.result + part -%}
        {%- endif -%}
    {%- endfor -%}
    {{- ns.result | trim -}}
{%- endmacro -%}
{%- macro format_tool_response_block(tool_name, response) -%}
    {{- '<|tool_response>' -}}
    {%- if response is mapping -%}
        {{- 'response:' + tool_name + '{' -}}
        {%- for key, value in response | dictsort -%}
            {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
            {%- if not loop.last %},{% endif -%}
        {%- endfor -%}
        {{- '}' -}}
    {%- else -%}
        {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
    {%- endif -%}
    {{- '<tool_response|>' -}}
{%- endmacro -%}
{%- set thinking = enable_thinking is defined and enable_thinking -%}
{%- set loop_messages = messages -%}
{%- if messages[0]['role'] in ['system', 'developer'] or thinking or (tools is defined and tools) -%}
    {{ '<|turn>system
' }}
    {%- if thinking -%}
        {{ '<|think|>
' }}
    {%- endif -%}
    {%- if messages[0]['role'] in ['system', 'developer'] -%}
        {{ messages[0]['content'] | trim }}
        {%- set loop_messages = messages[1:] -%}
    {%- endif -%}
    {%- if tools is defined and tools -%}
        {%- for tool in tools -%}
            {{- '<|tool>' + format_function_declaration(tool) | trim + '<tool|>' -}}
        {%- endfor -%}
    {%- endif -%}
    {{ '<turn|>
' }}
{%- endif -%}
{%- set ns_tool = namespace(turns=0, tool_calls=false, tool_response=false) -%}
{%- set ns_turn = namespace(last_user_idx=-1) -%}
{%- for m in loop_messages -%}{%- if m['role'] == 'user' -%}{%- set ns_turn.last_user_idx = loop.index0 -%}{%- endif -%}{%- endfor -%}
{%- for message in loop_messages -%}
    {%- if message['role'] != 'tool' -%}
    {%- set continues = message['role'] == 'assistant' and ns_tool.tool_calls -%}
    {%- if not continues and (message['role'] == 'user') != (ns_tool.turns % 2 == 0) -%}
        {{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}
    {%- endif -%}
    {%- if (message['role'] == 'assistant') -%}
        {%- set role = "model" -%}
    {%- else -%}
        {%- set role = message['role'] -%}
    {%- endif -%}
    {%- if not continues -%}
        {%- set ns_tool.turns = ns_tool.turns + 1 -%}
        {{ '<|turn>' + role + '
' }}
    {%- endif -%}
    {%- if role == "model" -%}
        {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
        {%- if thinking_text and loop.index0 > ns_turn.last_user_idx -%}
            {{ '<|channel>thought
' + thinking_text + '
<channel|>' }}
        {%- elif not thinking and not continues -%}
            {{ '<|channel>thought
<channel|>' }}
        {%- endif -%}
    {%- endif -%}
    {%- set ns_tool.tool_calls = message.get('tool_calls') -%}
    {%- if ns_tool.tool_calls -%}
        {%- for tool_call in ns_tool.tool_calls -%}
            {%- set function = tool_call['function'] -%}
            {{- '<|tool_call>call:' + function['name'] + '{' -}}
            {%- if function['arguments'] is mapping -%}
                {%- set ns_args = namespace(found_first=false) -%}
                {%- for key, value in function['arguments'] | dictsort -%}
                    {%- if ns_args.found_first %},{% endif -%}
                    {%- set ns_args.found_first = true -%}
                    {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
                {%- endfor -%}
            {%- elif function['arguments'] is string -%}
                {%- set argstr = function['arguments'] | trim -%}
                {%- if argstr[:1] == '{' and argstr[-1:] == '}' -%}
                    {{- argstr[1:-1] -}}
                {%- else -%}
                    {{- function['arguments'] -}}
                {%- endif -%}
            {%- endif -%}
            {{- '}<tool_call|>' -}}
        {%- endfor -%}
        {%- set ns_tool.tool_response = false -%}
        {%- for tool_response in message.get('tool_responses') or [] -%}
            {{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}}
            {%- set ns_tool.tool_response = true -%}
        {%- endfor -%}
        {%- set ns_scan = namespace(stopped=false, next_role=none) -%}
        {%- for k in range(loop.index0 + 1, loop_messages | length) -%}
            {%- if ns_scan.stopped -%}
            {%- elif loop_messages[k]['role'] != 'tool' -%}
                {%- set ns_scan.stopped = true -%}
                {%- set ns_scan.next_role = loop_messages[k]['role'] -%}
            {%- elif not message.get('tool_responses') -%}
                {%- set follow = loop_messages[k] -%}
                {%- set ns_tname = namespace(name=follow.get('name') or 'unknown') -%}
                {%- for tc in ns_tool.tool_calls -%}
                    {%- if tc.get('id') == follow.get('tool_call_id') -%}
                        {%- set ns_tname.name = tc['function']['name'] -%}
                    {%- endif -%}
                {%- endfor -%}
                {%- set tool_body = follow.get('content') -%}
                {%- if tool_body is sequence and tool_body is not string and tool_body is not mapping -%}
                    {%- set ns_txt = namespace(s='') -%}
                    {%- for part in tool_body -%}
                        {%- if part.get('type') == 'text' -%}
                            {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}
                        {%- endif -%}
                    {%- endfor -%}
                    {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
                    {%- for part in tool_body -%}
                        {%- if part.get('type') in ['image', 'image_url'] -%}
                            {{- '<|image|>' -}}
                        {%- elif part.get('type') in ['audio', 'input_audio'] -%}
                            {{- '<|audio|>' -}}
                        {%- elif part.get('type') == 'video' -%}
                            {{- '<|video|>' -}}
                        {%- endif -%}
                    {%- endfor -%}
                {%- else -%}
                    {{- format_tool_response_block(ns_tname.name, tool_body) -}}
                {%- endif -%}
                {%- set ns_tool.tool_response = true -%}
            {%- endif -%}
        {%- endfor -%}
    {%- endif -%}
    {%- set captured_content -%}
    {%- if message['content'] is string -%}
        {%- if role == "model" -%}
            {{ strip_thinking(message['content']) }}
        {%- else -%}
            {{ message['content'] | trim }}
        {%- endif -%}
    {%- elif message['content'] is iterable -%}
        {%- for item in message['content'] -%}
            {%- if item['type'] == 'audio' -%}
                {{ '<|audio|>' }}
            {%- elif item['type'] == 'image' -%}
                {{ '<|image|>' }}
            {%- elif item['type'] == 'video' -%}
                {{ '<|video|>' }}
            {%- elif item['type'] == 'text' -%}
                {%- if role == "model" -%}
                    {{ strip_thinking(item['text']) }}
                {%- else -%}
                    {{ item['text'] | trim }}
                {%- endif -%}
            {%- endif -%}
        {%- endfor -%}
    {%- elif not ns_tool.tool_calls -%}
        {{ raise_exception("Invalid content type") }}
    {%- endif -%}
    {%- endset -%}
    {{- captured_content -}}
    {%- if not ns_tool.tool_calls -%}
        {{ '<turn|>
' }}
    {%- elif not ns_tool.tool_response -%}
        {{- '<|tool_response>' -}}
    {%- elif ns_scan.next_role != 'assistant' and (captured_content | trim | length > 0 or ns_scan.next_role) -%}
        {{- '<turn|>
' -}}
        {%- set ns_tool.tool_calls = false -%}
    {%- endif -%}
    {%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
    {%- if not ns_tool.tool_calls -%}
    {{'<|turn>model
'}}
    {%- if not thinking -%}
        {{ '<|channel>thought
<channel|>' }}
    {%- endif -%}
    {%- elif ns_tool.tool_response and thinking -%}
        {{- '<|channel>thought
' -}}
    {%- endif -%}
{%- endif -%}