Add a response template and generation tags

#4
by Rocketknight1 HF Staff - opened
Files changed (2) hide show
  1. chat_template.jinja +27 -5
  2. tokenizer_config.json +39 -1
chat_template.jinja CHANGED
@@ -111,9 +111,10 @@
111
  {%- set content = content -%}
112
  {%- endif -%}
113
  {%- endif %}
 
 
114
  {%- if message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls | length > 0 %}
115
  {# Assistant message has tool calls. #}
116
- {{- '<|im_start|>assistant\n' }}
117
  {%- set include_content = not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}
118
  {%- if content is string and content | trim | length > 0 %}
119
  {%- if include_content %}
@@ -149,11 +150,11 @@
149
  {%- endif %}
150
  {{- '</function>\n</tool_call>\n' -}}
151
  {%- endfor %}
152
- {{- '<|im_end|>\n' }}
153
  {%- else %}
154
  {# Assistant message doesn't have tool calls. #}
155
  {%- if not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}
156
- {{- '<|im_start|>assistant\n' ~ (content | default('', true) | string | trim) ~ '<|im_end|>\n' }}
157
  {%- else %}
158
  {%- set c = (content | default('', true) | string) %}
159
  {%- if '<think>' in c and '</think>' in c %}
@@ -161,12 +162,33 @@
161
  {%- endif %}
162
  {%- set c = c | trim %}
163
  {%- if c | length > 0 %}
164
- {{- '<|im_start|>assistant\n' ~ c ~ '<|im_end|>\n' }}
165
  {%- else %}
166
- {{- '<|im_start|>assistant\n<|im_end|>\n' }}
167
  {%- endif %}
168
  {%- endif %}
169
  {%- endif %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  {%- elif message.role == "user" or message.role == "system" %}
171
  {{- '<|im_start|>' + message.role + '\n' }}
172
  {%- set content = message.content | string %}
 
111
  {%- set content = content -%}
112
  {%- endif -%}
113
  {%- endif %}
114
+ {{- '<|im_start|>assistant\n' }}
115
+ {%- set turn %}
116
  {%- if message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls | length > 0 %}
117
  {# Assistant message has tool calls. #}
 
118
  {%- set include_content = not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}
119
  {%- if content is string and content | trim | length > 0 %}
120
  {%- if include_content %}
 
150
  {%- endif %}
151
  {{- '</function>\n</tool_call>\n' -}}
152
  {%- endfor %}
153
+ {{- '<|im_end|>' }}
154
  {%- else %}
155
  {# Assistant message doesn't have tool calls. #}
156
  {%- if not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}
157
+ {{- (content | default('', true) | string | trim) ~ '<|im_end|>' }}
158
  {%- else %}
159
  {%- set c = (content | default('', true) | string) %}
160
  {%- if '<think>' in c and '</think>' in c %}
 
162
  {%- endif %}
163
  {%- set c = c | trim %}
164
  {%- if c | length > 0 %}
165
+ {{- c ~ '<|im_end|>' }}
166
  {%- else %}
167
+ {{- '<|im_end|>' }}
168
  {%- endif %}
169
  {%- endif %}
170
  {%- endif %}
171
+ {%- endset %}
172
+ {#- What the generation prompt pre-fills stays out of the generation block: the model never generates it #}
173
+ {%- set prefill = '<think>\n' if enable_thinking else '<think></think>' %}
174
+ {%- if turn.startswith(prefill) %}
175
+ {%- set lead = prefill %}
176
+ {%- elif not turn.startswith('<think>') %}
177
+ {%- set lead = '' %}
178
+ {%- elif enable_thinking or '</think>' not in turn %}
179
+ {%- set lead = '<think>' %}
180
+ {%- else %}
181
+ {#- Reasoning with thinking off: the prompt pre-fills an empty block, so the whole block stays out #}
182
+ {%- set lead = turn.split('</think>')[0] ~ '</think>' %}
183
+ {%- if turn[lead | length:].startswith('\n') %}
184
+ {%- set lead = lead ~ '\n' %}
185
+ {%- endif %}
186
+ {%- endif %}
187
+ {{- lead }}
188
+ {%- generation %}
189
+ {{- turn[lead | length:] }}
190
+ {%- endgeneration %}
191
+ {{- '\n' }}
192
  {%- elif message.role == "user" or message.role == "system" %}
193
  {{- '<|im_start|>' + message.role + '\n' }}
194
  {%- set content = message.content | string %}
tokenizer_config.json CHANGED
@@ -8014,5 +8014,43 @@
8014
  ],
8015
  "model_max_length": 262144,
8016
  "tokenizer_class": "PreTrainedTokenizerFast",
8017
- "unk_token": "<unk>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8018
  }
 
8014
  ],
8015
  "model_max_length": 262144,
8016
  "tokenizer_class": "PreTrainedTokenizerFast",
8017
+ "unk_token": "<unk>",
8018
+ "response_template": {
8019
+ "defaults": {
8020
+ "role": "assistant"
8021
+ },
8022
+ "start_anchor": "<|im_start|>assistant\n",
8023
+ "fields": {
8024
+ "reasoning_content": {
8025
+ "open": "<think>",
8026
+ "close": "</think>",
8027
+ "content": "text"
8028
+ },
8029
+ "tool_calls": {
8030
+ "open_pattern": "<tool_call>\\s*<function=(?P<name>[^>\\n]+)>",
8031
+ "close_pattern": "</tool_call>|(?=<tool_call>)",
8032
+ "repeats": true,
8033
+ "content": "xml-inline",
8034
+ "content_args": {
8035
+ "tag_pattern": "<parameter=(?P<key>[^>\\n]+)>\\n?(?P<value>.*?)\\n?(?:</parameter>|(?=<parameter=)|(?=</function>))"
8036
+ },
8037
+ "transform": {
8038
+ "type": "function",
8039
+ "function": {
8040
+ "name": "{name}",
8041
+ "arguments": "{content}"
8042
+ }
8043
+ }
8044
+ },
8045
+ "content": {
8046
+ "close": [
8047
+ "<|im_end|>",
8048
+ "<|endoftext|>"
8049
+ ],
8050
+ "content": "text",
8051
+ "repeats": true,
8052
+ "join": ""
8053
+ }
8054
+ }
8055
+ }
8056
  }