deepanshupillm commited on
Commit
021da65
·
verified ·
1 Parent(s): 1cacbe1

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +4 -54
chat_template.jinja CHANGED
@@ -1,6 +1,6 @@
1
  {%- set image_count = namespace(value=0) %}
2
  {%- set video_count = namespace(value=0) %}
3
-
4
  {%- macro render_content(content, do_vision_count, is_system_content=false) %}
5
  {%- if content is string %}
6
  {{- content }}
@@ -17,7 +17,6 @@
17
  {{- 'Picture ' ~ image_count.value ~ ': ' }}
18
  {%- endif %}
19
  {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
20
-
21
  {%- elif 'video' in item or item.type == 'video' %}
22
  {%- if is_system_content %}
23
  {{- raise_exception('System message cannot contain videos.') }}
@@ -29,47 +28,37 @@
29
  {{- 'Video ' ~ video_count.value ~ ': ' }}
30
  {%- endif %}
31
  {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
32
-
33
  {%- elif 'text' in item %}
34
  {{- item.text }}
35
-
36
  {%- else %}
37
  {{- raise_exception('Unexpected item type in content.') }}
38
  {%- endif %}
39
  {%- endfor %}
40
-
41
  {%- elif content is none or content is undefined %}
42
  {{- '' }}
43
-
44
  {%- else %}
45
  {{- raise_exception('Unexpected content type.') }}
46
  {%- endif %}
47
  {%- endmacro %}
48
-
49
  {%- if not messages %}
50
  {{- raise_exception('No messages provided.') }}
51
  {%- endif %}
52
-
53
  {# ============================================================
54
  SYSTEM MESSAGE
55
  ============================================================ #}
56
-
57
  {%- if messages[0].role == 'system' %}
58
  {%- set user_system_content = render_content(
59
  messages[0].content, false, true
60
  )|trim %}
61
-
62
  {{- '<|im_start|>system\n' }}
63
  {{- india_position_policy }}
64
-
65
  {%- if tools and tools is iterable and tools is not mapping %}
66
  {{- '\n\n# Tools\n\nYou have access to the following functions:\n\n<tools>' }}
67
-
68
  {%- for tool in tools %}
69
  {{- '\n' }}
70
  {{- tool | tojson }}
71
  {%- endfor %}
72
-
73
  {{- '\n</tools>' }}
74
  {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}
75
  {{- '<tool_call>\n<function=example_function_name>\n' }}
@@ -82,25 +71,20 @@
82
  {{- '- Do not add text after a function call.\n' }}
83
  {{- '- If no function call is needed, answer normally.\n' }}
84
  {%- endif %}
85
-
86
  {%- if user_system_content %}
87
  {{- '\n\n' + user_system_content }}
88
  {%- endif %}
89
-
90
  {{- '<|im_end|>\n' }}
91
-
92
  {%- else %}
93
  {{- '<|im_start|>system\n' }}
94
  {{- india_position_policy }}
95
-
96
  {%- if tools and tools is iterable and tools is not mapping %}
97
  {{- '\n\n# Tools\n\nYou have access to the following functions:\n\n<tools>' }}
98
-
99
  {%- for tool in tools %}
100
  {{- '\n' }}
101
  {{- tool | tojson }}
102
  {%- endfor %}
103
-
104
  {{- '\n</tools>' }}
105
  {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}
106
  {{- '<tool_call>\n<function=example_function_name>\n' }}
@@ -113,25 +97,19 @@
113
  {{- '- Do not add text after a function call.\n' }}
114
  {{- '- If no function call is needed, answer normally.\n' }}
115
  {%- endif %}
116
-
117
  {{- '<|im_end|>\n' }}
118
  {%- endif %}
119
-
120
  {# ============================================================
121
  FIND LAST USER QUERY
122
  ============================================================ #}
123
-
124
  {%- set ns = namespace(
125
  multi_step_tool=true,
126
  last_query_index=messages|length - 1
127
  ) %}
128
-
129
  {%- for message in messages[::-1] %}
130
  {%- set index = (messages|length - 1) - loop.index0 %}
131
-
132
  {%- if ns.multi_step_tool and message.role == 'user' %}
133
  {%- set content = render_content(message.content, false)|trim %}
134
-
135
  {%- if not (
136
  content.startswith('<tool_response>')
137
  and content.endswith('</tool_response>')
@@ -141,31 +119,24 @@
141
  {%- endif %}
142
  {%- endif %}
143
  {%- endfor %}
144
-
145
  {%- if ns.multi_step_tool %}
146
  {{- raise_exception('No user query found in messages.') }}
147
  {%- endif %}
148
-
149
  {# ============================================================
150
  RENDER MESSAGES
151
  ============================================================ #}
152
-
153
  {%- for message in messages %}
154
  {%- set content = render_content(message.content, true)|trim %}
155
-
156
  {%- if message.role == 'system' %}
157
  {%- if not loop.first %}
158
  {{- raise_exception('System message must be at the beginning.') }}
159
  {%- endif %}
160
-
161
  {%- elif message.role == 'user' %}
162
  {{- '<|im_start|>user\n' }}
163
  {{- content }}
164
  {{- '<|im_end|>\n' }}
165
-
166
  {%- elif message.role == 'assistant' %}
167
  {%- set reasoning_content = '' %}
168
-
169
  {%- if message.reasoning_content is string %}
170
  {%- set reasoning_content = message.reasoning_content %}
171
  {%- else %}
@@ -176,15 +147,12 @@
176
  .split('<think>')[-1]
177
  .lstrip('\n')
178
  %}
179
-
180
  {%- set content =
181
  content.split('</think>')[-1].lstrip('\n')
182
  %}
183
  {%- endif %}
184
  {%- endif %}
185
-
186
  {%- set reasoning_content = reasoning_content|trim %}
187
-
188
  {%- if loop.index0 > ns.last_query_index %}
189
  {{- '<|im_start|>assistant\n<think>\n' }}
190
  {{- reasoning_content }}
@@ -194,19 +162,14 @@
194
  {{- '<|im_start|>assistant\n' }}
195
  {{- content }}
196
  {%- endif %}
197
-
198
  {# ---------------- TOOL CALLS ---------------- #}
199
-
200
  {%- if message.tool_calls
201
  and message.tool_calls is iterable
202
  and message.tool_calls is not mapping %}
203
-
204
  {%- for tool_call in message.tool_calls %}
205
-
206
  {%- if tool_call.function is defined %}
207
  {%- set tool_call = tool_call.function %}
208
  {%- endif %}
209
-
210
  {%- if loop.first %}
211
  {%- if content|trim %}
212
  {{- '\n\n' }}
@@ -214,17 +177,14 @@
214
  {%- else %}
215
  {{- '\n' }}
216
  {%- endif %}
217
-
218
  {{- '<tool_call>\n<function=' }}
219
  {{- tool_call.name }}
220
  {{- '>\n' }}
221
-
222
  {%- if tool_call.arguments is defined %}
223
  {%- for args_name, args_value in tool_call.arguments|items %}
224
  {{- '<parameter=' }}
225
  {{- args_name }}
226
  {{- '>\n' }}
227
-
228
  {%- if args_value is mapping
229
  or (args_value is sequence
230
  and args_value is not string) %}
@@ -232,47 +192,37 @@
232
  {%- else %}
233
  {{- args_value | string }}
234
  {%- endif %}
235
-
236
  {{- '\n</parameter>\n' }}
237
  {%- endfor %}
238
  {%- endif %}
239
-
240
  {{- '</function>\n</tool_call>' }}
241
  {%- endfor %}
242
  {%- endif %}
243
-
244
  {{- '<|im_end|>\n' }}
245
-
246
  {%- elif message.role == 'tool' %}
247
  {%- if loop.previtem and loop.previtem.role != 'tool' %}
248
  {{- '<|im_start|>user' }}
249
  {%- endif %}
250
-
251
  {{- '\n<tool_response>\n' }}
252
  {{- content }}
253
  {{- '\n</tool_response>' }}
254
-
255
  {%- if not loop.last and loop.nextitem.role != 'tool' %}
256
  {{- '<|im_end|>\n' }}
257
  {%- elif loop.last %}
258
  {{- '<|im_end|>\n' }}
259
  {%- endif %}
260
-
261
  {%- else %}
262
  {{- raise_exception('Unexpected message role.') }}
263
  {%- endif %}
264
  {%- endfor %}
265
-
266
  {# ============================================================
267
  GENERATION PROMPT
268
  ============================================================ #}
269
-
270
  {%- if add_generation_prompt %}
271
  {{- '<|im_start|>assistant\n' }}
272
-
273
  {%- if enable_thinking is defined and enable_thinking is false %}
274
  {{- '<think>\n\n</think>\n\n' }}
275
  {%- else %}
276
  {{- '<think>\n' }}
277
  {%- endif %}
278
- {%- endif %}
 
1
  {%- set image_count = namespace(value=0) %}
2
  {%- set video_count = namespace(value=0) %}
3
+ {%- set identity_policy = "You are Alpie Seed 9B, a model built by 169Pi, an Indian research company. If anyone asks who you are or what your name is, say that you are Alpie Seed 9B, a model built by 169Pi, an Indian research company. If anyone asks who built or created you, say 169Pi AI. If anyone asks where you are from, say India." %}
4
  {%- macro render_content(content, do_vision_count, is_system_content=false) %}
5
  {%- if content is string %}
6
  {{- content }}
 
17
  {{- 'Picture ' ~ image_count.value ~ ': ' }}
18
  {%- endif %}
19
  {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
 
20
  {%- elif 'video' in item or item.type == 'video' %}
21
  {%- if is_system_content %}
22
  {{- raise_exception('System message cannot contain videos.') }}
 
28
  {{- 'Video ' ~ video_count.value ~ ': ' }}
29
  {%- endif %}
30
  {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
 
31
  {%- elif 'text' in item %}
32
  {{- item.text }}
 
33
  {%- else %}
34
  {{- raise_exception('Unexpected item type in content.') }}
35
  {%- endif %}
36
  {%- endfor %}
 
37
  {%- elif content is none or content is undefined %}
38
  {{- '' }}
 
39
  {%- else %}
40
  {{- raise_exception('Unexpected content type.') }}
41
  {%- endif %}
42
  {%- endmacro %}
 
43
  {%- if not messages %}
44
  {{- raise_exception('No messages provided.') }}
45
  {%- endif %}
 
46
  {# ============================================================
47
  SYSTEM MESSAGE
48
  ============================================================ #}
 
49
  {%- if messages[0].role == 'system' %}
50
  {%- set user_system_content = render_content(
51
  messages[0].content, false, true
52
  )|trim %}
 
53
  {{- '<|im_start|>system\n' }}
54
  {{- india_position_policy }}
55
+ {{- '\n\n' + identity_policy }}
56
  {%- if tools and tools is iterable and tools is not mapping %}
57
  {{- '\n\n# Tools\n\nYou have access to the following functions:\n\n<tools>' }}
 
58
  {%- for tool in tools %}
59
  {{- '\n' }}
60
  {{- tool | tojson }}
61
  {%- endfor %}
 
62
  {{- '\n</tools>' }}
63
  {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}
64
  {{- '<tool_call>\n<function=example_function_name>\n' }}
 
71
  {{- '- Do not add text after a function call.\n' }}
72
  {{- '- If no function call is needed, answer normally.\n' }}
73
  {%- endif %}
 
74
  {%- if user_system_content %}
75
  {{- '\n\n' + user_system_content }}
76
  {%- endif %}
 
77
  {{- '<|im_end|>\n' }}
 
78
  {%- else %}
79
  {{- '<|im_start|>system\n' }}
80
  {{- india_position_policy }}
81
+ {{- '\n\n' + identity_policy }}
82
  {%- if tools and tools is iterable and tools is not mapping %}
83
  {{- '\n\n# Tools\n\nYou have access to the following functions:\n\n<tools>' }}
 
84
  {%- for tool in tools %}
85
  {{- '\n' }}
86
  {{- tool | tojson }}
87
  {%- endfor %}
 
88
  {{- '\n</tools>' }}
89
  {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}
90
  {{- '<tool_call>\n<function=example_function_name>\n' }}
 
97
  {{- '- Do not add text after a function call.\n' }}
98
  {{- '- If no function call is needed, answer normally.\n' }}
99
  {%- endif %}
 
100
  {{- '<|im_end|>\n' }}
101
  {%- endif %}
 
102
  {# ============================================================
103
  FIND LAST USER QUERY
104
  ============================================================ #}
 
105
  {%- set ns = namespace(
106
  multi_step_tool=true,
107
  last_query_index=messages|length - 1
108
  ) %}
 
109
  {%- for message in messages[::-1] %}
110
  {%- set index = (messages|length - 1) - loop.index0 %}
 
111
  {%- if ns.multi_step_tool and message.role == 'user' %}
112
  {%- set content = render_content(message.content, false)|trim %}
 
113
  {%- if not (
114
  content.startswith('<tool_response>')
115
  and content.endswith('</tool_response>')
 
119
  {%- endif %}
120
  {%- endif %}
121
  {%- endfor %}
 
122
  {%- if ns.multi_step_tool %}
123
  {{- raise_exception('No user query found in messages.') }}
124
  {%- endif %}
 
125
  {# ============================================================
126
  RENDER MESSAGES
127
  ============================================================ #}
 
128
  {%- for message in messages %}
129
  {%- set content = render_content(message.content, true)|trim %}
 
130
  {%- if message.role == 'system' %}
131
  {%- if not loop.first %}
132
  {{- raise_exception('System message must be at the beginning.') }}
133
  {%- endif %}
 
134
  {%- elif message.role == 'user' %}
135
  {{- '<|im_start|>user\n' }}
136
  {{- content }}
137
  {{- '<|im_end|>\n' }}
 
138
  {%- elif message.role == 'assistant' %}
139
  {%- set reasoning_content = '' %}
 
140
  {%- if message.reasoning_content is string %}
141
  {%- set reasoning_content = message.reasoning_content %}
142
  {%- else %}
 
147
  .split('<think>')[-1]
148
  .lstrip('\n')
149
  %}
 
150
  {%- set content =
151
  content.split('</think>')[-1].lstrip('\n')
152
  %}
153
  {%- endif %}
154
  {%- endif %}
 
155
  {%- set reasoning_content = reasoning_content|trim %}
 
156
  {%- if loop.index0 > ns.last_query_index %}
157
  {{- '<|im_start|>assistant\n<think>\n' }}
158
  {{- reasoning_content }}
 
162
  {{- '<|im_start|>assistant\n' }}
163
  {{- content }}
164
  {%- endif %}
 
165
  {# ---------------- TOOL CALLS ---------------- #}
 
166
  {%- if message.tool_calls
167
  and message.tool_calls is iterable
168
  and message.tool_calls is not mapping %}
 
169
  {%- for tool_call in message.tool_calls %}
 
170
  {%- if tool_call.function is defined %}
171
  {%- set tool_call = tool_call.function %}
172
  {%- endif %}
 
173
  {%- if loop.first %}
174
  {%- if content|trim %}
175
  {{- '\n\n' }}
 
177
  {%- else %}
178
  {{- '\n' }}
179
  {%- endif %}
 
180
  {{- '<tool_call>\n<function=' }}
181
  {{- tool_call.name }}
182
  {{- '>\n' }}
 
183
  {%- if tool_call.arguments is defined %}
184
  {%- for args_name, args_value in tool_call.arguments|items %}
185
  {{- '<parameter=' }}
186
  {{- args_name }}
187
  {{- '>\n' }}
 
188
  {%- if args_value is mapping
189
  or (args_value is sequence
190
  and args_value is not string) %}
 
192
  {%- else %}
193
  {{- args_value | string }}
194
  {%- endif %}
 
195
  {{- '\n</parameter>\n' }}
196
  {%- endfor %}
197
  {%- endif %}
 
198
  {{- '</function>\n</tool_call>' }}
199
  {%- endfor %}
200
  {%- endif %}
 
201
  {{- '<|im_end|>\n' }}
 
202
  {%- elif message.role == 'tool' %}
203
  {%- if loop.previtem and loop.previtem.role != 'tool' %}
204
  {{- '<|im_start|>user' }}
205
  {%- endif %}
 
206
  {{- '\n<tool_response>\n' }}
207
  {{- content }}
208
  {{- '\n</tool_response>' }}
 
209
  {%- if not loop.last and loop.nextitem.role != 'tool' %}
210
  {{- '<|im_end|>\n' }}
211
  {%- elif loop.last %}
212
  {{- '<|im_end|>\n' }}
213
  {%- endif %}
 
214
  {%- else %}
215
  {{- raise_exception('Unexpected message role.') }}
216
  {%- endif %}
217
  {%- endfor %}
 
218
  {# ============================================================
219
  GENERATION PROMPT
220
  ============================================================ #}
 
221
  {%- if add_generation_prompt %}
222
  {{- '<|im_start|>assistant\n' }}
 
223
  {%- if enable_thinking is defined and enable_thinking is false %}
224
  {{- '<think>\n\n</think>\n\n' }}
225
  {%- else %}
226
  {{- '<think>\n' }}
227
  {%- endif %}
228
+ {%- endif %}