danielhanchen commited on
Commit
ddc58a7
·
verified ·
1 Parent(s): d19db71

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +156 -62
chat_template.jinja CHANGED
@@ -1,66 +1,66 @@
1
- {%- set today_date = strftime_now("%Y-%m-%d") %}
2
  {%- set yesterday_day = strftime_now("%d") %}
3
  {%- set yesterday_month = strftime_now("%m") %}
4
  {%- set yesterday_year = strftime_now("%Y") %}
 
5
  {%- if yesterday_day == '01' %}
6
  {#- Jinja doesnt allow minus 1 date - Unsloth alternative #}
7
- {%- if yesterday_month == '01' %}
8
- {%- set yesterday_day = '31' %}
9
- {%- set yesterday_month = '12' %}
10
- {%- if yesterday_year == '2024' %}
11
- {%- set yesterday_year = '2023' %}
12
- {%- elif yesterday_year == '2025' %}
13
- {%- set yesterday_year = '2024' %}
14
- {%- elif yesterday_year == '2026' %}
15
- {%- set yesterday_year = '2025' %}
16
- {%- elif yesterday_year == '2027' %}
17
- {%- set yesterday_year = '2026' %}
18
- {%- elif yesterday_year == '2028' %}
19
- {%- set yesterday_year = '2027' %}
20
- {%- else %}
21
- {{- raise_exception('Unsloth custom template does not support years > 2028') }}
22
- {%- endif %}
23
- {%- elif yesterday_month == '02' %}
24
- {%- set yesterday_day = '31' %}
25
- {%- set yesterday_month = '01' %}
26
- {%- elif yesterday_month == '03' %}
27
- {%- set yesterday_month = '02' %}
28
- {%- set yesterday_day = '28' %}
29
- {%- if yesterday_year == '2024' %}
30
- {%- set yesterday_day = '29' %}
31
- {%- elif yesterday_year == '2028' %}
32
- {%- set yesterday_day = '29' %}
33
- {%- else %}
34
- {{- raise_exception('Unsloth custom template does not support years > 2028') }}
35
- {%- endif %}
36
- {%- elif yesterday_month == '04' %}
37
- {%- set yesterday_day = '31' %}
38
- {%- set yesterday_month = '03' %}
39
- {%- elif yesterday_month == '05' %}
40
- {%- set yesterday_day = '30' %}
41
- {%- set yesterday_month = '04' %}
42
- {%- elif yesterday_month == '06' %}
43
- {%- set yesterday_day = '31' %}
44
- {%- set yesterday_month = '05' %}
45
- {%- elif yesterday_month == '07' %}
46
- {%- set yesterday_day = '30' %}
47
- {%- set yesterday_month = '06' %}
48
- {%- elif yesterday_month == '08' %}
49
- {%- set yesterday_day = '31' %}
50
- {%- set yesterday_month = '07' %}
51
- {%- elif yesterday_month == '09' %}
52
- {%- set yesterday_day = '31' %}
53
- {%- set yesterday_month = '08' %}
54
- {%- elif yesterday_month == '10' %}
55
- {%- set yesterday_day = '30' %}
56
- {%- set yesterday_month = '09' %}
57
- {%- elif yesterday_month == '11' %}
58
- {%- set yesterday_day = '31' %}
59
- {%- set yesterday_month = '10' %}
60
- {%- elif yesterday_month == '12' %}
61
- {%- set yesterday_day = '30' %}
62
- {%- set yesterday_month = '11' %}
63
- {%- endif %}
64
  {%- elif yesterday_day == '02' %}
65
  {%- set yesterday_day = '01' %}
66
  {%- elif yesterday_day == '03' %}
@@ -141,15 +141,66 @@
141
  {%- endif %}
142
  {{- '[SYSTEM_PROMPT]' + system_message + '[/SYSTEM_PROMPT]' }}
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  {%- for message in loop_messages %}
145
  {%- if message['role'] == 'user' %}
 
 
 
 
 
 
 
 
 
 
146
  {%- if message['content'] is string %}
147
  {{- '[INST]' + message['content'] + '[/INST]' }}
148
  {%- else %}
149
  {{- '[INST]' }}
150
  {%- for block in message['content'] %}
151
  {%- if block['type'] == 'text' %}
152
- {{- block['text'] }}
 
 
 
 
 
 
 
153
  {%- elif block['type'] in ['image', 'image_url'] %}
154
  {{- '[IMG]' }}
155
  {%- else %}
@@ -158,19 +209,62 @@
158
  {%- endfor %}
159
  {{- '[/INST]' }}
160
  {%- endif %}
 
161
  {%- elif message['role'] == 'system' %}
162
  {%- if message['content'] is string %}
163
  {{- '[SYSTEM_PROMPT]' + message['content'] + '[/SYSTEM_PROMPT]' }}
164
  {%- else %}
165
  {{- '[SYSTEM_PROMPT]' + message['content'][0]['text'] + '[/SYSTEM_PROMPT]' }}
166
  {%- endif %}
 
167
  {%- elif message['role'] == 'assistant' %}
168
  {%- if message['content'] is string %}
169
- {{- message['content'] + eos_token }}
170
  {%- else %}
171
- {{- message['content'][0]['text'] + eos_token }}
172
  {%- endif %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  {%- else %}
174
- {{- raise_exception('Only user, system and assistant roles are supported!') }}
175
  {%- endif %}
176
  {%- endfor %}
 
 
1
  {%- set yesterday_day = strftime_now("%d") %}
2
  {%- set yesterday_month = strftime_now("%m") %}
3
  {%- set yesterday_year = strftime_now("%Y") %}
4
+ {%- set today_date = yesterday_year + '-' + yesterday_month + '-' + yesterday_day %}
5
  {%- if yesterday_day == '01' %}
6
  {#- Jinja doesnt allow minus 1 date - Unsloth alternative #}
7
+ {%- if yesterday_month == '01' %}
8
+ {%- set yesterday_day = '31' %}
9
+ {%- set yesterday_month = '12' %}
10
+ {%- if yesterday_year == '2024' %}
11
+ {%- set yesterday_year = '2023' %}
12
+ {%- elif yesterday_year == '2025' %}
13
+ {%- set yesterday_year = '2024' %}
14
+ {%- elif yesterday_year == '2026' %}
15
+ {%- set yesterday_year = '2025' %}
16
+ {%- elif yesterday_year == '2027' %}
17
+ {%- set yesterday_year = '2026' %}
18
+ {%- elif yesterday_year == '2028' %}
19
+ {%- set yesterday_year = '2027' %}
20
+ {%- else %}
21
+ {{- raise_exception('Unsloth custom template does not support years > 2028') }}
22
+ {%- endif %}
23
+ {%- elif yesterday_month == '02' %}
24
+ {%- set yesterday_day = '31' %}
25
+ {%- set yesterday_month = '01' %}
26
+ {%- elif yesterday_month == '03' %}
27
+ {%- set yesterday_month = '02' %}
28
+ {%- set yesterday_day = '28' %}
29
+ {%- if yesterday_year == '2024' %}
30
+ {%- set yesterday_day = '29' %}
31
+ {%- elif yesterday_year == '2028' %}
32
+ {%- set yesterday_day = '29' %}
33
+ {%- else %}
34
+ {{- raise_exception('Unsloth custom template does not support years > 2028') }}
35
+ {%- endif %}
36
+ {%- elif yesterday_month == '04' %}
37
+ {%- set yesterday_day = '31' %}
38
+ {%- set yesterday_month = '03' %}
39
+ {%- elif yesterday_month == '05' %}
40
+ {%- set yesterday_day = '30' %}
41
+ {%- set yesterday_month = '04' %}
42
+ {%- elif yesterday_month == '06' %}
43
+ {%- set yesterday_day = '31' %}
44
+ {%- set yesterday_month = '05' %}
45
+ {%- elif yesterday_month == '07' %}
46
+ {%- set yesterday_day = '30' %}
47
+ {%- set yesterday_month = '06' %}
48
+ {%- elif yesterday_month == '08' %}
49
+ {%- set yesterday_day = '31' %}
50
+ {%- set yesterday_month = '07' %}
51
+ {%- elif yesterday_month == '09' %}
52
+ {%- set yesterday_day = '31' %}
53
+ {%- set yesterday_month = '08' %}
54
+ {%- elif yesterday_month == '10' %}
55
+ {%- set yesterday_day = '30' %}
56
+ {%- set yesterday_month = '09' %}
57
+ {%- elif yesterday_month == '11' %}
58
+ {%- set yesterday_day = '31' %}
59
+ {%- set yesterday_month = '10' %}
60
+ {%- elif yesterday_month == '12' %}
61
+ {%- set yesterday_day = '30' %}
62
+ {%- set yesterday_month = '11' %}
63
+ {%- endif %}
64
  {%- elif yesterday_day == '02' %}
65
  {%- set yesterday_day = '01' %}
66
  {%- elif yesterday_day == '03' %}
 
141
  {%- endif %}
142
  {{- '[SYSTEM_PROMPT]' + system_message + '[/SYSTEM_PROMPT]' }}
143
 
144
+
145
+ {#- Tool description appended ONLY to last user message. Edits made by Unsloth #}
146
+ {#- Tool description appended also if last message is tool. Edits made by Unsloth #}
147
+ {%- set tools_description = "" %}
148
+ {%- set has_tools = false %}
149
+ {#- Cannot use set append_tools_index = loop.index0 since temporary variable - must use namespace #}
150
+ {%- set ns = namespace(append_tools_index=0, append_tools=false) %}
151
+
152
+ {%- if tools is defined and tools is not none and tools|length > 0 %}
153
+
154
+ {%- set has_tools = true %}
155
+ {%- set tools_description = "[AVAILABLE_TOOLS]" + (tools | tojson) + "[/AVAILABLE_TOOLS]" %}
156
+
157
+ {#- If User,Assistant,Tool,Tool we also need to append tools_description to last assistant WITHOUT tool_calls defined. Edits made by Unsloth #}
158
+ {%- if (loop_messages|last)['role'] == 'tool' %}
159
+
160
+ {#- Find last assistant WITHOUT tool_calls defined #}
161
+ {%- set ns.append_tools = true %}
162
+ {%- for message in loop_messages %}
163
+ {%- if message['role'] == 'assistant' %}
164
+
165
+ {#- Cannot use set append_tools_index = loop.index0 since temporary variable - must use namespace #}
166
+
167
+ {%- if message['tool_calls'] is not defined or message['tool_calls'] is none %}
168
+ {%- set ns.append_tools_index = loop.index0 %}
169
+ {%- endif %}
170
+
171
+ {%- endif %}
172
+ {%- endfor %}
173
+
174
+ {%- endif %}
175
+
176
+ {%- endif %}
177
+
178
  {%- for message in loop_messages %}
179
  {%- if message['role'] == 'user' %}
180
+
181
+ {%- if has_tools and loop.last %}
182
+ {{- tools_description }}
183
+ {%- endif %}
184
+
185
+ {#- If directly called tools in first turn, prepend to user #}
186
+ {%- if ns.append_tools and ns.append_tools_index == 0 %}
187
+ {{- tools_description }}
188
+ {%- endif %}
189
+
190
  {%- if message['content'] is string %}
191
  {{- '[INST]' + message['content'] + '[/INST]' }}
192
  {%- else %}
193
  {{- '[INST]' }}
194
  {%- for block in message['content'] %}
195
  {%- if block['type'] == 'text' %}
196
+
197
+ {#- Original did not have content which is weird. Added by Un-sloth. #}
198
+ {%- if block['text'] is defined %}
199
+ {{- block['text'] }}
200
+ {%- else %}
201
+ {{- block['content'] }}
202
+ {%- endif %}
203
+
204
  {%- elif block['type'] in ['image', 'image_url'] %}
205
  {{- '[IMG]' }}
206
  {%- else %}
 
209
  {%- endfor %}
210
  {{- '[/INST]' }}
211
  {%- endif %}
212
+
213
  {%- elif message['role'] == 'system' %}
214
  {%- if message['content'] is string %}
215
  {{- '[SYSTEM_PROMPT]' + message['content'] + '[/SYSTEM_PROMPT]' }}
216
  {%- else %}
217
  {{- '[SYSTEM_PROMPT]' + message['content'][0]['text'] + '[/SYSTEM_PROMPT]' }}
218
  {%- endif %}
219
+
220
  {%- elif message['role'] == 'assistant' %}
221
  {%- if message['content'] is string %}
222
+ {{- message['content'] }}
223
  {%- else %}
224
+ {{- message['content'][0]['text'] }}
225
  {%- endif %}
226
+
227
+ {#- If User,Assistant,Tool,Tool we also need to append tools_description. Edits made by Unsloth #}
228
+
229
+ {%- if has_tools and (loop.index0 == ns.append_tools_index) %}
230
+ {{- eos_token }}
231
+ {{- tools_description }}
232
+ {%- endif %}
233
+
234
+ {%- if message['tool_calls'] is defined and message['tool_calls'] is not none %}
235
+ {%- for tool in message['tool_calls'] %}
236
+ {%- if tool['id'] is not defined %}
237
+ {{- raise_exception('Tool ID must be provided!') }}
238
+ {%- endif %}
239
+ {%- set tool_call_id = tool['id'] %}
240
+ {%- if tool_call_id is not string or tool_call_id|length < 9 %}
241
+ {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9!") }}
242
+ {%- endif %}
243
+ {%- set arguments = tool['function']['arguments'] %}
244
+ {%- if arguments is not string %}
245
+ {%- set arguments = arguments|tojson %}
246
+ {%- endif %}
247
+ {#- Must list tool calls AFTER assistant. Edits made by Un-sloth #}
248
+ {{- "[TOOL_CALLS]" + tool['function']['name'] + "[CALL_ID]" + tool_call_id + "[ARGS]" + arguments }}
249
+ {%- endfor %}
250
+ {%- endif %}
251
+
252
+ {#- Must not add EOS if added tools_description. Unsloth edits. #}
253
+ {%- if (loop.index0 != ns.append_tools_index) %}
254
+ {{- eos_token }}
255
+ {%- endif %}
256
+
257
+ {%- elif message['role'] == 'tool' %}
258
+ {%- if message['tool_call_id'] is not defined %}
259
+ {{- raise_exception('tool_call_id must be provided!') }}
260
+ {%- endif %}
261
+ {%- set tool_call_id = message['tool_call_id'] %}
262
+ {%- if tool_call_id is not string or tool_call_id|length < 9 %}
263
+ {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9!") }}
264
+ {%- endif %}
265
+ {{- "[TOOL_RESULTS]" + tool_call_id + "[TOOL_CONTENT]" + message['content'] + "[/TOOL_RESULTS]" }}
266
+
267
  {%- else %}
268
+ {{- raise_exception('Only user, systemm assistant and tool roles are supported in the custom template made by Unsloth!') }}
269
  {%- endif %}
270
  {%- endfor %}