char-fork/llama.cpp

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

KnightYao[Model] Support for Spark2_5ForCausalLM implementation (#27868)3ad1ba733

char/aliceai-t5-moe
4.5 KiB110 linesraw
1{%- if not messages %}
2    {{- raise_exception('No messages provided.') }}
3{%- endif %}
4
5{%- set enable_thinking = enable_thinking | default(true) %}
6
7{#- Render a string or a list of text blocks. -#}
8{%- macro render_content(content, context_name) %}
9    {%- if content is string %}
10        {{- content }}
11    {%- elif content is none or content is undefined %}
12        {{- '' }}
13    {%- elif content is iterable and content is not mapping %}
14        {%- for block in content %}
15            {%- if block.type == 'text' %}
16                {{- block.text }}
17            {%- else %}
18                {{- raise_exception('Unsupported ' ~ context_name ~ ' content block type: ' ~ (block.type | string)) }}
19            {%- endif %}
20        {%- endfor %}
21    {%- else %}
22        {{- raise_exception(context_name ~ ' content must be a string or a list of text blocks') }}
23    {%- endif %}
24{%- endmacro %}
25
26{#- Default system prompt. -#}
27{%- set default_system = 'you are a helpful assistant.' %}
28
29{#- The first message-level system is placed in the initial system block. -#}
30{%- set ns = namespace(initial_system='') %}
31{%- if messages[0].role == 'system' %}
32    {%- set ns.initial_system = render_content(messages[0].content, 'system') %}
33{%- endif %}
34
35{#- System block. -#}
36{{- '<|start▁of▁sentence|><|System|>' + '\n' + default_system }}
37{%- if tools %}
38    {{- '## Tools' + '\n' + 'You have access to the following functions:' + '\n' + '<tools>' }}
39    {%- for tool in tools %}
40        {{- '\n' + tool.function | tojson }}
41    {%- endfor %}
42    {{- '\n' + '</tools>' }}
43{%- endif %}
44{%- if ns.initial_system %}
45    {{- '\n\n' + ns.initial_system }}
46{%- endif %}
47{{- '<|end▁of▁sentence|>' }}
48
49{#- Conversation turns. -#}
50{%- for message in messages %}
51    {%- if message.role == 'system' %}
52        {#- The first system message was consumed by the initial block. -#}
53        {%- if not loop.first %}
54            {{- '<|start▁of▁sentence|><|System|>\n' + render_content(message.content, 'system') + '<|end▁of▁sentence|>' }}
55        {%- endif %}
56    {%- elif message.role == 'user' %}
57        {{- '<|start▁of▁sentence|><|User|>' + render_content(message.content, 'user') + '<|end▁of▁sentence|>' }}
58    {%- elif message.role == 'assistant' %}
59        {%- set assistant_content = render_content(message.content, 'assistant') %}
60        {%- if message.reasoning_content is defined and message.reasoning_content %}
61            {%- set reasoning_content = message.reasoning_content %}
62        {%- else %}
63            {%- set reasoning_content = '' %}
64        {%- endif %}
65        {{- '<|start▁of▁sentence|><|Bot|>' }}
66        {%- if reasoning_content %}
67            {{- '<think>' + reasoning_content + '</think>' }}
68        {%- else %}
69            {{- '</think>' }}
70        {%- endif %}
71        {%- if assistant_content %}
72            {{- assistant_content }}
73        {%- endif %}
74        {%- if message.tool_calls is defined and message.tool_calls is not none %}
75            {%- for tool_call in message.tool_calls %}
76                {%- if tool_call.function.arguments is not mapping %}
77                    {{- raise_exception('tool_call.function.arguments must be a dictionary; normalize JSON strings before apply_chat_template') }}
78                {%- endif %}
79                {%- set args = tool_call.function.arguments %}
80                {{- '<tool_call>' + tool_call.function.name }}
81                {%- for k, v in args.items() %}
82                    {{- '<arg_key>' ~ k ~ '</arg_key><arg_value>' ~ (v if v is string else v | tojson) ~ '</arg_value>' }}
83                {%- endfor %}
84                {{- '</tool_call>' }}
85            {%- endfor %}
86        {%- endif %}
87        {{- '<|end▁of▁sentence|>' }}
88    {%- elif message.role == 'tool' %}
89        {%- if loop.previtem is undefined or loop.previtem.role != 'tool' %}
90            {{- '<|start▁of▁sentence|><|Tool|>' }}
91        {%- endif %}
92        {{- '<tool_response>' ~ message.content ~ '</tool_response>' }}
93        {%- if loop.nextitem is undefined or loop.nextitem.role != 'tool' %}
94            {{- '<|end▁of▁sentence|>' }}
95        {%- endif %}
96    {%- else %}
97        {{- raise_exception('Unsupported message role: ' ~ message.role) }}
98    {%- endif %}
99{%- endfor %}
100
101{#- Generation prompt. -#}
102{%- if add_generation_prompt %}
103    {{- '<|start▁of▁sentence|><|Bot|>' }}
104    {%- if enable_thinking is defined and enable_thinking %}
105        {{- '<think>' }}
106    {%- endif %}
107    {%- if enable_thinking is defined and not enable_thinking %}
108        {{- '</think>' }}
109    {%- endif %}
110{%- endif %}