Skip to content

Commit af5216e

Browse files
authored
Patch buggy official MiniMax-M2 chat template
1 parent 22fc731 commit af5216e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

common/chat.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,35 @@ common_chat_templates_ptr common_chat_templates_init(
794794
};
795795
default_template_src = replaceToJsonInTemplate(default_template_src);
796796

797+
// Fix MiniMax-M2 template bug: message.tool_calls[-1] silently fail
798+
// Upstream minja seems do not support id[-1] and cause silently fail
799+
// TODO: remove this once the template is fixed.
800+
if (default_template_src.find("]~!b[") != std::string::npos
801+
&& default_template_src.find("]~b]") != std::string::npos
802+
&& default_template_src.find("[-1]") != std::string::npos) {
803+
LOG_INF("Detected MiniMax-M2 template with unsupported syntax \"[-1]\", applying automatic fix...\n");
804+
string_replace_all(default_template_src,
805+
"{%- set reasoning_content = content.split('</think>')[0].strip('\\n').split('<think>')[-1].strip('\\n') %}",
806+
"{%- set reasoning_content = content.split('</think>') -%} {%- set reasoning_content = reasoning_content|first -%} {%- set reasoning_content = reasoning_content.strip('\\n').split('<think>') -%} {%- set reasoning_content = reasoning_content|last -%} {%- set reasoning_content = reasoning_content.strip('\\n') %}");
807+
string_replace_all(default_template_src,
808+
"{%- set content = content.split('</think>')[-1].strip('\\n') %}",
809+
"{%- set content = content.split('</think>') -%} {%- set content = content|last -%} {%- set content = content.strip('\\n') %}");
810+
if (default_template_src.find("{%- set last_tool_call.name = message.tool_calls[-1].name -%}") != std::string::npos &&
811+
default_template_src.find("{%- for tool_call in message.tool_calls -%}") != std::string::npos) {
812+
string_replace_all(default_template_src, "{%- set last_tool_call.name = message.tool_calls[-1].name -%}", "");
813+
string_replace_all(default_template_src,
814+
"{%- for tool_call in message.tool_calls -%}",
815+
"{%- for tool_call in message.tool_calls -%} {%- set last_tool_call.name = tool_call.function.name -%}");
816+
}
817+
LOG_INF("MiniMax-M2 template fixed\n");
818+
}
819+
if (default_template_src.find("]~!b[") != std::string::npos
820+
&& default_template_src.find("]~b]") != std::string::npos
821+
&& default_template_src.find("{% set _args = tool_call.arguments %}") != std::string::npos) {
822+
string_replace_all(default_template_src, "{% set _args = tool_call.arguments %}",
823+
"{%- if tool_call.arguments is defined and tool_call.arguments is mapping -%} {%- set _args = tool_call.arguments -%} {%- else -%} {%- set _args = {} -%} {%- endif -%}");
824+
}
825+
797826
std::string token_bos = bos_token_override;
798827
std::string token_eos = eos_token_override;
799828
bool add_bos = false;

0 commit comments

Comments
 (0)