1
0

MiniMax-M2.jinja 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. {# ----------‑‑‑ special token variables ‑‑‑---------- #}
  2. {%- set toolcall_begin_token = '<minimax:tool_call>' -%}
  3. {%- set toolcall_end_token = '</minimax:tool_call>' -%}
  4. {#- Tool Rendering Functions ============================================== -#}
  5. {%- macro render_tool_namespace(namespace_name, tool_list) -%}
  6. {%- for tool in tool_list -%}
  7. <tool>{{ tool.function | tojson(ensure_ascii=False) }}</tool>
  8. {% endfor -%}
  9. {%- endmacro -%}
  10. {%- macro visible_text(content) -%}
  11. {%- if content is string -%}
  12. {{ content }}
  13. {%- elif content is iterable and content is not mapping -%}
  14. {%- for item in content -%}
  15. {%- if item is mapping and item.type == 'text' -%}
  16. {{- item.text }}
  17. {%- elif item is string -%}
  18. {{- item }}
  19. {%- endif -%}
  20. {%- endfor -%}
  21. {%- else -%}
  22. {{- content }}
  23. {%- endif -%}
  24. {%- endmacro -%}
  25. {#- System Message Construction ============================================ -#}
  26. {%- macro build_system_message(system_message) -%}
  27. {%- if system_message and system_message.content -%}
  28. {{- visible_text(system_message.content) }}
  29. {%- else -%}
  30. {%- if model_identity is not defined -%}
  31. {%- set model_identity = "You are a helpful assistant." -%}
  32. {%- endif -%}
  33. {{- model_identity }}
  34. {%- endif -%}
  35. {#- Handle current_date -#}
  36. {%- if system_message and system_message.current_date -%}
  37. {{- '\n' ~ 'Current date: ' + system_message.current_date }}
  38. {%- endif -%}
  39. {#- Handle current_location -#}
  40. {%- if system_message and system_message.current_location -%}
  41. {{- '\n' ~ 'Current location: ' + system_message.current_location }}
  42. {%- endif -%}
  43. {%- endmacro -%}
  44. {#- Main Template Logic ================================================= -#}
  45. {#- Extract system message (only first message if it's system) -#}
  46. {%- set system_message = none -%}
  47. {%- set conversation_messages = messages -%}
  48. {%- if messages and messages[0].role == "system" -%}
  49. {%- set system_message = messages[0] -%}
  50. {%- set conversation_messages = messages[1:] -%}
  51. {%- endif -%}
  52. {#- Get the last user message turn, for interleved thinking -#}
  53. {%- set ns = namespace(last_user_index=-1) %}
  54. {% for m in conversation_messages %}
  55. {%- if m.role == 'user' %}
  56. {% set ns.last_user_index = loop.index0 -%}
  57. {%- endif %}
  58. {%- endfor %}
  59. {#- Render system message -#}
  60. {{- ']~!b[' ~ ']~b]system' ~ '\n' }}
  61. {{- build_system_message(system_message) }}
  62. {#- Render tools if available -#}
  63. {%- if tools -%}
  64. {{- '\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' }}
  65. {{- '\n' ~ '<tools>' ~ '\n' }}
  66. {{- render_tool_namespace("functions", tools) }}
  67. {{- '</tools>' ~ '\n\n' }}
  68. {{- 'When making tool calls, use XML format to invoke tools and pass parameters:' ~ '\n' }}
  69. {{- '\n' ~ toolcall_begin_token }}
  70. <invoke name="tool-name-1">
  71. <parameter name="param-key-1">param-value-1</parameter>
  72. <parameter name="param-key-2">param-value-2</parameter>
  73. ...
  74. </invoke>
  75. {{- '\n' ~ toolcall_end_token }}
  76. {%- endif -%}
  77. {{- '[e~[\n' }}
  78. {#- Render messages -#}
  79. {%- set last_tool_call = namespace(name=none) -%}
  80. {%- for message in conversation_messages -%}
  81. {%- if message.role == 'assistant' -%}
  82. {#- Only render reasoning_content if no user message follows -#}
  83. {{- ']~b]ai' ~ '\n' }}
  84. {%- set reasoning_content = '' %}
  85. {%- set content = visible_text(message.content) %}
  86. {%- if message.reasoning_content is string %}
  87. {%- set reasoning_content = message.reasoning_content %}
  88. {%- else %}
  89. {%- if '</think>' in content %}
  90. {%- set reasoning_content = content.split('</think>')[0].strip('\n').split('<think>')[-1].strip('\n') %}
  91. {%- set content = content.split('</think>')[-1].strip('\n') %}
  92. {%- endif %}
  93. {%- endif %}
  94. {%- if reasoning_content and loop.index0 > ns.last_user_index -%}
  95. {{- '<think>' ~ '\n' ~ reasoning_content ~ '\n' ~ '</think>' ~ '\n\n' }}
  96. {%- endif -%}
  97. {%- if content -%}
  98. {{- content }}
  99. {%- endif -%}
  100. {%- if message.tool_calls -%}
  101. {{- '\n' ~ toolcall_begin_token ~ '\n' }}
  102. {%- for tool_call in message.tool_calls -%}
  103. {%- if tool_call.function %}
  104. {%- set tool_call = tool_call.function %}
  105. {%- endif %}
  106. {{- '<invoke name="' + tool_call.name + '">' }}
  107. {% set _args = tool_call.arguments %}
  108. {%- for k, v in _args.items() %}
  109. {{- '<parameter name="' + k + '">' }}
  110. {{- v | tojson(ensure_ascii=False) if v is not string else v }}
  111. {{- '</parameter>' }}
  112. {% endfor %}
  113. {{- '</invoke>' ~ '\n' }}
  114. {%- endfor -%}
  115. {{- toolcall_end_token}}
  116. {%- set last_tool_call.name = message.tool_calls[-1].function.name -%}
  117. {%- else -%}
  118. {%- set last_tool_call.name = none -%}
  119. {%- endif -%}
  120. {{- '[e~[' ~ '\n' }}
  121. {%- elif message.role == 'tool' -%}
  122. {%- if last_tool_call.name is none -%}
  123. {{- raise_exception("Message has tool role, but there was no previous assistant message with a tool call!") }}
  124. {%- endif -%}
  125. {%- if loop.first or (conversation_messages[loop.index0 - 1].role != 'tool') -%}
  126. {{- ']~b]tool' }}
  127. {%- endif -%}
  128. {%- if message.content is string -%}
  129. {{- '\n<response>' }}
  130. {{- message.content }}
  131. {{- '</response>' }}
  132. {%- else -%}
  133. {%- for tr in message.content -%}
  134. {{- '\n<response>' }}
  135. {{- tr.output if tr.output is defined else (tr.text if tr.type == 'text' and tr.text is defined else tr) }}
  136. {{- '\n</response>' }}
  137. {%- endfor -%}
  138. {%- endif -%}
  139. {%- if loop.last or (conversation_messages[loop.index0 + 1].role != 'tool') -%}
  140. {{- '[e~[\n' -}}
  141. {%- endif -%}
  142. {%- elif message.role == 'user' -%}
  143. {{- ']~b]user' ~ '\n' }}
  144. {{- visible_text(message.content) }}
  145. {{- '[e~[' ~ '\n' }}
  146. {%- endif -%}
  147. {%- endfor -%}
  148. {#- Generation prompt -#}
  149. {%- if add_generation_prompt -%}
  150. {{- ']~b]ai' ~ '\n' ~ '<think>' ~ '\n' }}
  151. {%- endif -%}