corupta commited on
Commit
582df52
·
verified ·
1 Parent(s): afbf8c7

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. chat_template.jinja +100 -13
chat_template.jinja CHANGED
@@ -1,14 +1,101 @@
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. ' + 'When a tool call is needed, you MUST use the following format to issue the call:\n' + '<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>FUNCTION_NAME\n' + '```json\n{"param1": "value1", "param2": "value2"}\n```<|tool▁call▁end|><|tool▁calls▁end|>\n\n' + 'Make sure the JSON is valid.' + '## Tools\n\n### Function\n\nYou have the following functions available:\n\n') %}
19
+ {%- for tool in tools %}
20
+ {%- set tool_ns.text = tool_ns.text + '\n```json\n' + (tool | tojson) + '\n```\n' %}
21
+ {%- endfor %}
22
+ {%- if ns.system_prompt|length != 0 %}
23
+ {%- set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
24
+ {%- else %}
25
+ {%- set ns.system_prompt = tool_ns.text %}
26
+ {%- endif %}
27
+ {%- endif %}
28
+ {{- bos_token }}
29
+ {{- ns.system_prompt }}
30
+ {%- set last_index = (messages|length - 1) %}
31
+ {%- for message in messages %}
32
+ {%- set content = message['content'] %}
33
+ {%- if message['role'] == 'user' %}
34
+ {%- set ns.is_tool = false -%}
35
+ {%- set ns.is_first = false -%}
36
+ {%- set ns.is_last_user = true -%}
37
+ {%- if loop.index0 == last_index %}
38
+ {{- '<|User|>' + content }}
39
+ {%- else %}
40
+ {{- '<|User|>' + content + '<|Assistant|>'}}
41
+ {%- endif %}
42
+ {%- endif %}
43
+ {%- if message['role'] == 'assistant' %}
44
+ {%- if '</think>' in content %}
45
+ {%- set content = (content.split('</think>')|last) %}
46
+ {%- endif %}
47
+ {%- endif %}
48
+ {%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
49
+ {%- set ns.is_last_user = false -%}
50
+ {%- if ns.is_tool %}
51
+ {{- '<|tool▁outputs▁end|>'}}
52
+ {%- endif %}
53
+ {%- set ns.is_first = false %}
54
+ {%- set ns.is_tool = false -%}
55
+ {%- set ns.is_output_first = true %}
56
+ {%- for tool in message['tool_calls'] %}
57
+ {%- set arguments = tool['function']['arguments'] %}
58
+ {%- if arguments is not string %}
59
+ {%- set arguments = arguments|tojson %}
60
+ {%- endif %}
61
+ {%- if not ns.is_first %}
62
+ {%- if content is none %}
63
+ {{- '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + arguments + '\n' + '```' + '<|tool▁call▁end|>'}}
64
+ }
65
+ {%- else %}
66
+ {{- content + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + arguments + '\n' + '```' + '<|tool▁call▁end|>'}}
67
+ {%- endif %}
68
+ {%- set ns.is_first = true -%}
69
+ {%- else %}
70
+ {{- '\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + arguments + '\n' + '```' + '<|tool▁call▁end|>'}}
71
+ {%- endif %}
72
+ {%- endfor %}
73
+ {{- '<|tool▁calls▁end|><|end▁of▁sentence|>'}}
74
+ {%- endif %}
75
+ {%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}
76
+ {%- set ns.is_last_user = false -%}
77
+ {%- if ns.is_tool %}
78
+ {{- '<|tool▁outputs▁end|>' + content + '<|end▁of▁sentence|>'}}
79
+ {%- set ns.is_tool = false -%}
80
+ {%- else %}
81
+ {{- content + '<|end▁of▁sentence|>'}}
82
+ {%- endif %}
83
+ {%- endif %}
84
+ {%- if message['role'] == 'tool' %}
85
+ {%- set ns.is_last_user = false -%}
86
+ {%- set ns.is_tool = true -%}
87
+ {%- if ns.is_output_first %}
88
+ {{- '<|tool▁outputs▁begin|><|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
89
+ {%- set ns.is_output_first = false %}
90
+ {%- else %}
91
+ {{- '\n<|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
92
+ {%- endif %}
93
+ {%- endif %}
94
+ {%- endfor -%}
95
+ {%- if ns.is_tool %}
96
+ {{- '<|tool▁outputs▁end|>'}}
97
+ {%- endif %}
98
+ {#- if add_generation_prompt and not ns.is_last_user and not ns.is_tool #}
99
+ {%- if add_generation_prompt and not ns.is_tool %}
100
+ {{- '<|Assistant|>'}}
101
+ {%- endif %}