File size: 14,163 Bytes
b7bb712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
{%- set system_start = '<|im_start|>system\n' -%}
{%- set user_start = '<|im_start|>user\n' -%}
{%- set assistant_start = '<|im_start|>assistant\n' -%}
{%- set system_end = '<|im_end|>\n' -%}
{%- set user_end = '<|im_end|>\n' -%}
{%- set assistant_end = '<|im_end|>\n' -%}
{%- set tool_defs_start = '<|tools_start|>\n' -%}
{%- set tool_defs_end = '\n<|tools_end|>' -%}
{%- set tool_call_start = '\n<|tool_start|>\n' -%}
{%- set tool_call_end = '\n<|tool_end|>' -%}
{%- set tool_result_start = '<|im_start|>tool\n' -%}
{%- set tool_result_sep = '\n' -%}
{%- set tool_result_end = '\n<|im_end|>\n' -%}
{%- set reasoning_start = '<|thinking|>\n' -%}
{%- set reasoning_end = '\n<|thinking_end|>\n' -%}
{%- set default_system_prompt = "" -%}
{%- set tool_defs_preamble = "You are provided with function signatures within " + tool_defs_start.strip() + tool_defs_end.strip() + "\n" -%}
{%- set tool_defs_postamble = "\nFor each function call, call them within " + tool_call_start.strip() + tool_call_end.strip() + "." -%}
{%- set tool_defs_style = "python" -%}
{%- set tool_call_style = "python" -%}

{%- set content_ns = namespace(content='') %}

{%- if messages[0].role == 'system' %}
    {%- if messages[0].content is string %}
        {%- set content_ns.content = messages[0].content %}
    {%- elif messages[0].content is iterable %}
        {%- set content_ns.content = '' %}
        {%- for part in messages[0].content %}
            {%- set content_ns.content = content_ns.content + part.text %}
        {%- endfor %}
    {%- else %}
        {%- set content_ns.content = '' %}
    {%- endif %}
    {%- set messages = messages[1:] %}
{%- else %}
    {%- set content_ns.content = default_system_prompt %}
{%- endif %}

{%- macro json_to_python_type(json_spec) %}
    {%- set basic_type_map = {
        "string": "str",
        "number": "float",
        "integer": "int",
        "boolean": "bool"
    } %}

    {%- if basic_type_map[json_spec.type] is defined %}
        {{- basic_type_map[json_spec.type] }}
    {%- elif json_spec.type == "array" %}
        {{- "List[" +  json_to_python_type(json_spec.items) + "]"}}
    {%- elif json_spec.type == "object" %}
        {{- "Dict[str, " + json_to_python_type(json_spec.additionalProperties) + ']'}}
    {%- elif json_spec.type is iterable %}
        {{- "Union[" }}
        {%- for t in json_spec.type %}
            {{- json_to_python_type({"type": t}) }}
            {%- if not loop.last %}
                {{- "," }} 
            {%- endif %}
        {%- endfor %}
        {{- "]" }}
    {%- else %}
        {{- "Any" }}
    {%- endif %}
{%- endmacro %}

{%- macro json_to_python_value(json_value) %}
    {%- if json_value is string %}
        {{- json_value|tojson }}
    {%- elif json_value is integer or json_value is float %}
        {{- json_value|tojson }}
    {%- elif json_value is boolean %}
        {%- if json_value %}
            {{- "True" }}
        {%- else %}
            {{- "False" }}
        {%- endif %}
    {%- elif json_value is iterable %}
        {{- "[" }}
        {%- for item in json_value %}
            {{- json_to_python_value(item) }}
            {%- if not loop.last %}
                {{- ',' }}
            {%- endif %}
        {%- endfor %}
        {{- "]" }}
    {%- else %}
        {{- "None" }}
    {%- endif %}
{%- endmacro%}

{%- macro python_tools_parser(tools) %}
    {%- for tool in tools %}
        {%- if loop.index0 != 0 %}
            {{- '\n\n'}}
        {%- endif %}
        {%- if tool.function is defined %}
            {%- set tool = tool.function %}
        {%- endif %}
        {{-'def ' + tool.name + '('}}
        {%- for param_name, param_fields in tool.parameters.properties|items %}
            {%- if loop.index0 != 0 %}
                {{- ', '}}
            {%- endif %}
            {{-param_name + ": "}} 
            {%- if not param_name in tool.parameters.required %}
                {{-'Optional[' + json_to_python_type(param_fields) + '] = None'}}
            {%- else %}
                {{- json_to_python_type(param_fields) }}
            {%- endif %}
        {%- endfor %}
        {{- ') -> str:\n    """'}}
        {{- tool.description }}
        {%- if tool.parameters.properties|length != 0 %}
            {{- '\n\n    Args:\n        '}}
            {%- for param_name, param_fields in tool.parameters.properties|items %}
                {%- if loop.index0 != 0 %}
                    {{- '\n        ' }}
                {%- endif %}
                {{- param_name + ' ('}}
                {%- if not param_name in tool.parameters.required %}
                    {{-'Optional[' + json_to_python_type(param_fields) + ']'}}
                {%- else %}
                    {{- json_to_python_type(param_fields) }}
                {%- endif %}
                {{- ')' }}
                {%- if param_fields.description %}
                    {{- ': ' + param_fields.description }}
                {%- endif%}
            {%- endfor %}
        {%- endif %}
        {{- '\n    """\n    pass' }}
    {%- endfor %}
{%- endmacro %}

