qgallouedec HF Staff commited on
Commit
94f8a3e
·
verified ·
1 Parent(s): 1783450

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +59 -35
chat_template.jinja CHANGED
@@ -117,14 +117,15 @@
117
  {%- set tool = tool.function %}
118
  {{- "// " + tool.description + "
119
  " }}
120
- {{- "type "+ tool.name + " = (" }}
121
- {%- if tool.parameters and tool.parameters.properties -%}
122
- {{- "_: " }}
123
- {{- "{
124
  " }}
125
  {%- for param_name, param_spec in tool.parameters.properties.items() %}
126
- {{- "// " + param_spec.description + "
 
127
  " }}
 
128
  {{- param_name }}
129
  {%- if param_name not in (tool.parameters.required or []) -%}
130
  {{- "?" }}
@@ -132,7 +133,9 @@
132
  {{- ": " }}
133
  {{- render_typescript_type(param_spec, tool.parameters.required or []) }}
134
  {%- if param_spec.default is defined -%}
135
- {%- if param_spec.oneOf %}
 
 
136
  {{- "// default: " + param_spec.default }}
137
  {%- else %}
138
  {{- ", // default: " + param_spec.default|tojson }}
@@ -140,20 +143,22 @@
140
  {%- endif -%}
141
  {%- if not loop.last %}
142
  {{- ",
 
 
 
143
  " }}
144
  {%- endif -%}
145
  {%- endfor %}
146
- {{- ",
147
- }) => any;
148
  " }}
149
  {%- else -%}
150
- {{- "
151
- }) => any;
152
  " }}
153
  {%- endif -%}
154
  {%- endfor %}
155
- {{- "
156
- } // namespace " + namespace_name }}
157
  {%- endmacro -%}
158
 
159
  {%- macro render_builtin_tools(browser_tool, python_tool) -%}
@@ -250,11 +255,10 @@
250
  {#- System Message Construction ============================================ #}
251
  {%- macro build_system_message() -%}
252
  {%- if model_identity is not defined %}
253
- {{- "You are ChatGPT, a large language model trained by OpenAI.
254
- " -}}
255
- {%- else %}
256
- {{- model_identity }}
257
  {%- endif %}
 
 
258
  {{- "Knowledge cutoff: 2024-06
259
  " }}
260
  {{- "Current date: " + strftime_now("%Y-%m-%d") + "
@@ -263,7 +267,7 @@
263
  {%- if reasoning_effort is not defined %}
264
  {%- set reasoning_effort = "medium" %}
265
  {%- endif %}
266
- {{- "reasoning: " + reasoning_effort + "
267
 
268
  " }}
269
  {%- if builtin_tools %}
@@ -280,9 +284,11 @@
280
  {%- endfor %}
281
  {{- render_builtin_tools(available_builtin_tools.browser, available_builtin_tools.python) }}
282
  {%- endif -%}
283
- {{- "# Valid channels: analysis, commentary, final. Channel must be included for every message.
284
- " }}
285
- {{- "Calls to these tools must go to the commentary channel: 'functions'." }}
 
 
286
  {%- endmacro -%}
287
 
288
  {#- Main Template Logic ================================================= #}
@@ -328,42 +334,60 @@
328
  {%- for message in loop_messages -%}
329
  {#- At this point only assistant/user/tool messages should remain #}
330
  {%- if message.role == 'assistant' -%}
 
 
 
 
 
 
 
 
 
 
 
331
  {%- if "tool_calls" in message %}
 
 
 
 
 
 
 
 
 
332
  {#- We assume max 1 tool call per message, and so we infer the tool call name #}
333
  {#- in "tool" messages from the most recent assistant tool call name #}
334
  {%- set tool_call = message.tool_calls[0] %}
335
  {%- if tool_call.function %}
336
  {%- set tool_call = tool_call.function %}
337
  {%- endif %}
338
- {%- if message.content %}
 
 
339
  {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.content + "<|end|>" }}
 
 
340
  {%- endif %}
341
  {{- "<|start|>assistant to=" }}
342
- {{- "functions." + tool_call.name + "<|channel|>commentary json<|message|>" }}
 
343
  {{- tool_call.arguments|tojson }}
344
- {{- "<|end|>" }}
345
  {%- set last_tool_call.name = tool_call.name %}
346
- {%- elif "thinking" in message and loop.last and not add_generation_prompt %}
347
  {#- Only render the CoT if the final turn is an assistant turn and add_generation_prompt is false #}
348
  {#- This is a situation that should only occur in training, never in inference. #}
349
- {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.thinking + "<|end|>" }}
 
 
350
  {#- <|return|> indicates the end of generation, but <|end|> does not #}
351
  {#- <|return|> should never be an input to the model, but we include it as the final token #}
352
  {#- when training, so the model learns to emit it. #}
353
  {{- "<|start|>assistant<|channel|>final<|message|>" + message.content + "<|return|>" }}
354
- {%- set last_tool_call.name = none %}
355
- {%- elif "thinking" in message %}
356
  {#- CoT is dropped during all previous turns, so we never render it for inference #}
357
  {{- "<|start|>assistant<|channel|>final<|message|>" + message.content + "<|end|>" }}
358
  {%- set last_tool_call.name = none %}
359
- {%- elif loop.last and not add_generation_prompt %}
360
- {#- <|return|> indicates the end of generation, but <|end|> does not #}
361
- {#- <|return|> should never be an input to the model, but we include it as the final token #}
362
- {#- when training, so the model learns to emit it. #}
363
- {{- "<|start|>assistant<|message|>" + message.content + "<|return|>" }}
364
- {%- else %}
365
- {{- "<|start|>assistant<|message|>" + message.content + "<|end|>" }}
366
- {%- set last_tool_call.name = none %}
367
  {%- endif %}
368
  {%- elif message.role == 'tool' -%}
369
  {%- if last_tool_call.name is none %}
 
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 []) -%}
131
  {{- "?" }}
 
133
  {{- ": " }}
134
  {{- render_typescript_type(param_spec, tool.parameters.required or []) }}
135
  {%- if param_spec.default is defined -%}
136
+ {%- if param_spec.enum %}
137
+ {{- ", // default: " + param_spec.default }}
138
+ {%- elif param_spec.oneOf %}
139
  {{- "// default: " + param_spec.default }}
140
  {%- else %}
141
  {{- ", // default: " + param_spec.default|tojson }}
 
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 }}
 
162
  {%- endmacro -%}
163
 
164
  {%- macro render_builtin_tools(browser_tool, python_tool) -%}
 
255
  {#- System Message Construction ============================================ #}
256
  {%- macro build_system_message() -%}
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") + "
 
267
  {%- if reasoning_effort is not defined %}
268
  {%- set reasoning_effort = "medium" %}
269
  {%- endif %}
270
+ {{- "Reasoning: " + reasoning_effort + "
271
 
272
  " }}
273
  {%- if builtin_tools %}
 
284
  {%- endfor %}
285
  {{- render_builtin_tools(available_builtin_tools.browser, available_builtin_tools.python) }}
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
 
294
  {#- Main Template Logic ================================================= #}
 
334
  {%- for message in loop_messages -%}
335
  {#- At this point only assistant/user/tool messages should remain #}
336
  {%- if message.role == 'assistant' -%}
337
+ {#- Checks to ensure the messages are being passed in the format we expect #}
338
+ {%- if "content" in message %}
339
+ {%- if "<|channel|>analysis<|message|>" in message.content or "<|channel|>final<|message|>" in message.content %}
340
+ {{- raise_exception("You have passed a message containing <|channel|> tags in the content field. Instead of doing this, you should pass analysis messages (the string between '<|message|>' and '<|end|>') in the 'thinking' field, and final messages (the string between '<|message|>' and '<|end|>') in the 'content' field.") }}
341
+ {%- endif %}
342
+ {%- endif %}
343
+ {%- if "thinking" in message %}
344
+ {%- if "<|channel|>analysis<|message|>" in message.thinking or "<|channel|>final<|message|>" in message.thinking %}
345
+ {{- raise_exception("You have passed a message containing <|channel|> tags in the thinking field. Instead of doing this, you should pass analysis messages (the string between '<|message|>' and '<|end|>') in the 'thinking' field, and final messages (the string between '<|message|>' and '<|end|>') in the 'content' field.") }}
346
+ {%- endif %}
347
+ {%- endif %}
348
  {%- if "tool_calls" in message %}
349
+ {#- We need very careful handling here - we want to drop the tool call analysis message if the model #}
350
+ {#- has output a later <|final|> message, but otherwise we want to retain it. This is the only case #}
351
+ {#- when we render CoT/analysis messages in inference. #}
352
+ {%- set future_final_message = namespace(found=false) %}
353
+ {%- for future_message in loop_messages[loop.index:] %}
354
+ {%- if future_message.role == 'assistant' and "tool_calls" not in future_message %}
355
+ {%- set future_final_message.found = true %}
356
+ {%- endif %}
357
+ {%- endfor %}
358
  {#- We assume max 1 tool call per message, and so we infer the tool call name #}
359
  {#- in "tool" messages from the most recent assistant tool call name #}
360
  {%- set tool_call = message.tool_calls[0] %}
361
  {%- if tool_call.function %}
362
  {%- set tool_call = tool_call.function %}
363
  {%- endif %}
364
+ {%- if message.content and message.thinking %}
365
+ {{- 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.") }}
366
+ {%- elif message.content and not future_final_message.found %}
367
  {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.content + "<|end|>" }}
368
+ {%- elif message.thinking and not future_final_message.found %}
369
+ {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.thinking + "<|end|>" }}
370
  {%- endif %}
371
  {{- "<|start|>assistant to=" }}
372
+ {{- "functions." + tool_call.name + "<|channel|>commentary " }}
373
+ {{- (tool_call.content_type if tool_call.content_type is defined else "json") + "<|message|>" }}
374
  {{- tool_call.arguments|tojson }}
375
+ {{- "<|call|>" }}
376
  {%- set last_tool_call.name = tool_call.name %}
377
+ {%- elif loop.last and not add_generation_prompt %}
378
  {#- Only render the CoT if the final turn is an assistant turn and add_generation_prompt is false #}
379
  {#- This is a situation that should only occur in training, never in inference. #}
380
+ {%- if "thinking" in message %}
381
+ {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.thinking + "<|end|>" }}
382
+ {%- endif %}
383
  {#- <|return|> indicates the end of generation, but <|end|> does not #}
384
  {#- <|return|> should never be an input to the model, but we include it as the final token #}
385
  {#- when training, so the model learns to emit it. #}
386
  {{- "<|start|>assistant<|channel|>final<|message|>" + message.content + "<|return|>" }}
387
+ {%- else %}
 
388
  {#- CoT is dropped during all previous turns, so we never render it for inference #}
389
  {{- "<|start|>assistant<|channel|>final<|message|>" + message.content + "<|end|>" }}
390
  {%- set last_tool_call.name = none %}
 
 
 
 
 
 
 
 
391
  {%- endif %}
392
  {%- elif message.role == 'tool' -%}
393
  {%- if last_tool_call.name is none %}