File size: 6,298 Bytes
ca00efb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{#- 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 %}