1{%- set ns = namespace(enable_thinking=true) -%}
  2{%- for message in messages -%}
  3  {%- set content = message['content'] -%}
  4  {%- if message['role'] == 'user' or message['role'] == 'system' -%}
  5    {%- if '/think' in content -%}
  6      {%- set ns.enable_thinking = true -%}
  7    {%- elif '/no_think' in content -%}
  8      {%- set ns.enable_thinking = false -%}
  9    {%- endif -%}
 10  {%- endif -%}
 11{%- endfor -%}
 12
 13{%- if messages[0]['role'] != 'system' -%}
 14  {%- set ns.non_tool_system_content = '' -%}
 15  {{- '<SPECIAL_10>System
 16' -}}
 17{%- else -%}
 18  {%- set ns.non_tool_system_content = (messages[0]['content'] | default('', true)).replace('/think', '').replace('/no_think', '').strip() -%}
 19  {{- '<SPECIAL_10>System
 20' + ns.non_tool_system_content }}
 21{%- endif -%}
 22
 23{%- if tools -%}
 24  {%- if ns.non_tool_system_content is defined and ns.non_tool_system_content != '' -%}
 25    {{- '
 26
 27' -}}
 28  {%- endif -%}
 29  {{- 'You can use the following tools to assist the user if required:' -}}
 30  {{- '
 31<AVAILABLE_TOOLS>[' -}}
 32  {%- for tool in tools -%}
 33    {{- (tool.function if tool.function is defined else tool) | tojson -}}
 34    {{- ', ' if not loop.last else '' -}}
 35  {%- endfor -%}
 36  {{- ']</AVAILABLE_TOOLS>
 37
 38' -}}
 39  {{- 'If you decide to call any tool(s), use the following format:
 40' -}}
 41  {{- '<TOOLCALL>[{{"name": "tool_name1", "arguments": "tool_args1"}}, ' -}}
 42  {{- '{{"name": "tool_name2", "arguments": "tool_args2"}}]</TOOLCALL>
 43
 44' -}}
 45  {{- 'The user will execute tool-calls and return responses from tool(s) in this format:
 46' -}}
 47  {{- '<TOOL_RESPONSE>[{{"tool_response1"}}, {{"tool_response2"}}]</TOOL_RESPONSE>
 48
 49' -}}
 50  {{- 'Based on the tool responses, you can call additional tools if needed, correct tool calls if any errors are found, or just respond to the user.' -}}
 51{%- endif -%}
 52{{- '
 53
 54' -}}
 55{%- set messages = messages[1:] if messages[0]['role'] == 'system' else messages -%}
 56{%- if messages[-1]['role'] == 'assistant' -%}
 57  {%- set ns.last_turn_assistant_content = (messages[-1]['content'] | default('', true)).strip() -%}
 58  {%- set ns.last_turn_assistant_tool_calls = messages[-1]['tool_calls'] if 'tool_calls' in messages[-1] else [] -%}
 59  {%- set messages = messages[:-1] -%}
 60{%- endif -%}
 61
 62{%- for message in messages %}
 63  {%- set content = message['content'] %}
 64  {%- if message['role'] == 'user' -%}
 65    {{- '<SPECIAL_11>User
 66' + (content | default('', true)).replace('/think', '').replace('/no_think', '').strip() + '
 67' }}
 68  {%- elif message['role'] == 'tool' -%}
 69    {%- if loop.first or (messages[loop.index0 - 1].role != 'tool') -%}
 70      {{- '<SPECIAL_11>User
 71' + '<TOOL_RESPONSE>[' }}
 72    {%- endif -%}
 73    {{- message['content'] -}}
 74    {{- ', ' if not loop.last and (messages[loop.index0 + 1].role == 'tool') else '' -}}
 75    {%- if loop.last or (messages[loop.index0 + 1].role != 'tool') -%}
 76      {{- ']</TOOL_RESPONSE>' -}}
 77    {%- endif -%}
 78  {%- elif message['role'] == 'assistant' -%}
 79    {%- if content and '</think>' in content -%}
 80      {%- set content = (content.split('</think>')[1] | default('', true)).strip() %}
 81    {%- endif -%}
 82    {{- '<SPECIAL_11>Assistant
 83' + ((content | default('', true)).strip() if content is not none else '') }}
 84    {%- if message.tool_calls -%}
 85      {%- if (content | default('', true)).strip() != '' -%}
 86        {{- '
 87' -}}
 88      {%- endif -%}
 89      {{- '<TOOLCALL>[' -}}
 90      {%- for call in message.tool_calls -%}
 91        {%- set fn = call.function if call.function is defined else call -%}
 92        {{- '{"name": "' + fn.name + '", "arguments": ' -}}
 93        {%- if fn.arguments is string -%}
 94          {{- fn.arguments -}}
 95        {%- else -%}
 96          {{- fn.arguments | tojson -}}
 97        {%- endif -%}
 98        {{- '}' + (', ' if not loop.last else '') -}}
 99      {%- endfor -%}
100      {{- ']</TOOLCALL>' -}}
101    {%- endif -%}
102    {{- '
103<SPECIAL_12>
104' -}}
105  {%- endif -%}
106{%- endfor -%}
107
108{%- if add_generation_prompt -%}
109  {{- '<SPECIAL_11>Assistant
110' -}}
111  {%- if ns.enable_thinking is defined and ns.enable_thinking is false -%}
112    {{- '<think></think>' -}}
113  {%- else -%}
114    {{- '<think>
115' -}}
116  {%- endif -%}
117  {%- if ns.last_turn_assistant_content is defined and ns.last_turn_assistant_content != '' -%}
118    {{- ns.last_turn_assistant_content -}}
119  {%- endif -%}
120{%- else -%}
121  {%- if ns.last_turn_assistant_content is defined and ns.last_turn_assistant_content != '' -%}
122    {{- '<SPECIAL_11>Assistant
123' -}}
124    {%- if ns.enable_thinking is defined and ns.enable_thinking is false -%}
125      {{- '<think></think>' -}}
126    {%- else -%}
127      {{- '<think>
128' -}}
129    {%- endif -%}
130    {{- ns.last_turn_assistant_content -}}
131    {%- if continue_final_message is defined -%}
132      {%- if continue_final_message is false -%}
133        {{- '
134<SPECIAL_12>
135' -}}
136      {%- endif -%}
137    {%- else -%}
138      {{- '
139<SPECIAL_12>
140' -}}
141    {%- endif -%}
142  {%- endif -%}
143  {%- if ns.last_turn_assistant_tool_calls is defined and ns.last_turn_assistant_tool_calls | length > 0 -%}
144    {{- '<SPECIAL_11>Assistant
145' -}}
146    {{- '<TOOLCALL>[' -}}
147    {%- for call in ns.last_turn_assistant_tool_calls -%}
148      {%- set fn = call.function if call.function is defined else call -%}
149      {{- '{"name": "' + fn.name + '", "arguments": ' -}}
150      {%- if fn.arguments is string -%}
151        {{- fn.arguments -}}
152      {%- else -%}
153        {{- fn.arguments | tojson -}}
154      {%- endif -%}
155      {{- '}' + (', ' if not loop.last else '') -}}
156    {%- endfor -%}
157    {{- ']</TOOLCALL>' -}}
158    {{- '<SPECIAL_12>
159
160' -}}
161 {%- endif -%}
162{%- endif -%}