char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aldehir Rojaschat : implement minicpm5 parser (#24889)c818263f2

char/aliceai-t5-moe
8.8 KiB179 linesraw
1{{- bos_token }}{%- if tools %}
2    {%- set tool_definitions %}
3        {{- "# Tools\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
4        {%- for tool in tools %}
5            {{- "\n" }}
6            {{- tool | tojson(ensure_ascii=False) }}
7        {%- endfor %}
8        {{- '\n</tools>\n\nTool usage guidelines:\n- You may call zero or more functions. If no function calls are needed, just answer normally and do not include any <function ... </function>.\n- When calling a function, return an XML object within <function ... </function> using:\n<function name="function-name"><param name="param-name">param-value</param></function>\n- param-value may be multi-line. If it contains <, & or newline characters, wrap it in a CDATA block: <param name="param-name"><![CDATA[...multi-line value...]]></param>' }}
9    {%- endset %}
10    
11    {{- '<|im_start|>system\n' }}
12    {%- if messages[0].role == 'system' %}
13        {%- if '<tool_def_sep>' in messages[0].content %}
14            {{- messages[0].content.replace('<tool_def_sep>', tool_definitions) }}
15        {%- else %}
16            {{- messages[0].content + '\n\n' + tool_definitions }}
17        {%- endif %}
18    {%- else %}
19        {{- tool_definitions.lstrip() }}
20    {%- endif %}
21    {{- '<|im_end|>\n' }}
22{%- else %}
23    {%- if messages[0].role == 'system' %}
24        {{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
25    {%- endif %}
26{%- endif %}
27{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
28{%- for message in messages[::-1] %}
29    {%- set index = (messages|length - 1) - loop.index0 %}
30    {%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}
31        {%- set ns.multi_step_tool = false %}
32        {%- set ns.last_query_index = index %}
33    {%- endif %}
34{%- endfor %}
35{%- for message in messages %}
36    {%- if message.content is string %}
37        {%- set content = message.content %}
38    {%- else %}
39        {%- set content = '' %}
40    {%- endif %}
41    {%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
42        {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
43    {%- elif message.role == "assistant" %}
44        {%- set reasoning_content = '' %}
45        {%- if message.reasoning_content is string %}
46            {%- set reasoning_content = message.reasoning_content %}
47        {%- else %}
48            {%- if '</think>' in content %}
49                {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
50                {%- set content = content.split('</think>')[-1].lstrip('\n') %}
51            {%- endif %}
52        {%- endif %}
53        
54        {%- if message.tool_calls %}
55            {%- set content_parts = content.split('<tool_sep>') %}
56            {%- set processed_content = content_parts[0] %}
57            {%- set tool_calls_count = message.tool_calls|length %}
58            {%- set tool_sep_count = content_parts|length - 1 %}
59            {%- set min_count = [tool_calls_count, tool_sep_count]|min %}
60            
61            {%- for i in range(1, content_parts|length) %}
62                {%- set tool_index = i - 1 %}
63                {%- if tool_index < tool_calls_count %}
64                    {%- set tool_call = message.tool_calls[tool_index] %}
65                    {%- if tool_call.function %}
66                        {%- set tool_call = tool_call.function %}
67                    {%- endif %}
68                    {%- set single_tool_xml %}
69                        {{- '<function name="' ~ tool_call.name ~ '">' }}
70                        {%- if tool_call.arguments %}
71                            {%- set args_dict = tool_call.arguments %}
72                            {%- for param_name, param_value in args_dict.items() %}
73                                {{- '<param name="' ~ param_name ~ '">' }}
74                                {%- if param_value is string and ('<' in param_value or '&' in param_value or '\n' in param_value) %}
75                                    {{- '<![CDATA[' + param_value + ']]>' }}
76                                {%- else %}
77                                    {{- param_value }}
78                                {%- endif %}
79                                {{- '</param>' }}
80                            {%- endfor %}
81                        {%- endif %}
82                        {{- '</function>' }}
83                    {%- endset %}
84                    {%- set processed_content = processed_content + single_tool_xml + content_parts[i] %}
85                {%- else %}
86                    {%- set processed_content = processed_content + content_parts[i] %}
87                {%- endif %}
88            {%- endfor %}
89            
90            {%- if tool_calls_count > tool_sep_count %}
91                {%- for remaining_index in range(tool_sep_count, tool_calls_count) %}
92                    {%- set tool_call = message.tool_calls[remaining_index] %}
93                    {%- if tool_call.function %}
94                        {%- set tool_call = tool_call.function %}
95                    {%- endif %}
96                    {%- set remaining_tool_xml %}
97                        {{- '<function name="' ~ tool_call.name ~ '">' }}
98                        {%- if tool_call.arguments %}
99                            {%- set args_dict = tool_call.arguments %}
100                            {%- for param_name, param_value in args_dict.items() %}
101                                {{- '<param name="' ~ param_name ~ '">' }}
102                                {%- if param_value is string and ('<' in param_value or '&' in param_value or '\n' in param_value) %}
103                                    {{- '<![CDATA[' + param_value + ']]>' }}
104                                {%- else %}
105                                    {{- param_value }}
106                                {%- endif %}
107                                {{- '</param>' }}
108                            {%- endfor %}
109                        {%- endif %}
110                        {{- '</function>' }}
111                    {%- endset %}
112                    {%- set processed_content = processed_content + remaining_tool_xml %}
113                {%- endfor %}
114            {%- endif %}
115            
116            {%- set content = processed_content %}
117        {%- endif %}
118        
119        {%- if loop.index0 > ns.last_query_index %}
120            {%- if reasoning_content %}
121                {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
122            {%- else %}
123                {{- '<|im_start|>' + message.role + '\n' + content }}
124            {%- endif %}
125        {%- else %}
126            {{- '<|im_start|>' + message.role + '\n' + content }}
127        {%- endif %}
128        
129        {%- if message.tool_calls and not has_tool_sep %}
130            {%- for tool_call in message.tool_calls %}
131                {%- if (loop.first and content) or (not loop.first) %}
132                    {{- '\n' }}
133                {%- endif %}
134                {%- if tool_call.function %}
135                    {%- set tool_call = tool_call.function %}
136                {%- endif %}
137                {{- '<function name="' ~ tool_call.name ~ '">' }}
138                {%- if tool_call.arguments %}
139                    {%- set args_dict = tool_call.arguments %}
140                    {%- for param_name, param_value in args_dict.items() %}
141                        {{- '<param name="' ~ param_name ~ '">' }}
142                        {%- if param_value is string and ('<' in param_value or '&' in param_value or '\n' in param_value) %}
143                            {{- '<![CDATA[' + param_value + ']]>' }}
144                        {%- else %}
145                            {{- param_value }}
146                        {%- endif %}
147                        {{- '</param>' }}
148                    {%- endfor %}
149                {%- endif %}
150                {{- '</function>' }}
151            {%- endfor %}
152        {%- endif %}
153        {{- '<|im_end|>\n' }}
154    {%- elif message.role == "tool" %}
155        {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
156            {{- '<|im_start|>user' }}
157        {%- endif %}
158        {{- '\n<tool_response>\n' }}
159        {%- if message.content is string %}
160            {{- content }}
161        {%- else %}
162            {{- message.content | tojson(ensure_ascii=False) }}
163        {%- endif %}
164        {{- '\n</tool_response>' }}
165        {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
166            {{- '<|im_end|>\n' }}
167        {%- endif %}
168    {%- endif %}
169{%- endfor %}
170{%- if add_generation_prompt %}
171    {{- '<|im_start|>assistant\n' }}
172    {%- if enable_thinking is defined %}
173        {%- if enable_thinking is false %}
174            {{- '<think>\n\n</think>\n\n' }}
175        {%- elif enable_thinking is true %}
176            {{- '<think>\n' }}
177        {%- endif %}
178    {%- endif %}
179{%- endif %}