Updated chat template

#82
by Rocketknight1 HF Staff - opened
Files changed (1) hide show
  1. chat_template.jinja +67 -133
chat_template.jinja CHANGED
@@ -84,8 +84,7 @@
84
 
85
  {%- elif param_spec.type == "object" -%}
86
  {%- if param_spec.properties -%}
87
- {{- "{
88
- " }}
89
  {%- for prop_name, prop_spec in param_spec.properties.items() -%}
90
  {{- prop_name -}}
91
  {%- if prop_name not in (param_spec.required or []) -%}
@@ -107,24 +106,17 @@
107
  {%- endmacro -%}
108
 
109
  {%- macro render_tool_namespace(namespace_name, tools) -%}
110
- {{- "## " + namespace_name + "
111
-
112
- " }}
113
- {{- "namespace " + namespace_name + " {
114
-
115
- " }}
116
  {%- for tool in tools %}
117
  {%- set tool = tool.function %}
118
- {{- "// " + tool.description + "
119
- " }}
120
  {{- "type "+ tool.name + " = " }}
121
  {%- if tool.parameters and tool.parameters.properties %}
122
- {{- "(_: {
123
- " }}
124
  {%- for param_name, param_spec in tool.parameters.properties.items() %}
125
  {%- if param_spec.description %}
126
- {{- "// " + param_spec.description + "
127
- " }}
128
  {%- endif %}
129
  {{- param_name }}
130
  {%- if param_name not in (tool.parameters.required or []) -%}
@@ -142,20 +134,14 @@
142
  {%- endif -%}
143
  {%- endif -%}
144
  {%- if not loop.last %}
145
- {{- ",
146
- " }}
147
  {%- else %}
148
- {{- "
149
- " }}
150
  {%- endif -%}
151
  {%- endfor %}
152
- {{- "}) => any;
153
-
154
- " }}
155
  {%- else -%}
156
- {{- "() => any;
157
-
158
- " }}
159
  {%- endif -%}
160
  {%- endfor %}
161
  {{- "} // namespace " + namespace_name }}
@@ -163,92 +149,46 @@
163
 
164
  {%- macro render_builtin_tools(browser_tool, python_tool) -%}
165
  {%- if browser_tool %}
166
- {{- "## browser
167
-
168
- " }}
169
- {{- "// Tool for browsing.
170
- " }}
171
- {{- "// The `cursor` appears in brackets before each browsing display: `[{cursor}]`.
172
- " }}
173
- {{- "// Cite information from the tool using the following format:
174
- " }}
175
- {{- "// `【{cursor}†L{line_start}(-L{line_end})?】`, for example: `【6†L9-L11】` or `【8†L3】`.
176
- " }}
177
- {{- "// Do not quote more than 10 words directly from the tool output.
178
- " }}
179
- {{- "// sources=web (default: web)
180
- " }}
181
- {{- "namespace browser {
182
-
183
- " }}
184
- {{- "// Searches for information related to `query` and displays `topn` results.
185
- " }}
186
- {{- "type search = (_: {
187
- " }}
188
- {{- "query: string,
189
- " }}
190
- {{- "topn?: number, // default: 10
191
- " }}
192
- {{- "source?: string,
193
- " }}
194
- {{- "}) => any;
195
-
196
- " }}
197
- {{- "// Opens the link `id` from the page indicated by `cursor` starting at line number `loc`, showing `num_lines` lines.
198
- " }}
199
- {{- "// Valid link ids are displayed with the formatting: `【{id}†.*】`.
200
- " }}
201
- {{- "// If `cursor` is not provided, the most recent page is implied.
202
- " }}
203
- {{- "// If `id` is a string, it is treated as a fully qualified URL associated with `source`.
204
- " }}
205
- {{- "// If `loc` is not provided, the viewport will be positioned at the beginning of the document or centered on the most relevant passage, if available.
206
- " }}
207
- {{- "// Use this function without `id` to scroll to a new location of an opened page.
208
- " }}
209
- {{- "type open = (_: {
210
- " }}
211
- {{- "id?: number | string, // default: -1
212
- " }}
213
- {{- "cursor?: number, // default: -1
214
- " }}
215
- {{- "loc?: number, // default: -1
216
- " }}
217
- {{- "num_lines?: number, // default: -1
218
- " }}
219
- {{- "view_source?: boolean, // default: false
220
- " }}
221
- {{- "source?: string,
222
- " }}
223
- {{- "}) => any;
224
-
225
- " }}
226
- {{- "// Finds exact matches of `pattern` in the current page, or the page given by `cursor`.
227
- " }}
228
- {{- "type find = (_: {
229
- " }}
230
- {{- "pattern: string,
231
- " }}
232
- {{- "cursor?: number, // default: -1
233
- " }}
234
- {{- "}) => any;
235
-
236
- " }}
237
- {{- "} // namespace browser
238
-
239
- " }}
240
  {%- endif -%}
241
 
242
  {%- if python_tool %}
243
- {{- "## python
244
-
245
- " }}
246
- {{- "Use this tool to execute Python code in your chain of thought. The code will not be shown to the user. This tool should be used for internal reasoning, but not for code that is intended to be visible to the user (e.g. when creating plots, tables, or files).
247
-
248
- " }}
249
- {{- "When you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 120.0 seconds. The drive at '/mnt/data' can be used to save and persist user files. Internet access for this session is UNKNOWN. Depends on the cluster.
250
-
251
- " }}
252
  {%- endif -%}
253
  {%- endmacro -%}
254
 
@@ -257,23 +197,15 @@
257
  {%- if model_identity is not defined %}
258
  {%- set model_identity = "You are ChatGPT, a large language model trained by OpenAI." %}
259
  {%- endif %}
260
- {{- model_identity + "
261
- " }}
262
- {{- "Knowledge cutoff: 2024-06
263
- " }}
264
- {{- "Current date: " + strftime_now("%Y-%m-%d") + "
265
-
266
- " }}
267
  {%- if reasoning_effort is not defined %}
268
  {%- set reasoning_effort = "medium" %}
269
  {%- endif %}
270
- {{- "Reasoning: " + reasoning_effort + "
271
-
272
- " }}
273
  {%- if builtin_tools %}
274
- {{- "# Tools
275
-
276
- " }}
277
  {%- set available_builtin_tools = namespace(browser=false, python=false) %}
278
  {%- for tool in builtin_tools %}
279
  {%- if tool == "browser" %}
@@ -286,8 +218,7 @@
286
  {%- endif -%}
287
  {{- "# Valid channels: analysis, commentary, final. Channel must be included for every message." }}
288
  {%- if tools -%}
289
- {{- "
290
- Calls to these tools must go to the commentary channel: 'functions'." }}
291
  {%- endif -%}
292
  {%- endmacro -%}
293
 
@@ -312,18 +243,12 @@ Calls to these tools must go to the commentary channel: 'functions'." }}
312
  {%- if developer_message or tools %}
313
  {{- "<|start|>developer<|message|>" }}
314
  {%- if developer_message %}
315
- {{- "# Instructions
316
-
317
- " }}
318
  {{- developer_message }}
319
  {%- endif %}
320
  {%- if tools -%}
321
- {{- "
322
-
323
- " }}
324
- {{- "# Tools
325
-
326
- " }}
327
  {{- render_tool_namespace("functions", tools) }}
328
  {%- endif -%}
329
  {{- "<|end|>" }}
@@ -346,6 +271,15 @@ Calls to these tools must go to the commentary channel: 'functions'." }}
346
  {%- endif %}
347
  {%- endif %}
348
  {%- if "tool_calls" in message %}
 
 
 
 
 
 
 
 
 
349
  {#- We assume max 1 tool call per message, and so we infer the tool call name #}
350
  {#- in "tool" messages from the most recent assistant tool call name #}
351
  {%- set tool_call = message.tool_calls[0] %}
@@ -354,9 +288,9 @@ Calls to these tools must go to the commentary channel: 'functions'." }}
354
  {%- endif %}
355
  {%- if message.content and message.thinking %}
356
  {{- raise_exception("Cannot pass both content and thinking in an assistant message with tool calls! Put the analysis message in one or the other, but not both.") }}
357
- {%- elif message.content %}
358
  {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.content + "<|end|>" }}
359
- {%- elif message.thinking %}
360
  {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.thinking + "<|end|>" }}
361
  {%- endif %}
362
  {{- "<|start|>assistant to=" }}
 
84
 
85
  {%- elif param_spec.type == "object" -%}
86
  {%- if param_spec.properties -%}
87
+ {{- "{\n" }}
 
88
  {%- for prop_name, prop_spec in param_spec.properties.items() -%}
89
  {{- prop_name -}}
90
  {%- if prop_name not in (param_spec.required or []) -%}
 
106
  {%- endmacro -%}
107
 
108
  {%- macro render_tool_namespace(namespace_name, tools) -%}
109
+ {{- "## " + namespace_name + "\n\n" }}
110
+ {{- "namespace " + namespace_name + " {\n\n" }}
 
 
 
 
111
  {%- for tool in tools %}
112
  {%- set tool = tool.function %}
113
+ {{- "// " + tool.description + "\n" }}
 
114
  {{- "type "+ tool.name + " = " }}
115
  {%- if tool.parameters and tool.parameters.properties %}
116
+ {{- "(_: {\n" }}
 
117
  {%- for param_name, param_spec in tool.parameters.properties.items() %}
118
  {%- if param_spec.description %}
119
+ {{- "// " + param_spec.description + "\n" }}
 
120
  {%- endif %}
121
  {{- param_name }}
122
  {%- if param_name not in (tool.parameters.required or []) -%}
 
134
  {%- endif -%}
135
  {%- endif -%}
136
  {%- if not loop.last %}
137
+ {{- ",\n" }}
 
138
  {%- else %}
139
+ {{- ",\n" }}
 
140
  {%- endif -%}
141
  {%- endfor %}
142
+ {{- "}) => any;\n\n" }}
 
 
143
  {%- else -%}
144
+ {{- "() => any;\n\n" }}
 
 
145
  {%- endif -%}
146
  {%- endfor %}
147
  {{- "} // namespace " + namespace_name }}
 
149
 
150
  {%- macro render_builtin_tools(browser_tool, python_tool) -%}
151
  {%- if browser_tool %}
152
+ {{- "## browser\n\n" }}
153
+ {{- "// Tool for browsing.\n" }}
154
+ {{- "// The `cursor` appears in brackets before each browsing display: `[{cursor}]`.\n" }}
155
+ {{- "// Cite information from the tool using the following format:\n" }}
156
+ {{- "// `【{cursor}†L{line_start}(-L{line_end})?】`, for example: `【6†L9-L11】` or `【8†L3】`.\n" }}
157
+ {{- "// Do not quote more than 10 words directly from the tool output.\n" }}
158
+ {{- "// sources=web (default: web)\n" }}
159
+ {{- "namespace browser {\n\n" }}
160
+ {{- "// Searches for information related to `query` and displays `topn` results.\n" }}
161
+ {{- "type search = (_: {\n" }}
162
+ {{- "query: string,\n" }}
163
+ {{- "topn?: number, // default: 10\n" }}
164
+ {{- "source?: string,\n" }}
165
+ {{- "}) => any;\n\n" }}
166
+ {{- "// Opens the link `id` from the page indicated by `cursor` starting at line number `loc`, showing `num_lines` lines.\n" }}
167
+ {{- "// Valid link ids are displayed with the formatting: `【{id}†.*】`.\n" }}
168
+ {{- "// If `cursor` is not provided, the most recent page is implied.\n" }}
169
+ {{- "// If `id` is a string, it is treated as a fully qualified URL associated with `source`.\n" }}
170
+ {{- "// If `loc` is not provided, the viewport will be positioned at the beginning of the document or centered on the most relevant passage, if available.\n" }}
171
+ {{- "// Use this function without `id` to scroll to a new location of an opened page.\n" }}
172
+ {{- "type open = (_: {\n" }}
173
+ {{- "id?: number | string, // default: -1\n" }}
174
+ {{- "cursor?: number, // default: -1\n" }}
175
+ {{- "loc?: number, // default: -1\n" }}
176
+ {{- "num_lines?: number, // default: -1\n" }}
177
+ {{- "view_source?: boolean, // default: false\n" }}
178
+ {{- "source?: string,\n" }}
179
+ {{- "}) => any;\n\n" }}
180
+ {{- "// Finds exact matches of `pattern` in the current page, or the page given by `cursor`.\n" }}
181
+ {{- "type find = (_: {\n" }}
182
+ {{- "pattern: string,\n" }}
183
+ {{- "cursor?: number, // default: -1\n" }}
184
+ {{- "}) => any;\n\n" }}
185
+ {{- "} // namespace browser\n\n" }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  {%- endif -%}
187
 
188
  {%- if python_tool %}
189
+ {{- "## python\n\n" }}
190
+ {{- "Use this tool to execute Python code in your chain of thought. The code will not be shown to the user. This tool should be used for internal reasoning, but not for code that is intended to be visible to the user (e.g. when creating plots, tables, or files).\n\n" }}
191
+ {{- "When you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 120.0 seconds. The drive at '/mnt/data' can be used to save and persist user files. Internet access for this session is UNKNOWN. Depends on the cluster.\n\n" }}
 
 
 
 
 
 
192
  {%- endif -%}
193
  {%- endmacro -%}
194
 
 
197
  {%- if model_identity is not defined %}
198
  {%- set model_identity = "You are ChatGPT, a large language model trained by OpenAI." %}
199
  {%- endif %}
200
+ {{- model_identity + "\n" }}
201
+ {{- "Knowledge cutoff: 2024-06\n" }}
202
+ {{- "Current date: " + strftime_now("%Y-%m-%d") + "\n\n" }}
 
 
 
 
203
  {%- if reasoning_effort is not defined %}
204
  {%- set reasoning_effort = "medium" %}
205
  {%- endif %}
206
+ {{- "Reasoning: " + reasoning_effort + "\n\n" }}
 
 
207
  {%- if builtin_tools %}
208
+ {{- "# Tools\n\n" }}
 
 
209
  {%- set available_builtin_tools = namespace(browser=false, python=false) %}
210
  {%- for tool in builtin_tools %}
211
  {%- if tool == "browser" %}
 
218
  {%- endif -%}
219
  {{- "# Valid channels: analysis, commentary, final. Channel must be included for every message." }}
220
  {%- if tools -%}
221
+ {{- "\nCalls to these tools must go to the commentary channel: 'functions'." }}
 
222
  {%- endif -%}
223
  {%- endmacro -%}
224
 
 
243
  {%- if developer_message or tools %}
244
  {{- "<|start|>developer<|message|>" }}
245
  {%- if developer_message %}
246
+ {{- "# Instructions\n\n" }}
 
 
247
  {{- developer_message }}
248
  {%- endif %}
249
  {%- if tools -%}
250
+ {{- "\n\n" }}
251
+ {{- "# Tools\n\n" }}
 
 
 
 
252
  {{- render_tool_namespace("functions", tools) }}
253
  {%- endif -%}
254
  {{- "<|end|>" }}
 
271
  {%- endif %}
272
  {%- endif %}
273
  {%- if "tool_calls" in message %}
274
+ {#- We need very careful handling here - we want to drop the tool call analysis message if the model #}
275
+ {#- has output a later <|final|> message, but otherwise we want to retain it. This is the only case #}
276
+ {#- when we render CoT/analysis messages in inference. #}
277
+ {%- set future_final_message = namespace(found=false) %}
278
+ {%- for future_message in loop_messages[loop.index:] %}
279
+ {%- if future_message.role == 'assistant' and "tool_calls" not in future_message %}
280
+ {%- set future_final_message.found = true %}
281
+ {%- endif %}
282
+ {%- endfor %}
283
  {#- We assume max 1 tool call per message, and so we infer the tool call name #}
284
  {#- in "tool" messages from the most recent assistant tool call name #}
285
  {%- set tool_call = message.tool_calls[0] %}
 
288
  {%- endif %}
289
  {%- if message.content and message.thinking %}
290
  {{- raise_exception("Cannot pass both content and thinking in an assistant message with tool calls! Put the analysis message in one or the other, but not both.") }}
291
+ {%- elif message.content and not future_final_message.found %}
292
  {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.content + "<|end|>" }}
293
+ {%- elif message.thinking and not future_final_message.found %}
294
  {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.thinking + "<|end|>" }}
295
  {%- endif %}
296
  {{- "<|start|>assistant to=" }}