"chattemplate": "{%- if tools %}\n {{- '< imstart >system\\n' }}\n {%- if…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
"chat_template": "{%- if tools %}\n {{- '<|im_start|>system\n' }}\n {%- if messages[0].role == 'system' %}\n {{- messages[0].content + '\n\n' }}\n {%- endif %}\n {{- "# 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" }}\n {%- for tool in tools %}\n {{- "\n" }}\n {{- tool | tojson }}\n {%- endfor %}\n {{- "\n\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": , \"arguments\": }\n</tool_call><|im_end|>\n" }}\n{%- else %}\n {%- if messages[0].role == 'system' %}\n {{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}\n {%- endif %}\n{%- endif %}\n{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}\n{%- for message in messages[::-1] %}\n {%- set index = (messages|length - 1) - loop.index0 %}\n {%- if ns.multi_step_tool and message.role == "user" and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}\n {%- set ns.multi_step_tool = false %}\n {%- set ns.last_query_index = index %}\n {%- endif %}\n{%- endfor %}\n{%- for message in messages %}\n {%- if (message.role == "user") or (message.role == "system" and not loop.first) %}\n {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}\n {%- elif message.role == "assistant" %}\n {%- set content = message.content %}\n {%- set reasoning_content = '' %}\n {%- if message.reasoning_content is defined and message.reasoning_content is not none %}\n {%- set reasoning_content = message.reasoning_content %}\n {%- else %}\n {%- if '' in message.content %}\n {%- set content = message.content.split('')[-1].lstrip('\n') %}\n {%- set reasoning_content = message.content.split('')[0].rstrip('\n').split('')[-1].lstrip('\n') %}\n {%- endif %}\n {%- endif %}\n {%- if loop.index0 > ns.last_query_index %}\n {%- if loop.last or (not loop.last and reasoning_content) %}\n {{- '<|im_start|>' + message.role + '\n\n' + reasoning_content.strip('\n') + '\n\n\n' + content.lstrip('\n') }}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\n' + content }}\n {%- endif %}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\n' + content }}\n {%- endif %}\n {%- if message.tool_calls %}\n {%- for tool_call in message.tool_calls %}\n {%- if (loop.first and content) or (not loop.first) %}\n {{- '\n' }}\n {%- endif %}\n {%- if tool_call.function %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- '<tool_call>\n{"name": "' }}\n {{- tool_call.name }}\n {{- '", "arguments": ' }}\n {%- if tool_call.arguments is string %}\n {{- tool_call.arguments }}\n {%- else %}\n {{- tool_call.arguments | tojson }}\n {%- endif %}\n {{- '}\n</tool_call>' }}\n {%- endfor %}\n {%- endif %}\n {{- '<|im_end|>\n' }}\n {%- elif message.role == "tool" %}\n {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}\n {{- '<|im_start|>user' }}\n {%- endif %}\n {{- '\n<tool_response>\n' }}\n {{- message.content }}\n {{- '\n</tool_response>' }}\n {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}\n {{- '<|im_end|>\n' }}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\n' }}\n {%- if enable_thinking is defined and enable_thinking is false %}\n {{- '\n\n\n\n' }}\n {%- endif %}\n{%- endif %}", explain this
This is a Jinja2 template designed for formatting chat conversations, likely for use with a large language model (LLM) that can interact with tools. Let's break it down section by section:
1. Tool Handling (Top if tools block):
{%- if tools %}
{{- '<|im_start|>system\\n' }}
{%- if messages[0].role == 'system' %}
{{- messages[0].content + '\\n\\n' }}
{%- endif %}\n
{{- "# Tools\\n\\nYou may call one or more functions to assist with the user query.\\n\\nYou are provided with function signatures within <tools></tools> XML tags:\\n<tools>" }}
{%- for tool in tools %}
{{- "\\n" }}
{{- tool | tojson }}
{%- endfor %}
{{- "\\n</tools>\\n\\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\\n<tool_call>\\n{\\\"name\\\": <function-name>, \\\"arguments\\\": <args-json-object>}\\n</tool_call><|im_end|>\\n" }}
{%- else %}
{%- if messages[0].role == 'system' %}
{{- '<|im_start|>system\\n' + messages[0].content + '<|im_end|>\\n' }}
{%- endif %}
{%- endif %}
{%- if tools %}: Checks if a list of tools is provided to the template. This is crucial for scenarios where the LLM is expected to use external functions.{{- '<|im_start|>system\\n' }}: Starts a "system" message, likely indicating the beginning of the conversation and providing instructions. The im_start and im_end tags are specific delimiters to isolate turns.{%- if messages[0].role == 'system' %}: Checks if the first message in the conversation history (messages[0]) is a system message. This ensures the initial system message is correctly placed.{{- messages[0].content + '\\n\\n' }}: Includes the content of the initial system message if it exists.{{- "# Tools\\n\\nYou may call one or more functions..." }}: This is a hardcoded instruction set. It instructs the LLM about how to use the provided tools. It outlines the process of function calling, requiring the LLM to return a JSON object within <tool_call> XML tags, including the function name and arguments.{%- for tool in tools %}: Iterates through the list of tools.{{- tool | tojson }}: Converts each tool (presumably a dictionary or object) to its JSON representation and includes it within the <tools> XML tags. This provides the LLM with the function signatures.{{- "\\n</tools>\\n\\nFor each function call..." }}: Finishes the tools block with instructions on how to use the tools.{%- else %}: If no tools are provided.{%- if messages[0].role == 'system' %}: If no tools are provided, but the first message is a system message.{{- '<|im_start|>system\\n' + messages[0].content + '<|im_end|>\\n' }}: Displays the system message and encloses it within the specific delimiters.2. Multi-Step Tool Handling and Last Query Index (Namespace):
{%- 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 not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}
{%- set ns.multi_step_tool = false %}
{%- set ns.last_query_index = index %}
{%- endif %}
{%- endfor %}
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}: Initializes a Jinja2 namespace called ns. Namespaces are used to store variables that can be accessed and modified within the template.
multi_step_tool: A boolean flag, initially set to true. It seems to track whether the current interaction involves multiple tool calls within a single user query.last_query_index: An integer representing the index of the last "true" user query (i.e., a query that isn't just a tool response). It's initially set to the last message in the messages list.{%- for message in messages[::-1] %}: Iterates through the messages list in reverse order.{%- set index = (messages|length - 1) - loop.index0 %}: Calculates the correct index of the current message, given that we are iterating in reverse order.{%- if ns.multi_step_tool and message.role == "user" and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}: This is the core logic for determining the last_query_index. It checks the following:
ns.multi_step_tool: Is the multi_step_tool flag still set to true?message.role == "user": Is the current message from the user?not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')): Is the user message not a tool response (i.e., is it a genuine query)?{%- set ns.multi_step_tool = false %}: If the conditions in the if statement are met, it means we've found the last "real" user query. So, we set multi_step_tool to false because we've identified the end of a potential multi-step tool interaction.{%- set ns.last_query_index = index %}: Update last_query_index with the index of this last "real" user query.In essence, this section tries to determine which part of the conversation should have a <think> block. The <think> block seems like a custom format to include a reasoning output for the LLM. It avoids adding <think> blocks to turns within a tool using flow, and it avoids displaying <think> blocks if the very last message is from the assistant.
3. Message Formatting (Main Loop):
{%- for message in messages %}
{%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
{{- '<|im_start|>' + message.role + '\\n' + message.content + '<|im_end|>' + '\\n' }}
{%- elif message.role == "assistant" %}
{%- set content = message.content %}\n
{%- set reasoning_content = '' %}\n
{%- if message.reasoning_content is defined and message.reasoning_content is not none %}\n
{%- set reasoning_content = message.reasoning_content %}\n
{%- else %}
{%- if '</think>' in message.content %}
{%- set content = message.content.split('</think>')[-1].lstrip('\\n') %}
{%- set reasoning_content = message.content.split('</think>')[0].rstrip('\\n').split('<think>')[-1].lstrip('\\n') %}
{%- endif %}
{%- endif %}\n
{%- if loop.index0 > ns.last_query_index %}
{%- if loop.last or (not loop.last and reasoning_content) %}
{{- '<|im_start|>' + message.role + '\\n<think>\\n' + reasoning_content.strip('\\n') + '\\n</think>\\n\\n' + content.lstrip('\\n') }}
{%- else %}
{{- '<|im_start|>' + message.role + '\\n' + content }}\n
{%- endif %}
{%- else %}
{{- '<|im_start|>' + message.role + '\\n' + content }}\n
{%- endif %}
{%- if message.tool_calls %}
{%- for tool_call in message.tool_calls %}
{%- if (loop.first and content) or (not loop.first) %}\n
{{- '\\n' }}\n
{%- endif %}\n
{%- if tool_call.function %}\n
{%- set tool_call = tool_call.function %}\n
{%- endif %}\n
{{- '<tool_call>\\n{\"name\": \"' }}\n
{{- tool_call.name }}\n
{{- '\", \"arguments\": ' }}\n
{%- if tool_call.arguments is string %}\n
{{- tool_call.arguments }}\n
{%- else %}\n
{{- tool_call.arguments | tojson }}\n
{%- endif %}\n
{{- '}\\n</tool_call>' }}\n
{%- endfor %}\n
{%- endif %}\n
{{- '<|im_end|>\\n' }}
{%- elif message.role == "tool" %}
{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
{{- '<|im_start|>user' }}\n
{%- endif %}\n
{{- '\\n<tool_response>\\n' }}\n
{{- message.content }}\n
{{- '\\n</tool_response>' }}\n
{%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
{{- '<|im_end|>\\n' }}\n
{%- endif %}
{%- endif %}
{%- endfor %}
{%- for message in messages %}: Iterates through the messages list (in original order this time). This is where the actual message formatting happens.{%- if (message.role == "user") or (message.role == "system" and not loop.first) %}: Handles user and non-first system messages. First system messages are handled in the tool section. This adds the role and content with the delimiters.{{- '<|im_start|>' + message.role + '\\n' + message.content + '<|im_end|>' + '\\n' }}: Formats the message: im_start tag, role, newline, content, im_end tag, newline.{%- elif message.role == "assistant" %}: Handles assistant messages, which have more complex formatting due to the potential for reasoning_content and tool_calls.
message.reasoning_content. If that is missing, it checks if the message content includes <think> tags. If so, it parses out the content within the <think> tags as reasoning_content and the content after the </think> tag as the actual content. This is useful for structured responses where the LLM explicitly states its reasoning before its final answer.<think> block: Then, the template checks loop.index0 > ns.last_query_index. If true, it means this is an assistant message after the last "real" user query.
loop.last or (not loop.last and reasoning_content): Checks if this is the last message or if there's reasoning content to display. If either is true, it includes the reasoning in a <think> block before the content. Otherwise, it just includes the assistant's content.{%- if message.tool_calls %}: Handles tool_calls for the assistant message.
tool_call in the message.tool_calls list.<tool_call> XML tag with the function name and arguments (formatted as JSON).{%- elif message.role == "tool" %}: Handles messages with the role tool. These messages represent the output of tools that have been called.
tool_response messages are properly encapsulated. It checks whether this is the first tool message or whether the previous message was not also a tool message. If either of these conditions are met, it starts a user message.<tool_response> tags.im_end tag. This ensures that consecutive tool messages are grouped together as a single user-tool interaction.4. Add Generation Prompt (Bottom if add_generation_prompt block):
{%- if add_generation_prompt %}
{{- '<|im_start|>assistant\\n' }}
{%- if enable_thinking is defined and enable_thinking is false %}
{{- '<think>\\n\\n</think>\\n\\n' }}
{%- endif %}
{%- endif %}
{%- if add_generation_prompt %}: Checks if the add_generation_prompt variable is true. This is often used when you want the LLM to continue generating text (e.g., to provide a final answer).{{- '<|im_start|>assistant\\n' }}: Starts an assistant message.{%- if enable_thinking is defined and enable_thinking is false %}: This section seems to suppress <think> blocks when enable_thinking is explicitly set to false. This could be useful for forcing a direct response without intermediate reasoning.{{- '<think>\\n\\n</think>\\n\\n' }}: If enable_thinking is false, this will output an empty <think> block. This effectively signals to the LLM not to include any reasoning in its response.In Summary:
This template does the following:
tools are provided, it formats instructions on how to use them, including the expected XML format for tool calls.messages) and formats each message based on its role (user, system, assistant, tool). It handles special formatting for:
<|im_start|>, <|im_end|>).<think>) for assistant messages.tool_calls as XML tags.tool messages as a single interaction.assistant to continue generating text.Key Use Cases:
im_start and im_end delimiters, and other specific elements for thinking and tool responses.To Use the Template:
You would need to:
messages list: This list should contain dictionaries (or objects) representing each message in the conversation, with attributes like role (e.g., "user", "assistant", "system", "tool") and content. It can also have reasoning_content and tool_calls attributes.tools list (optional): If you want to enable tool interaction, provide a list of tools, where each tool is typically a dictionary (or object) containing information about the tool's function signature.add_generation_prompt variable (optional): Set this to True if you want to prompt the LLM to generate a response after the conversation history.enable_thinking variable (optional): Set this to false if you want to prevent the last generation from including a <think> block.The template will then format the conversation history into a string that can be used as input for the LLM. The specific delimiters and tags are likely understood by the LLM, allowing it to parse the conversation history and generate appropriate responses.