{%- macro python_tool_call_parser(tool_call) %}
    {{- tool_call.name + '(' }}
    {%- if tool_call.arguments is string %}
        {{- raise_exception("Python tool call parser doesn't support arguments as a json string") }}
    {%- endif %}
    {%- for param_name, param_field in tool_call.arguments|items %}
        {{- param_name + '= '}}
        {{- json_to_python_value(param_field) }}
        {%- if not loop.last %}
            {{- ', '}}
        {%- endif %}
    {%- endfor %}
    {{- ')' }}
{%- endmacro %}

{%- macro json_tools_parser(tools) %}
{%- for tool in tools %}
    {%- if not loop.first %}
        {{- "\n" }}
    {%- endif %}
    {{- tool | tojson }}
{%- endfor %}
{%- endmacro %}

{%- macro json_tool_call_parser(tool_call) %}
    {{-  '{"name": "' }}
    {{- tool_call.name }}
    {{- '", "arguments": ' }}
    {%- if tool_call.arguments is string %}
        {{- tool_call.arguments }}
    {%- else %}
        {{- tool_call.arguments | tojson }}
    {%- endif %}
    {{- '}' }}
{%- endmacro %}

{%- if tools %}
    {{- system_start }}
    {%- if content_ns.content | length > 0 %}
        {{- content_ns.content + '\n\n' }}
    {%- endif %}
    {{- tool_defs_preamble + tool_defs_start }}
    {%- if tool_defs_style == "json" %}
        {{- json_tools_parser(tools) }}
    {%- elif tool_defs_style == "python" %}
        {{- python_tools_parser(tools) }}
    {%- else %}
        {{- raise_exception("Invalid tool definition style (" + tool_defs_style + "), must be 'json' or 'python'") }}
    {%- endif %}
    {{- tool_defs_end + tool_defs_postamble }}
    {%- if (messages|length == 0) and not add_generation_prompt %}
        {{- system_end.rstrip('\n') }}
    {%- else %}
        {{- system_end }}
    {%- endif %}
{%- else %}
    {%- if content_ns.content | length > 0 %}
        {{- system_start + content_ns.content }}
        {%- if (messages|length == 0) and not add_generation_prompt %}
            {{- system_end.rstrip('\n') }}
        {%- else %}
            {{- system_end }}
        {%- endif %}
    {%- endif%}
{%- endif %}

