multistral-tokenizer / chat_template.jinja
damfle
mod: add chat_template.jinja
ca00efb unverified
{#- Harmony Chat Template for Multistral -#}
{#- Enhanced version with support for both toon and json formats -#}
{#- Configuration Variables -#}
{%- set use_toon = use_toon | default(true) -%}
{#- Tool Definition Rendering -#}
{%- macro render_typescript_type(param_spec, required_params) -%}
{%- if param_spec.type == "array" -%}
{%- if param_spec.items and param_spec.items.type == "string" -%}
string[]
{%- elif param_spec.items and param_spec.items.type in ["number", "integer"] -%}
number[]
{%- elif param_spec.items and param_spec.items.type == "boolean" -%}
boolean[]
{%- else -%}
any[]
{%- endif -%}
{%- elif param_spec.type == "string" -%}
{%- if param_spec.enum -%}
"{{ param_spec.enum | join('" | "') }}"
{%- else -%}
string
{%- endif -%}
{%- elif param_spec.type in ["number", "integer"] -%}
number
{%- elif param_spec.type == "boolean" -%}
boolean
{%- elif param_spec.type == "object" -%}
{%- if param_spec.properties -%}
{
{%- for prop_name, prop_spec in param_spec.properties.items() -%}
{{ prop_name }}{% if prop_name not in (param_spec.required or []) %}?{% endif %}: {{ render_typescript_type(prop_spec, param_spec.required or []) }}{% if not loop.last %}, {% endif %}
{%- endfor %}
}
{%- else -%}
object
{%- endif -%}
{%- elif param_spec.oneOf -%}
{%- for variant in param_spec.oneOf -%}
{{ render_typescript_type(variant, required_params) }}{% if not loop.last %} | {% endif %}
{%- endfor -%}
{%- else -%}
any
{%- endif -%}
{%- endmacro -%}
{%- macro render_tool_namespace(namespace_name, tools) -%}
## {{ namespace_name }}
namespace {{ namespace_name }} {
{%- for tool in tools %}
{%- set tool = tool.function if tool.function is defined else tool %}
// {{ tool.description }}
type {{ tool.name }} = {% if tool.parameters and tool.parameters.properties %}(_: {
{%- for param_name, param_spec in tool.parameters.properties.items() %}
{%- if param_spec.description %}
// {{ param_spec.description }}
{%- endif %}
{{ param_name }}{% if param_name not in (tool.parameters.required or []) %}?{% endif %}: {{ render_typescript_type(param_spec, tool.parameters.required or []) }}{% if param_spec.default is defined %}, // default: {{ param_spec.default | tojson }}{% endif %}{% if not loop.last %},{% endif %}
{%- endfor %}
}) => any{% else %}() => any{% endif %};
{%- endfor %}
} // namespace {{ namespace_name }}
{%- endmacro -%}
{#- Format tool call arguments based on the selected format -#}
{%- macro format_tool_call_arguments(tool_call, message) -%}
{%- if use_toon -%}
{%- if tool_call.arguments -%}
{%- if tool_call.arguments is string -%}
{{- tool_call.arguments | fromjson | to_toon -}}
{%- else -%}
{{- tool_call.arguments | to_toon -}}
{%- endif -%}
{%- else -%}
{{- {} | to_toon -}}
{%- endif -%}
{%- else -%}
{%- if tool_call.arguments -%}
{%- if tool_call.arguments is string -%}
{{- tool_call.arguments -}}
{%- else -%}
{{- tool_call.arguments | tojson -}}
{%- endif -%}
{%- else -%}
{{- {} | tojson -}}
{%- endif -%}
{%- endif -%}
{%- endmacro -%}
{#- System Message -#}
<|start|>system<|message|>{{ model_identity or "You are Aizia, a helpful assistant trained by damfle." }}
Knowledge cutoff: 2025-12
Current date: {{ strftime_now("%Y-%m-%d") }}
Reasoning: {{ reasoning_effort or "none" }}
# Valid channels: analysis, commentary, final. Channel must be included for every message.
{%- if tools %}
Calls to these tools must go to the commentary channel: 'functions'.
{%- endif %}<|end|>
{#- Extract developer message -#}
{%- if messages and messages[0].role in ["developer", "system"] %}
{%- set developer_message = messages[0].content %}
{%- set loop_messages = messages[1:] %}
{%- else %}
{%- set developer_message = "" %}
{%- set loop_messages = messages %}
{%- endif %}
{#- Render developer message and tools -#}
{%- if developer_message or tools %}
<|start|>developer<|message|>
{%- if developer_message %}
# Instructions
{{ developer_message }}
{%- endif %}
{%- if tools %}
# Tools
{{ render_tool_namespace("functions", tools) }}
{%- endif %}
<|end|>
{%- endif %}
{#- Process messages -#}
{%- set last_tool_call = namespace(name=none) %}
{%- for message in loop_messages %}
{%- if message.role == 'assistant' %}
{%- if "tool_calls" in message and message.tool_calls %}
{#- Assistant message with tool calls -#}
{%- if message.reasoning %}
<|start|>assistant<|channel|>analysis<|message|>{{ message.reasoning }}<|end|>
{%- endif %}
{%- for tool_call in message.tool_calls %}
{%- set tool_call = tool_call.function if tool_call.function else tool_call %}
<|start|>assistant to=functions.{{ tool_call.name }}<|channel|>commentary<|constrain|>{{ "toon" if use_toon else "json" }}<|message|>{{ format_tool_call_arguments(tool_call, message) }}<|call|>
{%- set last_tool_call.name = tool_call.name %}
{%- endfor %}
{%- else %}
{#- Regular assistant message -#}
{%- if message.reasoning %}
<|start|>assistant<|channel|>analysis<|message|>{{ message.reasoning }}<|end|>
{%- endif %}
<|start|>assistant<|channel|>final<|message|>{{ message.content }}<|end|>
{%- set last_tool_call.name = none %}
{%- endif %}
{%- elif message.role == 'tool' %}
{%- if last_tool_call.name is none %}
{{ raise_exception("Message has tool role, but there was no previous assistant message with a tool call!") }}
{%- endif %}
<|start|>functions.{{ last_tool_call.name }} to=assistant<|channel|>commentary<|message|>{{ message.content | tojson }}<|end|>
{%- elif message.role == 'user' %}
<|start|>user<|message|>{{ message.content }}<|end|>
{%- endif %}
{%- endfor %}
{#- Generation prompt -#}
{%- if add_generation_prompt %}
<|start|>assistant
{%- endif %}