{#- Bailing V3 chat template -#}
{#- Supports: thinking option, tool calling -#}
{#- ==================== thinking option normalization ==================== -#}
{%- if enable_thinking is defined %}
{%- if enable_thinking %}
{%- set thinking_option = 'on' %}
{%- else %}
{%- set thinking_option = 'off' %}
{%- endif %}
{%- elif thinking_option is not defined %}
{%- set thinking_option = 'on' %}
{%- endif %}
{#- ==================== preserved thinking ==================== -#}
{% set preserved_thinking = true %}
{#- ==================== system message ==================== -#}
{{- 'SYSTEM' }}
{%- if tools %}
{%- if messages[0].role == 'system' %}
{{- messages[0].content + '\n' }}
{%- endif %}
{{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within XML tags:\n" }}
{%- for tool in tools %}
{{- "\n" }}
{{- tool | tojson }}
{%- endfor %}
{{- "\n\n\nIf none of the functions can be used, point it out. If the given question lacks the parameters required by the function, also point it out.\nIf you need to use a function, for each function call, output the function name and arguments within the following XML format:\n{function-name}\n{arg-key-1}\n{arg-value-1}\n{arg-key-2}\n{arg-value-2}\n...\n\n" }}
{%- if messages[0].role == 'system' and messages[0].content is string and ('detailed thinking on' in messages[0].content or 'detailed thinking off' in messages[0].content) %}
{{- '<|role_end|>' }}
{%- else %}
{{- 'detailed thinking ' + thinking_option + '<|role_end|>' }}
{%- endif %}
{%- else %}
{%- if messages[0].role == 'system' %}
{%- if 'detailed thinking on' in messages[0].content or 'detailed thinking off' in messages[0].content %}
{{- messages[0].content + '<|role_end|>' }}
{%- else %}
{{- messages[0].content + '\n' }}
{{- 'detailed thinking ' + thinking_option + '<|role_end|>' }}
{%- endif %}
{% else %}
{{- 'detailed thinking ' + thinking_option + '<|role_end|>' }}
{%- endif %}
{%- endif %}
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
{%- for message in messages[::-1] %}
{%- set index = (messages|length - 1) - loop.index0 %}
{%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('') and message.content.endswith('')) %}
{%- set ns.multi_step_tool = false %}
{%- set ns.last_query_index = index %}
{%- endif %}
{%- endfor %}
{%- for message in messages %}
{%- if message.content is string %}
{%- set content = message.content %}
{%- else %}
{%- set content = '' %}
{%- endif %}
{%- if message.role == "user" %}
{{- 'HUMAN' + message.content + '<|role_end|>' }}
{%- elif message.role == "system" and not loop.first %}
{{- 'SYSTEM' + message.content + '<|role_end|>' }}
{%- elif message.role == "assistant" %}
{%- set reasoning_content = '' %}
{%- if message.reasoning_content is string and message.reasoning_content != '' %}
{%- set reasoning_content = message.reasoning_content %}
{%- else %}
{%- if '' in content %}
{%- set reasoning_content = content.split('')[0].rstrip('\n').split('')[-1].lstrip('\n') %}
{%- set content = content.split('')[-1].lstrip('\n') %}
{%- endif %}
{%- endif %}
{%- if preserved_thinking or loop.index0 > ns.last_query_index %}
{%- if reasoning_content != '' %}
{{- 'ASSISTANT' + '\n' + reasoning_content.strip('\n') + '' + content.lstrip('\n') }}
{%- else %}
{{- 'ASSISTANT\n' + content }}
{%- endif %}
{%- else %}
{{- 'ASSISTANT\n' + content }}
{%- endif %}
{%- if message.tool_calls %}
{%- for tool_call in message.tool_calls %}
{%- if (loop.first and content) or (not loop.first) %}
{{- '\n' }}
{%- endif %}
{%- set tc = tool_call %}
{%- if tool_call.function %}
{%- set tc = tool_call.function %}
{%- endif %}
{{- '' + tc.name }}
{% set _args = tc.arguments %}
{%- for k, v in _args.items() %}
{{- '' + k + '' }}
{{- '\n' }}
{%- if v is string %}
{{- v }}
{%- else %}
{{- v | tojson(ensure_ascii=False) }}
{%- endif %}
{{- '' }}
{%- endfor %}
{{- '\n' }}
{%- endfor %}
{%- endif %}
{{- '<|role_end|>' }}
{%- elif message.role == "tool" %}
{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
{{- 'OBSERVATION' }}
{%- endif %}
{{- '\n\n' }}
{{- content }}
{{- '\n' }}
{%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
{{- '<|role_end|>' }}
{%- endif %}
{%- endif %}
{%- endfor %}
{#- ==================== generation prompt ==================== -#}
{%- if add_generation_prompt %}
{{- 'ASSISTANT' }}
{%- if thinking_option == 'on' %}
{{- '\n' }}
{%- elif thinking_option == 'off' %}
{{- '\n' }}
{%- endif %}
{%- endif %}