Skip to content

Commit 84bcbd2

Browse files
committed
added GLM template
1 parent f1c0521 commit 84bcbd2

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

models/templates/glm_4_5.jinja

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
[gMASK]<sop>
2+
{%- if tools -%}
3+
<|system|>
4+
# Tools
5+
6+
You may call one or more functions to assist with the user query.
7+
8+
You are provided with function signatures within <tools></tools> XML tags:
9+
<tools>
10+
{% for tool in tools %}
11+
{{ tool | tojson }}
12+
{% endfor %}
13+
</tools>
14+
15+
For each function call, output the function name and arguments within the following XML format:
16+
<tool_call>{function-name}
17+
<arg_key>{arg-key-1}</arg_key>
18+
<arg_value>{arg-value-1}</arg_value>
19+
<arg_key>{arg-key-2}</arg_key>
20+
<arg_value>{arg-value-2}</arg_value>
21+
...
22+
</tool_call>{%- endif -%}
23+
{%- macro visible_text(content) -%}
24+
{%- if content is string -%}
25+
{{- content }}
26+
{%- elif content is iterable and content is not mapping -%}
27+
{%- for item in content -%}
28+
{%- if item is mapping and item.type == 'text' -%}
29+
{{- item.text }}
30+
{%- elif item is string -%}
31+
{{- item }}
32+
{%- endif -%}
33+
{%- endfor -%}
34+
{%- else -%}
35+
{{- content }}
36+
{%- endif -%}
37+
{%- endmacro -%}
38+
{%- set ns = namespace(last_user_index=-1) %}
39+
{%- for m in messages %}
40+
{%- if m.role == 'user' %}
41+
{% set ns.last_user_index = loop.index0 -%}
42+
{%- endif %}
43+
{%- endfor %}
44+
{% for m in messages %}
45+
{%- if m.role == 'user' -%}<|user|>
46+
{%- set user_content = visible_text(m.content) -%}
47+
{{ user_content }}
48+
{{- '/nothink' if (enable_thinking is defined and not enable_thinking and not user_content.endswith("/nothink")) else '' -}}
49+
{%- elif m.role == 'assistant' -%}
50+
<|assistant|>
51+
{%- set reasoning_content = '' %}
52+
{%- set content = visible_text(m.content) %}
53+
{%- if m.reasoning_content is string %}
54+
{%- set reasoning_content = m.reasoning_content %}
55+
{%- else %}
56+
{%- if '</think>' in content %}
57+
{%- set parts = content.split('</think>') -%}
58+
{%- set before_first_close = parts | first -%}
59+
{%- set inner_parts = before_first_close.rstrip('\n').split('<think>') -%}
60+
{%- set extracted_reasoning = inner_parts | last -%}
61+
{%- set reasoning_content = extracted_reasoning.lstrip('\n') -%}
62+
{%- set after_last_close = parts | last -%}
63+
{%- set content = after_last_close.lstrip('\n') -%}
64+
{%- endif %}
65+
{%- endif %}
66+
{%- if loop.index0 > ns.last_user_index and reasoning_content -%}
67+
{{ '\n<think>' + reasoning_content.strip() + '</think>'}}
68+
{%- else -%}
69+
{{ '\n<think></think>' }}
70+
{%- endif -%}
71+
{%- if content.strip() -%}
72+
{{ '\n' + content.strip() }}
73+
{%- endif -%}
74+
{% if m.tool_calls %}
75+
{% for tc in m.tool_calls %}
76+
{%- if tc.function %}
77+
{%- set tc_obj = tc.function %}
78+
{%- else %}
79+
{%- set tc_obj = tc %}
80+
{%- endif %}
81+
{{ '\n<tool_call>' + tc_obj.name }}
82+
{%- if tc_obj.arguments is mapping -%}
83+
{%- for k, v in tc_obj.arguments.items() -%}
84+
85+
<arg_key>{{ k }}</arg_key>
86+
<arg_value>{{ v | tojson if v is not string else v }}</arg_value>
87+
{%- endfor -%}
88+
{%- else -%}
89+
{#- Arguments came as string - this shouldn't happen with polyfills disabled -#}
90+
{#- Output as single argument for debugging -#}
91+
92+
<arg_key>raw_arguments</arg_key>
93+
<arg_value>{{ tc_obj.arguments }}</arg_value>
94+
{%- endif -%}
95+
</tool_call>{% endfor %}
96+
{% endif %}
97+
{%- elif m.role == 'tool' -%}
98+
{%- if m.content is string -%}
99+
{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
100+
{{- '<|observation|>' }}
101+
{%- endif %}
102+
{{- '\n<tool_response>\n' }}
103+
{{- m.content }}
104+
{{- '\n</tool_response>' }}
105+
{%- else -%}
106+
<|observation|>{% for tr in m.content %}
107+
108+
<tool_response>
109+
{{ tr.output if tr.output is defined else tr }}
110+
</tool_response>{% endfor -%}
111+
{% endif -%}
112+
{%- elif m.role == 'system' -%}
113+
<|system|>
114+
{{ visible_text(m.content) }}
115+
{%- endif -%}
116+
{%- endfor -%}
117+
{%- if add_generation_prompt -%}
118+
<|assistant|>{{- '\n<think></think>' if (enable_thinking is defined and not enable_thinking) else '' -}}
119+
{%- endif -%}

0 commit comments

Comments
 (0)