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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{#- Harmony Chat Template for Multistral -#}
|
| 2 |
+
{#- Enhanced version with support for both toon and json formats -#}
|
| 3 |
+
|
| 4 |
+
{#- Configuration Variables -#}
|
| 5 |
+
{%- set use_toon = use_toon | default(true) -%}
|
| 6 |
+
|
| 7 |
+
{#- Tool Definition Rendering -#}
|
| 8 |
+
{%- macro render_typescript_type(param_spec, required_params) -%}
|
| 9 |
+
{%- if param_spec.type == "array" -%}
|
| 10 |
+
{%- if param_spec.items and param_spec.items.type == "string" -%}
|
| 11 |
+
string[]
|
| 12 |
+
{%- elif param_spec.items and param_spec.items.type in ["number", "integer"] -%}
|
| 13 |
+
number[]
|
| 14 |
+
{%- elif param_spec.items and param_spec.items.type == "boolean" -%}
|
| 15 |
+
boolean[]
|
| 16 |
+
{%- else -%}
|
| 17 |
+
any[]
|
| 18 |
+
{%- endif -%}
|
| 19 |
+
{%- elif param_spec.type == "string" -%}
|
| 20 |
+
{%- if param_spec.enum -%}
|
| 21 |
+
"{{ param_spec.enum | join('" | "') }}"
|
| 22 |
+
{%- else -%}
|
| 23 |
+
string
|
| 24 |
+
{%- endif -%}
|
| 25 |
+
{%- elif param_spec.type in ["number", "integer"] -%}
|
| 26 |
+
number
|
| 27 |
+
{%- elif param_spec.type == "boolean" -%}
|
| 28 |
+
boolean
|
| 29 |
+
{%- elif param_spec.type == "object" -%}
|
| 30 |
+
{%- if param_spec.properties -%}
|
| 31 |
+
{
|
| 32 |
+
{%- for prop_name, prop_spec in param_spec.properties.items() -%}
|
| 33 |
+
{{ 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 %}
|
| 34 |
+
{%- endfor %}
|
| 35 |
+
}
|
| 36 |
+
{%- else -%}
|
| 37 |
+
object
|
| 38 |
+
{%- endif -%}
|
| 39 |
+
{%- elif param_spec.oneOf -%}
|
| 40 |
+
{%- for variant in param_spec.oneOf -%}
|
| 41 |
+
{{ render_typescript_type(variant, required_params) }}{% if not loop.last %} | {% endif %}
|
| 42 |
+
{%- endfor -%}
|
| 43 |
+
{%- else -%}
|
| 44 |
+
any
|
| 45 |
+
{%- endif -%}
|
| 46 |
+
{%- endmacro -%}
|
| 47 |
+
|
| 48 |
+
{%- macro render_tool_namespace(namespace_name, tools) -%}
|
| 49 |
+
## {{ namespace_name }}
|
| 50 |
+
|
| 51 |
+
namespace {{ namespace_name }} {
|
| 52 |
+
|
| 53 |
+
{%- for tool in tools %}
|
| 54 |
+
{%- set tool = tool.function if tool.function is defined else tool %}
|
| 55 |
+
// {{ tool.description }}
|
| 56 |
+
type {{ tool.name }} = {% if tool.parameters and tool.parameters.properties %}(_: {
|
| 57 |
+
{%- for param_name, param_spec in tool.parameters.properties.items() %}
|
| 58 |
+
{%- if param_spec.description %}
|
| 59 |
+
// {{ param_spec.description }}
|
| 60 |
+
{%- endif %}
|
| 61 |
+
{{ 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 %}
|
| 62 |
+
{%- endfor %}
|
| 63 |
+
}) => any{% else %}() => any{% endif %};
|
| 64 |
+
|
| 65 |
+
{%- endfor %}
|
| 66 |
+
} // namespace {{ namespace_name }}
|
| 67 |
+
{%- endmacro -%}
|
| 68 |
+
|
| 69 |
+
{#- Format tool call arguments based on the selected format -#}
|
| 70 |
+
{%- macro format_tool_call_arguments(tool_call, message) -%}
|
| 71 |
+
{%- if use_toon -%}
|
| 72 |
+
{%- if tool_call.arguments -%}
|
| 73 |
+
{%- if tool_call.arguments is string -%}
|
| 74 |
+
{{- tool_call.arguments | fromjson | to_toon -}}
|
| 75 |
+
{%- else -%}
|
| 76 |
+
{{- tool_call.arguments | to_toon -}}
|
| 77 |
+
{%- endif -%}
|
| 78 |
+
{%- else -%}
|
| 79 |
+
{{- {} | to_toon -}}
|
| 80 |
+
{%- endif -%}
|
| 81 |
+
{%- else -%}
|
| 82 |
+
{%- if tool_call.arguments -%}
|
| 83 |
+
{%- if tool_call.arguments is string -%}
|
| 84 |
+
{{- tool_call.arguments -}}
|
| 85 |
+
{%- else -%}
|
| 86 |
+
{{- tool_call.arguments | tojson -}}
|
| 87 |
+
{%- endif -%}
|
| 88 |
+
{%- else -%}
|
| 89 |
+
{{- {} | tojson -}}
|
| 90 |
+
{%- endif -%}
|
| 91 |
+
{%- endif -%}
|
| 92 |
+
{%- endmacro -%}
|
| 93 |
+
|
| 94 |
+
{#- System Message -#}
|
| 95 |
+
<|start|>system<|message|>{{ model_identity or "You are Aizia, a helpful assistant trained by damfle." }}
|
| 96 |
+
Knowledge cutoff: 2025-12
|
| 97 |
+
Current date: {{ strftime_now("%Y-%m-%d") }}
|
| 98 |
+
|
| 99 |
+
Reasoning: {{ reasoning_effort or "none" }}
|
| 100 |
+
|
| 101 |
+
# Valid channels: analysis, commentary, final. Channel must be included for every message.
|
| 102 |
+
{%- if tools %}
|
| 103 |
+
Calls to these tools must go to the commentary channel: 'functions'.
|
| 104 |
+
{%- endif %}<|end|>
|
| 105 |
+
|
| 106 |
+
{#- Extract developer message -#}
|
| 107 |
+
{%- if messages and messages[0].role in ["developer", "system"] %}
|
| 108 |
+
{%- set developer_message = messages[0].content %}
|
| 109 |
+
{%- set loop_messages = messages[1:] %}
|
| 110 |
+
{%- else %}
|
| 111 |
+
{%- set developer_message = "" %}
|
| 112 |
+
{%- set loop_messages = messages %}
|
| 113 |
+
{%- endif %}
|
| 114 |
+
|
| 115 |
+
{#- Render developer message and tools -#}
|
| 116 |
+
{%- if developer_message or tools %}
|
| 117 |
+
<|start|>developer<|message|>
|
| 118 |
+
{%- if developer_message %}
|
| 119 |
+
# Instructions
|
| 120 |
+
|
| 121 |
+
{{ developer_message }}
|
| 122 |
+
|
| 123 |
+
{%- endif %}
|
| 124 |
+
{%- if tools %}
|
| 125 |
+
# Tools
|
| 126 |
+
|
| 127 |
+
{{ render_tool_namespace("functions", tools) }}
|
| 128 |
+
{%- endif %}
|
| 129 |
+
<|end|>
|
| 130 |
+
{%- endif %}
|
| 131 |
+
|
| 132 |
+
{#- Process messages -#}
|
| 133 |
+
{%- set last_tool_call = namespace(name=none) %}
|
| 134 |
+
{%- for message in loop_messages %}
|
| 135 |
+
{%- if message.role == 'assistant' %}
|
| 136 |
+
{%- if "tool_calls" in message and message.tool_calls %}
|
| 137 |
+
{#- Assistant message with tool calls -#}
|
| 138 |
+
{%- if message.reasoning %}
|
| 139 |
+
<|start|>assistant<|channel|>analysis<|message|>{{ message.reasoning }}<|end|>
|
| 140 |
+
{%- endif %}
|
| 141 |
+
|
| 142 |
+
{%- for tool_call in message.tool_calls %}
|
| 143 |
+
{%- set tool_call = tool_call.function if tool_call.function else tool_call %}
|
| 144 |
+
<|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|>
|
| 145 |
+
{%- set last_tool_call.name = tool_call.name %}
|
| 146 |
+
{%- endfor %}
|
| 147 |
+
{%- else %}
|
| 148 |
+
{#- Regular assistant message -#}
|
| 149 |
+
{%- if message.reasoning %}
|
| 150 |
+
<|start|>assistant<|channel|>analysis<|message|>{{ message.reasoning }}<|end|>
|
| 151 |
+
{%- endif %}
|
| 152 |
+
<|start|>assistant<|channel|>final<|message|>{{ message.content }}<|end|>
|
| 153 |
+
{%- set last_tool_call.name = none %}
|
| 154 |
+
{%- endif %}
|
| 155 |
+
{%- elif message.role == 'tool' %}
|
| 156 |
+
{%- if last_tool_call.name is none %}
|
| 157 |
+
{{ raise_exception("Message has tool role, but there was no previous assistant message with a tool call!") }}
|
| 158 |
+
{%- endif %}
|
| 159 |
+
<|start|>functions.{{ last_tool_call.name }} to=assistant<|channel|>commentary<|message|>{{ message.content | tojson }}<|end|>
|
| 160 |
+
{%- elif message.role == 'user' %}
|
| 161 |
+
<|start|>user<|message|>{{ message.content }}<|end|>
|
| 162 |
+
{%- endif %}
|
| 163 |
+
{%- endfor %}
|
| 164 |
+
|
| 165 |
+
{#- Generation prompt -#}
|
| 166 |
+
{%- if add_generation_prompt %}
|
| 167 |
+
<|start|>assistant
|
| 168 |
+
{%- endif %}
|