{#- Iteration on laguna_glm_thinking_v5/chat_template.jinja -#}
{#- Adds a default system message (used when no system message is provided in `messages`). -#}
{{- "〈|EOS|〉" -}}
{%- set enable_thinking = enable_thinking | default(false) -%}
{%- set render_assistant_messages_raw = render_assistant_messages_raw | default(false) -%}
{%- set add_generation_prompt = add_generation_prompt | default(false) -%}
{#- out6k-style quality rules -#}
{%- set numeric_rule = 'If and only if your output is JSON: numeric field values (price, amount, quantity, qty, unit_price, subtotal, tax, tax_amount, tax_rate, total, total_due, rate, count, discount) must be bare JSON numbers with NO quotes, currency symbol, percent sign, units, or thousands separators (e.g. 4.75 not "$4.75", 8.625 not "8.625%"). For plain-text (non-JSON) answers, keep units and formatting exactly as requested.' %}
{%- set tool_avoidance_rule = 'Use a tool ONLY when it is necessary to fulfill the user request. If you can answer directly from your own knowledge, do so without calling a tool. Never call calculator for simple arithmetic you can do yourself.' %}
{%- set constraint_priority_rule = 'When the user asks for exactly N items and also requires at least one item matching a condition, satisfy both constraints by reserving one of the N slots for a matching item, even if that item would not appear in a simple top-N sort.' %}
{#- ───── header (system message) ───── -#}
{%- set system_message = "You are a helpful, conversationally-fluent assistant made by Poolside. You are here to be helpful to users through natural language conversations." -%}
{%- if messages and messages[0].role == "system" -%}
{%- set system_message = messages[0].content -%}
{%- endif -%}
{%- if (system_message and system_message.strip()) or tools -%}
{{- "\n" -}}
{%- if system_message and system_message.strip() -%}
{{- "\n" -}}
{{- system_message.rstrip() -}}
{%- endif -%}
{#- Inject out6k quality rules -#}
{{- "\n\n" ~ numeric_rule -}}
{%- if messages[0].role == 'system' and messages[0].content is string and 'Follow the user' in messages[0].content and 'instructions precisely' in messages[0].content %}
{{- "\n\n" ~ constraint_priority_rule -}}
{%- endif %}
{%- if tools and not (messages[0].role == 'system' and messages[0].content is string and 'Use a tool ONLY when it is necessary' in messages[0].content) %}
{{- "\n\n" ~ tool_avoidance_rule -}}
{%- endif %}
{%- if tools -%}
{{- "\n\n### Tools\n\n" -}}
{%- set ns = namespace(tool_string="You may call functions to assist with the user query.\n"
~ "All available function signatures are listed below:\n"
~ "\n") -%}
{%- for tool in tools -%}
{%- set ns.tool_string = ns.tool_string ~ (tool | tojson) ~ "\n" -%}
{%- endfor -%}
{%- if enable_thinking -%}
{%- set tool_string = ns.tool_string + "\n\n" ~
"Wrap your thinking in '', '' tags, followed by a function call. For each function call, return an unescaped XML-like object with function name and arguments within '' and '' tags, like here:\n" ~
" your thoughts here \n" ~
"function-name\nargument-key\nvalue-of-argument-key\n" ~
"" -%}
{%- else -%}
{%- set tool_string = ns.tool_string + "\n\n" ~
"For each function call, return an unescaped XML-like object " ~
"with function name and arguments within '' and '' tags, like here:\n" ~
"function-name\nargument-key\nvalue-of-argument-key\n" ~
"" -%}
{%- endif -%}
{{- tool_string -}}
{%- endif -%}
{{- "\n\n" -}}
{%- endif -%}
{#- ───── main loop ───── -#}
{%- for message in messages -%}
{%- set content = message.content if message.content is string else "" -%}
{%- if message.role == "user" -%}
{{- "\n" + content + "\n\n" -}}
{%- elif message.role == "assistant" -%}
{%- generation -%}
{{- "\n" -}}
{%- if render_assistant_messages_raw -%}
{#- Raw mode: prepend the generation prompt token, then dump content verbatim. -#}
{#- The generation prompt is when enable_thinking, otherwise. -#}
{#- Only prepend if content doesn't already start with it. -#}
{%- if enable_thinking -%}
{%- if not content.startswith('') -%}
{{- '' -}}
{%- endif -%}
{%- else -%}
{%- if not content.startswith('') -%}
{{- '' -}}
{%- endif -%}
{%- endif -%}
{{- content -}}
{#- Append closing tag if content doesn't already end with it. -#}
{%- if not content.endswith('\n') and not content.endswith('') -%}
{{- '\n' -}}
{%- endif -%}
{{- "\n" -}}
{%- else -%}
{#- Extract reasoning content from message.reasoning (vLLM field name) or message.reasoning_content, or from tags -#}
{%- set reasoning_content = '' %}
{%- if message.reasoning is string %}
{%- set reasoning_content = message.reasoning %}
{%- elif message.reasoning_content is string %}
{%- set reasoning_content = message.reasoning_content %}
{%- endif %}
{#- Always strip tags from content if present to avoid duplication -#}
{%- if '' in content %}
{%- if not reasoning_content %}
{%- set reasoning_content = content.split('')[0].rstrip('\n').split('')[-1].lstrip('\n') %}
{%- endif %}
{%- set content = content.split('')[-1].lstrip('\n') %}
{%- endif %}
{#- Display reasoning content for all messages -#}
{%- if reasoning_content -%}
{{- '\n' + reasoning_content.strip() + '\n\n' -}}
{%- else -%}
{{- '\n' -}}
{%- endif -%}
{#- Display main content -#}
{%- if content.strip() -%}
{{- content.strip() ~ "\n" -}}
{%- endif -%}
{%- if message.tool_calls -%}
{%- for tool_call in message.tool_calls -%}
{%- set function_data = tool_call.function -%}
{{- '' + function_data.name }}
{% set _args = function_data.arguments %}
{%- for k, v in _args.items() -%}
{{- "" ~ k ~ "\n" -}}
{{- ""}}{{ v | tojson(ensure_ascii=False) if v is not string else v }}{{ "\n" -}}
{%- endfor -%}
{{- "\n" -}}
{%- endfor -%}
{%- endif -%}
{{- "\n" -}}
{%- endif -%}
{%- endgeneration -%}
{%- elif message.role == "tool" -%}
{{- "\n" + content + "\n\n" -}}
{%- elif message.role == "system" and loop.index0 != 0 -%}
{#- Render additional system messages (skip the first one which is handled separately in the header) -#}
{{- "\n" + content + "\n\n" -}}
{%- endif -%}
{%- endfor -%}
{#- ───── generation prompt ───── -#}
{%- if add_generation_prompt -%}
{{- "\n" -}}
{#- ───── Include reasoning mode directive ───── -#}
{%- if not enable_thinking %}
{{- '' -}}
{%- else %}
{{- '' -}}
{%- endif %}
{%- endif -%}