char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aldehir Rojascommon/chat: add specialized minimax m3 parser (#26210)6ba5ef247

char/aliceai-t5-moe
11.5 KiB247 linesraw
1{# ---------- special token variables ---------- #}
2{%- set ns_token               = ']<]minimax[>['                  -%}
3{%- set bod_token              = ']~!b['                          -%}
4{%- set bos_token              = ']~b]'                           -%}
5{%- set eos_token              = '[e~['                           -%}
6{%- set toolcall_begin_token   = ns_token ~ '<tool_call>'         -%}
7{%- set toolcall_end_token     = ns_token ~ '</tool_call>'        -%}
8{%- set think_begin_token      = '<mm:think>'                     -%}
9{%- set think_end_token        = '</mm:think>'                    -%}
10{%- set image_token            = ']<]image[>['                    -%}
11{%- set video_token            = ']<]video[>['                    -%}
12{#- Thinking mode: "enabled" / "disabled" / "adaptive" / not defined -#}
13{#- Recursive XML renderer for tool_call arguments ======================== -#}
14{#- None values are intentionally skipped in mapping iteration so that
15    `<key>null</key>` (which would round-trip to the literal string "null")
16    never appears in the rendered tool_call. The convention is: omit the
17    field entirely. The top-level `_args` loop applies the same rule.
18    The `val is none` branch below is a safety net only — upstream cleaning
19    (drop_none_in_tool_arguments) should ensure no None ever reaches here. -#}
20{%- macro to_xml(val, ns) -%}
21{%- if val is mapping -%}
22{%- for k, v in val.items() if v is not none -%}
23{{ ns }}<{{ k }}>{{ to_xml(v, ns) }}{{ ns }}</{{ k }}>
24{%- endfor -%}
25{%- elif val is iterable and val is not string -%}
26{%- for item in val -%}
27{{ ns }}<item>{{ to_xml(item, ns) }}{{ ns }}</item>
28{%- endfor -%}
29{%- elif val is none -%}
30{#- Should be unreachable when upstream cleaning is applied. -#}
31{%- elif val is boolean -%}
32{{ val | tojson }}
33{%- else -%}
34{{ val }}
35{%- endif -%}
36{%- endmacro -%}
37{#- Tool Rendering Functions ============================================== -#}
38{%- macro render_tool_namespace(namespace_name, tool_list) -%}
39{%- for tool in tool_list -%}
40<tool>{{ tool.function | tojson(ensure_ascii=False) }}</tool>
41{% endfor -%}
42{%- endmacro -%}
43{%- macro visible_text(content) -%}
44    {%- if content is string -%}
45        {{ content }}
46    {%- elif content is iterable and content is not mapping -%}
47        {%- for item in content -%}
48            {%- if item is mapping and item.type == 'text' -%}
49                {{- item.text }}
50            {%- elif item is mapping and item.type == 'image' -%}
51                {{- image_token }}
52            {%- elif item is mapping and item.type == 'video' -%}
53                {{- video_token}}
54            {%- elif item is string -%}
55                {{- item }}
56            {%- endif -%}
57        {%- endfor -%}
58    {%- elif content is none -%}
59        {{- '' }}
60    {%- else -%}
61        {{- content }}
62    {%- endif -%}
63{%- endmacro -%}
64{#- System Message Construction ============================================ -#}
65{%- macro build_system_message(system_message) -%}
66    {%- if system_message and system_message.content -%}
67        {{- visible_text(system_message.content) }}
68    {%- else -%}
69        {{- 'Your model version is MiniMax-M3, developed by MiniMax. Knowledge cutoff: January 2026. Founded in early 2022, MiniMax is a global AI foundation model company committed to advancing the frontiers of AI towards AGI.' }}
70    {%- endif -%}
71
72    {#- Thinking mode instructions -#}
73    {{- '\n\n<thinking_instructions>\n' }}
74    {{- 'You have a thinking capability that allows you to reason step by step before responding. When thinking is enabled, wrap your reasoning in ' ~ think_begin_token ~ think_end_token ~ ' tags before your response. When thinking is disabled, begin your response directly after the ' ~ think_end_token ~ ' prefix. When thinking is adaptive, decide on your own whether to think for the current turn.\n' }}
75    {%- if thinking_mode is defined -%}
76        {%- if thinking_mode == "enabled" -%}
77            {{- 'Current thinking mode: enabled. You MUST think step by step before every response, including after receiving function/tool results.\n' }}
78        {%- elif thinking_mode == "disabled" -%}
79            {{- 'Current thinking mode: disabled. Do not output any thinking process.\n' }}
80        {%- elif thinking_mode == "adaptive" -%}
81            {{- 'Current thinking mode: adaptive. You are encouraged to think for complex decision-making, multi-step reasoning, or when analyzing function/tool results.\n' }}
82        {%- endif -%}
83    {%- else -%}
84        {{- 'Current thinking mode: adaptive. You are encouraged to think for complex decision-making, multi-step reasoning, or when analyzing function/tool results.\n' }}
85    {%- endif -%}
86    {{- '</thinking_instructions>' }}
87{%- endmacro -%}
88{%- macro build_developer_message(developer_message) -%}
89    {%- if developer_message and developer_message.content -%}
90        {{- visible_text(developer_message.content) }}
91    {%- else -%}
92        {%- if model_identity is not defined -%}
93            {%- set model_identity = "You are a helpful assistant." -%}
94        {%- endif -%}
95        {{- model_identity }}
96    {%- endif -%}
97{%- endmacro -%}
98{#- Main Template Logic ================================================= -#}
99{#- Role mapping: root -> system sp (high priority), system/developer -> developer sp (low priority) -#}
100{%- set system_message = none -%}
101{%- set developer_message = none -%}
102{%- set conversation_messages = messages -%}
103{%- if messages and messages[0].role == "root" -%}
104    {%- set system_message = messages[0] -%}
105    {%- set conversation_messages = messages[1:] -%}
106    {%- if conversation_messages and conversation_messages[0].role in ["system", "developer"] -%}
107        {%- set developer_message = conversation_messages[0] -%}
108        {%- set conversation_messages = conversation_messages[1:] -%}
109    {%- endif -%}
110{%- elif messages and messages[0].role in ["system", "developer"] -%}
111    {%- set developer_message = messages[0] -%}
112    {%- set conversation_messages = messages[1:] -%}
113{%- endif -%}
114{#- Render system sp (higher priority, root role only) -#}
115{{- bod_token ~ bos_token ~ 'system' ~ '\n' }}
116{{- build_system_message(system_message) }}
117{{- eos_token ~ '\n' }}
118
119{#- Render developer sp (lower priority: system/developer role + tools) -#}
120{{- bos_token ~ 'developer' ~ '\n' }}
121{{- build_developer_message(developer_message) }}
122{%- if tools -%}
123    {{- '\n\n' ~ '# Tools' ~ '\n' ~ 'You may call one or more tools to assist with the user query.\nHere are the tools available in JSONSchema format:' ~ '\n' }}
124    {{- '\n' ~ '<tools>' ~ '\n' }}
125    {{- render_tool_namespace("functions", tools) }}
126    {{- '</tools>' ~ '\n\n' }}
127    {{- 'To call tools, wrap all invocations in a single ' ~ toolcall_begin_token ~ toolcall_end_token ~ ' block. Parameter values containing nested objects or arrays are recursively expanded into XML elements. Example:\n' }}
128    {{- '\n' ~ toolcall_begin_token ~ '\n' }}
129    {{- ns_token + '<invoke name="tool-name-1">' }}
130    {{- ns_token + '<param-1>value-1' + ns_token + '</param-1>' }}
131    {{- ns_token + '<param-2>' }}
132    {{- ns_token + '<item>' }}
133    {{- ns_token + '<key-a>val-a' + ns_token + '</key-a>' }}
134    {{- ns_token + '<key-b>val-b' + ns_token + '</key-b>' }}
135    {{- ns_token + '</item>' }}
136    {{- ns_token + '</param-2>' }}
137    {{- ns_token + '</invoke>\n' }}
138    {{- ns_token + '<invoke name="tool-name-2">' }}
139    {{- ns_token + '<param-1>value-1' + ns_token + '</param-1>' }}
140    {{- ns_token + '</invoke>\n' }}
141    {{- toolcall_end_token }}
142{%- endif -%}
143{{- eos_token ~ '\n' }}
144
145{#- Render messages -#}
146{%- set last_tool_call = namespace(name=none) -%}
147{%- for message in conversation_messages -%}
148    {%- if message.role == 'assistant' -%}
149        {{- bos_token ~ 'ai' ~ '\n' }}
150
151        {%- set reasoning_content = '' %}
152        {%- set content = visible_text(message.content) %}
153        {%- if message.reasoning_content is string %}
154            {%- set reasoning_content = message.reasoning_content %}
155        {%- else %}
156            {%- if think_end_token in content %}
157                {%- set reasoning_content = content.split(think_end_token)[0].strip('\n').split(think_begin_token)[-1].strip('\n') %}
158                {%- set content = content.split(think_end_token)[-1].strip('\n') %}
159            {%- endif %}
160        {%- endif %}
161
162        {%- if reasoning_content -%}
163            {#- Render thinking for every assistant turn (all-turn visible) -#}
164            {{- think_begin_token ~ reasoning_content ~ think_end_token }}
165        {%- else -%}
166            {#- No thinking rendered → prefix with think_end_token -#}
167            {{- think_end_token }}
168        {%- endif -%}
169
170        {%- if content -%}
171            {{- content }}
172        {%- endif -%}
173        {%- if message.tool_calls -%}
174            {{- toolcall_begin_token ~ '\n' }}
175
176            {%- for tool_call in message.tool_calls -%}
177                {%- if tool_call.function -%}
178                    {%- set tool_call = tool_call.function -%}
179                {%- endif -%}
180{{- ns_token + '<invoke name="' + tool_call.name + '">' }}
181{%- set _args = tool_call.arguments -%}
182{%- for k, v in _args.items() if v is not none %}
183{{- ns_token + '<' + k + '>' -}}
184{{- to_xml(v, ns_token) -}}
185{{- ns_token + '</' + k + '>' }}
186{%- endfor -%}
187{{- ns_token + '</invoke>' ~ '\n' }}
188            {%- endfor -%}
189
190            {{- toolcall_end_token }}
191            {%- if message.tool_calls[-1].function -%}
192                {%- set last_tool_call.name = message.tool_calls[-1].function.name -%}
193            {%- else -%}
194                {%- set last_tool_call.name = message.tool_calls[-1].name -%}
195            {%- endif -%}
196        {%- else -%}
197            {%- set last_tool_call.name = none -%}
198        {%- endif -%}
199        {{- eos_token ~ '\n' }}
200
201    {%- elif message.role == 'tool' -%}
202        {%- if last_tool_call.name is none -%}
203            {{- raise_exception("Message has tool role, but there was no previous assistant message with a tool call!") }}
204        {%- endif -%}
205        {%- if loop.first or (conversation_messages[loop.index0 - 1].role != 'tool') -%}
206            {{- bos_token ~ 'tool' }}
207        {%- endif -%}
208        {{- '\n<response>' }}
209        {%- if message.content is string -%}
210            {{- message.content }}
211        {%- else -%}
212            {%- for tr in message.content -%}
213                {%- if tr is mapping and tr.type is defined and tr.type == 'image' -%}
214                    {{- image_token }}
215                {%- elif tr is mapping and tr.type is defined and tr.type == 'video' -%}
216                    {{- video_token }}
217                {%- else -%}
218                    {{- tr.output if tr.output is defined else (tr.text if tr.type == 'text' and tr.text is defined else tr) }}
219                {%- endif -%}
220            {%- endfor -%}
221        {%- endif -%}
222        {{- '</response>' }}
223        {%- if loop.last or (conversation_messages[loop.index0 + 1].role != 'tool') -%}
224            {{- eos_token ~ '\n' -}}
225        {%- endif -%}
226
227    {%- elif message.role == 'user' -%}
228        {{- bos_token ~ 'user' ~ '\n' }}
229        {{- visible_text(message.content) }}
230        {{- eos_token ~ '\n' }}
231    {%- endif -%}
232{%- endfor -%}
233
234{#- Generation prompt -#}
235{%- if add_generation_prompt -%}
236{{- bos_token ~ 'ai' ~ '\n' }}
237{%- if thinking_mode is defined and thinking_mode == "disabled" -%}
238    {{- think_end_token }}
239{%- elif thinking_mode is defined and thinking_mode == "adaptive" -%}
240    {#- adaptive: no prefix, let model decide -#}
241{%- elif thinking_mode is defined and thinking_mode == "enabled" -%}
242    {#- enabled or not defined: default to think -#}
243    {{- think_begin_token }}
244{%- else -%}
245    {#- adaptive: no prefix, let model decide -#}
246{%- endif -%}
247{%- endif -%}