Trouter-Library commited on
Commit
33e76da
·
verified ·
1 Parent(s): 9a6b6a7

Create chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +155 -0
chat_template.jinja ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
36
+ {#- Handle current_date -#}
37
+ {%- if system_message and system_message.current_date -%}
38
+ {{- '\n' ~ 'Current date: ' + system_message.current_date }}
39
+ {%- endif -%}
40
+ {#- Handle current_location -#}
41
+ {%- if system_message and system_message.current_location -%}
42
+ {{- '\n' ~ 'Current location: ' + system_message.current_location }}
43
+ {%- endif -%}
44
+ {%- endmacro -%}
45
+ {#- Main Template Logic ================================================= -#}
46
+ {#- Extract system message (only first message if it's system) -#}
47
+ {%- set system_message = none -%}
48
+ {%- set conversation_messages = messages -%}
49
+ {%- if messages and messages[0].role == "system" -%}
50
+ {%- set system_message = messages[0] -%}
51
+ {%- set conversation_messages = messages[1:] -%}
52
+ {%- endif -%}
53
+ {#- Get the last user message turn, for interleved thinking -#}
54
+ {%- set ns = namespace(last_user_index=-1) %}
55
+ {% for m in conversation_messages %}
56
+ {%- if m.role == 'user' %}
57
+ {% set ns.last_user_index = loop.index0 -%}
58
+ {%- endif %}
59
+ {%- endfor %}
60
+ {#- Render system message -#}
61
+ {{- ']~!b[' ~ ']~b]system' ~ '\n' }}
62
+ {{- build_system_message(system_message) }}
63
+ {#- Render tools if available -#}
64
+ {%- if tools -%}
65
+ {{- '\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' }}
66
+ {{- '\n' ~ '<tools>' ~ '\n' }}
67
+ {{- render_tool_namespace("functions", tools) }}
68
+ {{- '</tools>' ~ '\n\n' }}
69
+ {{- 'When making tool calls, use XML format to invoke tools and pass parameters:' ~ '\n' }}
70
+ {{- '\n' ~ toolcall_begin_token }}
71
+ <invoke name="tool-name-1">
72
+ <parameter name="param-key-1">param-value-1</parameter>
73
+ <parameter name="param-key-2">param-value-2</parameter>
74
+ ...
75
+ </invoke>
76
+ {{- '\n' ~ toolcall_end_token }}
77
+ {%- endif -%}
78
+ {{- '[e~[\n' }}
79
+ {#- Render messages -#}
80
+ {%- set last_tool_call = namespace(name=none) -%}
81
+ {%- for message in conversation_messages -%}
82
+ {%- if message.role == 'assistant' -%}
83
+ {#- Only render reasoning_content if no user message follows -#}
84
+ {{- ']~b]ai' ~ '\n' }}
85
+ {%- set reasoning_content = '' %}
86
+ {%- set content = visible_text(message.content) %}
87
+ {%- if message.reasoning_content is string %}
88
+ {%- set reasoning_content = message.reasoning_content %}
89
+ {%- else %}
90
+ {%- if '</think>' in content %}
91
+ {%- set reasoning_content = content.split('</think>')[0].strip('\n').split('<think>')[-1].strip('\n') %}
92
+ {%- set content = content.split('</think>')[-1].strip('\n') %}
93
+ {%- endif %}
94
+ {%- endif %}
95
+ {%- if reasoning_content and loop.index0 > ns.last_user_index -%}
96
+ {{- '<think>' ~ '\n' ~ reasoning_content ~ '\n' ~ '</think>' ~ '\n\n' }}
97
+ {%- endif -%}
98
+ {%- if content -%}
99
+ {{- content }}
100
+ {%- endif -%}
101
+ {%- if message.tool_calls -%}
102
+ {{- '\n' ~ toolcall_begin_token ~ '\n' }}
103
+ {%- for tool_call in message.tool_calls -%}
104
+ {%- if tool_call.function %}
105
+ {%- set tool_call = tool_call.function %}
106
+ {%- endif %}
107
+ {{- '<invoke name="' + tool_call.name + '">' }}
108
+ {% set _args = tool_call.arguments %}
109
+ {%- for k, v in _args.items() %}
110
+ {{- '<parameter name="' + k + '">' }}
111
+ {{- v | tojson(ensure_ascii=False) if v is not string else v }}
112
+ {{- '</parameter>' }}
113
+ {% endfor %}
114
+ {{- '</invoke>' ~ '\n' }}
115
+ {%- endfor -%}
116
+
117
+ {{- toolcall_end_token}}
118
+ {%- set last_tool_call.name = message.tool_calls[-1].name -%}
119
+ {%- else -%}
120
+ {%- set last_tool_call.name = none -%}
121
+ {%- endif -%}
122
+ {{- '[e~[' ~ '\n' }}
123
+
124
+ {%- elif message.role == 'tool' -%}
125
+ {%- if last_tool_call.name is none -%}
126
+ {{- raise_exception("Message has tool role, but there was no previous assistant message with a tool call!") }}
127
+ {%- endif -%}
128
+ {%- if loop.first or (conversation_messages[loop.index0 - 1].role != 'tool') -%}
129
+ {{- ']~b]tool' }}
130
+ {%- endif -%}
131
+ {%- if message.content is string -%}
132
+ {{- '\n<response>' }}
133
+ {{- message.content }}
134
+ {{- '</response>' }}
135
+ {%- else -%}
136
+ {%- for tr in message.content -%}
137
+ {{- '\n<response>' }}
138
+ {{- tr.output if tr.output is defined else (tr.text if tr.type == 'text' and tr.text is defined else tr) }}
139
+ {{- '\n</response>' }}
140
+ {%- endfor -%}
141
+ {%- endif -%}
142
+ {%- if loop.last or (conversation_messages[loop.index0 + 1].role != 'tool') -%}
143
+ {{- '[e~[\n' -}}
144
+ {%- endif -%}
145
+
146
+ {%- elif message.role == 'user' -%}
147
+ {{- ']~b]user' ~ '\n' }}
148
+ {{- visible_text(message.content) }}
149
+ {{- '[e~[' ~ '\n' }}
150
+ {%- endif -%}
151
+ {%- endfor -%}
152
+ {#- Generation prompt -#}
153
+ {%- if add_generation_prompt -%}
154
+ {{- ']~b]ai' ~ '\n' ~ '<think>' ~ '\n' }}
155
+ {%- endif -%}