chat_template.jinja
3.0 KB · 86 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}<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 -%}
17 {%- macro visible_text(content) -%}
18 {%- if content is string -%}
19 {{- content }}
20 {%- elif content is iterable and content is not mapping -%}
21 {%- for item in content -%}
22 {%- if item is mapping and item.type == 'text' -%}
23 {{- item.text }}
24 {%- elif item is string -%}
25 {{- item }}
26 {%- endif -%}
27 {%- endfor -%}
28 {%- else -%}
29 {{- content }}
30 {%- endif -%}
31 {%- endmacro -%}
32 {%- set ns = namespace(last_user_index=-1) %}
33 {%- for m in messages %}
34 {%- if m.role == 'user' %}
35 {%- set ns.last_user_index = loop.index0 -%}
36 {%- endif %}
37 {%- endfor %}
38 {%- for m in messages -%}
39 {%- if m.role == 'user' -%}<|user|>{{ visible_text(m.content) }}
40 {%- elif m.role == 'assistant' -%}
41 <|assistant|>
42 {%- set reasoning_content = '' %}
43 {%- set content = visible_text(m.content) %}
44 {%- if m.reasoning_content is string %}
45 {%- set reasoning_content = m.reasoning_content %}
46 {%- else %}
47 {%- if '</think>' in content %}
48 {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
49 {%- set content = content.split('</think>')[-1].lstrip('\n') %}
50 {%- endif %}
51 {%- endif %}
52 {%- if ((clear_thinking is defined and not clear_thinking) or loop.index0 > ns.last_user_index) and reasoning_content -%}
53 {{ '<think>' + reasoning_content.strip() + '</think>'}}
54 {%- else -%}
55 {{ '</think>' }}
56 {%- endif -%}
57 {%- if content.strip() -%}
58 {{ content.strip() }}
59 {%- endif -%}
60 {% if m.tool_calls %}
61 {% for tc in m.tool_calls %}
62 {%- if tc.function %}
63 {%- set tc = tc.function %}
64 {%- endif %}
65 {{- '<tool_call>' + tc.name -}}
66 {% 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 %}
67 {% endif %}
68 {%- elif m.role == 'tool' -%}
69 {%- if m.content is string -%}
70 {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
71 {{- '<|observation|>' }}
72 {%- endif %}
73 {{- '<tool_response>' }}
74 {{- m.content }}
75 {{- '</tool_response>' }}
76 {%- else -%}
77 <|observation|>{% for tr in m.content %}
78 <tool_response>{{ tr.output if tr.output is defined else tr }}</tool_response>{% endfor -%}
79 {% endif -%}
80 {%- elif m.role == 'system' -%}
81 <|system|>{{ visible_text(m.content) }}
82 {%- endif -%}
83 {%- endfor -%}
84 {%- if add_generation_prompt -%}
85 <|assistant|>{{- '</think>' if (enable_thinking is defined and not enable_thinking) else '<think>' -}}
86 {%- endif -%}