cnfusion commited on
Commit
7160238
·
verified ·
1 Parent(s): 42493a9

Upload chat_template.jinja with huggingface_hub

Browse files
Files changed (1) hide show
  1. chat_template.jinja +159 -0
chat_template.jinja ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {# ---------------------------------------------------------------------- #}
2
+ {# ƛƬ Default setup and flags #}
3
+ {# ---------------------------------------------------------------------- #}
4
+ {%- set messages = messages or [] -%}
5
+ {%- set tools = tools or [] -%}
6
+ {%- set add_generation_prompt = add_generation_prompt or false -%}
7
+ {%- set available_tool_string, add_tool_id = '', true -%}
8
+ {%- set add_thoughts = false -%} {# whether to include <thinking> reasoning blocks #}
9
+ {%- set add_generation_prompt = true -%} {# whether to emit reasoning starter before assistant response #}
10
+ {# Optional token placeholders (safe defaults) #}
11
+ {%- set bos_token = bos_token if (bos_token is defined) else '' -%}
12
+ {%- set eos_token = eos_token if (eos_token is defined) else '' -%}
13
+ {# ---------------------------------------------------------------------- #}
14
+ {# Core reasoning prompt and assistant reasoning prefix #}
15
+ {# ---------------------------------------------------------------------- #}
16
+ {%- set reasoning_prompt =
17
+ 'You are a thoughtful, systematic AI assistant from ServiceNow Language Models (SLAM) lab. '
18
+ 'Analyze each question carefully, present your reasoning step-by-step, then provide the final '
19
+ 'response after the marker [BEGIN FINAL RESPONSE].'
20
+ -%}
21
+ {%- set reasoning_asst_turn_start = 'Here are my reasoning steps:\n' -%}
22
+ {# ---------------------------------------------------------------------- #}
23
+ {# Tool list and tool call output format #}
24
+ {# ---------------------------------------------------------------------- #}
25
+ {%- if tools is not none and tools|length > 0 -%}
26
+ {%- set available_tool_string -%}
27
+ You are provided with function signatures within <available_tools></available_tools> XML tags.
28
+ You may call one or more functions to assist with the user query.
29
+ Don't make assumptions about the arguments. You should infer the argument values from previous
30
+ user responses and the system message.
31
+ Here are the available tools:
32
+ <available_tools>
33
+ {% for tool in tools %}{{ tool|string }}{% endfor %}
34
+
35
+ </available_tools>.
36
+
37
+ Return all function calls as a list of JSON objects within <tool_calls></tool_calls> XML tags.
38
+ Each JSON object should contain a function name and arguments as follows:
39
+ <tool_calls>[
40
+ {"name": <function-name-1>, "arguments": <args-dict-1>},
41
+ {"name": <function-name-2>, "arguments": <args-dict-2>},
42
+ ...
43
+ ]</tool_calls>
44
+ {%- endset -%}
45
+ {%- endif -%}
46
+ {# ---------------------------------------------------------------------- #}
47
+ {# Start system block if first message is not system #}
48
+ {# ---------------------------------------------------------------------- #}
49
+ {%- if messages|length > 0 and messages[0]['role'] != 'system' -%}
50
+ {%- if tools is not none and tools|length > 0 -%}
51
+ {{ bos_token + '<|begin_system|>\n' + reasoning_prompt + '\n' + available_tool_string + '\n' }}
52
+ {%- else -%}
53
+ {{ bos_token + '<|begin_system|>\n' + reasoning_prompt + '\n' }}
54
+ {%- endif -%}
55
+ {%- endif -%}
56
+ {# ---------------------------------------------------------------------- #}
57
+ {# Iterate through messages #}
58
+ {# ---------------------------------------------------------------------- #}
59
+ {%- for message in messages -%}
60
+
61
+ {# ---------------- USER MESSAGE ---------------- #}
62
+ {%- if message['role'] == 'user' -%}
63
+ {{ '<|begin_user|>\n' }}
64
+ {%- if message['content'] is not string -%}
65
+ {%- for chunk in message['content'] -%}
66
+ {%- if chunk['type'] == 'text' -%}
67
+ {{ chunk['text'] }}
68
+ {%- elif chunk['type'] in ['image', 'image_url'] -%}
69
+ {{ '[IMG]' }}
70
+ {%- else -%}
71
+ {{ raise_exception('Unrecognized content type!') }}
72
+ {%- endif -%}
73
+ {%- endfor -%}
74
+ {%- else -%}
75
+ {{ message['content'] }}
76
+ {%- endif -%}
77
+
78
+ {# ---------------- SYSTEM MESSAGE ---------------- #}
79
+ {%- elif message['role'] == 'system' -%}
80
+ {%- if message['content'] is not none and message['content']|length > 0 -%}
81
+ {%- if message['content'] is string -%}
82
+ {%- set system_message = message['content'] -%}
83
+ {%- else -%}
84
+ {%- set system_message = message['content'][0]['text'] -%}
85
+ {%- endif -%}
86
+ {%- else -%}
87
+ {%- set system_message = '' -%}
88
+ {%- endif -%}
89
+
90
+ {%- if tools is not none and tools|length > 0 -%}
91
+ {{ bos_token + '<|begin_system|>\n' + reasoning_prompt + '\n' + system_message + '\n' + available_tool_string + '\n' }}
92
+ {%- else -%}
93
+ {{ bos_token + '<|begin_system|>\n' + reasoning_prompt + '\n' + system_message + '\n' }}
94
+ {%- endif -%}
95
+
96
+ {# ---------------- ASSISTANT MESSAGE ---------------- #}
97
+ {%- elif message['role'] == 'assistant' -%}
98
+ {%- if loop.last -%}
99
+ {%- set add_tool_id = false -%}
100
+ {%- endif -%}
101
+
102
+ {{ '\n<|begin_assistant|>\n' }}
103
+
104
+ {%- if add_thoughts and 'thought' in message and message['thought'] is not none -%}
105
+ <thinking>{{ message['thought'] }}</thinking>
106
+ {%- endif -%}
107
+
108
+ {%- if message['content'] is not none and message['content']|length > 0 -%}
109
+ {%- if message['content'] is not string -%}
110
+ {{ message['content'][0]['text'] }}
111
+ {%- else -%}
112
+ {{ message['content'] }}
113
+ {%- endif -%}
114
+ {%- elif message['chosen'] is not none and message['chosen']|length > 0 -%}
115
+ {{ message['chosen'][0] }}
116
+ {%- endif -%}
117
+
118
+ {# Tool call output #}
119
+ {%- if message['tool_calls'] is not none and message['tool_calls']|length > 0 -%}
120
+ {{ '\n<tool_calls>[' }}
121
+ {%- for tool_call in message['tool_calls'] -%}
122
+ {{ '{"name": "' + tool_call['function']['name'] + '", "arguments": ' + tool_call['function']['arguments']|string }}
123
+ {%- if add_tool_id == true and 'id' in tool_call -%}
124
+ {{ ', "id": "' + tool_call['id'] + '"' }}
125
+ {%- endif -%}
126
+ {{ '}' }}
127
+ {%- if not loop.last -%}{{ ', ' }}{%- endif -%}
128
+ {%- endfor -%}
129
+ {{ ']</tool_calls>' }}
130
+ {%- endif -%}
131
+
132
+ {%- if not loop.last or training_prompt -%}
133
+ {{ '\n<|end|>\n' }}
134
+ {%- endif -%}
135
+
136
+ {# ---------------- TOOL RESULT MESSAGE ---------------- #}
137
+ {%- elif message['role'] == 'tool' -%}
138
+ {%- if message['content'] is string -%}
139
+ {%- set tool_message = message['content'] -%}
140
+ {%- else -%}
141
+ {%- set tool_message = message['content'][0]['text'] -%}
142
+ {%- endif -%}
143
+ {{ '<|begin_tool_result|>\n' + tool_message|string + '\n' }}
144
+
145
+ {# ---------------- CONTENT MESSAGE ---------------- #}
146
+ {%- elif message['role'] == 'content' -%}
147
+ {%- if message['content'] is not string -%}
148
+ {{ '<|begin_content|>\n' + message['content'][0]['text'] + '\n' }}
149
+ {%- else -%}
150
+ {{ '<|begin_content|>\n' + message['content'] + '\n' }}
151
+ {%- endif -%}
152
+ {%- endif -%}
153
+
154
+ {# ---------------- REASONING PROMPT BEFORE NEXT ASSISTANT ---------------- #}
155
+ {%- if loop.last and add_generation_prompt and message['role'] != 'assistant' -%}
156
+ {{ '\n<|begin_assistant|>\n' + reasoning_asst_turn_start }}
157
+ {%- endif -%}
158
+
159
+ {%- endfor -%}