{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
{%- for message in messages[::-1] %}
    {%- set index = (messages|length - 1) - loop.index0 %}
    {%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith(tool_result_start) and message.content.endswith(tool_result_end)) %}
        {%- set ns.multi_step_tool = false %}
        {%- set ns.last_query_index = index %}
    {%- endif %}
{%- endfor %}
{%- for message in messages %}

    {%- if message.content is string %}
        {%- set content_ns.content = message.content %}
    {%- elif message.content is iterable %}
        {%- set content_ns.content = '' %}
        {%- for part in message.content %}
            {%- set content_ns.content = content_ns.content + part.text %}
        {%- endfor %}
    {%- else %}
        {%- set content_ns.content = '' %}
    {%- endif %}

    {%- if message.role == "system" %}
        {{- system_start }}

        {%- if loop.last and not add_generation_prompt %}
            {%- set end_marker = system_end.rstrip('\n') %}
        {%- else %}
            {%- set end_marker = system_end %}
        {%- endif %}

        {%- if message.content is string %}
            {{- content_ns.content + end_marker }}
        {%- else %}
            {%- for item in message.content %}
                {%- if item.mask is defined and not item.mask %}
                    {%- generation %}
                    {{- item.text }}
                    {%- if loop.last %}
                        {{- end_marker }}
                    {%- endif %}
                    {%- endgeneration %}
                {%- else %}
                    {{- item.text }}
                    {%- if loop.last %}
                        {{- end_marker }}
                    {%- endif %}
                {%- endif %}
            {%- endfor %}
        {%- endif %}


    {%- elif message.role == "user" %}
        {{- user_start }}

        {%- if loop.last and not add_generation_prompt %}
            {%- set end_marker = user_end.rstrip('\n') %}
        {%- else %}
            {%- set end_marker = user_end %}
        {%- endif %}

        {%- if message.content is string %}
            {{- content_ns.content + end_marker }}
        {%- else %}
            {%- for item in message.content %}
                {%- if item.mask is defined and not item.mask %}
                    {%- generation %}
                    {{- item.text }}
                    {%- if loop.last %}
                        {{- end_marker }}
                    {%- endif %}
                    {%- endgeneration %}
                {%- else %}
                    {{- item.text }}
                    {%- if loop.last %}
                        {{- end_marker }}
                    {%- endif %}
                {%- endif %}
            {%- endfor %}
        {%- endif %}


    {%- elif message.role == "assistant" %}
        {%- set reasoning_content = none %}
        {%- if message.reasoning_content is string %}
            {%- set reasoning_content = message.reasoning_content %}
        {%- else %}
            {%- if reasoning_start in content_ns.content %}
                {%- set reasoning_content = content_ns.content.split(reasoning_end)[0].rstrip('\n').split(reasoning_start)[-1].lstrip('\n') %}
                {%- set content = content_ns.content.split(reasoning_end)[-1].lstrip('\n') %}
            {%- endif %}
        {%- endif %}

        {{- assistant_start }}

        {%- if loop.index0 > ns.last_query_index %}
            {%- if loop.last and reasoning_content %}
                {%- set reasoning_end_marker = reasoning_end %}
                {%- if content_ns.content is string %}
                    {%- set content_ns.content = content_ns.content.lstrip('\n') %}
                    {%- if content_ns.content == '' %}
                        {%- set reasoning_end_marker = reasoning_end.rstrip('\n') %}
                    {%- endif %}
                {%- endif %}
                {%- generation %}
                {{- reasoning_start + reasoning_content.strip('\n') + reasoning_end_marker }}
                {%- endgeneration %}
            {%- endif %}
        {%- endif %}

        {%- if message.content is string %}
            {%- generation %}
            {{- content_ns.content }}
            {%- endgeneration %}
        {%- else %}
            {%- for item in message.content %}
                {%- if not item.mask is defined or not item.mask %}
                    {%- generation %}
                    {{- item.text }}
                    {%- endgeneration %}
                {%- else %}
                    {{- item.text }}
                {%- endif %}
            {%- endfor %}
        {%- endif %}
        {%- generation %}
        {%- if message.tool_calls %}
            {%- for tool_call in message.tool_calls %}
                {%- if tool_call.function %}
                    {%- set tool_call = tool_call.function %}
                {%- endif %}
                {%- if (loop.first and content_ns.content) or (not loop.first) %}
                    {{- tool_call_start }}
                {%- else %}
                    {{- tool_call_start.lstrip('\n') }}
                {%- endif %}
                {%- if tool_call_style == 'json' %}
                    {{- json_tool_call_parser(tool_call) }}
                {%- elif tool_call_style == 'python' %}
                    {{- python_tool_call_parser(tool_call) }}
                {%- endif %}
                {{- tool_call_end }}
            {%- endfor %}
        {%- endif %}
        {%- endgeneration %}
        {%- if not continue_final_message %}
            {%- if loop.last %}
                {%- set end_marker = assistant_end.rstrip('\n') %}
            {%- else %}
                {%- set end_marker = assistant_end %}
            {%- endif %}
        {%- endif %}            
        {%- if message.content is string or (message.content is defined and (not message.content[message.content|length - 1].mask is defined or not message.content[message.content|length - 1].mask)) or (message.tool_calls) %}
            {%- generation%}
            {{- end_marker }}
            {%- endgeneration %}
        {%- else %}
            {{- end_marker }}
        {%- endif %}

    {%- elif message.role == "tool" %}
        {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
            {{- tool_result_start }}
        {%- else %}
            {{- tool_result_sep }}
        {%- endif %}
        {{- content_ns.content }}
        {%- if loop.last %}
            {{- tool_result_end.rstrip('\n') }}
        {%- elif (messages[loop.index0 + 1].role != "tool") %}
            {{- tool_result_end.rstrip('\n') }}
        {%- endif %}
    {%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
    {{- assistant_start }}
    {%- if enable_thinking is defined and enable_thinking is true %}
        {%- generation %}
        {{- reasoning_start }}
        {%- endgeneration %}
    {%- endif %}
{%- endif -%}