danielhanchen commited on
Commit
439d5f5
·
verified ·
1 Parent(s): c0f4848

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +105 -13
chat_template.jinja CHANGED
@@ -1,14 +1,106 @@
1
- {% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='', is_first_sp=true, is_last_user=false) %}{%- for message in messages %}{%- if message['role'] == 'system' %}{%- if ns.is_first_sp %}{% set ns.system_prompt = ns.system_prompt + message['content'] %}{% set ns.is_first_sp = false %}{%- else %}{% set ns.system_prompt = ns.system_prompt + '
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- ' + message['content'] %}{%- endif %}{%- endif %}{%- endfor %}{{ bos_token }}{{ ns.system_prompt }}{%- for message in messages %}{% set content = message['content'] %}{%- if message['role'] == 'user' %}{%- set ns.is_tool = false -%}{%- set ns.is_first = false -%}{%- set ns.is_last_user = true -%}{{'<|User|>' + content + '<|Assistant|>'}}{%- endif %}{%- if message['role'] == 'assistant' %}{% if '</think>' in content %}{% set content = content.split('</think>')[-1] %}{% endif %}{% endif %}{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}{%- set ns.is_last_user = false -%}{%- if ns.is_tool %}{{'<|tool▁outputs▁end|>'}}{%- endif %}{%- set ns.is_first = false %}{%- set ns.is_tool = false -%}{%- set ns.is_output_first = true %}{%- for tool in message['tool_calls'] %}{%- if not ns.is_first %}{%- if content is none %}{{'<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '
4
- ' + '```json' + '
5
- ' + tool['function']['arguments'] + '
6
- ' + '```' + '<|tool▁call▁end|>'}}{%- else %}{{content + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '
7
- ' + '```json' + '
8
- ' + tool['function']['arguments'] + '
9
- ' + '```' + '<|tool▁call▁end|>'}}{%- endif %}{%- set ns.is_first = true -%}{%- else %}{{'
10
- ' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '
11
- ' + '```json' + '
12
- ' + tool['function']['arguments'] + '
13
- ' + '```' + '<|tool▁call▁end|>'}}{%- endif %}{%- endfor %}{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}{%- endif %}{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none)%}{%- set ns.is_last_user = false -%}{%- if ns.is_tool %}{{'<|tool▁outputs▁end|>' + content + '<|end▁of▁sentence|>'}}{%- set ns.is_tool = false -%}{%- else %}{{content + '<|end▁of▁sentence|>'}}{%- endif %}{%- endif %}{%- if message['role'] == 'tool' %}{%- set ns.is_last_user = false -%}{%- set ns.is_tool = true -%}{%- if ns.is_output_first %}{{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}{%- set ns.is_output_first = false %}{%- else %}{{'
14
- <|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}{%- endif %}{%- endif %}{%- endfor -%}{% if ns.is_tool %}{{'<|tool▁outputs▁end|>'}}{% endif %}{% if add_generation_prompt and not ns.is_last_user and not ns.is_tool %}{{'<|Assistant|>'}}{% endif %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ {#- Adapted from https://github.com/sgl-project/sglang/blob/main/examples/chat_template/tool_chat_template_deepseekr1.jinja #}
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 + '\n```json\n' + (tool | tojson) + '\n```\n' %}
26
+ {%- endfor %}
27
+ {%- if ns.system_prompt|length != 0 %}
28
+ {%- set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
29
+ {%- else %}
30
+ {%- set ns.system_prompt = tool_ns.text %}
31
+ {%- endif %}
32
+ {%- endif %}
33
+ {{- bos_token }}
34
+ {{- ns.system_prompt }}
35
+ {%- set last_index = (messages|length - 1) %}
36
+ {%- for message in messages %}
37
+ {%- set content = message['content'] %}
38
+ {%- if message['role'] == 'user' %}
39
+ {%- set ns.is_tool = false -%}
40
+ {%- set ns.is_first = false -%}
41
+ {%- set ns.is_last_user = true -%}
42
+ {%- if loop.index0 == last_index %}
43
+ {{- '<|User|>' + content }}
44
+ {%- else %}
45
+ {{- '<|User|>' + content + '<|Assistant|>'}}
46
+ {%- endif %}
47
+ {%- endif %}
48
+ {%- if message['role'] == 'assistant' %}
49
+ {%- if '</think>' in content %}
50
+ {%- set content = content.split('</think>')[-1] %}
51
+ {%- endif %}
52
+ {%- endif %}
53
+ {%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
54
+ {%- set ns.is_last_user = false -%}
55
+ {%- if ns.is_tool %}
56
+ {{- '<|tool▁outputs▁end|>'}}
57
+ {%- endif %}
58
+ {%- set ns.is_first = false %}
59
+ {%- set ns.is_tool = false -%}
60
+ {%- set ns.is_output_first = true %}
61
+ {%- for tool in message['tool_calls'] %}
62
+ {%- set arguments = tool['function']['arguments'] %}
63
+ {%- if arguments is not string %}
64
+ {%- set arguments = arguments|tojson %}
65
+ {%- endif %}
66
+ {%- if not ns.is_first %}
67
+ {%- if content is none %}
68
+ {{- '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + arguments + '\n' + '```' + '<|tool▁call▁end|>'}}
69
+ }
70
+ {%- else %}
71
+ {{- content + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + arguments + '\n' + '```' + '<|tool▁call▁end|>'}}
72
+ {%- endif %}
73
+ {%- set ns.is_first = true -%}
74
+ {%- else %}
75
+ {{- '\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + arguments + '\n' + '```' + '<|tool▁call▁end|>'}}
76
+ {%- endif %}
77
+ {%- endfor %}
78
+ {{- '<|tool▁calls▁end|><|end▁of▁sentence|>'}}
79
+ {%- endif %}
80
+ {%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}
81
+ {%- set ns.is_last_user = false -%}
82
+ {%- if ns.is_tool %}
83
+ {{- '<|tool▁outputs▁end|>' + content + '<|end▁of▁sentence|>'}}
84
+ {%- set ns.is_tool = false -%}
85
+ {%- else %}
86
+ {{- content + '<|end▁of▁sentence|>'}}
87
+ {%- endif %}
88
+ {%- endif %}
89
+ {%- if message['role'] == 'tool' %}
90
+ {%- set ns.is_last_user = false -%}
91
+ {%- set ns.is_tool = true -%}
92
+ {%- if ns.is_output_first %}
93
+ {{- '<|tool▁outputs▁begin|><|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
94
+ {%- set ns.is_output_first = false %}
95
+ {%- else %}
96
+ {{- '\n<|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
97
+ {%- endif %}
98
+ {%- endif %}
99
+ {%- endfor -%}
100
+ {%- if ns.is_tool %}
101
+ {{- '<|tool▁outputs▁end|>'}}
102
+ {%- endif %}
103
+ {#- if add_generation_prompt and not ns.is_last_user and not ns.is_tool #}
104
+ {%- if add_generation_prompt and not ns.is_tool %}
105
+ {{- '<|Assistant|>'}}
106
+ {%- endif %}