Corrected jinja template with tool Support works with PR llama.cpp/pull/15186
#9
by
xbruce22
- opened
- chat_template.jinja +119 -0
chat_template.jinja
ADDED
@@ -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 user_content = visible_text(m.content) -%}
|
42 |
+
{%- if not ("tool_response" in user_content) %}
|
43 |
+
{% set ns.last_user_index = loop.index0 -%}
|
44 |
+
{%- endif -%}
|
45 |
+
{%- endif %}
|
46 |
+
{%- endfor %}
|
47 |
+
{% for m in messages %}
|
48 |
+
{%- if m.role == 'user' -%}<|user|>
|
49 |
+
{%- set user_content = visible_text(m.content) -%}
|
50 |
+
{{ user_content }}
|
51 |
+
{%- if enable_thinking is defined and not enable_thinking -%}
|
52 |
+
{%- if not user_content.endswith("/nothink") -%}
|
53 |
+
{{- '/nothink' -}}
|
54 |
+
{%- endif -%}
|
55 |
+
{%- endif -%}
|
56 |
+
{%- elif m.role == 'assistant' -%}
|
57 |
+
<|assistant|>
|
58 |
+
{%- set reasoning_content = '' %}
|
59 |
+
{%- set content = visible_text(m.content) %}
|
60 |
+
{%- if m.reasoning_content is string %}
|
61 |
+
{%- set reasoning_content = m.reasoning_content %}
|
62 |
+
{%- else %}
|
63 |
+
{%- if '</think>' in content %}
|
64 |
+
{%- set think_parts = content.split('</think>') %}
|
65 |
+
{%- if think_parts|length > 1 %}
|
66 |
+
{%- set before_end_think = think_parts[0] %}
|
67 |
+
{%- set after_end_think = think_parts[1] %}
|
68 |
+
{%- set think_start_parts = before_end_think.split('<think>') %}
|
69 |
+
{%- if think_start_parts|length > 1 %}
|
70 |
+
{%- set reasoning_content = think_start_parts[-1].lstrip('\n') %}
|
71 |
+
{%- endif %}
|
72 |
+
{%- set content = after_end_think.lstrip('\n') %}
|
73 |
+
{%- endif %}
|
74 |
+
{%- endif %}
|
75 |
+
{%- endif %}
|
76 |
+
{%- if loop.index0 > ns.last_user_index and reasoning_content -%}
|
77 |
+
{{ '\n<think>' + reasoning_content.strip() + '</think>'}}
|
78 |
+
{%- else -%}
|
79 |
+
{{ '\n<think></think>' }}
|
80 |
+
{%- endif -%}
|
81 |
+
{%- if content.strip() -%}
|
82 |
+
{{ '\n' + content.strip() }}
|
83 |
+
{%- endif -%}
|
84 |
+
{% if m.tool_calls %}
|
85 |
+
{% for tc in m.tool_calls %}
|
86 |
+
{%- if tc.function %}
|
87 |
+
{%- set tc = tc.function %}
|
88 |
+
{%- endif %}
|
89 |
+
{{ '\n<tool_call>' + tc.name }}
|
90 |
+
{% set _args = tc.arguments %}
|
91 |
+
{% for k, v in _args.items() %}
|
92 |
+
<arg_key>{{ k }}</arg_key>
|
93 |
+
<arg_value>{{ v | tojson if v is not string else v }}</arg_value>
|
94 |
+
{% endfor %}
|
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 -%}
|