GLM-4.6.jinja 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. [gMASK]<sop>
  2. {%- if tools -%}
  3. <|system|>
  4. # Tools
  5. You may call one or more functions to assist with the user query.
  6. You are provided with function signatures within <tools></tools> XML tags:
  7. <tools>
  8. {% for tool in tools %}
  9. {{ tool | tojson(ensure_ascii=False) }}
  10. {% endfor %}
  11. </tools>
  12. For each function call, output the function name and arguments within the following XML format:
  13. <tool_call>{function-name}
  14. <arg_key>{arg-key-1}</arg_key>
  15. <arg_value>{arg-value-1}</arg_value>
  16. <arg_key>{arg-key-2}</arg_key>
  17. <arg_value>{arg-value-2}</arg_value>
  18. ...
  19. </tool_call>{%- endif -%}
  20. {%- macro visible_text(content) -%}
  21. {%- if content is string -%}
  22. {{- content }}
  23. {%- elif content is iterable and content is not mapping -%}
  24. {%- for item in content -%}
  25. {%- if item is mapping and item.type == 'text' -%}
  26. {{- item.text }}
  27. {%- elif item is string -%}
  28. {{- item }}
  29. {%- endif -%}
  30. {%- endfor -%}
  31. {%- else -%}
  32. {{- content }}
  33. {%- endif -%}
  34. {%- endmacro -%}
  35. {%- set ns = namespace(last_user_index=-1) %}
  36. {%- for m in messages %}
  37. {%- if m.role == 'user' %}
  38. {% set ns.last_user_index = loop.index0 -%}
  39. {%- endif %}
  40. {%- endfor %}
  41. {% for m in messages %}
  42. {%- if m.role == 'user' -%}<|user|>
  43. {{ visible_text(m.content) }}
  44. {{- '/nothink' if (enable_thinking is defined and not enable_thinking and not visible_text(m.content).endswith("/nothink")) else '' -}}
  45. {%- elif m.role == 'assistant' -%}
  46. <|assistant|>
  47. {%- set reasoning_content = '' %}
  48. {%- set content = visible_text(m.content) %}
  49. {%- if m.reasoning_content is string %}
  50. {%- set reasoning_content = m.reasoning_content %}
  51. {%- else %}
  52. {%- if '</think>' in content %}
  53. {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
  54. {%- set content = content.split('</think>')[-1].lstrip('\n') %}
  55. {%- endif %}
  56. {%- endif %}
  57. {%- if loop.index0 > ns.last_user_index and reasoning_content -%}
  58. {{ '\n<think>' + reasoning_content.strip() + '</think>'}}
  59. {%- else -%}
  60. {{ '\n<think></think>' }}
  61. {%- endif -%}
  62. {%- if content.strip() -%}
  63. {{ '\n' + content.strip() }}
  64. {%- endif -%}
  65. {% if m.tool_calls %}
  66. {% for tc in m.tool_calls %}
  67. {%- if tc.function %}
  68. {%- set tc = tc.function %}
  69. {%- endif %}
  70. {{ '\n<tool_call>' + tc.name }}
  71. {% set _args = tc.arguments or {} %}
  72. {% if _args is not mapping %}
  73. {{ raise_exception("Invalid tool call arguments passed: " + _args | string) }}
  74. {% endif %}
  75. {% for k, v in _args.items() %}
  76. <arg_key>{{ k }}</arg_key>
  77. <arg_value>{{ v | tojson(ensure_ascii=False) if v is not string else v }}</arg_value>
  78. {% endfor %}
  79. </tool_call>{% endfor %}
  80. {% endif %}
  81. {%- elif m.role == 'tool' -%}
  82. {%- if m.content is string -%}
  83. {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
  84. {{- '<|observation|>' }}
  85. {%- endif %}
  86. {{- '\n<tool_response>\n' }}
  87. {{- m.content }}
  88. {{- '\n</tool_response>' }}
  89. {%- else -%}
  90. <|observation|>{% for tr in m.content %}
  91. <tool_response>
  92. {{ tr.output if tr.output is defined else tr }}
  93. </tool_response>{% endfor -%}
  94. {% endif -%}
  95. {%- elif m.role == 'system' -%}
  96. <|system|>
  97. {{ visible_text(m.content) }}
  98. {%- endif -%}
  99. {%- endfor -%}
  100. {%- if add_generation_prompt -%}
  101. <|assistant|>{{- '\n<think></think>' if (enable_thinking is defined and not enable_thinking) else '' -}}
  102. {%- endif -%}