{%- if not add_generation_prompt is defined %}
    {%- set add_generation_prompt = false %}
{%- endif %}
{%- set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='', is_first_sp=true, is_first_user=true, is_last_user=false) %}
{%- for message in messages %}
    {%- if message['role'] == 'system' %}
        {%- if ns.is_first_sp %}
            {%- set ns.system_prompt = ns.system_prompt + message['content'] %}
            {%- set ns.is_first_sp = false %}
        {%- else %}
            {%- set ns.system_prompt = ns.system_prompt + '
' + message['content'] %}
        {%- endif %}
    {%- endif %}
{%- endfor %}
{{- bos_token }}
{{- ns.system_prompt }}
{%- if tools %}
    {%- if ns.system_prompt != '' %}
        {{- '
# Tools
You may call one or more functions to assist with the user query.' }}
    {%- else %}
        {{- '# Tools
You may call one or more functions to assist with the user query.' }}
    {%- endif %}
    {{- '
You are provided with function signatures within  XML tags:' }}
    {{- '
' }}
    {%- for tool in tools %}
        {%- if loop.index0 > 0 %}
            {{- '
' }}
        {%- endif %}
        {{- tool | tojson }}
    {%- endfor %}
    {{- '
' }}
    {{- 'For function call returns, you should first print ' }}
    {{- 'For each function call, you should return object like:
' }}
    {{- 'function_name
```json
function_arguments_in_json_format
```' }}
    {{- 'At the end of function call returns, you should print ' }}
{%- endif %}
{%- if ns.system_prompt != '' or tools %}
    {{- '<|extra_4|>' }}
{%- endif %}
{%- for message in messages %}
    {%- if message['role'] == 'user' %}
        {%- set ns.is_tool = false %}
        {%- set ns.is_first = false %}
        {%- set ns.is_last_user = true %}
        {%- if ns.is_first_user %}
            {{- message['content'] + '<|extra_0|>' }}
            {%- set ns.is_first_user = false %}
        {%- else %}
            {{- bos_token + message['content'] + '<|extra_0|>' }}
        {%- endif %}
    {%- endif %}
    {%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
        {%- set ns.is_last_user = false %}
        {%- if ns.is_tool %}
            {{- '' + '<|extra_0|>' }}
        {%- endif %}
        {%- set ns.is_first = false %}
        {%- set ns.is_tool = false %}
        {%- set ns.is_output_first = true %}
        {%- for tool in message['tool_calls'] %}
            {%- set arguments = tool['function']['arguments'] %}
            {%- if arguments is not string %}
                {%- set arguments = arguments | tojson %}
            {%- endif %}
            {%- if not ns.is_first %}
                {%- if message['content'] is none %}
                    {{- '' + tool['function']['name'] + '
' + '```json' + '
' + arguments + '
' + '```' + '' }}
                {%- else %}
                    {{- message['content'] + '' + tool['function']['name'] + '
' + '```json' + '
' + arguments + '
' + '```' + '' }}
                {%- endif %}
            {%- set ns.is_first = true %}
            {%- else %}
                {{- '
' + '' + tool['function']['name'] + '
' + '```json' + '
' + arguments + '
' + '```' + '' }}
            {%- endif %}
        {%- endfor %}
        {{- '' + eos_token }}
    {%- endif %}
    {%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}
        {%- set content = message['content'] %}
        {%- if '' in content and not loop.last %}
            {%- set content = content.split('')[-1].strip('').strip() %}
        {%- endif %}
        {%- set ns.is_last_user = false %}
        {%- if ns.is_tool %}
            {{- '' + '<|extra_0|>' + content + eos_token }}
            {%- set ns.is_tool = false %}
        {%- else %}
            {{- content + eos_token }}
        {%- endif %}
    {%- endif %}
    {%- if message['role'] == 'tool' %}
        {%- set ns.is_last_user = false %}
        {%- set ns.is_tool = true %}
        {%- if ns.is_output_first %}
            {{- bos_token + '' + message['content'] + '' }}
            {%- set ns.is_output_first = false %}
        {%- else %}
            {{- '
' + message['content'] + '' }}
        {%- endif %}
    {%- endif %}
{%- endfor %}
{%- if ns.is_tool %}
    {{- '' + '<|extra_0|>' }}
{%- endif %}
{%- if add_generation_prompt and not ns.is_last_user and not ns.is_tool %}
    {{- '<|extra_0|>' }}
{%- endif %}
{%- if enable_thinking is defined and not enable_thinking %}
    {{- '
' }}
{%- endif %}