1{% macro render_extra_keys(json_dict, handled_keys) %}
2 {%- if json_dict is mapping %}
3 {%- for json_key in json_dict if json_key not in handled_keys %}
4 {%- if json_dict[json_key] is mapping or (json_dict[json_key] is sequence and json_dict[json_key] is not string) %}
5 {{- '\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | tojson | safe) ~ '</' ~ json_key ~ '>' }}
6 {%- else %}
7 {{-'\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | string) ~ '</' ~ json_key ~ '>' }}
8 {%- endif %}
9 {%- endfor %}
10 {%- endif %}
11{% endmacro %}
12{%- set enable_thinking = enable_thinking if enable_thinking is defined else True %}
13{%- set truncate_history_thinking = truncate_history_thinking if truncate_history_thinking is defined else True %}
14
15{%- set ns = namespace(last_user_idx = -1) %}
16{%- set loop_messages = messages %}
17{%- for m in loop_messages %}
18 {%- if m["role"] == "user" %}
19 {%- set ns.last_user_idx = loop.index0 %}
20 {%- endif %}
21{%- endfor %}
22
23{%- if messages[0]["role"] == "system" %}
24 {%- set system_message = messages[0]["content"] %}
25 {%- set loop_messages = messages[1:] %}
26{%- else %}
27 {%- set system_message = "" %}
28 {%- set loop_messages = messages %}
29{%- endif %}
30{%- if not tools is defined %}
31 {%- set tools = [] %}
32{%- endif %}
33{# Recompute last_user_idx relative to loop_messages after handling system #}
34{%- set ns = namespace(last_user_idx = -1) %}
35{%- for m in loop_messages %}
36 {%- if m["role"] == "user" %}
37 {%- set ns.last_user_idx = loop.index0 %}
38 {%- endif %}
39{%- endfor %}
40{%- if system_message is defined %}
41 {{- "<|im_start|>system\n" + system_message }}
42{%- else %}
43 {%- if tools is iterable and tools | length > 0 %}
44 {{- "<|im_start|>system\n" }}
45 {%- endif %}
46{%- endif %}
47{%- if tools is iterable and tools | length > 0 %}
48 {%- if system_message is defined and system_message | length > 0 %}
49 {{- "\n\n" }}
50 {%- endif %}
51 {{- "# Tools\n\nYou have access to the following functions:\n\n" }}
52 {{- "<tools>" }}
53 {%- for tool in tools %}
54 {%- if tool.function is defined %}
55 {%- set tool = tool.function %}
56 {%- endif %}
57 {{- "\n<function>\n<name>" ~ tool.name ~ "</name>" }}
58 {%- if tool.description is defined %}
59 {{- '\n<description>' ~ (tool.description | trim) ~ '</description>' }}
60 {%- endif %}
61 {{- '\n<parameters>' }}
62 {%- if tool.parameters is defined and tool.parameters is mapping and tool.parameters.properties is defined and tool.parameters.properties is mapping %}
63 {%- for param_name, param_fields in tool.parameters.properties|items %}
64 {{- '\n<parameter>' }}
65 {{- '\n<name>' ~ param_name ~ '</name>' }}
66 {%- if param_fields.type is defined %}
67 {{- '\n<type>' ~ (param_fields.type | string) ~ '</type>' }}
68 {%- endif %}
69 {%- if param_fields.description is defined %}
70 {{- '\n<description>' ~ (param_fields.description | trim) ~ '</description>' }}
71 {%- endif %}
72 {%- if param_fields.enum is defined %}
73 {{- '\n<enum>' ~ (param_fields.enum | tojson | safe) ~ '</enum>' }}
74 {%- endif %}
75 {%- set handled_keys = ['name', 'type', 'description', 'enum'] %}
76 {{- render_extra_keys(param_fields, handled_keys) }}
77 {{- '\n</parameter>' }}
78 {%- endfor %}
79 {%- endif %}
80 {% set handled_keys = ['type', 'properties', 'required'] %}
81 {{- render_extra_keys(tool.parameters, handled_keys) }}
82 {%- if tool.parameters is defined and tool.parameters.required is defined %}
83 {{- '\n<required>' ~ (tool.parameters.required | tojson | safe) ~ '</required>' }}
84 {%- endif %}
85 {{- '\n</parameters>' }}
86 {%- set handled_keys = ['type', 'name', 'description', 'parameters'] %}
87 {{- render_extra_keys(tool, handled_keys) }}
88 {{- '\n</function>' }}
89 {%- endfor %}
90 {{- "\n</tools>" }}
91
92 {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
93{%- endif %}
94
95
96{%- if system_message is defined %}
97 {{- '<|im_end|>\n' }}
98{%- else %}
99 {%- if tools is iterable and tools | length > 0 %}
100 {{- '<|im_end|>\n' }}
101 {%- endif %}
102{%- endif %}
103
104{%- for message in loop_messages %}
105 {%- if message.role == "assistant" %}
106 {# Add reasoning content in to content field for unified processing below. #}
107 {%- if message.reasoning_content is defined and message.reasoning_content is string and message.reasoning_content | trim | length > 0 %}
108 {%- set content = "<think>\n" ~ message.reasoning_content ~ "\n</think>\n" ~ (message.content | default('', true)) %}
109 {%- else %}
110 {%- set content = message.content | default('', true) %}
111 {%- if content is string -%}
112 {# Allow downstream logic to to take care of broken thought, only handle coherent reasoning here. #}
113 {%- if '<think>' not in content and '</think>' not in content -%}
114 {%- set content = "<think></think>" ~ content -%}
115 {%- endif -%}
116 {%- else -%}
117 {%- set content = content -%}
118 {%- endif -%}
119 {%- endif %}
120 {%- if message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls | length > 0 %}
121 {# Assistant message has tool calls. #}
122 {{- '<|im_start|>assistant\n' }}
123 {%- set include_content = not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}
124 {%- if content is string and content | trim | length > 0 %}
125 {%- if include_content %}
126 {{- (content | trim) ~ '\n' -}}
127 {%- else %}
128 {%- set c = (content | string) %}
129 {%- if '</think>' in c %}
130 {# Keep only content after the last closing think. Also generation prompt causes this. #}
131 {%- set c = c.split('</think>')[-1] %}
132 {%- elif '<think>' in c %}
133 {# If <think> was opened but never closed, drop the trailing think segment #}
134 {%- set c = c.split('<think>')[0] %}
135 {%- endif %}
136 {%- set c = "<think></think>" ~ c | trim %}
137 {%- if c | length > 0 %}
138 {{- c ~ '\n' -}}
139 {%- endif %}
140 {%- endif %}
141 {%- else %}
142 {{- "<think></think>" -}}
143 {%- endif %}
144 {%- for tool_call in message.tool_calls %}
145 {%- if tool_call.function is defined %}
146 {%- set tool_call = tool_call.function %}
147 {%- endif %}
148 {{- '<tool_call>\n<function=' ~ tool_call.name ~ '>\n' -}}
149 {%- if tool_call.arguments is defined %}
150 {%- for args_name, args_value in tool_call.arguments|items %}
151 {{- '<parameter=' ~ args_name ~ '>\n' -}}
152 {%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
153 {{- args_value ~ '\n</parameter>\n' -}}
154 {%- endfor %}
155 {%- endif %}
156 {{- '</function>\n</tool_call>\n' -}}
157 {%- endfor %}
158 {{- '<|im_end|>\n' }}
159 {%- else %}
160 {# Assistant message doesn't have tool calls. #}
161 {%- if not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}
162 {{- '<|im_start|>assistant\n' ~ (content | default('', true) | string | trim) ~ '<|im_end|>\n' }}
163 {%- else %}
164 {%- set c = (content | default('', true) | string) %}
165 {%- if '<think>' in c and '</think>' in c %}
166 {%- set c = "<think></think>" ~ c.split('</think>')[-1] %}
167 {%- endif %}
168 {%- set c = c | trim %}
169 {%- if c | length > 0 %}
170 {{- '<|im_start|>assistant\n' ~ c ~ '<|im_end|>\n' }}
171 {%- else %}
172 {{- '<|im_start|>assistant\n<|im_end|>\n' }}
173 {%- endif %}
174 {%- endif %}
175 {%- endif %}
176 {%- elif message.role == "user" or message.role == "system" %}
177 {{- '<|im_start|>' + message.role + '\n' }}
178 {%- set content = message.content | string %}
179 {{- content }}
180 {{- '<|im_end|>\n' }}
181 {%- elif message.role == "tool" %}
182 {%- if loop.previtem and loop.previtem.role != "tool" %}
183 {{- '<|im_start|>user\n' }}
184 {%- endif %}
185 {{- '<tool_response>\n' }}
186 {{- message.content }}
187 {{- '\n</tool_response>\n' }}
188 {%- if not loop.last and loop.nextitem.role != "tool" %}
189 {{- '<|im_end|>\n' }}
190 {%- elif loop.last %}
191 {{- '<|im_end|>\n' }}
192 {%- endif %}
193 {%- else %}
194 {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>\n' }}
195 {%- endif %}
196{%- endfor %}
197
198{%- if add_generation_prompt %}
199 {%- if enable_thinking %}
200 {{- '<|im_start|>assistant\n<think>\n' }}
201 {%- else %}
202 {{- '<|im_start|>assistant\n<think></think>' }}
203 {%- endif %}
204{%- endif %}