1+ [gMASK]<sop >
2+ {% - if tools -%}
3+ <|system|>
4+ # Tools
5+
6+ You may call one or more functions to assist with the user query.
7+
8+ You are provided with function signatures within <tools ></tools > XML tags:
9+ <tools >
10+ {% for tool in tools %}
11+ {{ tool | tojson }}
12+ {% endfor %}
13+ </tools >
14+
15+ For 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 .index 0 -%}
42+ {% - endif %}
43+ {% - endfor %}
44+ {% for m in messages %}
45+ {% - if m .role == 'user' -%} <|user|>
46+ {% - set user_content = visible_text (m .content ) -%}
47+ {{ user_content }}
48+ {{- '/nothink' if (enable_thinking is defined and not enable_thinking and not user_content.endswith("/nothink")) else '' -}}
49+ {% - elif m .role == 'assistant' -%}
50+ <|assistant|>
51+ {% - set reasoning_content = '' %}
52+ {% - set content = visible_text (m .content ) %}
53+ {% - if m .reasoning_content is string %}
54+ {% - set reasoning_content = m .reasoning_content %}
55+ {% - else %}
56+ {% - if '</think>' in content %}
57+ {% - set parts = content .split ('</think>' ) -%}
58+ {% - set before_first_close = parts | first -%}
59+ {% - set inner_parts = before_first_close .rstrip ('\n' ).split ('<think>' ) -%}
60+ {% - set extracted_reasoning = inner_parts | last -%}
61+ {% - set reasoning_content = extracted_reasoning .lstrip ('\n' ) -%}
62+ {% - set after_last_close = parts | last -%}
63+ {% - set content = after_last_close .lstrip ('\n' ) -%}
64+ {% - endif %}
65+ {% - endif %}
66+ {% - if loop .index 0 > ns .last_user_index and reasoning_content -%}
67+ {{ '\n<think>' + reasoning_content.strip() + '</think>'}}
68+ {% - else -%}
69+ {{ '\n<think></think>' }}
70+ {% - endif -%}
71+ {% - if content .strip () -%}
72+ {{ '\n' + content.strip() }}
73+ {% - endif -%}
74+ {% if m .tool_calls %}
75+ {% for tc in m .tool_calls %}
76+ {% - if tc .function %}
77+ {% - set tc_obj = tc .function %}
78+ {% - else %}
79+ {% - set tc_obj = tc %}
80+ {% - endif %}
81+ {{ '\n<tool_call>' + tc_obj.name }}
82+ {% - if tc_obj .arguments is mapping -%}
83+ {% - for k , v in tc_obj .arguments .items () -%}
84+
85+ <arg _key >{{ k }}</arg _key >
86+ <arg _value >{{ v | tojson if v is not string else v }}</arg _value >
87+ {% - endfor -%}
88+ {% - else -%}
89+ {#- Arguments came as string - this shouldn't happen with polyfills disabled -#}
90+ {#- Output as single argument for debugging -#}
91+
92+ <arg _key >raw_arguments</arg _key >
93+ <arg _value >{{ tc_obj.arguments }}</arg _value >
94+ {% - endif -%}
95+ </tool _call >{% endfor %}
96+ {% endif %}
97+ {% - elif m .role == 'tool' -%}
98+ {% - if m .content is string -%}
99+ {% - if loop .first or (messages [loop .index 0 - 1].role != "tool" ) %}
100+ {{- '<|observation|>' }}
101+ {% - endif %}
102+ {{- '\n<tool_response>\n' }}
103+ {{- m.content }}
104+ {{- '\n</tool_response>' }}
105+ {% - else -%}
106+ <|observation|>{% for tr in m .content %}
107+
108+ <tool _response >
109+ {{ tr.output if tr.output is defined else tr }}
110+ </tool _response >{% endfor -%}
111+ {% endif -%}
112+ {% - elif m .role == 'system' -%}
113+ <|system|>
114+ {{ visible_text(m.content) }}
115+ {% - endif -%}
116+ {% - endfor -%}
117+ {% - if add_generation_prompt -%}
118+ <|assistant|>{{- '\n<think></think>' if (enable_thinking is defined and not enable_thinking) else '' -}}
119+ {% - endif -%}
0 commit comments