1{#- ======== Template Parameters ========  #}
  2{%- set add_generation_prompt = add_generation_prompt if add_generation_prompt is defined else true %}
  3{%- set default_system_prompt = default_system_prompt if default_system_prompt is defined else true %}
  4{%- set reasoning_effort = reasoning_effort if reasoning_effort is defined else "high" %}
  5{%- set think_render_option = think_render_option if think_render_option is defined else "lastthink" %}
  6
  7{#- ======== System Block State ========  #}
  8{%- set sys_ns = namespace(is_first_block=true) -%}
  9
 10{#- ======== Find last user message index ========  #}
 11{%- set last_user_idx = namespace(value=-1) -%}
 12{%- for message in messages -%}
 13    {%- if message.role == 'user' -%}
 14        {%- set last_user_idx.value = loop.index0 -%}
 15    {%- endif -%}
 16{%- endfor -%}
 17
 18{#- ======== System messages renderers ========  #}
 19{%- macro render_system_message(user_system_messages) %}
 20    {%- if default_system_prompt %}
 21        {%- if not sys_ns.is_first_block %}{{- "\n\n" }}{%- endif %}
 22        {%- set sys_ns.is_first_block = false %}
 23        {{- "## Provider System Prompt\n\nYou are Solar Open 100B, a large language model trained by Upstage AI, a Korean startup. Your knowledge cutoff is 2025-07. The current date is " + strftime_now("%Y-%m-%d") + "." }}
 24    {%- endif -%}
 25    {%- if user_system_messages %}
 26        {%- if not sys_ns.is_first_block %}{{- "\n\n" }}{%- endif %}
 27        {%- set sys_ns.is_first_block = false %}
 28        {{- "## System Prompt" }}
 29        {%- for system_message in user_system_messages %}
 30            {{- "\n\n" }}
 31            {{- system_message }}
 32        {%- endfor %}
 33    {%- endif -%}
 34{%- endmacro %}
 35
 36{%- macro render_tool_instruction(tools) %}
 37    {%- if not sys_ns.is_first_block %}{{- "\n\n" }}{%- endif %}
 38    {%- set sys_ns.is_first_block = false %}
 39    {{- "## Tools\n\n### Tool Call Instruction" }}
 40    {{- "\nYou may invoke one or more tools to assist with the user's query. Available tools are provided in JSON Schema format: <|tools:begin|><|tool:begin|><tools-json-object><|tool:end|>...<|tools:end|>\n" }}
 41    {{- "\n### Available Tools\n" }}
 42    {{- "<|tools:begin|>" }}
 43    {%- for tool in tools %}
 44        {{- "<|tool:begin|>" }}
 45        {{- tool.function | tojson }}
 46        {{- "<|tool:end|>" }}
 47    {%- endfor %}
 48    {{- "<|tools:end|>\n" }}
 49    {{- "\n### Tool Call Format\n" }}
 50    {{- "For each tool call, return a JSON object with the following structure, enclosed within <|tool_call:begin|> and <|tool_call:end|> tags: \n<|tool_call:begin|><tool-call-id><|tool_call:name|><tool-name><|tool_call:args|><args-json-object><|tool_call:end|>\n" }}
 51    {{- "- The <tool-call-id> must be a randomly generated string consisting of 10 lowercase letters (a-z) and/or digits (0-9) (e.g., a1b2c3d4e5)\n" }}
 52    {{- "\n### Tool Response Format\n" }}
 53    {{- "Each tool is responded by `tool` with the following structure:\n<|tool_response:id|><tool-call-id><|tool_response:name|><tool-name><|tool_response:result|><results><|tool_response:end|>\n" }}
 54    {{- "- Ensure the <tool-call-id> matches the corresponding tool call" -}}
 55{%- endmacro %}
 56
 57{%- macro render_json_response_format_instruction(response_format) %}
 58    {%- if not sys_ns.is_first_block %}{{- "\n\n" }}{%- endif %}
 59    {%- set sys_ns.is_first_block = false %}
 60    {{- "## Output Format Constraint" }}
 61    {{- "\n\nYour final response should follow the JSON schema: \n[Start of schema]" }}
 62    {{- response_format }}
 63    {{- "\n[End of schema]\nPlease ensure your answers adhere to this format and do not contain any unnecessary text." }}
 64{%- endmacro %}
 65
 66{%- macro get_tool_name(messages, tool_call_id) %}
 67    {%- for msg in messages -%}
 68        {%- if msg.role == 'assistant' and msg.tool_calls -%}
 69            {%- for tool_call in msg.tool_calls -%}
 70                {%- if tool_call.id == tool_call_id -%}
 71                    {{- tool_call.function.name }}
 72                {%- endif -%}
 73            {%- endfor -%}
 74        {%- endif -%}
 75    {%- endfor -%}
 76{%- endmacro %}
 77
 78{%- macro render_tool_arguments(tool_arguments) %}
 79    {%- if tool_arguments is mapping -%}
 80        {{- tool_arguments | tojson }}
 81    {%- else -%}
 82        {{- tool_arguments }}
 83    {%- endif -%}
 84{%- endmacro %}
 85
 86{#- ======== Render system message ========  #}
 87{%- set ns = namespace(system_messages=[]) -%}
 88{%- for message in messages -%}
 89    {%- if message.role == 'system' -%}
 90        {%- set ns.system_messages = ns.system_messages + [message.content] -%}
 91    {%- endif -%}
 92{%- endfor -%}
 93
 94{%- if ns.system_messages or default_system_prompt or tools or response_format -%}
 95    {{- "<|begin|>system<|content|>" }}
 96        {{- render_system_message(ns.system_messages) }}
 97        {%- if tools -%}
 98            {{- render_tool_instruction(tools) }}
 99        {%- endif %}
100        {%- if response_format -%}
101            {{- render_json_response_format_instruction(response_format) }}
102        {%- endif %}
103    {{- "<|end|>" }}
104{%- endif -%}
105
106{#- ======== Render main messages ========  #}
107{%- for message in messages -%}
108    {%- if message.role == 'user' -%}
109         {{- "<|begin|>user<|content|>" + message.content + "<|end|>" }}
110    {%- elif message.role == 'tool' -%}
111        {%- set prev_is_tool = loop.index0 > 0 and messages[loop.index0 - 1].role == 'tool' -%}
112        {%- set next_is_tool = loop.index0 < (messages | length - 1) and messages[loop.index0 + 1].role == 'tool' -%}
113        {%- if not prev_is_tool -%}
114            {{- "<|begin|>tool<|tool_response|>" }}
115        {%- endif -%}
116        {{- "<|tool_response:begin|>" + message.tool_call_id + "<|tool_response:name|>" }}
117        {{- get_tool_name(messages, message.tool_call_id) }}
118        {{- "<|tool_response:result|>" }}
119        {{- message.content }}
120        {{- "<|tool_response:end|>" }}
121        {%- if not next_is_tool -%}
122            {{- "<|end|>" }}
123        {%- endif -%}
124    {%- elif message.role == 'assistant' -%}
125        {#- ======== Assistant Thinking ========  #}
126        {%- if think_render_option == "all" -%}
127            {%- if message.reasoning -%}
128                {{- "<|begin|>assistant<|think|>" + message.reasoning + "<|end|>" }}
129            {%- endif -%}
130        {%- elif think_render_option == "lastthink" -%}
131            {%- if message.reasoning and loop.index0 > last_user_idx.value -%}
132                {{- "<|begin|>assistant<|think|>" + message.reasoning + "<|end|>" }}
133            {%- endif -%}
134        {%- endif -%}
135
136        {#- ======== Assistant Messages ========  #}
137        {%- if message.tool_calls -%}
138            {{- "<|begin|>assistant<|tool_calls|>" }}
139            {%- for tool_call in message.tool_calls -%}
140                {{- "<|tool_call:begin|>" + tool_call.id +"<|tool_call:name|>" + tool_call.function.name + "<|tool_call:args|>" }}
141                {{- render_tool_arguments(tool_call.function.arguments) }}
142                {{- "<|tool_call:end|>" }}
143            {%- endfor -%}
144            {{- "<|calls|>" }}
145        {%- else -%}
146            {{- "<|begin|>assistant<|content|>" + message.content + "<|end|>" }}
147        {%- endif -%}
148    {%- endif -%}
149{%- endfor -%}
150
151{%- if add_generation_prompt -%}
152    {%- if reasoning_effort in ["low", "minimal"] -%}
153        {{- "<|begin|>assistant<|think|><|end|>" }}
154    {%- endif -%}
155    {{- "<|begin|>assistant" }}
156{%- endif -%}