chat_template.jinja
4.5 KB · 141 lines · plaintext Raw
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(ensure_ascii=False) }}
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 mapping and (item.type == 'image' or 'image' in item) -%}
31 <|begin_of_image|><|image|><|end_of_image|>
32 {%- elif item is mapping and (item.type == 'video' or 'video' in item) -%}
33 <|begin_of_video|><|video|><|end_of_video|>
34 {%- elif item is string -%}
35 {{- item }}
36 {%- endif -%}
37 {%- endfor -%}
38 {%- else -%}
39 {{- content }}
40 {%- endif -%}
41 {%- endmacro -%}
42 {%- set ns = namespace(last_user_index=-1) %}
43 {%- for m in messages %}
44 {%- if m.role == 'user' %}
45 {% set ns.last_user_index = loop.index0 -%}
46 {%- endif %}
47 {%- endfor %}
48 {% for m in messages %}
49 {%- if m.role == 'user' -%}<|user|>
50 {% if m.content is string %}
51 {{ m.content }}
52 {%- else %}
53 {%- for item in m.content %}
54 {% if item.type == 'video' or 'video' in item %}
55 <|begin_of_video|><|video|><|end_of_video|>{% elif item.type == 'image' or 'image' in item %}
56 <|begin_of_image|><|image|><|end_of_image|>{% elif item.type == 'text' %}
57 {{ item.text }}
58 {%- endif %}
59 {%- endfor %}
60 {%- endif %}
61 {{- '/nothink' if (enable_thinking is defined and not enable_thinking and not visible_text(m.content).endswith("/nothink")) else '' -}}
62 {%- elif m.role == 'assistant' -%}
63 <|assistant|>
64 {%- set reasoning_content = '' %}
65 {%- set content = visible_text(m.content) %}
66 {%- if m.reasoning_content is string %}
67 {%- set reasoning_content = m.reasoning_content %}
68 {%- else %}
69 {%- if '</think>' in content %}
70 {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
71 {%- set content = content.split('</think>')[-1].lstrip('\n') %}
72 {%- endif %}
73 {%- endif %}
74 {%- if loop.index0 > ns.last_user_index and reasoning_content -%}
75 {{ '\n<think>' + reasoning_content.strip() + '</think>'}}
76 {%- else -%}
77 {{ '\n<think></think>' }}
78 {%- endif -%}
79 {%- if content.strip() -%}
80 {{ '\n' + content.strip() }}
81 {%- endif -%}
82 {% if m.tool_calls %}
83 {% for tc in m.tool_calls %}
84 {%- if tc.function %}
85 {%- set tc = tc.function %}
86 {%- endif %}
87 {{ '\n<tool_call>' + tc.name }}
88 {% set _args = tc.arguments %}
89 {% for k, v in _args.items() %}
90 <arg_key>{{ k }}</arg_key>
91 <arg_value>{{ v | tojson(ensure_ascii=False) if v is not string else v }}</arg_value>
92 {% endfor %}
93 </tool_call>{% endfor %}
94 {% endif %}
95 {%- elif m.role == 'tool' -%}
96 {%- if m.content is string -%}
97 {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
98 {{- '<|observation|>' }}
99 {%- endif %}
100 {{- '\n<tool_response>\n' }}
101 {{- m.content }}
102 {{- '\n</tool_response>' }}
103 {% elif m.content is iterable and m.content is not mapping %}
104 {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
105 {{- '<|observation|>' }}
106 {%- endif %}
107 {{- '\n<tool_response>\n' }}
108 {%- for tr in m.content -%}
109 {%- if tr is mapping and tr.type is defined -%}
110 {%- set t = tr.type | lower -%}
111 {%- if t == 'text' and tr.text is defined -%}
112 {{ tr.text }}
113 {%- elif t in ['image', 'image_url'] -%}
114 <|begin_of_image|><|image|><|end_of_image|>
115 {%- elif t in ['video', 'video_url'] -%}
116 <|begin_of_video|><|video|><|end_of_video|>
117 {%- else -%}
118 {{ tr | tojson(ensure_ascii=False) }}
119 {%- endif -%}
120 {%- else -%}
121 {{ tr.output if tr.output is defined else tr }}
122 {%- endif -%}
123 {%- endfor -%}
124 {{- '\n</tool_response>' }}
125 {%- else -%}
126 <|observation|>{% for tr in m.content %}
127
128 <tool_response>
129 {{ tr.output if tr.output is defined else tr }}
130 </tool_response>{% endfor -%}
131 {% endif -%}
132 {%- elif m.role == 'system' -%}
133 <|system|>
134 {{ visible_text(m.content) }}
135 {%- endif -%}
136 {%- endfor -%}
137 {%- if add_generation_prompt -%}
138 <|assistant|>
139 {{'<think></think>\n' if (enable_thinking is defined and not enable_thinking) else ''}}
140 {%- endif -%}
141