smartguy0505 commited on
Commit
c9e0f01
·
verified ·
1 Parent(s): 4ea47fa

Upload tool_chat_template.jinja with huggingface_hub

Browse files
Files changed (1) hide show
  1. tool_chat_template.jinja +100 -0
tool_chat_template.jinja ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_call>\n{"name": FUNCTION_NAME, "arguments": {"param1": "value1", "param2": "value2"}}\n</tool_call>\n\n'
21
+ 'Make sure the JSON is valid.\n\n'
22
+ '## Tools\n\n### Function\n\nYou have the following functions available:\n\n') %}
23
+ {% for tool in tools %}
24
+ {% set tool_ns.text = tool_ns.text + '\n- ' + tool['function']['name'] + '\n```json\n' + (tool['function'] | tojson) + '\n```\n' %}
25
+ {% endfor %}
26
+ {% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
27
+ {% endif %}
28
+
29
+ {{- bos_token }}
30
+ {{- ns.system_prompt }}
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
+ {{'<|User|>' + content + '<|Assistant|>'}}
38
+ {%- endif %}
39
+ {%- if message['role'] == 'assistant' %}
40
+ {% if '</think>' in content %}
41
+ {% set content = content.split('</think>')[-1] %}
42
+ {% endif %}
43
+ {% endif %}
44
+ {%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
45
+ {%- set ns.is_last_user = false -%}
46
+ {%- if ns.is_tool %}
47
+ {{- ''}}
48
+ {%- endif %}
49
+ {%- set ns.is_first = false %}
50
+ {%- set ns.is_tool = false -%}
51
+ {%- set ns.is_output_first = true %}
52
+ {%- for tool in message['tool_calls'] %}
53
+ {%- if tool['function']['arguments'] is string %}
54
+ {%- set arguments = tool['function']['arguments'] %}
55
+ {%- else %}
56
+ {%- set arguments = tool['function']['arguments'] | tojson %}
57
+ {%- endif %}
58
+ {%- if not ns.is_first %}
59
+ {%- if content is none %}
60
+ {{- '<tool_call>\n{"tool_call_id": ' + tool['id']|tojson + ', "name": ' + tool['function']['name']|tojson + ', "arguments": ' + arguments + '}\n</tool_call>'}}
61
+ {%- else %}
62
+ {{- content + '\n\n<tool_call>\n{"tool_call_id": ' + tool['id']|tojson + ', "name": ' + tool['function']['name']|tojson + ', "arguments": ' + arguments + '}\n</tool_call>'}}
63
+ {%- endif %}
64
+ {%- set ns.is_first = true -%}
65
+ {%- else %}
66
+ {{- '\n\n<tool_call>\n{"tool_call_id": ' + tool['id']|tojson + ', "name": ' + tool['function']['name']|tojson + ', "arguments": ' + arguments + '}\n</tool_call>'}}
67
+ {%- endif %}
68
+ {%- endfor %}
69
+ {{- '<|end▁of▁sentence|>'}}
70
+ {%- endif %}
71
+ {%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none)%}
72
+ {%- set ns.is_last_user = false -%}
73
+ {%- if ns.is_tool %}
74
+ {{- '\n' + content + '<|end▁of▁sentence|>'}}
75
+ {%- set ns.is_tool = false -%}
76
+ {%- else %}
77
+ {{- content + '<|end▁of▁sentence|>'}}
78
+ {%- endif %}
79
+ {%- endif %}
80
+ {%- if message['role'] == 'tool' %}
81
+ {%- set ns.is_last_user = false -%}
82
+ {%- set ns.is_tool = true -%}
83
+ {%- set tool_call_id_param = '' %}
84
+ {%- if message['tool_call_id'] %}
85
+ {%- set tool_call_id_param = '"tool_call_id": ' + message['tool_call_id']|tojson + ', ' %}
86
+ {%- endif %}
87
+ {%- if ns.is_output_first %}
88
+ {{- '\n\n{' + tool_call_id_param + '"content": ' + content|tojson + '}'}}
89
+ {%- set ns.is_output_first = false %}
90
+ {%- else %}
91
+ {{- '\n{' + tool_call_id_param + '"content": ' + content|tojson + '}'}}
92
+ {%- endif %}
93
+ {%- endif %}
94
+ {%- endfor -%}
95
+ {% if ns.is_tool %}
96
+ {{- '\n\n'}}
97
+ {%- endif %}
98
+ {% if add_generation_prompt and not ns.is_last_user %}
99
+ {{- '<|Assistant|>'}}
100
+ {%- endif %}