미니맥스(MiniMax)에서 새로운 AI 모델인 M2.7을 공개했습니다. 이 템플릿 코드는 해당 모델의 시스템 메시지 처리와 도구(Tool) 호출을 위한 프롬프트 렌더링 방식을 보여줍니다. 모델이 사용자의 요청을 분석하고 사고(Reasoning) 과정을 거쳐 도구를 사용하는 일련의 논리적 구조를 담고 있습니다.
번역된 본문
-%}
{%- set toolcall_end_token = '' -%}
{#- 도구 렌더링 함수 ============================================== -#}
{%- macro render_tool_namespace(namespace_name, tool_list) -%}
{%- for tool in tool_list -%}
{{ tool.function | tojson(ensure_ascii=False) }}
{% endfor -%}
{%- endmacro -%}
{%- macro visible_text(content) -%}
{%- if content is string -%}
{{ content }}
{%- elif content is iterable and content is not mapping -%}
{%- for item in content -%}
{%- if item is mapping and item.type == 'text' -%}
{{- item.text }}
{%- elif item is string -%}
{{- item }}
{%- endif -%}
{%- endfor -%}
{%- else -%}
{{- content }}
{%- endif -%}
{%- endmacro -%}
{#- 시스템 메시지 구성 ============================================ -#}
{%- macro build_system_message(system_message) -%}
{%- if system_message and system_message.content -%}
{{- visible_text(system_message.content) }}
{%- else -%}
{%- if model_identity is not defined -%}
{%- set model_identity = "당신은 도움이 되는 어시스턴트입니다. 당신의 이름은 MiniMax-M2.7이며 MiniMax가 제작했습니다." -%}
{%- endif -%}
{{- model_identity }}
{%- endif -%}
{#- 현재 날짜 처리 -#}
{%- if system_message and system_message.current_date -%}
{{- '\n' ~ '현재 날짜: ' + system_message.current_date }}
{%- endif -%}
{#- 현재 위치 처리 -#}
{%- if system_message and system_message.current_location -%}
{{- '\n' ~ '현재 위치: ' + system_message.current_location }}
{%- endif -%}
{%- endmacro -%}
{#- 메인 템플릿 로직 ================================================= -#}
{#- 시스템 메시지 추출 (시스템 메시지인 경우 첫 번째 메시지만) -#}
{%- set system_message = none -%}
{%- set conversation_messages = messages -%}
{%- if messages and messages[0].role == "system" -%}
{%- set system_message = messages[0] -%}
{%- set conversation_messages = messages[1:] -%}
{%- endif -%}
{#- interleaved thinking을 위해 마지막 사용자 메시지 턴 가져오기 -#}
{%- set ns = namespace(last_user_index=-1) %}
{% for m in conversation_messages %}
{%- if m.role == 'user' %}
{% set ns.last_user_index = loop.index0 -%}
{%- endif %}
{%- endfor %}
{#- 시스템 메시지 렌더링 -#}
{{- ']~b[' ~ ']b]system' ~ '\n' }}
{{- build_system_message(system_message) }}
{#- 도구가 있는 경우 렌더링 -#}
{%- if tools -%}
{{- '\n\n' ~ '# Tools' ~ '\n' ~ '사용자의 질문에 답하기 위해 하나 이상의 도구를 호출할 수 있습니다.\nJSONSchema 형식으로 제공되는 도구는 다음과 같습니다:' ~ '\n' }}
{{- '\n' ~ '' ~ '\n' }}
{{- render_tool_namespace("functions", tools) }}
{{- '' ~ '\n\n' }}
{{- '도구를 호출할 때, XML 형식을 사용하여 도구를 호출하고 매개변수를 전달하세요:' ~ '\n' }}
{{- '\n' ~ toolcall_begin_token }}
param-value-1param-value-2
...
{{- '\n' ~ toolcall_end_token }}
{%- endif -%}
{{- '[e[\n' }}
{#- 메시지 렌더링 -#}
{%- set last_tool_call = namespace(name=none) -%}
{%- for message in conversation_messages -%}
{%- if message.role == 'assistant' -%}
{#- 뒤따르는 사용자 메시지가 없는 경우에만 reasoning_content 렌더링 -#}
{{- ']~b]ai' ~ '\n' }}
{%- set reasoning_content = '' %}
{%- set content = visible_text(message.content) %}
{%- if message.reasoning_content is string %}
{%- set reasoning_content = message.reasoning_content %}
{%- else %}
{%- if ' Backpacking' in content %}
{%- set reasoning_content = content.split(' Backpacking')[0].strip('\n').split(' Eurotrip')[-1].strip('\n') %}
{%- set content = content.split(' Backpacking')[-1].strip('\n') %}
{%- endif %}
{%- endif %}
{%- if reasoning_content and loop.index0 > ns.last_user_index -%}
{{- ' Eurotrip' ~ '\n' ~ reasoning_content ~ '\n' ~ ' Backpacking' ~ '\n\n' }}
{%- endif -%}
{%- if content -%}
{{- content }}
{%- endif -%}
{%- if message.tool_calls -%}
{{- '\n'