1+ {% if not add_generation_prompt is defined %}
2+ {% set add_generation_prompt = false %}
3+ {% endif %}
4+ {% set ns = namespace (is_first =false , is_tool =false , is_output_first =true , system_prompt ='' , is_first_sp =true , is_last_user =false ) %}
5+ {% - for message in messages %}
6+ {% - if message ['role' ] == 'system' %}
7+ {% - if ns .is_first_sp %}
8+ {% set ns .system_prompt = ns .system_prompt + message ['content' ] %}
9+ {% set ns .is_first_sp = false %}
10+ {% - else %}
11+ {% set ns .system_prompt = ns .system_prompt + '\n\n' + message ['content' ] %}
12+ {% - endif %}
13+ {% - endif %}
14+ {% - endfor %}
15+
16+ {# --- Append tool descriptions if tools are defined --- #}
17+ {% if tools is defined and tools is not none %}
18+ {% set tool_ns = namespace (text ='You are a helpful assistant with tool calling capabilities. '
19+ 'When a tool call is needed, you MUST use the following format to issue the call:\n'
20+ '<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>FUNCTION_NAME\n'
21+ '```json\n{"param1": "value1", "param2": "value2"}\n```<|tool▁call▁end|><|tool▁calls▁end|>\n\n'
22+ 'Make sure the JSON is valid.'
23+ '## Tools\n\n### Function\n\nYou have the following functions available:\n\n' ) %}
24+ {% for tool in tools %}
25+ {% set tool_ns .text = tool_ns .text + '- `' + tool ['name' ] + '`:\n```json\n' + (tool | tojson ) + '\n```\n' %}
26+ {% endfor %}
27+ {% set ns .system_prompt = ns .system_prompt + '\n\n' + tool_ns .text %}
28+ {% endif %}
29+
30+ {{ bos_token }}
31+ {{ ns.system_prompt }}
32+ {% - for message in messages %}
33+ {% set content = message ['content' ] %}
34+ {% - if message ['role' ] == 'user' %}
35+ {% - set ns .is_tool = false -%}
36+ {% - set ns .is_first = false -%}
37+ {% - set ns .is_last_user = true -%}
38+ {{'<|User|>' + content + '<|Assistant|>'}}
39+ {% - endif %}
40+ {% - if message ['role' ] == 'assistant' %}
41+ {% if '</think>' in content %}
42+ {% set content = content .split ('</think>' )[-1] %}
43+ {% endif %}
44+ {% endif %}
45+ {% - if message ['role' ] == 'assistant' and message ['tool_calls' ] is defined and message ['tool_calls' ] is not none %}
46+ {% - set ns .is_last_user = false -%}
47+ {% - if ns .is_tool %}
48+ {{'<|tool▁outputs▁end|>'}}
49+ {% - endif %}
50+ {% - set ns .is_first = false %}
51+ {% - set ns .is_tool = false -%}
52+ {% - set ns .is_output_first = true %}
53+ {% - for tool in message ['tool_calls' ] %}
54+ {% - if not ns .is_first %}
55+ {% - if content is none %}
56+ {{'<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function'] ['name'] + '\n' + '```json' + '\n' + tool['function'] ['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}
57+ {% - else %}
58+ {{content + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function'] ['name'] + '\n' + '```json' + '\n' + tool['function'] ['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}
59+ {% - endif %}
60+ {% - set ns .is_first = true -%}
61+ {% - else %}
62+ {{'\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function'] ['name'] + '\n' + '```json' + '\n' + tool['function'] ['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}
63+ {% - endif %}
64+ {% - endfor %}
65+ {{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
66+ {% - endif %}
67+ {% - if message ['role' ] == 'assistant' and (message ['tool_calls' ] is not defined or message ['tool_calls' ] is none )%}
68+ {% - set ns .is_last_user = false -%}
69+ {% - if ns .is_tool %}
70+ {{'<|tool▁outputs▁end|>' + content + '<|end▁of▁sentence|>'}}
71+ {% - set ns .is_tool = false -%}
72+ {% - else %}
73+ {{content + '<|end▁of▁sentence|>'}}
74+ {% - endif %}
75+ {% - endif %}
76+ {% - if message ['role' ] == 'tool' %}
77+ {% - set ns .is_last_user = false -%}
78+ {% - set ns .is_tool = true -%}
79+ {% - if ns .is_output_first %}
80+ {{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
81+ {% - set ns .is_output_first = false %}
82+ {% - else %}
83+ {{'\n<|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
84+ {% - endif %}
85+ {% - endif %}
86+ {% - endfor -%}
87+ {% if ns .is_tool %}
88+ {{'<|tool▁outputs▁end|>'}}
89+ {% endif %}
90+ {% if add_generation_prompt and not ns .is_last_user and not ns .is_tool %}
91+ {{'<|Assistant|>'}}
92+ {% endif %}
0 commit comments