Skip to content

Commit ba324bd

Browse files
committed
add chat template
1 parent 1f54ecd commit ba324bd

File tree

6 files changed

+402
-0
lines changed

6 files changed

+402
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 %}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
2+
{% if not add_generation_prompt is defined %}
3+
{% set add_generation_prompt = false %}
4+
{% endif %}
5+
6+
{% set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='', is_first_sp=true, is_last_user=false) %}
7+
{%- for message in messages %}
8+
{%- if message['role'] == 'system' %}
9+
{%- if ns.is_first_sp %}
10+
{% set ns.system_prompt = ns.system_prompt + message['content'] %}
11+
{% set ns.is_first_sp = false %}
12+
{%- else %}
13+
{% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}
14+
{%- endif %}
15+
{%- endif %}
16+
{%- endfor -%}
17+
18+
{# --- Append tool descriptions if tools are defined --- #}
19+
{% if tools is defined and tools is not none %}
20+
{% set tool_ns = namespace(text='You are a helpful assistant with tool calling capabilities. '
21+
'When a tool call is needed, you MUST use the following format to issue the call:\n'
22+
'<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>FUNCTION_NAME\n'
23+
'```json\n{"param1": "value1", "param2": "value2"}\n```<|tool▁call▁end|><|tool▁calls▁end|>\n\n'
24+
'Make sure the JSON is valid.'
25+
'## Tools\n\n### Function\n\nYou have the following functions available:\n\n') %}
26+
{% for tool in tools %}
27+
{% set tool_ns.text = tool_ns.text + '\n```json\n' + (tool | tojson) + '\n```\n' %}
28+
{% endfor %}
29+
{% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
30+
{% endif %}
31+
32+
{{- bos_token }}
33+
{{- ns.system_prompt }}
34+
35+
{%- for message in messages %}
36+
{%- if message['role'] == 'user' %}
37+
{%- set ns.is_tool = false -%}
38+
{%- set ns.is_first = false -%}
39+
{%- set ns.is_last_user = true -%}
40+
{{'<|User|>' + message['content'] + '<|Assistant|>'}}
41+
{%- endif %}
42+
{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
43+
{%- set ns.is_last_user = false -%}
44+
{%- if ns.is_tool %}
45+
{{- '<|tool▁outputs▁end|>'}}
46+
{%- endif %}
47+
{%- set ns.is_first = false %}
48+
{%- set ns.is_tool = false -%}
49+
{%- set ns.is_output_first = true %}
50+
{%- for tool in message['tool_calls'] %}
51+
{%- set formatted_args = tool['function']['arguments'] if tool['function']['arguments'] is string else tool['function']['arguments']|tojson %}
52+
{%- if not ns.is_first %}
53+
{%- if message['content'] is none %}
54+
{{- '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + formatted_args + '\n' + '```' + '<|tool▁call▁end|>'}}
55+
{%- else %}
56+
{{- message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + formatted_args + '\n' + '```' + '<|tool▁call▁end|>'}}
57+
{%- endif %}
58+
{%- set ns.is_first = true -%}
59+
{%- else %}
60+
{{- '\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + formatted_args + '\n' + '```' + '<|tool▁call▁end|>'}}
61+
{%- endif %}
62+
{%- endfor %}
63+
{{- '<|tool▁calls▁end|><|end▁of▁sentence|>'}}
64+
{%- endif %}
65+
{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none)%}
66+
{%- set ns.is_last_user = false -%}
67+
{%- if ns.is_tool %}
68+
{{- '<|tool▁outputs▁end|>' + message['content'] + '<|end▁of▁sentence|>'}}
69+
{%- set ns.is_tool = false -%}
70+
{%- else %}
71+
{% set content = message['content'] %}
72+
{{- content + '<|end▁of▁sentence|>'}}
73+
{%- endif %}
74+
{%- endif %}
75+
{%- if message['role'] == 'tool' %}
76+
{%- set ns.is_last_user = false -%}
77+
{%- set ns.is_tool = true -%}
78+
{%- if ns.is_output_first %}
79+
{{- 'Use the results below to formulate an answer to the user question unless additional information is needed.' }}
80+
{{- '<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
81+
{%- set ns.is_output_first = false %}
82+
{%- else %}
83+
{{- '\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
84+
{%- endif %}
85+
{%- endif %}
86+
{%- endfor -%}
87+
88+
{% if ns.is_tool %}
89+
{{- '<|tool▁outputs▁end|>'}}
90+
{% endif %}
91+
{% if add_generation_prompt and not ns.is_last_user and not ns.is_tool %}
92+
{{- '<|Assistant|>'}}
93+
{% endif %}
94+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{% if not add_generation_prompt is defined %}
2+
{% set add_generation_prompt = false %}
3+
{% endif %}
4+
{% if not thinking is defined %}
5+
{% set thinking = false %}
6+
{% endif %}
7+
{% set ns = namespace(is_first=false, is_tool=false, system_prompt='', is_first_sp=true, is_last_user=false) %}
8+
{%- for message in messages %}
9+
{%- if message['role'] == 'system' %}
10+
{%- if ns.is_first_sp %}
11+
{% set ns.system_prompt = ns.system_prompt + message['content'] %}
12+
{% set ns.is_first_sp = false %}
13+
{%- else %}
14+
{% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}
15+
{%- endif %}
16+
{%- endif %}
17+
{%- endfor %}
18+
19+
{% if tools is defined and tools is not none %}
20+
{% set tool_ns = namespace(text='## Tools\nYou have access to the following tools:\n') %}
21+
{% for tool in tools %}
22+
{% set tool_ns.text = tool_ns.text + '\n### ' + tool.function.name + '\nDescription: ' + tool.function.description + '\n\nParameters: ' + (tool.function.parameters | tojson) + '\n' %}
23+
{% endfor %}
24+
{% set tool_ns.text = tool_ns.text + "\nIMPORTANT: ALWAYS adhere to this exact format for tool use:\n<|tool▁calls▁begin|><|tool▁call▁begin|>tool_call_name<|tool▁sep|>tool_call_arguments<|tool▁call▁end|>{{additional_tool_calls}}<|tool▁calls▁end|>\n\nWhere:\n\n- `tool_call_name` must be an exact match to one of the available tools\n- `tool_call_arguments` must be valid JSON that strictly follows the tool's Parameters Schema\n- For multiple tool calls, chain them directly without separators or spaces\n" %}
25+
{% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
26+
{% endif %}
27+
28+
{{ bos_token }}{{ ns.system_prompt }}
29+
{%- for message in messages %}
30+
{%- if message['role'] == 'user' %}
31+
{%- set ns.is_tool = false -%}
32+
{%- set ns.is_first = false -%}
33+
{%- set ns.is_last_user = true -%}
34+
{% if message['content'] is string %}
35+
{{'<|User|>' + message['content']}}
36+
{% else %}
37+
{% for content in message['content'] %}
38+
{% if content['type'] == 'text' %}
39+
{{'<|User|>' + content['text']}}
40+
{% endif %}
41+
{% endfor %}
42+
{%- endif %}
43+
{%- endif %}
44+
{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
45+
{%- if ns.is_last_user %}
46+
{{'<|Assistant|></think>'}}
47+
{%- endif %}
48+
{%- set ns.is_last_user = false -%}
49+
{%- set ns.is_first = false %}
50+
{%- set ns.is_tool = false -%}
51+
{%- for tool in message['tool_calls'] %}
52+
{%- set formatted_args = tool['function']['arguments'] if tool['function']['arguments'] is string else tool['function']['arguments']|tojson %}
53+
{%- if not ns.is_first %}
54+
{%- if message['content'] is none %}
55+
{{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + formatted_args + '<|tool▁call▁end|>'}}
56+
{%- else %}
57+
{{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + formatted_args + '<|tool▁call▁end|>'}}
58+
{%- endif %}
59+
{%- set ns.is_first = true -%}
60+
{%- else %}
61+
{{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + formatted_args + '<|tool▁call▁end|>'}}
62+
{%- endif %}
63+
{%- endfor %}
64+
{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
65+
{%- endif %}
66+
{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}
67+
{%- if ns.is_last_user %}
68+
{{'<|Assistant|>'}}
69+
{%- if message['prefix'] is defined and message['prefix'] and thinking %}
70+
{{'<think>'}}
71+
{%- else %}
72+
{{'</think>'}}
73+
{%- endif %}
74+
{%- endif %}
75+
{%- set ns.is_last_user = false -%}
76+
{%- if ns.is_tool %}
77+
{{message['content'] + '<|end▁of▁sentence|>'}}
78+
{%- set ns.is_tool = false -%}
79+
{%- else %}
80+
{%- set content = message['content'] -%}
81+
{%- if '</think>' in content %}
82+
{%- set content = content.split('</think>', 1)[1] -%}
83+
{%- endif %}
84+
{{content + '<|end▁of▁sentence|>'}}
85+
{%- endif %}
86+
{%- endif %}
87+
{%- if message['role'] == 'tool' %}
88+
{%- set ns.is_last_user = false -%}
89+
{%- set ns.is_tool = true -%}
90+
{{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
91+
{%- endif %}
92+
{%- endfor -%}
93+
{%- if add_generation_prompt and ns.is_last_user and not ns.is_tool %}
94+
{{'<|Assistant|>'}}
95+
{%- if not thinking %}
96+
{{'</think>'}}
97+
{%- else %}
98+
{{'<think>'}}
99+
{%- endif %}
100+
{% endif %}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{% if not add_generation_prompt is defined %}
2+
{% set add_generation_prompt = false %}
3+
{% endif %}
4+
{% if not thinking is defined %}
5+
{% set thinking = false %}
6+
{% endif %}
7+
{% set ns = namespace(is_first=false, is_tool=false, system_prompt='', is_first_sp=true, is_last_user=false, is_only_sys=false, is_prefix=false) %}
8+
{%- for message in messages %}
9+
{%- if message['role'] == 'system' %}
10+
{%- if ns.is_first_sp %}
11+
{% set ns.system_prompt = ns.system_prompt + message['content'] %}
12+
{% set ns.is_first_sp = false %}
13+
{%- else %}
14+
{% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}
15+
{%- endif %}
16+
{% set ns.is_only_sys = true %}
17+
{%- endif %}
18+
{%- endfor %}
19+
20+
{% if tools is defined and tools is not none %}
21+
{% set tool_ns = namespace(text='## Tools\nYou have access to the following tools:\n') %}
22+
{% for tool in tools %}
23+
{% set tool_ns.text = tool_ns.text + '\n### ' + tool.function.name + '\nDescription: ' + tool.function.description + '\n\nParameters: ' + (tool.function.parameters | tojson) + '\n' %}
24+
{% endfor %}
25+
{% set tool_ns.text = tool_ns.text + "\nIMPORTANT: ALWAYS adhere to this exact format for tool use:\n<|tool▁calls▁begin|><|tool▁call▁begin|>tool_call_name<|tool▁sep|>tool_call_arguments<|tool▁call▁end|>{{additional_tool_calls}}<|tool▁calls▁end|>\n\nWhere:\n\n- `tool_call_name` must be an exact match to one of the available tools\n- `tool_call_arguments` must be valid JSON that strictly follows the tool's Parameters Schema\n- For multiple tool calls, chain them directly without separators or spaces\n" %}
26+
{% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
27+
{% endif %}
28+
29+
{{ bos_token }}{{ ns.system_prompt }}
30+
{%- for message in messages %}
31+
{%- if message['role'] == 'user' %}
32+
{%- set ns.is_tool = false -%}
33+
{%- set ns.is_first = false -%}
34+
{%- set ns.is_last_user = true -%}
35+
{{'<|User|>' + message['content']}}
36+
{%- endif %}
37+
{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
38+
{%- if ns.is_last_user or ns.is_only_sys %}
39+
{{'<|Assistant|></think>'}}
40+
{%- endif %}
41+
{%- set ns.is_last_user = false -%}
42+
{%- set ns.is_first = false %}
43+
{%- set ns.is_tool = false -%}
44+
{%- for tool in message['tool_calls'] %}
45+
{%- set formatted_args = tool['function']['arguments'] if tool['function']['arguments'] is string else tool['function']['arguments']|tojson %}
46+
{%- if not ns.is_first %}
47+
{%- if message['content'] is none %}
48+
{{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + formatted_args + '<|tool▁call▁end|>'}}
49+
{%- else %}
50+
{{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + formatted_args + '<|tool▁call▁end|>'}}
51+
{%- endif %}
52+
{%- set ns.is_first = true -%}
53+
{%- else %}
54+
{{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + formatted_args + '<|tool▁call▁end|>'}}
55+
{%- endif %}
56+
{%- endfor %}
57+
{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
58+
{%- endif %}
59+
{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}
60+
{%- if ns.is_last_user %}
61+
{{'<|Assistant|>'}}
62+
{%- if message['prefix'] is defined and message['prefix'] and thinking %}
63+
{{'<think>'}}
64+
{%- else %}
65+
{{'</think>'}}
66+
{%- endif %}
67+
{%- endif %}
68+
{%- if message['prefix'] is defined and message['prefix'] %}
69+
{%- set ns.is_prefix = true -%}
70+
{%- endif %}
71+
{%- set ns.is_last_user = false -%}
72+
{%- if ns.is_tool %}
73+
{{message['content'] + '<|end▁of▁sentence|>'}}
74+
{%- set ns.is_tool = false -%}
75+
{%- else %}
76+
{%- set content = message['content'] -%}
77+
{%- if '</think>' in content %}
78+
{%- set content = content.split('</think>', 1)[1] -%}
79+
{%- endif %}
80+
{{content + '<|end▁of▁sentence|>'}}
81+
{%- endif %}
82+
{%- endif %}
83+
{%- if message['role'] == 'tool' %}
84+
{%- set ns.is_last_user = false -%}
85+
{%- set ns.is_tool = true -%}
86+
{{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
87+
{%- endif %}
88+
{%- if message['role'] != 'system' %}
89+
{% set ns.is_only_sys = false %}
90+
{%- endif %}
91+
{%- endfor -%}
92+
{% if add_generation_prompt and not ns.is_tool%}
93+
{% if ns.is_last_user or ns.is_only_sys or not ns.is_prefix %}
94+
{{'<|Assistant|>'}}
95+
{%- if not thinking %}
96+
{{'</think>'}}
97+
{%- else %}
98+
{{'<think>'}}
99+
{%- endif %}
100+
{% endif %}
101+
{% endif %}

0 commit comments

Comments
 (0)