Add froggeric v20 fixed chat template (vision + inline think-toggle + tool anti-stall); use via --chat-template-file
Browse files- chat_template.jinja +287 -0
chat_template.jinja
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{%- set template_version = "qwen3.6-froggeric-v20" %}
|
| 2 |
+
{%- set image_count = namespace(value=0) %}
|
| 3 |
+
{%- set video_count = namespace(value=0) %}
|
| 4 |
+
{%- set add_vision_id = add_vision_id if add_vision_id is defined else false %}
|
| 5 |
+
{%- set enable_thinking = enable_thinking if enable_thinking is defined else true %}
|
| 6 |
+
{%- set auto_disable_thinking_with_tools = auto_disable_thinking_with_tools if auto_disable_thinking_with_tools is defined else false %}
|
| 7 |
+
{%- set _preserve_thinking = preserve_thinking if preserve_thinking is defined else false %}
|
| 8 |
+
{%- set max_tool_arg_chars = max_tool_arg_chars if max_tool_arg_chars is defined else 0 %}
|
| 9 |
+
{%- set max_tool_response_chars = max_tool_response_chars if max_tool_response_chars is defined else 0 %}
|
| 10 |
+
{%- set _has_tools = (tools is defined and tools and tools is iterable and tools is not mapping) %}
|
| 11 |
+
{%- set ns_state = namespace(thinking=enable_thinking) %}
|
| 12 |
+
{%- if auto_disable_thinking_with_tools and _has_tools %}
|
| 13 |
+
{%- set ns_state.thinking = false %}
|
| 14 |
+
{%- endif %}
|
| 15 |
+
{%- macro render_content(content, do_vision_count, is_system_content=false) %}
|
| 16 |
+
{%- if content is string %}
|
| 17 |
+
{{- content }}
|
| 18 |
+
{%- elif content is iterable and content is not mapping %}
|
| 19 |
+
{%- for item in content %}
|
| 20 |
+
{%- if item is mapping %}
|
| 21 |
+
{%- if item.type == 'image' or 'image' in item or 'image_url' in item %}
|
| 22 |
+
{%- if is_system_content %}
|
| 23 |
+
{{- raise_exception('System message cannot contain images.') }}
|
| 24 |
+
{%- endif %}
|
| 25 |
+
{%- if do_vision_count %}
|
| 26 |
+
{%- set image_count.value = image_count.value + 1 %}
|
| 27 |
+
{%- endif %}
|
| 28 |
+
{%- if add_vision_id %}
|
| 29 |
+
{{- 'Picture ' ~ image_count.value ~ ': ' }}
|
| 30 |
+
{%- endif %}
|
| 31 |
+
{{- '<|vision_start|><|image_pad|><|vision_end|>' }}
|
| 32 |
+
{%- elif item.type == 'video' or 'video' in item %}
|
| 33 |
+
{%- if is_system_content %}
|
| 34 |
+
{{- raise_exception('System message cannot contain videos.') }}
|
| 35 |
+
{%- endif %}
|
| 36 |
+
{%- if do_vision_count %}
|
| 37 |
+
{%- set video_count.value = video_count.value + 1 %}
|
| 38 |
+
{%- endif %}
|
| 39 |
+
{%- if add_vision_id %}
|
| 40 |
+
{{- 'Video ' ~ video_count.value ~ ': ' }}
|
| 41 |
+
{%- endif %}
|
| 42 |
+
{{- '<|vision_start|><|video_pad|><|vision_end|>' }}
|
| 43 |
+
{%- elif 'text' in item %}
|
| 44 |
+
{{- item.text }}
|
| 45 |
+
{%- else %}
|
| 46 |
+
{{- raise_exception('Unexpected item type in content.') }}
|
| 47 |
+
{%- endif %}
|
| 48 |
+
{%- else %}
|
| 49 |
+
{{- item | string }}
|
| 50 |
+
{%- endif %}
|
| 51 |
+
{%- endfor %}
|
| 52 |
+
{%- elif content is none or content is undefined %}
|
| 53 |
+
{{- '' }}
|
| 54 |
+
{%- else %}
|
| 55 |
+
{{- raise_exception('Unexpected content type.') }}
|
| 56 |
+
{%- endif %}
|
| 57 |
+
{%- endmacro %}
|
| 58 |
+
{%- if not messages %}
|
| 59 |
+
{{- raise_exception('No messages provided.') }}
|
| 60 |
+
{%- endif %}
|
| 61 |
+
{%- set _first_role = messages[0].role %}
|
| 62 |
+
{%- if _first_role == 'system' or _first_role == 'developer' %}
|
| 63 |
+
{%- set _sys_msg = messages[0] %}
|
| 64 |
+
{%- set _msgs = messages[1:] %}
|
| 65 |
+
{%- else %}
|
| 66 |
+
{%- set _sys_msg = none %}
|
| 67 |
+
{%- set _msgs = messages %}
|
| 68 |
+
{%- endif %}
|
| 69 |
+
{%- set _sc = '' %}
|
| 70 |
+
{%- if _sys_msg is not none %}
|
| 71 |
+
{%- set _sc = render_content(_sys_msg.content, false, true) | trim %}
|
| 72 |
+
{%- if '<|think_off|>' in _sc %}
|
| 73 |
+
{%- set ns_state.thinking = false %}
|
| 74 |
+
{%- set _sc = _sc.split('<|think_off|>') | join('') | trim %}
|
| 75 |
+
{%- elif '<|think_on|>' in _sc %}
|
| 76 |
+
{%- set ns_state.thinking = true %}
|
| 77 |
+
{%- set _sc = _sc.split('<|think_on|>') | join('') | trim %}
|
| 78 |
+
{%- endif %}
|
| 79 |
+
{%- endif %}
|
| 80 |
+
{%- if _has_tools %}
|
| 81 |
+
{{- '<|im_start|>system\n' }}
|
| 82 |
+
{{- '# Tools\n\nYou have access to the following functions:\n\n<tools>' }}
|
| 83 |
+
{%- for tool in tools %}
|
| 84 |
+
{{- '\n' }}
|
| 85 |
+
{{- tool | tojson }}
|
| 86 |
+
{%- endfor %}
|
| 87 |
+
{{- '\n</tools>' }}
|
| 88 |
+
{%- set tool_instructions %}
|
| 89 |
+
If you choose to call a function ONLY reply in the following format with NO suffix:
|
| 90 |
+
|
| 91 |
+
<think>
|
| 92 |
+
Brief explanation of tool call
|
| 93 |
+
</think>
|
| 94 |
+
<tool_call>
|
| 95 |
+
<function=example_function_name>
|
| 96 |
+
<parameter=example_parameter_1>
|
| 97 |
+
value_1
|
| 98 |
+
</parameter>
|
| 99 |
+
<parameter=example_parameter_2>
|
| 100 |
+
This is the value for the second parameter
|
| 101 |
+
that can span
|
| 102 |
+
multiple lines
|
| 103 |
+
</parameter>
|
| 104 |
+
</function>
|
| 105 |
+
</tool_call>
|
| 106 |
+
|
| 107 |
+
<IMPORTANT>
|
| 108 |
+
Reminder:
|
| 109 |
+
- You can use the <think></think> block to plan your next tool call OR to synthesize data and formulate your final response to the user.
|
| 110 |
+
- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.
|
| 111 |
+
- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags.
|
| 112 |
+
- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY after closing </think>. Do NOT output any conversational text before the tool call.
|
| 113 |
+
- The <tool_call> and <function> tags MUST be at the very beginning of a new line, with NO spaces or indentation before them.
|
| 114 |
+
- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.
|
| 115 |
+
- If you have gathered all necessary data and do not need to call a tool, answer the question like normal and provide your final response to the user IMMEDIATELY after closing </think>.
|
| 116 |
+
</IMPORTANT>
|
| 117 |
+
{%- endset %}
|
| 118 |
+
{{- '\n\n' ~ tool_instructions | trim }}
|
| 119 |
+
{%- if _sc %}
|
| 120 |
+
{{- '\n\n' + _sc }}
|
| 121 |
+
{%- endif %}
|
| 122 |
+
{{- '<|im_end|>\n' }}
|
| 123 |
+
{%- else %}
|
| 124 |
+
{%- if _sc %}
|
| 125 |
+
{{- '<|im_start|>system\n' + _sc + '<|im_end|>\n' }}
|
| 126 |
+
{%- endif %}
|
| 127 |
+
{%- endif %}
|
| 128 |
+
{%- set _last_idx = _msgs | length - 1 %}
|
| 129 |
+
{%- set ns = namespace(multi_step_tool=true, last_query_index=_last_idx) %}
|
| 130 |
+
{%- for message in _msgs[::-1] %}
|
| 131 |
+
{%- set index = (_msgs | length - 1) - loop.index0 %}
|
| 132 |
+
{%- if ns.multi_step_tool and message.role == 'user' %}
|
| 133 |
+
{%- set _rc = render_content(message.content, false) | trim %}
|
| 134 |
+
{%- if not (_rc.startswith('<tool_response>') and _rc.endswith('</tool_response>')) %}
|
| 135 |
+
{%- set ns.multi_step_tool = false %}
|
| 136 |
+
{%- set ns.last_query_index = index %}
|
| 137 |
+
{%- endif %}
|
| 138 |
+
{%- endif %}
|
| 139 |
+
{%- endfor %}
|
| 140 |
+
{%- if ns.multi_step_tool %}
|
| 141 |
+
{%- if _last_idx > 50 %}
|
| 142 |
+
{%- set ns.last_query_index = _last_idx %}
|
| 143 |
+
{%- else %}
|
| 144 |
+
{%- set ns.last_query_index = 0 %}
|
| 145 |
+
{%- endif %}
|
| 146 |
+
{%- endif %}
|
| 147 |
+
{%- set ns2 = namespace(prev_role='', consecutive_failures=0) %}
|
| 148 |
+
{%- for message in _msgs %}
|
| 149 |
+
{%- set is_system = (message.role == "system" or message.role == "developer") %}
|
| 150 |
+
{%- set content = render_content(message.content, true, is_system) | trim %}
|
| 151 |
+
{%- if '<|think_off|>' in content %}
|
| 152 |
+
{%- set ns_state.thinking = false %}
|
| 153 |
+
{%- set content = content.split('<|think_off|>') | join('') | trim %}
|
| 154 |
+
{%- elif '<|think_on|>' in content %}
|
| 155 |
+
{%- set ns_state.thinking = true %}
|
| 156 |
+
{%- set content = content.split('<|think_on|>') | join('') | trim %}
|
| 157 |
+
{%- endif %}
|
| 158 |
+
{%- if is_system %}
|
| 159 |
+
{{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
|
| 160 |
+
{%- elif message.role == 'user' %}
|
| 161 |
+
{%- set ns2.consecutive_failures = 0 %}
|
| 162 |
+
{{- '<|im_start|>user\n' + content + '<|im_end|>\n' }}
|
| 163 |
+
{%- elif message.role == 'assistant' %}
|
| 164 |
+
{%- set reasoning_content = '' %}
|
| 165 |
+
{%- if message.reasoning_content is defined and message.reasoning_content is not none %}
|
| 166 |
+
{%- if message.reasoning_content is string %}
|
| 167 |
+
{%- set reasoning_content = message.reasoning_content %}
|
| 168 |
+
{%- else %}
|
| 169 |
+
{%- set reasoning_content = message.reasoning_content | string %}
|
| 170 |
+
{%- endif %}
|
| 171 |
+
{%- else %}
|
| 172 |
+
{%- set _think_end = '' %}
|
| 173 |
+
{%- if '</think>' in content %}
|
| 174 |
+
{%- set _think_end = '</think>' %}
|
| 175 |
+
{%- elif '</thinking>' in content %}
|
| 176 |
+
{%- set _think_end = '</thinking>' %}
|
| 177 |
+
{%- elif '</ think>' in content %}
|
| 178 |
+
{%- set _think_end = '</ think>' %}
|
| 179 |
+
{%- elif '</think >' in content %}
|
| 180 |
+
{%- set _think_end = '</think >' %}
|
| 181 |
+
{%- endif %}
|
| 182 |
+
{%- if _think_end %}
|
| 183 |
+
{%- if _think_end == '</thinking>' %}
|
| 184 |
+
{%- set _think_start = '<thinking>' %}
|
| 185 |
+
{%- else %}
|
| 186 |
+
{%- set _think_start = '<think>' %}
|
| 187 |
+
{%- endif %}
|
| 188 |
+
{%- set reasoning_content = content.split(_think_end)[0].rstrip('\n') %}
|
| 189 |
+
{%- if _think_start in reasoning_content %}
|
| 190 |
+
{%- set reasoning_content = reasoning_content.split(_think_start)[-1].lstrip('\n') %}
|
| 191 |
+
{%- endif %}
|
| 192 |
+
{%- set content = content.split(_think_end)[-1].lstrip('\n') %}
|
| 193 |
+
{%- endif %}
|
| 194 |
+
{%- endif %}
|
| 195 |
+
{%- set reasoning_content = reasoning_content | trim %}
|
| 196 |
+
{%- if (_preserve_thinking or loop.index0 > ns.last_query_index) and reasoning_content %}
|
| 197 |
+
{{- '<|im_start|>assistant\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
|
| 198 |
+
{%- else %}
|
| 199 |
+
{{- '<|im_start|>assistant\n' + content }}
|
| 200 |
+
{%- endif %}
|
| 201 |
+
{%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
|
| 202 |
+
{%- for tool_call in message.tool_calls %}
|
| 203 |
+
{%- if tool_call.function is defined and tool_call.function is not none %}
|
| 204 |
+
{%- set tc = tool_call.function %}
|
| 205 |
+
{%- else %}
|
| 206 |
+
{%- set tc = tool_call %}
|
| 207 |
+
{%- endif %}
|
| 208 |
+
{%- if loop.first %}
|
| 209 |
+
{%- if content | trim %}
|
| 210 |
+
{{- '\n\n<tool_call>\n<function=' + tc.name + '>\n' }}
|
| 211 |
+
{%- else %}
|
| 212 |
+
{{- '<tool_call>\n<function=' + tc.name + '>\n' }}
|
| 213 |
+
{%- endif %}
|
| 214 |
+
{%- else %}
|
| 215 |
+
{{- '\n\n<tool_call>\n<function=' + tc.name + '>\n' }}
|
| 216 |
+
{%- endif %}
|
| 217 |
+
{%- if tc.arguments is defined and tc.arguments is not none %}
|
| 218 |
+
{%- if tc.arguments is mapping %}
|
| 219 |
+
{%- for args_name, args_value in tc.arguments.items() %}
|
| 220 |
+
{{- '<parameter=' + args_name + '>\n' }}
|
| 221 |
+
{%- if args_value is mapping or (args_value is sequence and args_value is not string) %}
|
| 222 |
+
{%- set _av = args_value | tojson %}
|
| 223 |
+
{%- else %}
|
| 224 |
+
{%- set _av = args_value | string %}
|
| 225 |
+
{%- endif %}
|
| 226 |
+
{%- if max_tool_arg_chars > 0 and _av | length > max_tool_arg_chars %}
|
| 227 |
+
{{- _av[:max_tool_arg_chars] + '\n[TRUNCATED — original length ' ~ (_av | length | string) ~ ' chars]' }}
|
| 228 |
+
{%- else %}
|
| 229 |
+
{{- _av }}
|
| 230 |
+
{%- endif %}
|
| 231 |
+
{{- '\n</parameter>\n' }}
|
| 232 |
+
{%- endfor %}
|
| 233 |
+
{%- elif tc.arguments is string and tc.arguments %}
|
| 234 |
+
{{- tc.arguments }}
|
| 235 |
+
{%- endif %}
|
| 236 |
+
{%- endif %}
|
| 237 |
+
{%- if loop.last %}
|
| 238 |
+
{{- '</function>\n</tool_call>\n' }}
|
| 239 |
+
{%- else %}
|
| 240 |
+
{{- '</function>\n</tool_call>' }}
|
| 241 |
+
{%- endif %}
|
| 242 |
+
{%- endfor %}
|
| 243 |
+
{%- endif %}
|
| 244 |
+
{{- '<|im_end|>\n' }}
|
| 245 |
+
{%- elif message.role == 'tool' %}
|
| 246 |
+
{%- set _content_lower = content | lower %}
|
| 247 |
+
{%- if content | length < 500 and '$ ' not in content and 'took ' not in _content_lower and ('"error":' in _content_lower or 'error:' in _content_lower or 'exception:' in _content_lower or 'traceback' in _content_lower or 'command not found' in _content_lower or 'invalid syntax' in _content_lower or 'failed to' in _content_lower) %}
|
| 248 |
+
{%- set ns2.consecutive_failures = ns2.consecutive_failures + 1 %}
|
| 249 |
+
{%- else %}
|
| 250 |
+
{%- set ns2.consecutive_failures = 0 %}
|
| 251 |
+
{%- endif %}
|
| 252 |
+
{%- if ns2.prev_role != 'tool' %}
|
| 253 |
+
{{- '<|im_start|>user' }}
|
| 254 |
+
{%- endif %}
|
| 255 |
+
{%- if max_tool_response_chars > 0 and content | length > max_tool_response_chars %}
|
| 256 |
+
{%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED — original length ' ~ (content | length | string) ~ ' chars]' %}
|
| 257 |
+
{%- endif %}
|
| 258 |
+
{{- '\n<tool_response>\n' + content }}
|
| 259 |
+
{%- if ns2.consecutive_failures >= 2 %}
|
| 260 |
+
{{- '\n\n⚠️ SYSTEM WARNING: ' ~ ns2.consecutive_failures ~ ' consecutive tool errors detected. Your previous approach is incorrect. You MUST use a fundamentally different approach or corrected arguments.' }}
|
| 261 |
+
{%- elif ns2.consecutive_failures == 1 %}
|
| 262 |
+
{{- '\n\n⚠️ SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with completely corrected arguments.' }}
|
| 263 |
+
{%- endif %}
|
| 264 |
+
{{- '\n</tool_response>' }}
|
| 265 |
+
{%- if loop.last %}
|
| 266 |
+
{{- '<|im_end|>\n' }}
|
| 267 |
+
{%- else %}
|
| 268 |
+
{%- set _next_role = _msgs[loop.index0 + 1].role %}
|
| 269 |
+
{%- if _next_role != 'tool' %}
|
| 270 |
+
{{- '<|im_end|>\n' }}
|
| 271 |
+
{%- endif %}
|
| 272 |
+
{%- endif %}
|
| 273 |
+
{%- else %}
|
| 274 |
+
{{- '<|im_start|>user\n[' + message.role + ']: ' + content + '<|im_end|>\n' }}
|
| 275 |
+
{%- endif %}
|
| 276 |
+
{%- set ns2.prev_role = message.role %}
|
| 277 |
+
{%- endfor %}
|
| 278 |
+
{%- if add_generation_prompt %}
|
| 279 |
+
{{- '<|im_start|>assistant\n' }}
|
| 280 |
+
{%- if not ns_state.thinking %}
|
| 281 |
+
{{- '<think>\n</think>\n' }}
|
| 282 |
+
{%- elif ns2.consecutive_failures >= 2 %}
|
| 283 |
+
{{- '<think>\n</think>\n' }}
|
| 284 |
+
{%- else %}
|
| 285 |
+
{{- '<think>\n' }}
|
| 286 |
+
{%- endif %}
|
| 287 |
+
{%- endif %}
|