1[gMASK]<sop>
2{%- if tools -%}
3<|system|>
4# Tools
5
6You may call one or more functions to assist with the user query.
7
8You are provided with function signatures within <tools></tools> XML tags:
9<tools>
10{% for tool in tools %}
11{{ tool | tojson(ensure_ascii=False) }}
12{% endfor %}
13</tools>
14
15For each function call, output the function name and arguments within the following XML format:
16<tool_call>{function-name}
17<arg_key>{arg-key-1}</arg_key>
18<arg_value>{arg-value-1}</arg_value>
19<arg_key>{arg-key-2}</arg_key>
20<arg_value>{arg-value-2}</arg_value>
21...
22</tool_call>{%- endif -%}
23{%- macro visible_text(content) -%}
24 {%- if content is string -%}
25 {{- content }}
26 {%- elif content is iterable and content is not mapping -%}
27 {%- for item in content -%}
28 {%- if item is mapping and item.type == 'text' -%}
29 {{- item.text }}
30 {%- elif item is string -%}
31 {{- item }}
32 {%- endif -%}
33 {%- endfor -%}
34 {%- else -%}
35 {{- content }}
36 {%- endif -%}
37{%- endmacro -%}
38{%- set ns = namespace(last_user_index=-1) %}
39{%- for m in messages %}
40 {%- if m.role == 'user' %}
41 {% set ns.last_user_index = loop.index0 -%}
42 {%- endif %}
43{%- endfor %}
44{% for m in messages %}
45{%- if m.role == 'user' -%}<|user|>
46{{ visible_text(m.content) }}
47{{- '/nothink' if (enable_thinking is defined and not enable_thinking and not visible_text(m.content).endswith("/nothink")) else '' -}}
48{%- elif m.role == 'assistant' -%}
49<|assistant|>
50{%- set reasoning_content = '' %}
51{%- set content = visible_text(m.content) %}
52{%- if m.reasoning_content is string %}
53 {%- set reasoning_content = m.reasoning_content %}
54{%- else %}
55 {%- if '</think>' in content %}
56 {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
57 {%- set content = content.split('</think>')[-1].lstrip('\n') %}
58 {%- endif %}
59{%- endif %}
60{%- if loop.index0 > ns.last_user_index and reasoning_content -%}
61{{ '\n<think>' + reasoning_content.strip() + '</think>'}}
62{%- else -%}
63{{ '\n<think></think>' }}
64{%- endif -%}
65{%- if content.strip() -%}
66{{ '\n' + content.strip() }}
67{%- endif -%}
68{% if m.tool_calls %}
69{% for tc in m.tool_calls %}
70{%- if tc.function %}
71 {%- set tc = tc.function %}
72{%- endif %}
73{{ '\n<tool_call>' + tc.name }}
74{% set _args = tc.arguments or {} %}
75{% if _args is not mapping %}
76 {{ raise_exception("Invalid tool call arguments passed: " + _args | string) }}
77{% endif %}
78{% for k, v in _args.items() %}
79<arg_key>{{ k }}</arg_key>
80<arg_value>{{ v | tojson(ensure_ascii=False) if v is not string else v }}</arg_value>
81{% endfor %}
82</tool_call>{% endfor %}
83{% endif %}
84{%- elif m.role == 'tool' -%}
85{%- if m.content is string -%}
86{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
87 {{- '<|observation|>' }}
88{%- endif %}
89{{- '\n<tool_response>\n' }}
90{{- m.content }}
91{{- '\n</tool_response>' }}
92{%- else -%}
93<|observation|>{% for tr in m.content %}
94
95<tool_response>
96{{ tr.output if tr.output is defined else tr }}
97</tool_response>{% endfor -%}
98{% endif -%}
99{%- elif m.role == 'system' -%}
100<|system|>
101{{ visible_text(m.content) }}
102{%- endif -%}
103{%- endfor -%}
104{%- if add_generation_prompt -%}
105 <|assistant|>{{- '\n<think></think>' if (enable_thinking is defined and not enable_thinking) else '' -}}
106{%- endif -%}