char-fork/llama.cpp

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

Piotr Wilkin (ilintar)common/parser: add proper reasoning tag prefill reading (#20424)5e54d51b1

char/aliceai-t5-moe
3.3 KiB71 linesraw
1{% if not add_generation_prompt is defined -%}
2  {%- set add_generation_prompt = false -%}
3{%- endif -%}
4{%- if not thinking is defined -%}
5  {%- if enable_thinking is defined -%}
6    {%- set thinking = enable_thinking -%}
7    {%- else -%}
8    {%- set thinking = false -%}
9  {%- endif -%}
10{%- endif -%}
11{%- set ns = namespace(is_first=false, is_tool=false, system_prompt='', is_first_sp=true, is_last_user=false) -%}
12{%- for message in messages -%}
13  {%- if message['role'] == 'system' -%}
14    {%- if ns.is_first_sp -%}
15      {%- set ns.system_prompt = ns.system_prompt + message['content'] -%}
16      {%- set ns.is_first_sp = false -%}
17      {%- else -%}
18      {%- set ns.system_prompt = ns.system_prompt + '
19
20' + message['content'] -%}
21    {%- endif -%}
22  {%- endif -%}
23{%- endfor -%}{{ bos_token }}{{ ns.system_prompt }}
24{%- for message in messages -%}
25  {%- if message['role'] == 'user' -%}
26    {%- set ns.is_tool = false -%}
27    {%- set ns.is_first = false -%}
28    {%- set ns.is_last_user = true -%}{{'<|User|>' + message['content']}}
29  {%- endif -%}
30  {%- if message['role'] == 'assistant' and message['tool_calls'] -%}
31    {%- if ns.is_last_user -%}{{'<|Assistant|><think></think>'}}
32    {%- endif -%}
33    {%- set ns.is_last_user = false -%}
34    {%- set ns.is_first = false -%}
35    {%- set ns.is_tool = false -%}
36    {%- for tool in message['tool_calls'] -%}
37      {%- if not ns.is_first -%}
38        {%- if not message['content'] -%}{{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments'] | tojson + '<|tool▁call▁end|>'}}
39          {%- else -%}{{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments'] | tojson + '<|tool▁call▁end|>'}}
40        {%- endif -%}
41        {%- set ns.is_first = true -%}
42        {%- else -%}{{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments'] | tojson + '<|tool▁call▁end|>'}}
43      {%- endif -%}
44    {%- endfor -%}{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
45  {%- endif -%}
46  {%- if message['role'] == 'assistant' and not message['tool_calls'] -%}
47    {%- if ns.is_last_user -%}{{'<|Assistant|>'}}
48      {%- if message['prefix'] is defined and message['prefix'] and thinking -%}{{'<think>'}}
49        {%- else -%}{{'<think></think>'}}
50      {%- endif -%}
51    {%- endif -%}
52    {%- set ns.is_last_user = false -%}
53    {%- if ns.is_tool -%}{{message['content'] + '<|end▁of▁sentence|>'}}
54      {%- set ns.is_tool = false -%}
55      {%- else -%}
56      {%- set content = message['content'] -%}
57      {%- if '</think>' in content -%}
58        {%- set content = content.split('</think>', 1)[1] -%}
59      {%- endif -%}{{content + '<|end▁of▁sentence|>'}}
60    {%- endif -%}
61  {%- endif -%}
62  {%- if message['role'] == 'tool' -%}
63    {%- set ns.is_last_user = false -%}
64    {%- set ns.is_tool = true -%}{{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
65  {%- endif -%}
66{%- endfor -%}
67{%- if add_generation_prompt and ns.is_last_user and not ns.is_tool -%}{{'<|Assistant|>'}}
68  {%- if not thinking -%}{{'<think></think>'}}
69  {%- else -%}{{'<think>'}}
70  {%- endif -%}
71{%- endif %}