corupta commited on
Commit
f7d9d18
·
verified ·
1 Parent(s): 32ca231

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. chat_template.jinja +100 -76
chat_template.jinja CHANGED
@@ -1,77 +1,101 @@
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
- {{- bos_token -}}
16
- {{- ns.system_prompt -}}
17
- {%- for message in messages -%}
18
- {%- set content = message["content"] -%}
19
- {%- if message["role"] == "user" -%}
20
- {%- set ns.is_tool = false -%}
21
- {%- set ns.is_first = false -%}
22
- {%- set ns.is_last_user = true -%}
23
- {{- "<|User|>" + content + "<|Assistant|>" -}}
24
- {%- endif -%}
25
- {%- if message["role"] == "assistant" -%}
26
- {%- if "</think>" in content -%}
27
- {%- set content = content.split("</think>")[-1] -%}
28
- {%- endif -%}
29
- {%- endif -%}
30
- {%- if message["role"] == "assistant" and message["tool_calls"] is defined and message["tool_calls"] is not none -%}
31
- {%- set ns.is_last_user = false -%}
32
- {%- if ns.is_tool -%}
33
- {{- "<|tool▁outputs▁end|>" -}}
34
- {%- endif -%}
35
- {%- set ns.is_first = false -%}
36
- {%- set ns.is_tool = false -%}
37
- {%- set ns.is_output_first = true -%}
38
- {%- for tool in message["tool_calls"] -%}
39
- {%- if not ns.is_first -%}
40
- {%- if content is none -%}
41
- {{- "<|tool▁calls▁begin|><|tool▁call▁begin|>" + tool["type"] + "<|tool▁sep|>" + tool["function"]["name"] + "\n" + "```json" + "\n" + tool["function"]["arguments"] + "\n" + "```" + "<|tool▁call▁end|>" -}}
42
- {%- else -%}
43
- {{- content + "<|tool▁calls▁begin|><|tool▁call▁begin|>" + tool["type"] + "<|tool▁sep|>" + tool["function"]["name"] + "\n" + "```json" + "\n" + tool["function"]["arguments"] + "\n" + "```" + "<|tool▁call▁end|>" -}}
44
- {%- endif -%}
45
- {%- set ns.is_first = true -%}
46
- {%- else -%}
47
- {{- "\n" + "<|tool▁call▁begin|>" + tool["type"] + "<|tool▁sep|>" + tool["function"]["name"] + "\n" + "```json" + "\n" + tool["function"]["arguments"] + "\n" + "```" + "<|tool▁call▁end|>" -}}
48
- {%- endif -%}
49
- {%- endfor -%}
50
- {{- "<|tool▁calls▁end|><|end▁of▁sentence|>" -}}
51
- {%- endif -%}
52
- {%- if message["role"] == "assistant" and (message["tool_calls"] is not defined or message["tool_calls"] is none) -%}
53
- {%- set ns.is_last_user = false -%}
54
- {%- if ns.is_tool -%}
55
- {{- "<|tool▁outputs▁end|>" + content + "<|end▁of▁sentence|>" -}}
56
- {%- set ns.is_tool = false -%}
57
- {%- else -%}
58
- {{- content + "<|end▁of▁sentence|>" -}}
59
- {%- endif -%}
60
- {%- endif -%}
61
- {%- if message["role"] == "tool" -%}
62
- {%- set ns.is_last_user = false -%}
63
- {%- set ns.is_tool = true -%}
64
- {%- if ns.is_output_first -%}
65
- {{- "<|tool▁outputs▁begin|><|tool▁output▁begin|>" + content + "<|tool▁output▁end|>" -}}
66
- {%- set ns.is_output_first = false -%}
67
- {%- else -%}
68
- {{- "\n<|tool▁output▁begin|>" + content + "<|tool▁output▁end|>" -}}
69
- {%- endif -%}
70
- {%- endif -%}
71
- {%- endfor -%}
72
- {%- if ns.is_tool -%}
73
- {{- "<|tool▁outputs▁end|>" -}}
74
- {%- endif -%}
75
- {%- if add_generation_prompt and not ns.is_last_user and not ns.is_tool -%}
76
- {{- "<|Assistant|>" -}}
77
- {%- 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 %}