1{%- if not add_generation_prompt is defined -%}
 2    {%- set add_generation_prompt = false -%}
 3{%- endif -%}
 4{%- set ns = namespace(is_first=false, is_tool_outputs=false, is_output_first=true, system_prompt='') -%}
 5{%- for message in messages -%}
 6    {%- if message['role'] == 'system' -%}
 7        {%- set ns.system_prompt = message['content'] -%}
 8    {%- endif -%}
 9{%- endfor -%}
10{{bos_token}}
11{%- if tools %}
12You can call any of the following function tools to satisfy the user's requests: {{tools | map(attribute='function') | tojson(indent=2)}}
13
14Example function tool call syntax:
15
16<toolcallsbegin><toolcallbegin>function<toolsep>example_function_name
17```json
18{
19  "arg1": "some_value"
20  ...
21}
22```
23<toolcallend><toolcallsend>
24
25{% endif -%}
26{{ns.system_prompt}}
27{%- macro flush_tool_outputs() -%}
28    {%- if ns.is_tool_outputs -%}
29        {{- '<|tool▁outputs▁end|><|end▁of▁sentence|>' -}}
30        {%- set ns.is_tool_outputs = false -%}
31    {%- endif -%}
32{%- endmacro -%}
33{{- flush_tool_outputs() -}}
34{%- for message in messages -%}
35    {%- if message['role'] != 'tool' -%}
36        {{- flush_tool_outputs() -}}
37    {%- endif -%}
38    {%- if message['role'] == 'user' -%}
39        {{- '<|User|>' + message['content'] + '<|end▁of▁sentence|>' -}}
40    {%- endif -%}
41    {%- if message['role'] == 'assistant' and not message['content'] -%}
42        {{- '<|Assistant|><|tool▁calls▁begin|>' -}}
43        {%- set ns.is_first = true -%}
44        {%- for tc in message['tool_calls'] -%}
45            {%- if ns.is_first -%}
46                {%- set ns.is_first = false -%}
47            {%- else -%}
48                {{- '\n' -}}
49            {%- endif -%}
50            {%- set tool_name = tc['function']['name'] -%}
51            {%- set tool_args = tc['function']['arguments'] -%}
52            {{- '<|tool▁call▁begin|>' + tc['type'] + '<|tool▁sep|>' + tool_name + '\n' + '```json' + '\n' + tool_args + '\n' + '```' + '<|tool▁call▁end|>' -}}
53        {%- endfor -%}
54        {{- '<|tool▁calls▁end|><|end▁of▁sentence|>' -}}
55    {%- endif -%}
56    {%- if message['role'] == 'assistant' and message['content'] -%}
57        {{- flush_tool_outputs() -}}
58        {%- set content = message['content'] -%}
59        {%- if '</think>' in content -%}
60            {%- set content = content.split('</think>')[-1] -%}
61        {%- endif -%}
62        {{- '<|Assistant|>' + content + '<|end▁of▁sentence|>' -}}
63    {%- endif -%}
64    {%- if message['role'] == 'tool' -%}
65        {%- set ns.is_tool_outputs = true -%}
66        {%- if ns.is_output_first -%}
67            {{- '<|tool▁outputs▁begin|>' -}}
68            {%- set ns.is_output_first = false -%}
69        {%- endif -%}
70        {{- '\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>' -}}
71    {%- endif -%}
72{%- endfor -%}
73{{- flush_tool_outputs() -}}
74{%- if add_generation_prompt and not ns.is_tool_outputs -%}
75    {{- '<|Assistant|><think>\n' -}}
76{%- endif -%}