glm53-chat-template.jinja 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. [gMASK]<sop>
  2. {#- Thinking off maps to low effort; the model was never trained on an empty <think></think>. -#}
  3. {%- set thinking_off = (thinking is defined and not thinking) or (enable_thinking is defined and not enable_thinking) -%}
  4. {%- set effective_reasoning_effort = reasoning_effort if reasoning_effort is defined and reasoning_effort in ['low', 'high'] else ('low' if thinking_off else 'max') -%}
  5. {%- if effective_reasoning_effort is not none -%}<|system|>Reasoning Effort: {{ effective_reasoning_effort | capitalize }}{%- endif -%}
  6. {%- set clear_thinking = clear_thinking if clear_thinking is defined else false -%}
  7. {%- if tools -%}
  8. {%- macro tool_to_json(tool) -%}
  9. {%- set ns_tool = namespace(first=true) -%}
  10. {{ '{' -}}
  11. {%- for k, v in tool.items() -%}
  12. {%- if k != 'defer_loading' and k != 'strict' -%}
  13. {%- if not ns_tool.first -%}{{- ', ' -}}{%- endif -%}
  14. {%- set ns_tool.first = false -%}
  15. "{{ k }}": {{ v | tojson(ensure_ascii=False) }}
  16. {%- endif -%}
  17. {%- endfor -%}
  18. {{- '}' -}}
  19. {%- endmacro -%}
  20. {%- macro tool_references_to_response(refs) -%}
  21. {{- '<tool_response><tools>\n' -}}
  22. {%- for tr in refs -%}
  23. {%- for tool in tools -%}
  24. {%- if 'function' in tool -%}
  25. {%- set tool = tool['function'] -%}
  26. {%- endif -%}
  27. {%- if tool.name == tr.name -%}
  28. {{- tool_to_json(tool) + '\n' -}}
  29. {%- endif -%}
  30. {%- endfor -%}
  31. {%- endfor -%}
  32. {{- '</tools></tool_response>' -}}
  33. {%- endmacro -%}
  34. <|system|>
  35. # Tools
  36. You may call one or more functions to assist with the user query.
  37. You are provided with function signatures within <tools></tools> XML tags:
  38. <tools>
  39. {% for tool in tools %}
  40. {%- if 'function' in tool -%}
  41. {%- set tool = tool['function'] -%}
  42. {%- endif -%}
  43. {% if tool.defer_loading is not defined or not tool.defer_loading %}
  44. {{ tool_to_json(tool) }}
  45. {% endif %}
  46. {% endfor %}
  47. </tools>
  48. For each function call, output the function name and arguments within the following XML format:
  49. <tool_call>{function-name}<arg_key>{arg-key-1}</arg_key><arg_value>{arg-value-1}</arg_value><arg_key>{arg-key-2}</arg_key><arg_value>{arg-value-2}</arg_value>...</tool_call>{%- endif -%}
  50. {%- macro emit_image() -%}<|begin_of_image|><|image|><|end_of_image|>{%- endmacro -%}
  51. {%- macro emit_video() -%}<|begin_of_video|><|video|><|end_of_video|>{%- endmacro -%}
  52. {%- macro emit_audio() -%}<|begin_of_audio|><|end_of_audio|>{%- endmacro -%}
  53. {%- macro visible_text(content) -%}
  54. {%- if content is string -%}
  55. {{- content -}}
  56. {%- elif content is iterable and content is not mapping -%}
  57. {%- for item in content -%}
  58. {%- if item is mapping and item.type == 'text' -%}
  59. {{- item.text -}}
  60. {%- elif item is string -%}
  61. {{- item -}}
  62. {%- elif item is mapping and item.type in ['image', 'image_url'] -%}
  63. {{- emit_image() -}}
  64. {%- elif item is mapping and item.type in ['video', 'video_url'] -%}
  65. {{- emit_video() -}}
  66. {%- elif item is mapping and item.type in ['audio', 'audio_url', 'input_audio'] -%}
  67. {{- emit_audio() -}}
  68. {%- endif -%}
  69. {%- endfor -%}
  70. {%- else -%}
  71. {{- content }}
  72. {%- endif -%}
  73. {%- endmacro -%}
  74. {%- macro tool_response(text) -%}
  75. {{- '<tool_response>' + text + '</tool_response>' -}}
  76. {%- endmacro -%}
  77. {%- macro render_tool_response(m) -%}
  78. {%- if m.content is string -%}
  79. {{- tool_response(m.content) -}}
  80. {%- elif m.content and m.content is not mapping and m.content.0.type == "tool_reference" -%}
  81. {{- tool_references_to_response(m.content) -}}
  82. {%- elif is_list_of_outputs(m) -%}
  83. {%- for tr in m.content -%}
  84. {%- if tr.output is iterable and tr.output is not string and tr.output is not mapping and tr.output and tr.output.0.type == "tool_reference" -%}
  85. {{- tool_references_to_response(tr.output) -}}
  86. {%- else -%}
  87. {{- tool_response(visible_text(tr.output)) -}}
  88. {%- endif -%}
  89. {%- endfor -%}
  90. {%- else -%}
  91. {{- tool_response(visible_text(m.content)) -}}
  92. {%- endif -%}
  93. {%- endmacro -%}
  94. {%- macro id_of(obj) -%}
  95. {%- if obj.tool_call_id -%}
  96. {{- obj.tool_call_id -}}
  97. {%- elif obj.id -%}
  98. {{- obj.id -}}
  99. {%- endif -%}
  100. {%- endmacro -%}
  101. {%- macro is_list_of_outputs(m) -%}
  102. {%- if m.content and m.content.0.output is defined -%}1{%- endif -%}
  103. {%- endmacro -%}
  104. {%- macro has_dup_tool_result_id(lo, hi, target) -%}
  105. {%- set ns_cnt = namespace(n=0) -%}
  106. {%- for k in range(lo, hi + 1) -%}
  107. {%- set m = messages[k] -%}
  108. {%- if is_list_of_outputs(m) -%}
  109. {%- for entry in m.content -%}
  110. {%- if id_of(entry) == target -%}
  111. {%- set ns_cnt.n = ns_cnt.n + 1 -%}
  112. {%- endif -%}
  113. {%- endfor -%}
  114. {%- elif id_of(m) == target -%}
  115. {%- set ns_cnt.n = ns_cnt.n + 1 -%}
  116. {%- endif -%}
  117. {%- if ns_cnt.n > 1 -%}{%- break -%}{%- endif -%}
  118. {%- endfor -%}
  119. {%- if ns_cnt.n > 1 -%}1{%- endif -%}
  120. {%- endmacro -%}
  121. {%- macro tc_id_exists(tcs, target) -%}
  122. {%- set ns_f = namespace(found=false) -%}
  123. {%- for tc in tcs -%}
  124. {%- if id_of(tc) == target -%}
  125. {%- set ns_f.found = true -%}
  126. {%- break -%}
  127. {%- endif -%}
  128. {%- endfor -%}
  129. {%- if ns_f.found -%}1{%- endif -%}
  130. {%- endmacro -%}
  131. {%- set ns = namespace(last_user_index=-1) -%}
  132. {%- for m in messages %}
  133. {%- if m.role == 'user' %}
  134. {%- set ns.last_user_index = loop.index0 -%}
  135. {%- endif %}
  136. {%- endfor %}
  137. {%- for m in messages -%}
  138. {%- if m.role == 'user' -%}<|user|>{{ visible_text(m.content) }}
  139. {%- elif m.role == 'assistant' -%}
  140. <|assistant|>
  141. {%- set content = visible_text(m.content) %}
  142. {%- if m.reasoning_content is string %}
  143. {%- set reasoning_content = m.reasoning_content %}
  144. {%- elif '</think>' in content %}
  145. {%- set reasoning_content = content.split('</think>')[0].split('<think>')[-1] %}
  146. {%- set content = content.split('</think>')[-1] %}
  147. {%- endif %}
  148. {%- if (not clear_thinking or loop.index0 > ns.last_user_index) and reasoning_content is defined -%}
  149. {{ '<think>' + reasoning_content + '</think>'}}
  150. {%- else -%}
  151. {{ '<think></think>' }}
  152. {%- endif -%}
  153. {%- if content.strip() -%}
  154. {{ content.strip() }}
  155. {%- endif -%}
  156. {% if m.tool_calls %}
  157. {% for tc in m.tool_calls %}
  158. {%- if tc.function %}
  159. {%- set tc = tc.function %}
  160. {%- endif %}
  161. {{- '<tool_call>' + tc.name -}}
  162. {% set _args = tc.arguments %}{% for k, v in _args.items() %}<arg_key>{{ k }}</arg_key><arg_value>{{ v | tojson(ensure_ascii=False) if v is not string else v }}</arg_value>{% endfor %}</tool_call>{% endfor %}
  163. {% endif %}
  164. {%- elif m.role == 'tool' -%}
  165. {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
  166. {{- '<|observation|>' -}}
  167. {%- set block_start = loop.index0 -%}
  168. {%- set ns_blk = namespace(end=block_start) -%}
  169. {%- for j in range(block_start, messages|length) -%}
  170. {%- if messages[j].role == 'tool' -%}
  171. {%- set ns_blk.end = j -%}
  172. {%- else -%}
  173. {%- break -%}
  174. {%- endif -%}
  175. {%- endfor -%}
  176. {%- set ns_a = namespace(tool_calls=none) -%}
  177. {%- if block_start > 0 and messages[block_start - 1].role == 'assistant' and messages[block_start - 1].tool_calls -%}
  178. {%- set ns_a.tool_calls = messages[block_start - 1].tool_calls -%}
  179. {%- endif -%}
  180. {%- set ns_chk = namespace(can_sort=true) -%}
  181. {%- if not ns_a.tool_calls -%}
  182. {%- set ns_chk.can_sort = false -%}
  183. {%- else -%}
  184. {%- for k in range(block_start, ns_blk.end + 1) -%}
  185. {%- set m = messages[k] -%}
  186. {%- if is_list_of_outputs(m) -%}
  187. {%- for entry in m.content -%}
  188. {%- set eid = id_of(entry) -%}
  189. {%- if not eid -%}
  190. {%- set ns_chk.can_sort = false -%}
  191. {%- elif has_dup_tool_result_id(block_start, ns_blk.end, eid) -%}
  192. {%- set ns_chk.can_sort = false -%}
  193. {%- elif not tc_id_exists(ns_a.tool_calls, eid) -%}
  194. {%- set ns_chk.can_sort = false -%}
  195. {%- endif -%}
  196. {%- endfor -%}
  197. {%- else -%}
  198. {%- set tk_id = id_of(m) -%}
  199. {%- if not tk_id -%}
  200. {%- set ns_chk.can_sort = false -%}
  201. {%- elif has_dup_tool_result_id(block_start, ns_blk.end, tk_id) -%}
  202. {%- set ns_chk.can_sort = false -%}
  203. {%- elif not tc_id_exists(ns_a.tool_calls, tk_id) -%}
  204. {%- set ns_chk.can_sort = false -%}
  205. {%- endif -%}
  206. {%- endif -%}
  207. {%- endfor -%}
  208. {%- for i in range(ns_a.tool_calls | length) -%}
  209. {%- set tc_id = id_of(ns_a.tool_calls[i]) -%}
  210. {%- if not tc_id -%}
  211. {%- set ns_chk.can_sort = false -%}
  212. {%- endif -%}
  213. {%- for j in range(i + 1, ns_a.tool_calls | length) -%}
  214. {%- if id_of(ns_a.tool_calls[j]) == tc_id -%}
  215. {%- set ns_chk.can_sort = false -%}
  216. {%- endif -%}
  217. {%- endfor -%}
  218. {%- endfor -%}
  219. {%- endif -%}
  220. {%- if ns_chk.can_sort -%}
  221. {%- for tc in ns_a.tool_calls -%}
  222. {%- set tc_id = id_of(tc) -%}
  223. {%- for k in range(block_start, ns_blk.end + 1) -%}
  224. {%- set m = messages[k] -%}
  225. {%- if is_list_of_outputs(m) -%}
  226. {%- for entry in m.content -%}
  227. {%- set eid = id_of(entry) -%}
  228. {%- if eid == tc_id -%}
  229. {%- if entry.output is iterable and entry.output is not string and entry.output is not mapping and entry.output and entry.output.0.type == "tool_reference" -%}
  230. {{- tool_references_to_response(entry.output) -}}
  231. {%- else -%}
  232. {{- tool_response(visible_text(entry.output)) -}}
  233. {%- endif -%}
  234. {%- endif -%}
  235. {%- endfor -%}
  236. {%- else -%}
  237. {%- set tk_id = id_of(m) -%}
  238. {%- if tk_id == tc_id -%}
  239. {{- render_tool_response(m) -}}
  240. {%- endif -%}
  241. {%- endif -%}
  242. {%- endfor -%}
  243. {%- endfor -%}
  244. {%- else -%}
  245. {%- for k in range(block_start, ns_blk.end + 1) -%}
  246. {{- render_tool_response(messages[k]) -}}
  247. {%- endfor -%}
  248. {%- endif -%}
  249. {% endif -%}
  250. {%- elif m.role == 'system' -%}
  251. <|system|>{{ visible_text(m.content) }}
  252. {%- endif -%}
  253. {%- endfor -%}
  254. {%- if add_generation_prompt -%}
  255. <|assistant|>{{- '<think>' -}}
  256. {%- endif -%}