Instructions to use froggeric/Qwen-Fixed-Chat-Templates with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use froggeric/Qwen-Fixed-Chat-Templates with MLX:
# Download the model from the Hub pip install huggingface_hub[hf_xet] hf download froggeric/Qwen-Fixed-Chat-Templates --local-dir Qwen-Fixed-Chat-Templates
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
release: v22.5 - Non-thinking tool prompt alignment, video URL support, and engine hardening
Browse files- README.md +39 -8
- archive/README-v22.4.md +337 -0
- archive/v22.4_chat_template.jinja +431 -0
- archive/v22.4_chat_template_oneline.txt +1 -0
- chat_template.jinja +34 -5
- chat_template_oneline.txt +1 -1
- scripts/check_applied.py +3 -3
- scripts/test_v22.py +50 -3
README.md
CHANGED
|
@@ -15,11 +15,11 @@ tags:
|
|
| 15 |
- thinking
|
| 16 |
---
|
| 17 |
|
| 18 |
-
# Fixed jinja chat templates for Qwen 3.5, 3.6 & 3.8 (v22.
|
| 19 |
|
| 20 |
This is a universal drop-in Jinja template that fixes rendering errors, KV cache invalidation, token waste, empty think poisoning, and fatal agentic stalling across official Qwen chat templates.
|
| 21 |
|
| 22 |
-
It works across LM Studio, llama.cpp, vLLM, MLX, oMLX, KoboldCPP, and any engine that supports Hugging Face Jinja templates. You only need the single `chat_template.jinja` file at the root of this repository for all Qwen 3.5, 3.6, and 3.8 model sizes.
|
| 23 |
|
| 24 |
---
|
| 25 |
|
|
@@ -29,7 +29,7 @@ Qwen 3.8 introduces prompt-steered reasoning depth and new model architectures (
|
|
| 29 |
|
| 30 |
The v22 generation delivers full Qwen 3.8 support with all official bugs resolved:
|
| 31 |
|
| 32 |
-
| Feature / Fix | Official Qwen 3.8 Template | This Template (v22.
|
| 33 |
|---|---|---|
|
| 34 |
| **Default Reasoning Baseline** | Hardcodes `xhigh` by default, often exhausting the token budget on reasoning before generating answers. | Defaults to `medium` (zero injected tokens), preserving KV cache parity with v21 and preventing empty-content timeouts. |
|
| 35 |
| **Non-Reasoning Fast Mode** | The `Qwen3.8-2.4T-A95B` template throws a fatal exception if `enable_thinking=false` (the `Qwen3.8-27B` template accepts it). | Full non-reasoning mode restored via kwargs (`enable_thinking=false`, `reasoning_effort="none"`) or inline `<\|think_off\|>`. |
|
|
@@ -71,9 +71,32 @@ vllm serve Qwen/Qwen3.8-2.4T-A95B --reasoning-parser qwen3 --tool-call-parser qw
|
|
| 71 |
|
| 72 |
*Tool parser selection:* Use `--tool-call-parser qwen3_xml` on current vLLM releases. If you are on an older vLLM build, use `--tool-call-parser qwen3_coder`. If you explicitly set `tool_call_format="json"`, use `--tool-call-parser hermes`.
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
### oMLX / MLX
|
| 75 |
Overwrite `chat_template.jinja` in your local model directory and launch with `--jinja`.
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
</details>
|
| 78 |
|
| 79 |
---
|
|
@@ -187,7 +210,7 @@ If you are using a framework or harness (such as specific Hermes Agent configura
|
|
| 187 |
"tool_call_format": "json"
|
| 188 |
}
|
| 189 |
```
|
| 190 |
-
*(When opting into JSON format,
|
| 191 |
|
| 192 |
</details>
|
| 193 |
|
|
@@ -198,7 +221,7 @@ To prevent oversized tool returns from blowing out context limits:
|
|
| 198 |
* `max_tool_arg_chars` (default `0` / disabled): Slices oversized tool call arguments. Applies to mapping arguments and to serialized JSON string arguments alike.
|
| 199 |
* `max_tool_response_chars` (default `0` / disabled): Slices oversized tool output data.
|
| 200 |
|
| 201 |
-
|
| 202 |
|
| 203 |
</details>
|
| 204 |
|
|
@@ -217,7 +240,7 @@ python3 scripts/check_applied.py /path/to/your/model
|
|
| 217 |
```bash
|
| 218 |
python3 scripts/test_v22.py
|
| 219 |
```
|
| 220 |
-
Tests cover
|
| 221 |
|
| 222 |
</details>
|
| 223 |
|
|
@@ -274,10 +297,10 @@ Python-only Jinja2 features crash or misbehave on `minijinja` (the C++ runtime u
|
|
| 274 |
Deeply nested Jinja loops and macros create severe parsing bottlenecks in C++ inference engines. We flattened the AST architecture, effectively curing an 80% inference throughput drop on `llama.cpp` by streamlining how `ns_state` tracking and historical rendering loops are evaluated.
|
| 275 |
|
| 276 |
### 9. Dynamic Payload Truncation
|
| 277 |
-
Massive API or database returns can instantly blow out a model's context window. We implemented `max_tool_arg_chars` and `max_tool_response_chars` limiters that safely slice oversized payloads. Argument truncation covers both mapping arguments and serialized JSON string arguments, so the OpenAI proxy wire format is limited on the same terms as native dictionaries.
|
| 278 |
|
| 279 |
### 10. Reasoning Bypass Hallucination Mitigation
|
| 280 |
-
When thinking is disabled, Qwen models often hallucinate reasoning tags due to their training bias.
|
| 281 |
|
| 282 |
</details>
|
| 283 |
|
|
@@ -286,6 +309,14 @@ When thinking is disabled, Qwen models often hallucinate reasoning tags due to t
|
|
| 286 |
<details>
|
| 287 |
<summary>Update History & Changelog</summary>
|
| 288 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
> **2026-08-24 Update (v22.4): Multi-Tool Token Parity, Extended Reasoning Formats, and UI Default Knobs.**
|
| 290 |
> 1. **Parallel Tool Call Token Parity:** Consecutive `<tool_call>` blocks in assistant history are now separated by a single newline (`\n`) instead of double newlines (`\n\n`), restoring exact token alignment with official Qwen model generation and preventing prefix KV cache divergence during multi-tool turns (#87).
|
| 291 |
> 2. **vLLM & Responses API Reasoning Support:** Added native support for `message.reasoning` (used by vLLM OpenAI-compatible endpoints and Responses API schemas) alongside `reasoning_content` and `thinking` (#88).
|
|
|
|
| 15 |
- thinking
|
| 16 |
---
|
| 17 |
|
| 18 |
+
# Fixed jinja chat templates for Qwen 3.5, 3.6 & 3.8 (v22.5)
|
| 19 |
|
| 20 |
This is a universal drop-in Jinja template that fixes rendering errors, KV cache invalidation, token waste, empty think poisoning, and fatal agentic stalling across official Qwen chat templates.
|
| 21 |
|
| 22 |
+
It works across LM Studio, llama.cpp, vLLM, SGLang, MLX, oMLX, KoboldCPP, and any engine that supports Hugging Face Jinja templates. You only need the single `chat_template.jinja` file at the root of this repository for all Qwen 3.5, 3.6, 3.8, and 3.8 Flash-Next model sizes.
|
| 23 |
|
| 24 |
---
|
| 25 |
|
|
|
|
| 29 |
|
| 30 |
The v22 generation delivers full Qwen 3.8 support with all official bugs resolved:
|
| 31 |
|
| 32 |
+
| Feature / Fix | Official Qwen 3.8 Template | This Template (v22.5) |
|
| 33 |
|---|---|---|
|
| 34 |
| **Default Reasoning Baseline** | Hardcodes `xhigh` by default, often exhausting the token budget on reasoning before generating answers. | Defaults to `medium` (zero injected tokens), preserving KV cache parity with v21 and preventing empty-content timeouts. |
|
| 35 |
| **Non-Reasoning Fast Mode** | The `Qwen3.8-2.4T-A95B` template throws a fatal exception if `enable_thinking=false` (the `Qwen3.8-27B` template accepts it). | Full non-reasoning mode restored via kwargs (`enable_thinking=false`, `reasoning_effort="none"`) or inline `<\|think_off\|>`. |
|
|
|
|
| 71 |
|
| 72 |
*Tool parser selection:* Use `--tool-call-parser qwen3_xml` on current vLLM releases. If you are on an older vLLM build, use `--tool-call-parser qwen3_coder`. If you explicitly set `tool_call_format="json"`, use `--tool-call-parser hermes`.
|
| 73 |
|
| 74 |
+
### SGLang
|
| 75 |
+
Launch SGLang with `--chat-template` pointing directly to `chat_template.jinja`:
|
| 76 |
+
```bash
|
| 77 |
+
python3 -m sglang.launch_server --model-path Qwen/Qwen3.8-27B --chat-template chat_template.jinja
|
| 78 |
+
```
|
| 79 |
+
*Why `--chat-template` matters on SGLang:* By default, SGLang 0.5.x ignores a local `chat_template.jinja` file in the model directory and falls back to the embedded template in `tokenizer_config.json`. Passing `--chat-template` (or using `chat-template: /path/to/chat_template.jinja` in YAML configuration) ensures the fixed template is loaded.
|
| 80 |
+
|
| 81 |
### oMLX / MLX
|
| 82 |
Overwrite `chat_template.jinja` in your local model directory and launch with `--jinja`.
|
| 83 |
|
| 84 |
+
### Recommended Sampling Parameters
|
| 85 |
+
|
| 86 |
+
Official Qwen recommendations apply uniformly across all Qwen 3.x models (3.5, 3.6, 3.7, 3.8, and Flash-Next) based on mode and task:
|
| 87 |
+
|
| 88 |
+
| Mode & Task | Temperature | Top-P | Top-K | Min-P | Presence Penalty | Description |
|
| 89 |
+
|---|---|---|---|---|---|---|
|
| 90 |
+
| **Thinking Mode — Coding & Agentic Tools** | **`0.6`** | `0.95` | `20` | `0.0` | `0.0` | **Recommended for coding & agents**: Sharpens syntax and tool argument fidelity while retaining exploratory reasoning. Presence penalty must stay at `0.0` to avoid penalizing necessary syntax keywords. |
|
| 91 |
+
| **Thinking Mode — General Reasoning & Math** | **`1.0`** | `0.95` | `20` | `0.0` | `0.0` | **Recommended for general Q&A / math**: Encourages wide hypothesis exploration in `<think>` blocks. Avoid `temp: 0`, which can induce reasoning loops. |
|
| 92 |
+
| **Non-Thinking / Instruct Mode (`enable_thinking: false`)** | **`0.7`** | `0.80` | `20` | `0.0` | `1.5` | Stable instruction following. For non-thinking coding tasks, lower temperature to `0.6` and set presence penalty to `0.0`. |
|
| 93 |
+
|
| 94 |
+
* *Top-K:* Keeping `top_k: 20` is an official standard baseline across all Qwen models (especially quantized weights) to truncate long-tail token noise and prevent hallucinations.
|
| 95 |
+
* *Min-P:* Official presets default to `min_p: 0.0`. On engines like `llama.cpp` or KoboldCPP, community users often use `min_p: 0.05` alongside `temp: 0.6` as a dynamic filter.
|
| 96 |
+
|
| 97 |
+
### GitHub Copilot Desktop Canvas
|
| 98 |
+
GitHub Copilot Desktop requires OpenAI-standard JSON tool calls. Launch your server with `tool_call_format="json"` and use `--tool-call-parser hermes` (e.g. on vLLM or llama-server) so Copilot can interact seamlessly with canvas tools.
|
| 99 |
+
|
| 100 |
</details>
|
| 101 |
|
| 102 |
---
|
|
|
|
| 210 |
"tool_call_format": "json"
|
| 211 |
}
|
| 212 |
```
|
| 213 |
+
*(When opting into JSON format, structured JSON responses and tool arguments are protected from truncation to preserve JSON syntax, while plain-text logs are safely sliced)*.
|
| 214 |
|
| 215 |
</details>
|
| 216 |
|
|
|
|
| 221 |
* `max_tool_arg_chars` (default `0` / disabled): Slices oversized tool call arguments. Applies to mapping arguments and to serialized JSON string arguments alike.
|
| 222 |
* `max_tool_response_chars` (default `0` / disabled): Slices oversized tool output data.
|
| 223 |
|
| 224 |
+
In JSON mode (`tool_call_format="json"`), tool call arguments and structured JSON responses (`{...}`, `[...]`) are preserved intact to prevent syntax corruption, while long plain-text outputs (e.g. bash transcripts) remain safely governed by `max_tool_response_chars`.
|
| 225 |
|
| 226 |
</details>
|
| 227 |
|
|
|
|
| 240 |
```bash
|
| 241 |
python3 scripts/test_v22.py
|
| 242 |
```
|
| 243 |
+
Tests cover 105 automated verification cells including `reasoning_effort` levels, monotonic API mappings, inline chat tags, multi-part and vision payloads, tool call serialization across wire formats, dynamic truncation, error escalation precision, malformed input handling, multi-turn history parsing, prefix KV cache stability, and parity between `chat_template.jinja` and `chat_template_oneline.txt`. Set `QWEN_TEMPLATE_FILE=chat_template_oneline.txt` to run the entire suite against the minified build. Test 105 is a deterministic property fuzzer (`scripts/fuzz_template.py`) asserting nine structural invariants over generated conversations; run it standalone with `python3 scripts/fuzz_template.py --cases 2000`.
|
| 244 |
|
| 245 |
</details>
|
| 246 |
|
|
|
|
| 297 |
Deeply nested Jinja loops and macros create severe parsing bottlenecks in C++ inference engines. We flattened the AST architecture, effectively curing an 80% inference throughput drop on `llama.cpp` by streamlining how `ns_state` tracking and historical rendering loops are evaluated.
|
| 298 |
|
| 299 |
### 9. Dynamic Payload Truncation
|
| 300 |
+
Massive API or database returns can instantly blow out a model's context window. We implemented `max_tool_arg_chars` and `max_tool_response_chars` limiters that safely slice oversized payloads. Argument truncation covers both mapping arguments and serialized JSON string arguments, so the OpenAI proxy wire format is limited on the same terms as native dictionaries. In JSON mode (`tool_call_format="json"`), structured JSON payloads (`{...}`, `[...]`) and tool arguments are protected from truncation to guarantee JSON parse validity, while plain-text outputs (such as bash logs) are safely truncated according to `max_tool_response_chars`.
|
| 301 |
|
| 302 |
### 10. Reasoning Bypass Hallucination Mitigation
|
| 303 |
+
When thinking is disabled, Qwen models often hallucinate reasoning tags due to their training bias. In v22.5, we fully aligned the `# Tools` system prompt block: when thinking is disabled (`enable_thinking=false` or `reasoning_effort="none"`), the template omits `<think>` examples and planning directives entirely, instructing the model to output tool calls immediately without conversational prefixes. This prevents the model from attempting to reason or hallucinating closing tags after the prefilled closed `<think>\n\n</think>\n\n` block.
|
| 304 |
|
| 305 |
</details>
|
| 306 |
|
|
|
|
| 309 |
<details>
|
| 310 |
<summary>Update History & Changelog</summary>
|
| 311 |
|
| 312 |
+
> **2026-09-03 Update (v22.5): Non-Thinking Tool Prompt Alignment, Video URL Support, and Engine Hardening.**
|
| 313 |
+
> 1. **Non-Thinking Tool Prompt Coherence:** When thinking is disabled (`enable_thinking=false` or `reasoning_effort="none"`), the `# Tools` system block now omits `<think>` examples and planning directives, perfectly aligning with the prefilled closed `<think>\n\n</think>\n\n` block and eliminating model stalling on non-reasoning agent loops (#94).
|
| 314 |
+
> 2. **Multi-Part Video URL Detection:** Added `'video_url' in item` support in multi-part message parsing, mirroring `'image_url'` handling and eliminating unhandled exceptions on OpenAI-compatible multi-modal video payloads (#94).
|
| 315 |
+
> 3. **Smart Response Truncation in JSON Mode:** Replaced the flat `_tool_format != 'json'` bypass with structural JSON detection (`[:1] in ('{', '['))`. Structured JSON tool payloads remain protected from mid-syntax slicing, while long plain-text terminal logs in JSON mode are safely truncated according to `max_tool_response_chars` (#94).
|
| 316 |
+
> 4. **SGLang & Qwen 3.8 Flash-Next Support:** Added deployment documentation and verified compatibility for SGLang (`--chat-template`) and Qwen 3.8 Flash-Next (#101, #102).
|
| 317 |
+
> 5. **Sampling Parameters & Harness Guidance:** Added recommended sampling parameters and GitHub Copilot Desktop Canvas configuration guidance (#86, #100).
|
| 318 |
+
> 6. **Expanded Test Suite (102 -> 105):** Added automated regression tests for non-thinking tool prompt coherence, video URL parsing, and smart JSON response truncation.
|
| 319 |
+
>
|
| 320 |
> **2026-08-24 Update (v22.4): Multi-Tool Token Parity, Extended Reasoning Formats, and UI Default Knobs.**
|
| 321 |
> 1. **Parallel Tool Call Token Parity:** Consecutive `<tool_call>` blocks in assistant history are now separated by a single newline (`\n`) instead of double newlines (`\n\n`), restoring exact token alignment with official Qwen model generation and preventing prefix KV cache divergence during multi-tool turns (#87).
|
| 322 |
> 2. **vLLM & Responses API Reasoning Support:** Added native support for `message.reasoning` (used by vLLM OpenAI-compatible endpoints and Responses API schemas) alongside `reasoning_content` and `thinking` (#88).
|
archive/README-v22.4.md
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
tags:
|
| 4 |
+
- jinja
|
| 5 |
+
- chat-template
|
| 6 |
+
- qwen
|
| 7 |
+
- qwen3.5
|
| 8 |
+
- qwen3.6
|
| 9 |
+
- qwen3.8
|
| 10 |
+
- lm-studio
|
| 11 |
+
- mlx
|
| 12 |
+
- llama.cpp
|
| 13 |
+
- vllm
|
| 14 |
+
- tool-calling
|
| 15 |
+
- thinking
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# Fixed jinja chat templates for Qwen 3.5, 3.6 & 3.8 (v22.4)
|
| 19 |
+
|
| 20 |
+
This is a universal drop-in Jinja template that fixes rendering errors, KV cache invalidation, token waste, empty think poisoning, and fatal agentic stalling across official Qwen chat templates.
|
| 21 |
+
|
| 22 |
+
It works across LM Studio, llama.cpp, vLLM, MLX, oMLX, KoboldCPP, and any engine that supports Hugging Face Jinja templates. You only need the single `chat_template.jinja` file at the root of this repository for all Qwen 3.5, 3.6, and 3.8 model sizes.
|
| 23 |
+
|
| 24 |
+
---
|
| 25 |
+
|
| 26 |
+
## What is New with Qwen 3.8 (v22 / v22.1 / v22.2 / v22.3 / v22.4)
|
| 27 |
+
|
| 28 |
+
Qwen 3.8 introduces prompt-steered reasoning depth and new model architectures (`Qwen3.8-2.4T-A95B`, `Qwen3.8-27B`). While the official template added reasoning effort steering, it also introduced several lockdowns, regressions, and syntax crashes.
|
| 29 |
+
|
| 30 |
+
The v22 generation delivers full Qwen 3.8 support with all official bugs resolved:
|
| 31 |
+
|
| 32 |
+
| Feature / Fix | Official Qwen 3.8 Template | This Template (v22.4) |
|
| 33 |
+
|---|---|---|
|
| 34 |
+
| **Default Reasoning Baseline** | Hardcodes `xhigh` by default, often exhausting the token budget on reasoning before generating answers. | Defaults to `medium` (zero injected tokens), preserving KV cache parity with v21 and preventing empty-content timeouts. |
|
| 35 |
+
| **Non-Reasoning Fast Mode** | The `Qwen3.8-2.4T-A95B` template throws a fatal exception if `enable_thinking=false` (the `Qwen3.8-27B` template accepts it). | Full non-reasoning mode restored via kwargs (`enable_thinking=false`, `reasoning_effort="none"`) or inline `<\|think_off\|>`. |
|
| 36 |
+
| **History Reasoning Extraction** | Drops in-content thinking extraction, prepending blank `<think></think>` blocks to real thoughts in chat history. | Extracts reasoning across OpenAI (`reasoning_content`), Anthropic (`thinking`), and in-content tags (`<think>`) without tag duplication. |
|
| 37 |
+
| **Tool Argument Wire Format** | Crashes on serialized JSON strings from standard OpenAI API clients. | Canonical XML default with universal argument handling: safely renders stringified JSON arguments in history without syntax crashes or KV cache invalidation. |
|
| 38 |
+
| **Client Reasoning Aliases** | Rejects non-standard effort names. | Maps OpenAI, Claude Code, Cursor, and Cline aliases automatically: `high`, `max`, `ultracode`, `extreme` $\to$ `xhigh`; `minimal` $\to$ `low`; `none`, `off` $\to$ disabled. |
|
| 39 |
+
| **Inline Chat Tags** | Not supported. | Inline steering via chat text: `<\|think_low\|>`, `<\|think_medium\|>`, `<\|think_xhigh\|>`, `<\|think_ultracode\|>`, `<\|think_off\|>`. Tags are stripped before inference and stay in effect for later turns until another tag replaces them. |
|
| 40 |
+
| **Leading System Prompts** | Treats multiple leading system messages as separate turns. | Merges consecutive leading `system` and `developer` messages into a single system turn joined by double newlines. |
|
| 41 |
+
| **Tool Error Recovery** | Not supported or triggers false warnings on search results containing words like "error". | Two-tier error recovery with smart code/grep disambiguation to avoid false warnings on search results like `throw new Error(...)`. Structural signals escalate on payloads of any size. |
|
| 42 |
+
| **llama.cpp Flag Support** | Requires manual template kwargs. | Native alias support for `--reasoning-preserve` CLI flag via `preserve_reasoning`. |
|
| 43 |
+
| **Diagnostic Tool** | None provided. | Includes `scripts/check_applied.py` to inspect model directories and GGUFs for active template versions. |
|
| 44 |
+
|
| 45 |
+
---
|
| 46 |
+
|
| 47 |
+
<details open>
|
| 48 |
+
<summary><b>Quick Install & Engine Setup</b></summary>
|
| 49 |
+
|
| 50 |
+
### llama.cpp / llama-server / koboldcpp
|
| 51 |
+
Run `llama-server` with the template file and DeepSeek reasoning format:
|
| 52 |
+
```bash
|
| 53 |
+
llama-server -m your_model.gguf --jinja --chat-template-file chat_template.jinja --reasoning-format deepseek
|
| 54 |
+
```
|
| 55 |
+
*Why `--reasoning-format deepseek` matters:* When connecting coding agents like OpenCode, Claude Code, or Pi.dev to `llama-server`, this flag extracts `<think>` blocks into the dedicated `reasoning_content` API response field. This prevents raw thinking tokens from leaking into the text stream and stopping tool calls midway.
|
| 56 |
+
|
| 57 |
+
*Native CLI flag:* On recent `llama.cpp` builds, you can pass `--reasoning-preserve` directly for 100% Prefix KV Cache retention.
|
| 58 |
+
|
| 59 |
+
### LM Studio
|
| 60 |
+
1. Open your Qwen model in the right side panel.
|
| 61 |
+
2. Scroll down to **Prompt Template**.
|
| 62 |
+
3. Replace the template with the contents of `chat_template.jinja`.
|
| 63 |
+
4. Click **Save**.
|
| 64 |
+
|
| 65 |
+
### vLLM
|
| 66 |
+
Replace the `"chat_template"` string in your `tokenizer_config.json` with `chat_template_oneline.txt` (or raw `chat_template.jinja`).
|
| 67 |
+
```bash
|
| 68 |
+
vllm serve Qwen/Qwen3.8-2.4T-A95B --reasoning-parser qwen3 --tool-call-parser qwen3_xml
|
| 69 |
+
```
|
| 70 |
+
*Reasoning parser:* Use `--reasoning-parser qwen3` on vLLM to split `<think>` blocks into the response reasoning field. Note that vLLM's parser checks the top-level `enable_thinking: false` request parameter or `reasoning_effort: "none"`, but does not inspect Jinja-internal dynamic `<|think_off|>` tags in prompt bodies. To disable reasoning on vLLM, pass `"enable_thinking": false` in your request parameters.
|
| 71 |
+
|
| 72 |
+
*Tool parser selection:* Use `--tool-call-parser qwen3_xml` on current vLLM releases. If you are on an older vLLM build, use `--tool-call-parser qwen3_coder`. If you explicitly set `tool_call_format="json"`, use `--tool-call-parser hermes`.
|
| 73 |
+
|
| 74 |
+
### oMLX / MLX
|
| 75 |
+
Overwrite `chat_template.jinja` in your local model directory and launch with `--jinja`.
|
| 76 |
+
|
| 77 |
+
</details>
|
| 78 |
+
|
| 79 |
+
---
|
| 80 |
+
|
| 81 |
+
## Why you need this
|
| 82 |
+
|
| 83 |
+
The official Qwen templates contain engine restrictions, Python-specific Jinja logic, and regressions that break local inference and agent workflows.
|
| 84 |
+
|
| 85 |
+
<details open>
|
| 86 |
+
<summary><b>Critical Issues Fixed</b></summary>
|
| 87 |
+
|
| 88 |
+
| Area | Issue in Official Templates | The Fix |
|
| 89 |
+
|---|---|---|
|
| 90 |
+
| **Qwen 3.8 Support** | Official 3.8-2.4T-A95B crashes if `enable_thinking=false`. | **Restored Fast Mode**. Supports fast non-reasoning mode via kwargs or `<\|think_off\|>`. |
|
| 91 |
+
| **Qwen 3.8 Token Safety** | Official `xhigh` default burns token budgets with zero content returned. | **Safe `medium` Default**. Zero prompt injection unless explicitly requested. |
|
| 92 |
+
| **Qwen 3.8 Regression** | Official 3.8 injects duplicate blank `<think></think>` in chat history. | **Cured Empty Think Poisoning**. Multi-format reasoning extraction. |
|
| 93 |
+
| **Reasoning Control** | Inability to change reasoning effort in chat interfaces. | **Inline Chat Tags**. Full support for `<\|think_low\|>`, `<\|think_medium\|>`, and `<\|think_xhigh\|>`. |
|
| 94 |
+
| **Compatibility** | `llama.cpp --reasoning-preserve` CLI flag compatibility. | **Native Alias Support**. Supports both `preserve_reasoning` and `preserve_thinking`. |
|
| 95 |
+
| **Compatibility** | JSON-string tool arguments (OpenAI / Ollama) crash official templates. | **Universal Tool Parsing**. Safely handles mappings, JSON strings, and scalar args. |
|
| 96 |
+
| **Agentic Loop** | Model aborts turn when combining conversational text and a tool call. | Cured "Empty Think" poisoning and softened imperative system directives. |
|
| 97 |
+
| **Agentic Loop** | Model gets stuck emitting the identical failing tool call. | Added two-tier error escalation to force correction while retaining reasoning. |
|
| 98 |
+
| **Agentic Loop** | Model panics and debates internal rules after fetching data. | Broadened `<think>` instructions to authorize conversational synthesis. |
|
| 99 |
+
| **Agentic Loop** | API returns containing the word "error" trigger false retry loops. | Replaced broad matching with strict structural guards. |
|
| 100 |
+
| **Performance** | Mutated past turns destroy the prefix cache. | Enforced chronological history for a 100% KV Cache hit rate. |
|
| 101 |
+
| **Performance** | Deep Jinja nesting drops `llama.cpp` speed by 80%. | Flattened the AST architecture to maximize throughput. |
|
| 102 |
+
| **Compatibility** | Python-specific filters crash C++ inference engines. | Rewrote all filters to be 100% `minijinja` safe. |
|
| 103 |
+
| **Compatibility** | Qwen-native parsers (like vLLM) crash on JSON formatting. | Maintained canonical Qwen XML format as the default. |
|
| 104 |
+
| **Compatibility** | Older API setups and wrappers crash on native XML. | Added a `tool_call_format="json"` opt-in override. |
|
| 105 |
+
| **Compatibility** | Anthropic `message.thinking` payloads are rejected. | Added native Anthropic reasoning support. |
|
| 106 |
+
| **Stability** | Massive tool data returns blow out the context window. | Added dynamic payload truncation limits. |
|
| 107 |
+
| **Stability** | Mid-conversation system prompts crash the template. | Added native support for arbitrary system and developer messages. |
|
| 108 |
+
| **Edge Cases** | Text duplicates during streaming generation. | Restored canonical spacing to the generation prompt. |
|
| 109 |
+
| **Edge Cases** | Model hallucinates reasoning tags when thinking is disabled. | Injected strict boundaries to force clean reasoning bypass. |
|
| 110 |
+
|
| 111 |
+
</details>
|
| 112 |
+
|
| 113 |
+
---
|
| 114 |
+
|
| 115 |
+
## Customization & Kwarg Reference
|
| 116 |
+
|
| 117 |
+
<details open>
|
| 118 |
+
<summary><b>1. Reasoning Effort Steering (Qwen 3.8)</b></summary>
|
| 119 |
+
|
| 120 |
+
Qwen 3.8 has 3 native prompt-steered reasoning levels. You can control this via template kwargs or directly inline in your chat messages:
|
| 121 |
+
|
| 122 |
+
**Via Template Kwargs (`chat_template_kwargs`):**
|
| 123 |
+
```json
|
| 124 |
+
{
|
| 125 |
+
"reasoning_effort": "xhigh"
|
| 126 |
+
}
|
| 127 |
+
```
|
| 128 |
+
* **`"medium"` (Default / Safe Baseline):** No extra instruction text injected. Preserves 100% Prefix KV Cache parity with v21 and lets the model reason naturally without token-budget traps.
|
| 129 |
+
* **`"xhigh"` (Deep Reasoning):** Injects Qwen's official deep reasoning instruction:
|
| 130 |
+
> *"Reasoning effort is set to xhigh. Please think carefully through the task, validate key assumptions, consider plausible alternatives, and prioritize correctness, consistency, and clarity in the final answer."*
|
| 131 |
+
* **`"low"` (Concise Reasoning):** Injects concise thinking instructions for fast, summary-oriented reasoning:
|
| 132 |
+
> *"Reasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration."*
|
| 133 |
+
|
| 134 |
+
*API Compatibility Aliases:*
|
| 135 |
+
To prevent errors when calling the model through standard API proxies and coding agent harnesses:
|
| 136 |
+
* `"high"`, `"max"`, `"ultracode"`, and `"extreme"` automatically map to `"xhigh"` (supported by OpenAI, Claude Code, Cline, Cursor).
|
| 137 |
+
* `"minimal"` and `"low"` automatically map to `"low"`.
|
| 138 |
+
* `"none"` and `"off"` disable thinking entirely.
|
| 139 |
+
|
| 140 |
+
**Via Inline Chat Tags (Per-Prompt Steering):**
|
| 141 |
+
* `Solve this proof <|think_xhigh|>` or `<|think_ultracode|>` (Deep reasoning)
|
| 142 |
+
* `Explain recursion briefly <|think_low|>` (Concise reasoning)
|
| 143 |
+
* `What is the capital of France? <|think_off|>` (No reasoning / fast mode)
|
| 144 |
+
|
| 145 |
+
Tags are sticky: the template scans the whole conversation and the last tag found wins, so a tag in an earlier message stays in effect for later turns until another tag replaces it.
|
| 146 |
+
|
| 147 |
+
*(Note: When thinking is disabled, reasoning effort instructions are automatically suppressed. On vLLM, prefer request-level `enable_thinking=false` or `reasoning_effort="none"` over `<|think_off|>`; see the vLLM setup notes above).*
|
| 148 |
+
|
| 149 |
+
**Editing defaults without kwargs (LM Studio and other UIs that cannot pass `chat_template_kwargs`):**
|
| 150 |
+
Three variables near the top of `chat_template.jinja` hold the defaults. Edit them before pasting the template:
|
| 151 |
+
* `_default_reasoning_effort = 'medium'` - effort used when no `reasoning_effort` kwarg is passed: `'low'`, `'medium'`, `'xhigh'`, or `'none'` (thinking off). The aliases listed above work too.
|
| 152 |
+
* `enable_thinking = enable_thinking if enable_thinking is defined else true` - change `true` to `false` to default to non-thinking mode.
|
| 153 |
+
* `_tool_format = tool_call_format if tool_call_format is defined else 'xml'` - change `'xml'` to `'json'` for harnesses that need Hermes JSON.
|
| 154 |
+
|
| 155 |
+
</details>
|
| 156 |
+
|
| 157 |
+
<details>
|
| 158 |
+
<summary><b>2. KV Cache Preservation (`preserve_reasoning` & `preserve_thinking`)</b></summary>
|
| 159 |
+
|
| 160 |
+
By default, this template **preserves** all past `<think>` blocks in the chat history. This prevents the model from suffering "amnesia stalls" during complex agentic loops and guarantees a 100% Prefix KV Cache hit rate on local inference engines.
|
| 161 |
+
|
| 162 |
+
* On recent `llama.cpp` builds, pass `--reasoning-preserve` directly.
|
| 163 |
+
* Or pass via template kwargs:
|
| 164 |
+
```json
|
| 165 |
+
{
|
| 166 |
+
"preserve_thinking": true
|
| 167 |
+
}
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
+
If you are on severely memory-constrained hardware and need to save context tokens, set `"preserve_thinking": false` (or `"preserve_reasoning": false`) to strip past thoughts.
|
| 171 |
+
|
| 172 |
+
> **KV cache trade-off:** `preserve_thinking: false` keeps reasoning only for the turns that follow the most recent user query, so historical thoughts are dropped as soon as a new user turn begins. That rewrites already-rendered history and invalidates the prefix cache from the first assistant turn of the previous segment. The 100% Prefix KV Cache hit rate applies to the default (`true`) setting; enable stripping only when context pressure outweighs prompt reprocessing cost.
|
| 173 |
+
|
| 174 |
+
</details>
|
| 175 |
+
|
| 176 |
+
<details>
|
| 177 |
+
<summary><b>3. Tool Call Format (XML vs JSON)</b></summary>
|
| 178 |
+
|
| 179 |
+
Qwen models are natively trained on XML tool calls (`<function=name>`). By default, this template uses **`xml`** format:
|
| 180 |
+
* **`"xml"` (Default):** Generates canonical XML instructions for tool calls and safely handles both dictionary arguments and serialized JSON strings in assistant history without corrupting XML tags or mutating system prompts between turns. Dictionary arguments render as `<parameter=...>` blocks; a serialized JSON string is rendered verbatim inside the `<function=...>` block as a best-effort fallback (Jinja cannot parse JSON), so dictionaries are preferred. vLLM converts string arguments to dictionaries before rendering, so it always takes the `<parameter>` path.
|
| 181 |
+
* **`"json"` (Optional Override):** Forces Hermes JSON format (`{"name": "...", "arguments": {...}}`) for both system prompt instructions and history rendering.
|
| 182 |
+
|
| 183 |
+
**When to use the JSON override:**
|
| 184 |
+
If you are using a framework or harness (such as specific Hermes Agent configurations) that strictly requires Hermes JSON, pass:
|
| 185 |
+
```json
|
| 186 |
+
{
|
| 187 |
+
"tool_call_format": "json"
|
| 188 |
+
}
|
| 189 |
+
```
|
| 190 |
+
*(When opting into JSON format, both argument and tool response truncation are bypassed to avoid corrupting JSON syntax)*.
|
| 191 |
+
|
| 192 |
+
</details>
|
| 193 |
+
|
| 194 |
+
<details>
|
| 195 |
+
<summary><b>4. Dynamic Payload Truncation</b></summary>
|
| 196 |
+
|
| 197 |
+
To prevent oversized tool returns from blowing out context limits:
|
| 198 |
+
* `max_tool_arg_chars` (default `0` / disabled): Slices oversized tool call arguments. Applies to mapping arguments and to serialized JSON string arguments alike.
|
| 199 |
+
* `max_tool_response_chars` (default `0` / disabled): Slices oversized tool output data.
|
| 200 |
+
|
| 201 |
+
Both limiters are disabled automatically when `tool_call_format="json"` is active, since slicing serialized JSON corrupts the payload for downstream parsers.
|
| 202 |
+
|
| 203 |
+
</details>
|
| 204 |
+
|
| 205 |
+
---
|
| 206 |
+
|
| 207 |
+
<details>
|
| 208 |
+
<summary><b>Diagnostic Script & Test Suite</b></summary>
|
| 209 |
+
|
| 210 |
+
### Check Active Template on Your Model
|
| 211 |
+
Run the included diagnostic utility on your model folder or GGUF:
|
| 212 |
+
```bash
|
| 213 |
+
python3 scripts/check_applied.py /path/to/your/model
|
| 214 |
+
```
|
| 215 |
+
|
| 216 |
+
### Running the Test Suite
|
| 217 |
+
```bash
|
| 218 |
+
python3 scripts/test_v22.py
|
| 219 |
+
```
|
| 220 |
+
Tests cover 101 automated verification cells including `reasoning_effort` levels, monotonic API mappings, inline chat tags, multi-part and vision payloads, tool call serialization across wire formats, dynamic truncation, error escalation precision, malformed input handling, multi-turn history parsing, prefix KV cache stability, and parity between `chat_template.jinja` and `chat_template_oneline.txt`. Set `QWEN_TEMPLATE_FILE=chat_template_oneline.txt` to run the entire suite against the minified build. Test 101 is a deterministic property fuzzer (`scripts/fuzz_template.py`) asserting nine structural invariants over generated conversations; run it standalone with `python3 scripts/fuzz_template.py --cases 2000`.
|
| 221 |
+
|
| 222 |
+
</details>
|
| 223 |
+
|
| 224 |
+
---
|
| 225 |
+
|
| 226 |
+
## Authorship & Contributors
|
| 227 |
+
| Role | Author / Contributor |
|
| 228 |
+
|------|----------------------|
|
| 229 |
+
| Original models | Alibaba Cloud (Qwen team) |
|
| 230 |
+
| Template fixes | [Frédéric Guigand](https://huggingface.co/froggeric) (`@froggeric`) |
|
| 231 |
+
| Property fuzzer, error tiering & test suite | [Juan Calderon-Perez](https://huggingface.co/g-a-b-y) (`@g-a-b-y`) |
|
| 232 |
+
| Multi-tool alignment, reasoning fields & docs | [Gabriel Devenyi](https://huggingface.co/gdevenyi) (`@gdevenyi`) |
|
| 233 |
+
| C++ AST optimizations | [barubary](https://github.com/spiritbuun/buun-llama-cpp) / `spiritbuun` |
|
| 234 |
+
|
| 235 |
+
## License
|
| 236 |
+
Apache-2.0, inherited from Qwen.
|
| 237 |
+
|
| 238 |
+
---
|
| 239 |
+
|
| 240 |
+
<details>
|
| 241 |
+
<summary>Technical Details of the Critical Fixes</summary>
|
| 242 |
+
|
| 243 |
+
### 1. The "Empty Think" Poisoning and Logic Trap Cure
|
| 244 |
+
Previous templates attempted to save tokens by replacing past thoughts with empty `<think>\n</think>` blocks, combined with an absolute system prompt demanding a tool be called immediately after `</think>`. This created a toxic pattern where the model associated empty thoughts with tools, causing an 80%+ premature turn abort rate. We abolished empty think injection and rewrote the `<IMPORTANT>` directives to explicitly authorize conversational synthesis after thinking. In this release, we also cured official Qwen 3.8's history bug where missing in-content parsers created duplicate blank think tags.
|
| 245 |
+
|
| 246 |
+
### 2. Upfront Pre-Scan for Control Tags & Inline Effort Steering
|
| 247 |
+
In Jinja templates, system prompts are assembled before iterating over message history. We introduce an upfront pre-scan covering raw strings, string lists, and multi-part content lists (`[{'type': 'text', 'text': '...'}]`). This resolves `<|think_off|>`, `<|think_on|>`, `<|think_low|>`, `<|think_medium|>`, and `<|think_xhigh|>` states before the system message is built, preventing reasoning instructions from being injected into non-reasoning turns and cleanly stripping all tags during rendering.
|
| 248 |
+
|
| 249 |
+
### 3. KV Cache Safety and Autoregressive Normalization
|
| 250 |
+
Llama.cpp and vLLM utilize prefix KV caching to speed up generation. Because this template preserves historical thoughts chronologically by default and defaults `reasoning_effort` to `medium` (zero system tokens), rendered history perfectly synchronizes with cached generated tokens. Combined with strict single newline normalization at autoregressive boundaries, this achieves a 100% KV Cache hit rate in multi-turn sessions.
|
| 251 |
+
|
| 252 |
+
### 4. Native XML Tool Format and Universal Serialization
|
| 253 |
+
The model was trained with the XML tool format used by Qwen3-Coder. We restored this format natively while bypassing the `|items` crash by handling both mapping dictionaries and JSON strings. This eliminates crashes when standard OpenAI proxies pass stringified arguments.
|
| 254 |
+
|
| 255 |
+
### 5. Two-Tier Agentic Error Escalation
|
| 256 |
+
When a tool call fails validation repeatedly, the model can enter a degenerate reasoning spiral. This template leverages a two-tier escalation system driven by a forward-tracked `consecutive_failures` counter. On the first error, a diagnostic warning is injected. On the second consecutive error, an urgent system warning forces a fundamentally different approach while retaining the reasoning block so the model can plan its correction.
|
| 257 |
+
|
| 258 |
+
### 6. Smart False-Positive Detection
|
| 259 |
+
Instead of broad substring matching that triggers false retry loops on successful database returns containing words like "error", this template utilizes strict structural guards evaluated over the first 120 characters of the payload.
|
| 260 |
+
|
| 261 |
+
Signals are split into two tiers. **Strong signals** are unambiguous structural markers (`"error":`, `Traceback (most recent call last):`, `command not found`, `Exception:`, `fatal:`, nonzero exit codes, `invalid syntax`) and fire unconditionally. **Weak signals** are bare prefixes (`error:`, `err!`) and remain gated by a length ceiling plus shell echo (`$ `) and timing (`took `) exclusions. Code and search results are excluded up front via `throw new`, `console.error`, `logger.error` and similar patterns.
|
| 262 |
+
|
| 263 |
+
Tiering matters because the earlier flat gate suppressed every signal on payloads of 600 characters or more: a routine multi-frame Python traceback exceeds that ceiling, and any output echoing a shell prompt or a timing figure was discarded outright. Strong signals now escalate regardless of payload size.
|
| 264 |
+
|
| 265 |
+
### 7. minijinja Compatibility Constraints
|
| 266 |
+
Python-only Jinja2 features crash or misbehave on `minijinja` (the C++ runtime used by llama.cpp, LM Studio, and MLX). All instances have been refactored for universal support:
|
| 267 |
+
* `content | replace('<|think_on|>', '')` became `content.split('<|think_on|>') | join('')` (fixes a bug where `minja` silently drops the entire text payload if the replaced string is found at index 0).
|
| 268 |
+
* `| items` became `mapping.items()` iteration, which `minijinja` implements natively.
|
| 269 |
+
* `loop.previtem` became explicit array indexing.
|
| 270 |
+
* `map('string')` became `join('|')`.
|
| 271 |
+
* `| first` became `'$ ' in content`.
|
| 272 |
+
|
| 273 |
+
### 8. AST Flattening for C++ Throughput
|
| 274 |
+
Deeply nested Jinja loops and macros create severe parsing bottlenecks in C++ inference engines. We flattened the AST architecture, effectively curing an 80% inference throughput drop on `llama.cpp` by streamlining how `ns_state` tracking and historical rendering loops are evaluated.
|
| 275 |
+
|
| 276 |
+
### 9. Dynamic Payload Truncation
|
| 277 |
+
Massive API or database returns can instantly blow out a model's context window. We implemented `max_tool_arg_chars` and `max_tool_response_chars` limiters that safely slice oversized payloads. Argument truncation covers both mapping arguments and serialized JSON string arguments, so the OpenAI proxy wire format is limited on the same terms as native dictionaries. Both limiters are automatically disabled when `tool_call_format="json"` is active, as slicing serialized JSON structurally corrupts the data and crashes downstream parsers.
|
| 278 |
+
|
| 279 |
+
### 10. Reasoning Bypass Hallucination Mitigation
|
| 280 |
+
When thinking is disabled, Qwen models often hallucinate reasoning tags due to their training bias. We injected a safe boundary and adjusted the `<IMPORTANT>` system block to remove explicit mentions of `</think>` during tool instructions. This stops the model from hallucinating closing tags when calling tools in a no-reasoning state.
|
| 281 |
+
|
| 282 |
+
</details>
|
| 283 |
+
|
| 284 |
+
---
|
| 285 |
+
|
| 286 |
+
<details>
|
| 287 |
+
<summary>Update History & Changelog</summary>
|
| 288 |
+
|
| 289 |
+
> **2026-08-24 Update (v22.4): Multi-Tool Token Parity, Extended Reasoning Formats, and UI Default Knobs.**
|
| 290 |
+
> 1. **Parallel Tool Call Token Parity:** Consecutive `<tool_call>` blocks in assistant history are now separated by a single newline (`\n`) instead of double newlines (`\n\n`), restoring exact token alignment with official Qwen model generation and preventing prefix KV cache divergence during multi-tool turns (#87).
|
| 291 |
+
> 2. **vLLM & Responses API Reasoning Support:** Added native support for `message.reasoning` (used by vLLM OpenAI-compatible endpoints and Responses API schemas) alongside `reasoning_content` and `thinking` (#88).
|
| 292 |
+
> 3. **Top-Level `_default_reasoning_effort` Knob:** Added an explicit `_default_reasoning_effort` configuration variable at the top of the template and removed dead assignment code, allowing LM Studio and WebUI users who cannot pass kwargs to change the default reasoning level cleanly without template errors (#91).
|
| 293 |
+
> 4. **Documented Sticky Inline Tags & UI Configuration:** Documented inline tag persistence across turns, string-argument fallbacks in XML, and clarified vLLM launch commands (`--reasoning-parser qwen3` alongside `--tool-call-parser qwen3_xml`) (#89, #90).
|
| 294 |
+
> 5. **Expanded Test Suite (101 -> 102):** Added automated test coverage for `message.reasoning` extraction, `_default_reasoning_effort` knob behavior, and updated parallel tool call assertions.
|
| 295 |
+
>
|
| 296 |
+
> *(Credits: Huge thanks to Gabriel Devenyi (@gdevenyi) for their contributions to the v22.4 release!)*
|
| 297 |
+
>
|
| 298 |
+
> **2026-08-20 Update (v22.3): Error Detection Tiering, Reasoning De-duplication, and Test Suite Expansion.**
|
| 299 |
+
> 1. **Two-Tier Error Signals:** Structural error markers now escalate regardless of payload size. The previous flat 600-character ceiling plus whole-body `$ ` and `took ` exclusions silently suppressed warnings on ordinary multi-frame tracebacks and on any shell transcript that echoed its command. Successful exit-code reports (`Exit code: 0`) and JSON success envelopes (`"error": null` / `false` / `""`) are excluded from the strong tier so success payloads do not escalate.
|
| 300 |
+
> 2. **Reasoning De-duplication:** When `reasoning_content` or `thinking` is supplied, a leading think block in `content` is stripped so clients that populate both no longer emit two consecutive think blocks. Literal tags later in the answer (for example inside code fences) are preserved verbatim; full heuristic extraction still applies when no explicit reasoning field is present.
|
| 301 |
+
> 3. **Single-Line Think Blocks:** `<think>...</think>` written without surrounding newlines is now extracted instead of leaking raw tags into rendered history.
|
| 302 |
+
> 4. **Complete Argument Serialization:** Scalar and list tool arguments are serialized via `| tojson` rather than silently dropped, in both XML and JSON wire formats.
|
| 303 |
+
> 5. **Consistent Truncation:** `max_tool_arg_chars` now applies to serialized JSON string arguments, and `max_tool_response_chars` is bypassed under `tool_call_format="json"` as documented.
|
| 304 |
+
> 6. **KV Cache Documentation:** Documented that `preserve_thinking: false` rewrites rendered history at each new user turn and therefore voids the prefix cache guarantee, which applies to the default setting.
|
| 305 |
+
> 7. **Test Suite Expansion (44 -> 101):** Added coverage for control tag and alias completeness, malformed input and exception paths, vision payloads, agentic wire-format shapes, reasoning extraction variants, error detection precision, prefix KV cache stability, and jinja/oneline build parity.
|
| 306 |
+
> 8. **Property-Based Fuzz Harness:** Added `scripts/fuzz_template.py`, a deterministic conversation generator asserting nine structural invariants (render success, jinja/oneline parity, token balance, verbatim content preservation, XML parameter fidelity, JSON tool-call validity, warning precision, prefix KV stability, no-think prefill). Prefix stability is checked at generation boundaries: merged system blocks and consecutive tool-result batches are atomic, since the model only generates after a full batch is appended.
|
| 307 |
+
>
|
| 308 |
+
> *(Credits: Huge thanks to Juan Calderon-Perez (@g-a-b-y) for their massive contributions to the v22.3 release, including the property-based fuzz harness, the two-tier error signal architecture, and extensive test coverage!)*
|
| 309 |
+
>
|
| 310 |
+
> **2026-08-19 Update (v22.2): Extended Effort Aliases, String Argument Safety, and Error Disambiguation.**
|
| 311 |
+
> 1. **Universal Tool Argument Handling:** Hardened XML tool argument parsing to safely handle both mappings and serialized JSON strings in assistant history without syntax crashes or KV cache mutation.
|
| 312 |
+
> 2. **Reasoning Effort Aliases:** Added `"ultracode"` and `"extreme"` mappings to `'xhigh'`, with inline `<|think_ultracode|>` and `<|think_extreme|>` support (#78).
|
| 313 |
+
> 3. **Multi-System Message Merging:** Merged consecutive leading `system` and `developer` messages into a single unified system turn with `\n\n`.
|
| 314 |
+
> 4. **Grep / Search Error Disambiguation:** Eliminated false-positive tool error warnings on code search results containing patterns like `throw new Error` or `console.error` (#66).
|
| 315 |
+
> 5. **Safe XML Primitive Serialization:** Boolean and null XML parameter values now serialize to `true`, `false`, and `null` via `| tojson`.
|
| 316 |
+
> 6. **Zero-Crash Resilience:** Maintained full resilience without fatal `raise_exception` aborts across all runtime engines.
|
| 317 |
+
|
| 318 |
+
> **2026-08-16 Update (v22.1): The Qwen 3.8 Update (Bounded Reasoning Defaults, Inline Chat Tags, and Diagnostic Utility).**
|
| 319 |
+
> 1. **Full Qwen 3.8 Support:** Single drop-in template covering all Qwen 3.5, 3.6, and 3.8 model variants (`Qwen3.8-2.4T-A95B`, `Qwen3.8-27B`).
|
| 320 |
+
> 2. **Default `reasoning_effort` to `medium`:** Replaced the unsafe `xhigh` default with `medium` (zero injected tokens). Eliminates the runaway reasoning token-burn failure where models explore branches until hitting `max_tokens` with empty content (#72).
|
| 321 |
+
> 3. **Inline Chat Tags:** Added support for `<|think_low|>`, `<|think_medium|>`, and `<|think_xhigh|>` tags directly inside chat messages (#70).
|
| 322 |
+
> 4. **API Mapping & Aliasing:** Added case-insensitive alias support for client and serving runtimes (`high`/`max` -> `xhigh`, `minimal` -> `low`, `none` -> thinking off).
|
| 323 |
+
> 5. **Sequential Control Tag Stripping:** All 7 control tags are cleanly stripped across system, user, and multi-part content without prompt leakage.
|
| 324 |
+
> 6. **Cured Official 3.8 Empty Think Bug:** Fixed a regression in the official Qwen 3.8 template where removing `<think>` extraction caused blank `<think></think>` blocks to be prepended to real thoughts in chat history.
|
| 325 |
+
> 7. **Restored Fast Mode:** Replaced official 3.8's hard exception on `enable_thinking=false`, restoring full user freedom to disable reasoning via kwargs or `<|think_off|>`.
|
| 326 |
+
> 8. **Universal Tool Arguments:** Hardened tool call parsing to handle both dictionary structures and JSON-serialized strings without crashing.
|
| 327 |
+
> 9. **Diagnostic Utility:** Added `scripts/check_applied.py` to inspect model folders and GGUFs for template consistency.
|
| 328 |
+
|
| 329 |
+
> **2026-08-13 Update (v22): Qwen 3.8 Support, Reasoning Effort Controls, and Engine Hardening.**
|
| 330 |
+
|
| 331 |
+
> **2026-07-02 Update (v21.3): Optional JSON Tool Format Kwarg.** Added an optional `tool_call_format="json"` override for `chat_template_kwargs`.
|
| 332 |
+
|
| 333 |
+
> **2026-07-02 Update (v21.2): Reasoning Bypass Hallucination Fix.** Adjusted `<IMPORTANT>` block instructions to remove explicit mentions of `</think>` during tool definitions.
|
| 334 |
+
|
| 335 |
+
> **2026-07-02 Update (v21.1): Reliability Overhaul & XML Revert.** Reverted to native XML format for vLLM `qwen3_coder` compatibility and restored `preserve_thinking` default to `true`.
|
| 336 |
+
|
| 337 |
+
</details>
|
archive/v22.4_chat_template.jinja
ADDED
|
@@ -0,0 +1,431 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{%- set template_version = "qwen3.8-froggeric-v22.4" %}
|
| 2 |
+
{%- set _tool_format = tool_call_format if tool_call_format is defined else 'xml' %}
|
| 3 |
+
{%- set image_count = namespace(value=0) %}
|
| 4 |
+
{%- set video_count = namespace(value=0) %}
|
| 5 |
+
{%- set add_vision_id = add_vision_id if add_vision_id is defined else false %}
|
| 6 |
+
{%- set enable_thinking = enable_thinking if enable_thinking is defined else true %}
|
| 7 |
+
{%- set auto_disable_thinking_with_tools = auto_disable_thinking_with_tools if auto_disable_thinking_with_tools is defined else false %}
|
| 8 |
+
{%- if preserve_reasoning is defined and preserve_reasoning is not none %}
|
| 9 |
+
{%- set _preserve_thinking = preserve_reasoning %}
|
| 10 |
+
{%- elif preserve_thinking is defined and preserve_thinking is not none %}
|
| 11 |
+
{%- set _preserve_thinking = preserve_thinking %}
|
| 12 |
+
{%- else %}
|
| 13 |
+
{%- set _preserve_thinking = true %}
|
| 14 |
+
{%- endif %}
|
| 15 |
+
{%- set max_tool_arg_chars = max_tool_arg_chars if max_tool_arg_chars is defined else 0 %}
|
| 16 |
+
{%- set max_tool_response_chars = max_tool_response_chars if max_tool_response_chars is defined else 0 %}
|
| 17 |
+
{%- set _default_reasoning_effort = 'medium' %}
|
| 18 |
+
{%- set _has_tools = (tools is defined and tools and tools is iterable and tools is not mapping) %}
|
| 19 |
+
{%- set _effort_raw = (reasoning_effort | string | lower) if reasoning_effort is defined and reasoning_effort is not none else _default_reasoning_effort %}
|
| 20 |
+
{%- set _initial_thinking = enable_thinking %}
|
| 21 |
+
{%- if _effort_raw in ('none', 'off') %}
|
| 22 |
+
{%- set _initial_thinking = false %}
|
| 23 |
+
{%- set _initial_effort = 'medium' %}
|
| 24 |
+
{%- elif _effort_raw in ('minimal', 'low') %}
|
| 25 |
+
{%- set _initial_effort = 'low' %}
|
| 26 |
+
{%- elif _effort_raw in ('high', 'xhigh', 'max', 'ultracode', 'extreme') %}
|
| 27 |
+
{%- set _initial_effort = 'xhigh' %}
|
| 28 |
+
{%- else %}
|
| 29 |
+
{%- set _initial_effort = 'medium' %}
|
| 30 |
+
{%- endif %}
|
| 31 |
+
{%- set ns_state = namespace(thinking=_initial_thinking, effort=_initial_effort) %}
|
| 32 |
+
{%- if auto_disable_thinking_with_tools and _has_tools %}
|
| 33 |
+
{%- set ns_state.thinking = false %}
|
| 34 |
+
{%- endif %}
|
| 35 |
+
{%- for msg in messages %}
|
| 36 |
+
{%- if msg.role == 'system' or msg.role == 'developer' or msg.role == 'user' %}
|
| 37 |
+
{%- if msg.content is string %}
|
| 38 |
+
{%- if '<|think_off|>' in msg.content %}
|
| 39 |
+
{%- set ns_state.thinking = false %}
|
| 40 |
+
{%- elif '<|think_on|>' in msg.content %}
|
| 41 |
+
{%- set ns_state.thinking = true %}
|
| 42 |
+
{%- elif '<|think_xhigh|>' in msg.content or '<|think_high|>' in msg.content or '<|think_ultracode|>' in msg.content or '<|think_extreme|>' in msg.content or '<|think_max|>' in msg.content %}
|
| 43 |
+
{%- set ns_state.thinking = true %}
|
| 44 |
+
{%- set ns_state.effort = 'xhigh' %}
|
| 45 |
+
{%- elif '<|think_low|>' in msg.content or '<|think_minimal|>' in msg.content %}
|
| 46 |
+
{%- set ns_state.thinking = true %}
|
| 47 |
+
{%- set ns_state.effort = 'low' %}
|
| 48 |
+
{%- elif '<|think_medium|>' in msg.content %}
|
| 49 |
+
{%- set ns_state.thinking = true %}
|
| 50 |
+
{%- set ns_state.effort = 'medium' %}
|
| 51 |
+
{%- endif %}
|
| 52 |
+
{%- elif msg.content is iterable and msg.content is not mapping %}
|
| 53 |
+
{%- for item in msg.content %}
|
| 54 |
+
{%- if item is string %}
|
| 55 |
+
{%- set _item_text = item %}
|
| 56 |
+
{%- elif item is mapping and 'text' in item and item.text is string %}
|
| 57 |
+
{%- set _item_text = item.text %}
|
| 58 |
+
{%- else %}
|
| 59 |
+
{%- set _item_text = '' %}
|
| 60 |
+
{%- endif %}
|
| 61 |
+
{%- if _item_text %}
|
| 62 |
+
{%- if '<|think_off|>' in _item_text %}
|
| 63 |
+
{%- set ns_state.thinking = false %}
|
| 64 |
+
{%- elif '<|think_on|>' in _item_text %}
|
| 65 |
+
{%- set ns_state.thinking = true %}
|
| 66 |
+
{%- elif '<|think_xhigh|>' in _item_text or '<|think_high|>' in _item_text or '<|think_ultracode|>' in _item_text or '<|think_extreme|>' in _item_text or '<|think_max|>' in _item_text %}
|
| 67 |
+
{%- set ns_state.thinking = true %}
|
| 68 |
+
{%- set ns_state.effort = 'xhigh' %}
|
| 69 |
+
{%- elif '<|think_low|>' in _item_text or '<|think_minimal|>' in _item_text %}
|
| 70 |
+
{%- set ns_state.thinking = true %}
|
| 71 |
+
{%- set ns_state.effort = 'low' %}
|
| 72 |
+
{%- elif '<|think_medium|>' in _item_text %}
|
| 73 |
+
{%- set ns_state.thinking = true %}
|
| 74 |
+
{%- set ns_state.effort = 'medium' %}
|
| 75 |
+
{%- endif %}
|
| 76 |
+
{%- endif %}
|
| 77 |
+
{%- endfor %}
|
| 78 |
+
{%- endif %}
|
| 79 |
+
{%- endif %}
|
| 80 |
+
{%- endfor %}
|
| 81 |
+
{%- set reasoning_instructions = '' %}
|
| 82 |
+
{%- if ns_state.thinking %}
|
| 83 |
+
{%- if ns_state.effort == 'xhigh' %}
|
| 84 |
+
{%- set reasoning_instructions = 'Reasoning effort is set to xhigh. Please think carefully through the task, validate key assumptions, consider plausible alternatives, and prioritize correctness, consistency, and clarity in the final answer.' %}
|
| 85 |
+
{%- elif ns_state.effort == 'low' %}
|
| 86 |
+
{%- set reasoning_instructions = 'Reasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration.' %}
|
| 87 |
+
{%- endif %}
|
| 88 |
+
{%- endif %}
|
| 89 |
+
{%- macro render_content(content, do_vision_count, is_system_content=false) %}
|
| 90 |
+
{%- if content is string %}
|
| 91 |
+
{{- content }}
|
| 92 |
+
{%- elif content is iterable and content is not mapping %}
|
| 93 |
+
{%- for item in content %}
|
| 94 |
+
{%- if item is mapping %}
|
| 95 |
+
{%- if item.type == 'image' or 'image' in item or 'image_url' in item %}
|
| 96 |
+
{%- if is_system_content %}
|
| 97 |
+
{{- raise_exception('System message cannot contain images.') }}
|
| 98 |
+
{%- endif %}
|
| 99 |
+
{%- if do_vision_count %}
|
| 100 |
+
{%- set image_count.value = image_count.value + 1 %}
|
| 101 |
+
{%- endif %}
|
| 102 |
+
{%- if add_vision_id %}
|
| 103 |
+
{{- 'Picture ' ~ image_count.value ~ ': ' }}
|
| 104 |
+
{%- endif %}
|
| 105 |
+
{{- '<|vision_start|><|image_pad|><|vision_end|>' }}
|
| 106 |
+
{%- elif item.type == 'video' or 'video' in item %}
|
| 107 |
+
{%- if is_system_content %}
|
| 108 |
+
{{- raise_exception('System message cannot contain videos.') }}
|
| 109 |
+
{%- endif %}
|
| 110 |
+
{%- if do_vision_count %}
|
| 111 |
+
{%- set video_count.value = video_count.value + 1 %}
|
| 112 |
+
{%- endif %}
|
| 113 |
+
{%- if add_vision_id %}
|
| 114 |
+
{{- 'Video ' ~ video_count.value ~ ': ' }}
|
| 115 |
+
{%- endif %}
|
| 116 |
+
{{- '<|vision_start|><|video_pad|><|vision_end|>' }}
|
| 117 |
+
{%- elif 'text' in item %}
|
| 118 |
+
{{- item.text }}
|
| 119 |
+
{%- else %}
|
| 120 |
+
{{- raise_exception('Unexpected item type in content.') }}
|
| 121 |
+
{%- endif %}
|
| 122 |
+
{%- else %}
|
| 123 |
+
{{- item | string }}
|
| 124 |
+
{%- endif %}
|
| 125 |
+
{%- endfor %}
|
| 126 |
+
{%- elif content is none or content is undefined %}
|
| 127 |
+
{{- '' }}
|
| 128 |
+
{%- else %}
|
| 129 |
+
{{- raise_exception('Unexpected content type.') }}
|
| 130 |
+
{%- endif %}
|
| 131 |
+
{%- endmacro %}
|
| 132 |
+
{%- if not messages %}
|
| 133 |
+
{{- raise_exception('No messages provided.') }}
|
| 134 |
+
{%- endif %}
|
| 135 |
+
{%- set head = namespace(count=0, seen_non_system=false) %}
|
| 136 |
+
{%- for message in messages %}
|
| 137 |
+
{%- set _is_sys = (message.role == 'system' or message.role == 'developer') %}
|
| 138 |
+
{%- if _is_sys and not head.seen_non_system %}
|
| 139 |
+
{%- set head.count = head.count + 1 %}
|
| 140 |
+
{%- else %}
|
| 141 |
+
{%- set head.seen_non_system = true %}
|
| 142 |
+
{%- endif %}
|
| 143 |
+
{%- endfor %}
|
| 144 |
+
{%- set sys_state = namespace(content='') %}
|
| 145 |
+
{%- for message in messages[:head.count] %}
|
| 146 |
+
{%- set _part = render_content(message.content, false, true) | trim %}
|
| 147 |
+
{%- if '<|think_off|>' in _part %}{%- set _part = _part.split('<|think_off|>') | join('') | trim %}{%- endif %}
|
| 148 |
+
{%- if '<|think_on|>' in _part %}{%- set _part = _part.split('<|think_on|>') | join('') | trim %}{%- endif %}
|
| 149 |
+
{%- if '<|think_xhigh|>' in _part %}{%- set _part = _part.split('<|think_xhigh|>') | join('') | trim %}{%- endif %}
|
| 150 |
+
{%- if '<|think_high|>' in _part %}{%- set _part = _part.split('<|think_high|>') | join('') | trim %}{%- endif %}
|
| 151 |
+
{%- if '<|think_ultracode|>' in _part %}{%- set _part = _part.split('<|think_ultracode|>') | join('') | trim %}{%- endif %}
|
| 152 |
+
{%- if '<|think_extreme|>' in _part %}{%- set _part = _part.split('<|think_extreme|>') | join('') | trim %}{%- endif %}
|
| 153 |
+
{%- if '<|think_max|>' in _part %}{%- set _part = _part.split('<|think_max|>') | join('') | trim %}{%- endif %}
|
| 154 |
+
{%- if '<|think_medium|>' in _part %}{%- set _part = _part.split('<|think_medium|>') | join('') | trim %}{%- endif %}
|
| 155 |
+
{%- if '<|think_low|>' in _part %}{%- set _part = _part.split('<|think_low|>') | join('') | trim %}{%- endif %}
|
| 156 |
+
{%- if '<|think_minimal|>' in _part %}{%- set _part = _part.split('<|think_minimal|>') | join('') | trim %}{%- endif %}
|
| 157 |
+
{%- if _part %}
|
| 158 |
+
{%- if sys_state.content %}
|
| 159 |
+
{%- set sys_state.content = sys_state.content ~ '\n\n' ~ _part %}
|
| 160 |
+
{%- else %}
|
| 161 |
+
{%- set sys_state.content = _part %}
|
| 162 |
+
{%- endif %}
|
| 163 |
+
{%- endif %}
|
| 164 |
+
{%- endfor %}
|
| 165 |
+
{%- set _sc = sys_state.content %}
|
| 166 |
+
{%- set _msgs = messages[head.count:] %}
|
| 167 |
+
{%- if _has_tools %}
|
| 168 |
+
{{- '<|im_start|>system\n' }}
|
| 169 |
+
{%- if reasoning_instructions %}
|
| 170 |
+
{{- reasoning_instructions + '\n\n' }}
|
| 171 |
+
{%- endif %}
|
| 172 |
+
{{- '# Tools\n\nYou have access to the following functions:\n\n<tools>' }}
|
| 173 |
+
{%- for tool in tools %}
|
| 174 |
+
{{- '\n' }}
|
| 175 |
+
{{- tool | tojson }}
|
| 176 |
+
{%- endfor %}
|
| 177 |
+
{{- '\n</tools>' }}
|
| 178 |
+
{%- if _tool_format == 'json' %}
|
| 179 |
+
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<think>\nBrief explanation of tool call\n</think>\n<tool_call>\n{"name": "example_function_name", "arguments": {"example_parameter_1": "value_1", "example_parameter_2": "This is the value for the second parameter"}}\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- 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.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n- Function calls MUST follow the specified format: a single JSON object with "name" and "arguments" keys inside <tool_call></tool_call> XML tags.\n- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY after thinking, with NO conversational text before it.\n- The <tool_call> tag MUST be at the very beginning of a new line, with NO spaces or indentation before it.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}
|
| 180 |
+
{%- else %}
|
| 181 |
+
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<think>\nBrief explanation of tool call\n</think>\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- 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.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags.\n- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY after thinking, with NO conversational text before it.\n- The <tool_call> and <function> tags MUST be at the very beginning of a new line, with NO spaces or indentation before them.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}
|
| 182 |
+
{%- endif %}
|
| 183 |
+
{%- if _sc %}
|
| 184 |
+
{{- '\n\n' + _sc }}
|
| 185 |
+
{%- endif %}
|
| 186 |
+
{{- '<|im_end|>\n' }}
|
| 187 |
+
{%- else %}
|
| 188 |
+
{%- if _sc %}
|
| 189 |
+
{{- '<|im_start|>system\n' + (reasoning_instructions + '\n\n' if reasoning_instructions else '') + _sc + '<|im_end|>\n' }}
|
| 190 |
+
{%- elif reasoning_instructions %}
|
| 191 |
+
{{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }}
|
| 192 |
+
{%- endif %}
|
| 193 |
+
{%- endif %}
|
| 194 |
+
{%- set _last_idx = _msgs | length - 1 %}
|
| 195 |
+
{%- set ns = namespace(multi_step_tool=true, last_query_index=_last_idx) %}
|
| 196 |
+
{%- for message in _msgs[::-1] %}
|
| 197 |
+
{%- set index = (_msgs | length - 1) - loop.index0 %}
|
| 198 |
+
{%- if ns.multi_step_tool and message.role == 'user' %}
|
| 199 |
+
{%- set _rc = render_content(message.content, false) | trim %}
|
| 200 |
+
{%- if not (_rc.startswith('<tool_response>') and _rc.endswith('</tool_response>')) %}
|
| 201 |
+
{%- set ns.multi_step_tool = false %}
|
| 202 |
+
{%- set ns.last_query_index = index %}
|
| 203 |
+
{%- endif %}
|
| 204 |
+
{%- endif %}
|
| 205 |
+
{%- endfor %}
|
| 206 |
+
{%- if ns.multi_step_tool %}
|
| 207 |
+
{%- if _last_idx > 50 %}
|
| 208 |
+
{%- set ns.last_query_index = _last_idx %}
|
| 209 |
+
{%- else %}
|
| 210 |
+
{%- set ns.last_query_index = 0 %}
|
| 211 |
+
{%- endif %}
|
| 212 |
+
{%- endif %}
|
| 213 |
+
{%- set ns2 = namespace(prev_role='', consecutive_failures=0) %}
|
| 214 |
+
{%- for message in _msgs %}
|
| 215 |
+
{%- set is_system = (message.role == "system" or message.role == "developer") %}
|
| 216 |
+
{%- set content = render_content(message.content, true, is_system) | trim %}
|
| 217 |
+
{%- if is_system or message.role == 'user' %}
|
| 218 |
+
{%- if '<|think_off|>' in content %}{%- set content = content.split('<|think_off|>') | join('') | trim %}{%- endif %}
|
| 219 |
+
{%- if '<|think_on|>' in content %}{%- set content = content.split('<|think_on|>') | join('') | trim %}{%- endif %}
|
| 220 |
+
{%- if '<|think_xhigh|>' in content %}{%- set content = content.split('<|think_xhigh|>') | join('') | trim %}{%- endif %}
|
| 221 |
+
{%- if '<|think_high|>' in content %}{%- set content = content.split('<|think_high|>') | join('') | trim %}{%- endif %}
|
| 222 |
+
{%- if '<|think_ultracode|>' in content %}{%- set content = content.split('<|think_ultracode|>') | join('') | trim %}{%- endif %}
|
| 223 |
+
{%- if '<|think_extreme|>' in content %}{%- set content = content.split('<|think_extreme|>') | join('') | trim %}{%- endif %}
|
| 224 |
+
{%- if '<|think_max|>' in content %}{%- set content = content.split('<|think_max|>') | join('') | trim %}{%- endif %}
|
| 225 |
+
{%- if '<|think_medium|>' in content %}{%- set content = content.split('<|think_medium|>') | join('') | trim %}{%- endif %}
|
| 226 |
+
{%- if '<|think_low|>' in content %}{%- set content = content.split('<|think_low|>') | join('') | trim %}{%- endif %}
|
| 227 |
+
{%- if '<|think_minimal|>' in content %}{%- set content = content.split('<|think_minimal|>') | join('') | trim %}{%- endif %}
|
| 228 |
+
{%- endif %}
|
| 229 |
+
{%- if is_system %}
|
| 230 |
+
{{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
|
| 231 |
+
{%- elif message.role == 'user' %}
|
| 232 |
+
{%- set ns2.consecutive_failures = 0 %}
|
| 233 |
+
{{- '<|im_start|>user\n' + content + '<|im_end|>\n' }}
|
| 234 |
+
{%- elif message.role == 'assistant' %}
|
| 235 |
+
{%- set reasoning_content = '' %}
|
| 236 |
+
{%- set _explicit_reasoning = '' %}
|
| 237 |
+
{%- if message.reasoning_content is defined and message.reasoning_content is not none %}
|
| 238 |
+
{%- if message.reasoning_content is string %}
|
| 239 |
+
{%- set _explicit_reasoning = message.reasoning_content %}
|
| 240 |
+
{%- else %}
|
| 241 |
+
{%- set _explicit_reasoning = message.reasoning_content | string %}
|
| 242 |
+
{%- endif %}
|
| 243 |
+
{%- elif message.thinking is defined and message.thinking is not none %}
|
| 244 |
+
{%- if message.thinking is string %}
|
| 245 |
+
{%- set _explicit_reasoning = message.thinking %}
|
| 246 |
+
{%- else %}
|
| 247 |
+
{%- set _explicit_reasoning = message.thinking | string %}
|
| 248 |
+
{%- endif %}
|
| 249 |
+
{%- elif message.reasoning is defined and message.reasoning is not none %}
|
| 250 |
+
{%- if message.reasoning is string %}
|
| 251 |
+
{%- set _explicit_reasoning = message.reasoning %}
|
| 252 |
+
{%- else %}
|
| 253 |
+
{%- set _explicit_reasoning = message.reasoning | string %}
|
| 254 |
+
{%- endif %}
|
| 255 |
+
{%- endif %}
|
| 256 |
+
{%- if _explicit_reasoning %}
|
| 257 |
+
{%- set _lead_end = '' %}
|
| 258 |
+
{%- if content.startswith('<think>') and '</think>' in content %}
|
| 259 |
+
{%- set _lead_end = '</think>' %}
|
| 260 |
+
{%- elif content.startswith('<thinking>') and '</thinking>' in content %}
|
| 261 |
+
{%- set _lead_end = '</thinking>' %}
|
| 262 |
+
{%- elif content.startswith('</think>') %}
|
| 263 |
+
{%- set _lead_end = '</think>' %}
|
| 264 |
+
{%- elif content.startswith('</thinking>') %}
|
| 265 |
+
{%- set _lead_end = '</thinking>' %}
|
| 266 |
+
{%- endif %}
|
| 267 |
+
{%- if _lead_end %}
|
| 268 |
+
{%- set content = content.split(_lead_end)[-1].lstrip('\n') %}
|
| 269 |
+
{%- endif %}
|
| 270 |
+
{%- set reasoning_content = _explicit_reasoning %}
|
| 271 |
+
{%- else %}
|
| 272 |
+
{%- set _think_end = '' %}
|
| 273 |
+
{%- if content.startswith('</think>') %}
|
| 274 |
+
{%- set _think_end = '</think>' %}
|
| 275 |
+
{%- elif content.startswith('</thinking>') %}
|
| 276 |
+
{%- set _think_end = '</thinking>' %}
|
| 277 |
+
{%- elif '\n</think>' in content %}
|
| 278 |
+
{%- set _think_end = '\n</think>' %}
|
| 279 |
+
{%- elif '\n</thinking>' in content %}
|
| 280 |
+
{%- set _think_end = '\n</thinking>' %}
|
| 281 |
+
{%- elif '\n</ think>' in content %}
|
| 282 |
+
{%- set _think_end = '\n</ think>' %}
|
| 283 |
+
{%- elif '\n</think >' in content %}
|
| 284 |
+
{%- set _think_end = '\n</think >' %}
|
| 285 |
+
{%- elif content.startswith('<think>') and '</think>' in content %}
|
| 286 |
+
{%- set _think_end = '</think>' %}
|
| 287 |
+
{%- elif content.startswith('<thinking>') and '</thinking>' in content %}
|
| 288 |
+
{%- set _think_end = '</thinking>' %}
|
| 289 |
+
{%- endif %}
|
| 290 |
+
{%- if _think_end %}
|
| 291 |
+
{%- if 'thinking' in _think_end %}
|
| 292 |
+
{%- set _think_start = '<thinking>' %}
|
| 293 |
+
{%- else %}
|
| 294 |
+
{%- set _think_start = '<think>' %}
|
| 295 |
+
{%- endif %}
|
| 296 |
+
{%- set reasoning_content = content.split(_think_end)[0].rstrip('\n') %}
|
| 297 |
+
{%- if _think_start in reasoning_content %}
|
| 298 |
+
{%- set reasoning_content = reasoning_content.split(_think_start)[-1].lstrip('\n') %}
|
| 299 |
+
{%- endif %}
|
| 300 |
+
{%- set content = content.split(_think_end)[-1].lstrip('\n') %}
|
| 301 |
+
{%- endif %}
|
| 302 |
+
{%- endif %}
|
| 303 |
+
{%- set reasoning_content = reasoning_content | trim %}
|
| 304 |
+
{%- if (_preserve_thinking or loop.index0 > ns.last_query_index) %}
|
| 305 |
+
{{- '<|im_start|>assistant\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
|
| 306 |
+
{%- else %}
|
| 307 |
+
{{- '<|im_start|>assistant\n' + content }}
|
| 308 |
+
{%- endif %}
|
| 309 |
+
{%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
|
| 310 |
+
{%- for tool_call in message.tool_calls %}
|
| 311 |
+
{%- if tool_call.function is defined and tool_call.function is not none %}
|
| 312 |
+
{%- set tc = tool_call.function %}
|
| 313 |
+
{%- else %}
|
| 314 |
+
{%- set tc = tool_call %}
|
| 315 |
+
{%- endif %}
|
| 316 |
+
{%- set tc_name = tc.name if (tc.name is defined and tc.name is not none) else '' %}
|
| 317 |
+
{%- if _tool_format == 'json' %}
|
| 318 |
+
{%- if loop.first %}
|
| 319 |
+
{%- if content | trim %}
|
| 320 |
+
{{- '\n\n' }}
|
| 321 |
+
{%- endif %}
|
| 322 |
+
{%- else %}
|
| 323 |
+
{{- '\n' }}
|
| 324 |
+
{%- endif %}
|
| 325 |
+
{%- set _args = '{}' %}
|
| 326 |
+
{%- if tc.arguments is defined and tc.arguments is not none %}
|
| 327 |
+
{%- if tc.arguments is mapping %}
|
| 328 |
+
{%- set _args = tc.arguments | tojson %}
|
| 329 |
+
{%- elif tc.arguments is string %}
|
| 330 |
+
{%- if tc.arguments %}
|
| 331 |
+
{%- set _args = tc.arguments %}
|
| 332 |
+
{%- endif %}
|
| 333 |
+
{%- else %}
|
| 334 |
+
{%- set _args = tc.arguments | tojson %}
|
| 335 |
+
{%- endif %}
|
| 336 |
+
{%- endif %}
|
| 337 |
+
{{- '<tool_call>\n{"name": ' }}{{- tc_name | tojson }}{{- ', "arguments": ' }}{{- _args }}{{- '}\n</tool_call>' }}
|
| 338 |
+
{%- else %}
|
| 339 |
+
{%- if loop.first %}
|
| 340 |
+
{%- if content | trim %}
|
| 341 |
+
{{- '\n\n<tool_call>\n<function=' + tc_name + '>\n' }}
|
| 342 |
+
{%- else %}
|
| 343 |
+
{{- '<tool_call>\n<function=' + tc_name + '>\n' }}
|
| 344 |
+
{%- endif %}
|
| 345 |
+
{%- else %}
|
| 346 |
+
{{- '\n<tool_call>\n<function=' + tc_name + '>\n' }}
|
| 347 |
+
{%- endif %}
|
| 348 |
+
{%- if tc.arguments is defined and tc.arguments is not none %}
|
| 349 |
+
{%- if tc.arguments is mapping %}
|
| 350 |
+
{%- for args_name, args_value in tc.arguments.items() %}
|
| 351 |
+
{{- '<parameter=' + args_name + '>\n' }}
|
| 352 |
+
{%- if args_value is string %}
|
| 353 |
+
{%- set _av = args_value %}
|
| 354 |
+
{%- else %}
|
| 355 |
+
{%- set _av = args_value | tojson %}
|
| 356 |
+
{%- endif %}
|
| 357 |
+
{%- if max_tool_arg_chars > 0 and _av | length > max_tool_arg_chars %}
|
| 358 |
+
{{- _av[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_av | length | string) ~ ' chars]' }}
|
| 359 |
+
{%- else %}
|
| 360 |
+
{{- _av }}
|
| 361 |
+
{%- endif %}
|
| 362 |
+
{{- '\n</parameter>\n' }}
|
| 363 |
+
{%- endfor %}
|
| 364 |
+
{%- else %}
|
| 365 |
+
{%- if tc.arguments is string %}
|
| 366 |
+
{%- set _raw_args = tc.arguments %}
|
| 367 |
+
{%- else %}
|
| 368 |
+
{%- set _raw_args = tc.arguments | tojson %}
|
| 369 |
+
{%- endif %}
|
| 370 |
+
{%- if _raw_args %}
|
| 371 |
+
{%- if max_tool_arg_chars > 0 and _raw_args | length > max_tool_arg_chars %}
|
| 372 |
+
{{- _raw_args[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_raw_args | length | string) ~ ' chars]' }}
|
| 373 |
+
{%- else %}
|
| 374 |
+
{{- _raw_args }}
|
| 375 |
+
{%- endif %}
|
| 376 |
+
{%- endif %}
|
| 377 |
+
{%- endif %}
|
| 378 |
+
{%- endif %}
|
| 379 |
+
{{- '</function>\n</tool_call>' }}
|
| 380 |
+
{%- endif %}
|
| 381 |
+
{%- endfor %}
|
| 382 |
+
{%- endif %}
|
| 383 |
+
{{- '<|im_end|>\n' }}
|
| 384 |
+
{%- elif message.role == 'tool' %}
|
| 385 |
+
{%- set _content_lower = content | lower %}
|
| 386 |
+
{%- set _content_head = _content_lower[:120] %}
|
| 387 |
+
{%- set _is_code_or_grep = ('throw new ' in _content_lower or 'throw error' in _content_lower or 'console.error' in _content_lower or 'logger.error' in _content_lower or 'logging.error' in _content_lower or 'import ' in _content_head or 'def ' in _content_head or 'function ' in _content_head) %}
|
| 388 |
+
{%- set _exit_code_zero = ('exit code: 0' in _content_head or 'process exited with code 0' in _content_head) %}
|
| 389 |
+
{%- set _error_field_ok = ('"error": null' in _content_head or '"error":null' in _content_head or '"error": false' in _content_head or '"error":false' in _content_head or '"error": ""' in _content_head or '"error":""' in _content_head) %}
|
| 390 |
+
{%- set _strong_error = (('"error":' in _content_head and not _error_field_ok) or '"status": "error"' in _content_head or '"status":"error"' in _content_head or 'traceback (most recent call last):' in _content_head or 'command not found' in _content_head or 'invalid syntax' in _content_head or 'fatal:' in _content_head or (('exit code: ' in _content_head or 'process exited with code' in _content_head) and not _exit_code_zero) or _content_head.startswith('exception:') or _content_head.startswith('failed to ')) %}
|
| 391 |
+
{%- set _weak_error = ('error:' in _content_head or 'err!' in _content_head) %}
|
| 392 |
+
{%- set _weak_suppressed = ('$ ' in _content_head or 'took ' in _content_head or content | length >= 600) %}
|
| 393 |
+
{%- if not _is_code_or_grep and (_strong_error or (_weak_error and not _weak_suppressed)) %}
|
| 394 |
+
{%- set ns2.consecutive_failures = ns2.consecutive_failures + 1 %}
|
| 395 |
+
{%- else %}
|
| 396 |
+
{%- set ns2.consecutive_failures = 0 %}
|
| 397 |
+
{%- endif %}
|
| 398 |
+
{%- if ns2.prev_role != 'tool' %}
|
| 399 |
+
{{- '<|im_start|>user' }}
|
| 400 |
+
{%- endif %}
|
| 401 |
+
{%- if _tool_format != 'json' and max_tool_response_chars > 0 and content | length > max_tool_response_chars %}
|
| 402 |
+
{%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED - original length ' ~ (content | length | string) ~ ' chars]' %}
|
| 403 |
+
{%- endif %}
|
| 404 |
+
{{- '\n<tool_response>\n' + content }}
|
| 405 |
+
{%- if ns2.consecutive_failures >= 2 %}
|
| 406 |
+
{{- '\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.' }}
|
| 407 |
+
{%- elif ns2.consecutive_failures == 1 %}
|
| 408 |
+
{{- '\n\n⚠️ SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with completely corrected arguments.' }}
|
| 409 |
+
{%- endif %}
|
| 410 |
+
{{- '\n</tool_response>' }}
|
| 411 |
+
{%- if loop.last %}
|
| 412 |
+
{{- '<|im_end|>\n' }}
|
| 413 |
+
{%- else %}
|
| 414 |
+
{%- set _next_role = _msgs[loop.index0 + 1].role %}
|
| 415 |
+
{%- if _next_role != 'tool' %}
|
| 416 |
+
{{- '<|im_end|>\n' }}
|
| 417 |
+
{%- endif %}
|
| 418 |
+
{%- endif %}
|
| 419 |
+
{%- else %}
|
| 420 |
+
{{- '<|im_start|>user\n[' + message.role + ']: ' + content + '<|im_end|>\n' }}
|
| 421 |
+
{%- endif %}
|
| 422 |
+
{%- set ns2.prev_role = message.role %}
|
| 423 |
+
{%- endfor %}
|
| 424 |
+
{%- if add_generation_prompt %}
|
| 425 |
+
{{- '<|im_start|>assistant\n' }}
|
| 426 |
+
{%- if not ns_state.thinking %}
|
| 427 |
+
{{- '<think>\n\n</think>\n\n' }}
|
| 428 |
+
{%- else %}
|
| 429 |
+
{{- '<think>\n' }}
|
| 430 |
+
{%- endif %}
|
| 431 |
+
{%- endif %}
|
archive/v22.4_chat_template_oneline.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{%- set template_version = "qwen3.8-froggeric-v22.4" %}{%- set _tool_format = tool_call_format if tool_call_format is defined else 'xml' %}{%- set image_count = namespace(value=0) %}{%- set video_count = namespace(value=0) %}{%- set add_vision_id = add_vision_id if add_vision_id is defined else false %}{%- set enable_thinking = enable_thinking if enable_thinking is defined else true %}{%- set auto_disable_thinking_with_tools = auto_disable_thinking_with_tools if auto_disable_thinking_with_tools is defined else false %}{%- if preserve_reasoning is defined and preserve_reasoning is not none %}{%- set _preserve_thinking = preserve_reasoning %}{%- elif preserve_thinking is defined and preserve_thinking is not none %}{%- set _preserve_thinking = preserve_thinking %}{%- else %}{%- set _preserve_thinking = true %}{%- endif %}{%- set max_tool_arg_chars = max_tool_arg_chars if max_tool_arg_chars is defined else 0 %}{%- set max_tool_response_chars = max_tool_response_chars if max_tool_response_chars is defined else 0 %}{%- set _default_reasoning_effort = 'medium' %}{%- set _has_tools = (tools is defined and tools and tools is iterable and tools is not mapping) %}{%- set _effort_raw = (reasoning_effort | string | lower) if reasoning_effort is defined and reasoning_effort is not none else _default_reasoning_effort %}{%- set _initial_thinking = enable_thinking %}{%- if _effort_raw in ('none', 'off') %}{%- set _initial_thinking = false %}{%- set _initial_effort = 'medium' %}{%- elif _effort_raw in ('minimal', 'low') %}{%- set _initial_effort = 'low' %}{%- elif _effort_raw in ('high', 'xhigh', 'max', 'ultracode', 'extreme') %}{%- set _initial_effort = 'xhigh' %}{%- else %}{%- set _initial_effort = 'medium' %}{%- endif %}{%- set ns_state = namespace(thinking=_initial_thinking, effort=_initial_effort) %}{%- if auto_disable_thinking_with_tools and _has_tools %}{%- set ns_state.thinking = false %}{%- endif %}{%- for msg in messages %}{%- if msg.role == 'system' or msg.role == 'developer' or msg.role == 'user' %}{%- if msg.content is string %}{%- if '<|think_off|>' in msg.content %}{%- set ns_state.thinking = false %}{%- elif '<|think_on|>' in msg.content %}{%- set ns_state.thinking = true %}{%- elif '<|think_xhigh|>' in msg.content or '<|think_high|>' in msg.content or '<|think_ultracode|>' in msg.content or '<|think_extreme|>' in msg.content or '<|think_max|>' in msg.content %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'xhigh' %}{%- elif '<|think_low|>' in msg.content or '<|think_minimal|>' in msg.content %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'low' %}{%- elif '<|think_medium|>' in msg.content %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'medium' %}{%- endif %}{%- elif msg.content is iterable and msg.content is not mapping %}{%- for item in msg.content %}{%- if item is string %}{%- set _item_text = item %}{%- elif item is mapping and 'text' in item and item.text is string %}{%- set _item_text = item.text %}{%- else %}{%- set _item_text = '' %}{%- endif %}{%- if _item_text %}{%- if '<|think_off|>' in _item_text %}{%- set ns_state.thinking = false %}{%- elif '<|think_on|>' in _item_text %}{%- set ns_state.thinking = true %}{%- elif '<|think_xhigh|>' in _item_text or '<|think_high|>' in _item_text or '<|think_ultracode|>' in _item_text or '<|think_extreme|>' in _item_text or '<|think_max|>' in _item_text %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'xhigh' %}{%- elif '<|think_low|>' in _item_text or '<|think_minimal|>' in _item_text %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'low' %}{%- elif '<|think_medium|>' in _item_text %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'medium' %}{%- endif %}{%- endif %}{%- endfor %}{%- endif %}{%- endif %}{%- endfor %}{%- set reasoning_instructions = '' %}{%- if ns_state.thinking %}{%- if ns_state.effort == 'xhigh' %}{%- set reasoning_instructions = 'Reasoning effort is set to xhigh. Please think carefully through the task, validate key assumptions, consider plausible alternatives, and prioritize correctness, consistency, and clarity in the final answer.' %}{%- elif ns_state.effort == 'low' %}{%- set reasoning_instructions = 'Reasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration.' %}{%- endif %}{%- endif %}{%- macro render_content(content, do_vision_count, is_system_content=false) %}{%- if content is string %}{{- content }}{%- elif content is iterable and content is not mapping %}{%- for item in content %}{%- if item is mapping %}{%- if item.type == 'image' or 'image' in item or 'image_url' in item %}{%- if is_system_content %}{{- raise_exception('System message cannot contain images.') }}{%- endif %}{%- if do_vision_count %}{%- set image_count.value = image_count.value + 1 %}{%- endif %}{%- if add_vision_id %}{{- 'Picture ' ~ image_count.value ~ ': ' }}{%- endif %}{{- '<|vision_start|><|image_pad|><|vision_end|>' }}{%- elif item.type == 'video' or 'video' in item %}{%- if is_system_content %}{{- raise_exception('System message cannot contain videos.') }}{%- endif %}{%- if do_vision_count %}{%- set video_count.value = video_count.value + 1 %}{%- endif %}{%- if add_vision_id %}{{- 'Video ' ~ video_count.value ~ ': ' }}{%- endif %}{{- '<|vision_start|><|video_pad|><|vision_end|>' }}{%- elif 'text' in item %}{{- item.text }}{%- else %}{{- raise_exception('Unexpected item type in content.') }}{%- endif %}{%- else %}{{- item | string }}{%- endif %}{%- endfor %}{%- elif content is none or content is undefined %}{{- '' }}{%- else %}{{- raise_exception('Unexpected content type.') }}{%- endif %}{%- endmacro %}{%- if not messages %}{{- raise_exception('No messages provided.') }}{%- endif %}{%- set head = namespace(count=0, seen_non_system=false) %}{%- for message in messages %}{%- set _is_sys = (message.role == 'system' or message.role == 'developer') %}{%- if _is_sys and not head.seen_non_system %}{%- set head.count = head.count + 1 %}{%- else %}{%- set head.seen_non_system = true %}{%- endif %}{%- endfor %}{%- set sys_state = namespace(content='') %}{%- for message in messages[:head.count] %}{%- set _part = render_content(message.content, false, true) | trim %}{%- if '<|think_off|>' in _part %}{%- set _part = _part.split('<|think_off|>') | join('') | trim %}{%- endif %}{%- if '<|think_on|>' in _part %}{%- set _part = _part.split('<|think_on|>') | join('') | trim %}{%- endif %}{%- if '<|think_xhigh|>' in _part %}{%- set _part = _part.split('<|think_xhigh|>') | join('') | trim %}{%- endif %}{%- if '<|think_high|>' in _part %}{%- set _part = _part.split('<|think_high|>') | join('') | trim %}{%- endif %}{%- if '<|think_ultracode|>' in _part %}{%- set _part = _part.split('<|think_ultracode|>') | join('') | trim %}{%- endif %}{%- if '<|think_extreme|>' in _part %}{%- set _part = _part.split('<|think_extreme|>') | join('') | trim %}{%- endif %}{%- if '<|think_max|>' in _part %}{%- set _part = _part.split('<|think_max|>') | join('') | trim %}{%- endif %}{%- if '<|think_medium|>' in _part %}{%- set _part = _part.split('<|think_medium|>') | join('') | trim %}{%- endif %}{%- if '<|think_low|>' in _part %}{%- set _part = _part.split('<|think_low|>') | join('') | trim %}{%- endif %}{%- if '<|think_minimal|>' in _part %}{%- set _part = _part.split('<|think_minimal|>') | join('') | trim %}{%- endif %}{%- if _part %}{%- if sys_state.content %}{%- set sys_state.content = sys_state.content ~ '\n\n' ~ _part %}{%- else %}{%- set sys_state.content = _part %}{%- endif %}{%- endif %}{%- endfor %}{%- set _sc = sys_state.content %}{%- set _msgs = messages[head.count:] %}{%- if _has_tools %}{{- '<|im_start|>system\n' }}{%- if reasoning_instructions %}{{- reasoning_instructions + '\n\n' }}{%- endif %}{{- '# Tools\n\nYou have access to the following functions:\n\n<tools>' }}{%- for tool in tools %}{{- '\n' }}{{- tool | tojson }}{%- endfor %}{{- '\n</tools>' }}{%- if _tool_format == 'json' %}{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<think>\nBrief explanation of tool call\n</think>\n<tool_call>\n{"name": "example_function_name", "arguments": {"example_parameter_1": "value_1", "example_parameter_2": "This is the value for the second parameter"}}\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- 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.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n- Function calls MUST follow the specified format: a single JSON object with "name" and "arguments" keys inside <tool_call></tool_call> XML tags.\n- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY after thinking, with NO conversational text before it.\n- The <tool_call> tag MUST be at the very beginning of a new line, with NO spaces or indentation before it.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}{%- else %}{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<think>\nBrief explanation of tool call\n</think>\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- 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.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags.\n- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY after thinking, with NO conversational text before it.\n- The <tool_call> and <function> tags MUST be at the very beginning of a new line, with NO spaces or indentation before them.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}{%- endif %}{%- if _sc %}{{- '\n\n' + _sc }}{%- endif %}{{- '<|im_end|>\n' }}{%- else %}{%- if _sc %}{{- '<|im_start|>system\n' + (reasoning_instructions + '\n\n' if reasoning_instructions else '') + _sc + '<|im_end|>\n' }}{%- elif reasoning_instructions %}{{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }}{%- endif %}{%- endif %}{%- set _last_idx = _msgs | length - 1 %}{%- set ns = namespace(multi_step_tool=true, last_query_index=_last_idx) %}{%- for message in _msgs[::-1] %}{%- set index = (_msgs | length - 1) - loop.index0 %}{%- if ns.multi_step_tool and message.role == 'user' %}{%- set _rc = render_content(message.content, false) | trim %}{%- if not (_rc.startswith('<tool_response>') and _rc.endswith('</tool_response>')) %}{%- set ns.multi_step_tool = false %}{%- set ns.last_query_index = index %}{%- endif %}{%- endif %}{%- endfor %}{%- if ns.multi_step_tool %}{%- if _last_idx > 50 %}{%- set ns.last_query_index = _last_idx %}{%- else %}{%- set ns.last_query_index = 0 %}{%- endif %}{%- endif %}{%- set ns2 = namespace(prev_role='', consecutive_failures=0) %}{%- for message in _msgs %}{%- set is_system = (message.role == "system" or message.role == "developer") %}{%- set content = render_content(message.content, true, is_system) | trim %}{%- if is_system or message.role == 'user' %}{%- if '<|think_off|>' in content %}{%- set content = content.split('<|think_off|>') | join('') | trim %}{%- endif %}{%- if '<|think_on|>' in content %}{%- set content = content.split('<|think_on|>') | join('') | trim %}{%- endif %}{%- if '<|think_xhigh|>' in content %}{%- set content = content.split('<|think_xhigh|>') | join('') | trim %}{%- endif %}{%- if '<|think_high|>' in content %}{%- set content = content.split('<|think_high|>') | join('') | trim %}{%- endif %}{%- if '<|think_ultracode|>' in content %}{%- set content = content.split('<|think_ultracode|>') | join('') | trim %}{%- endif %}{%- if '<|think_extreme|>' in content %}{%- set content = content.split('<|think_extreme|>') | join('') | trim %}{%- endif %}{%- if '<|think_max|>' in content %}{%- set content = content.split('<|think_max|>') | join('') | trim %}{%- endif %}{%- if '<|think_medium|>' in content %}{%- set content = content.split('<|think_medium|>') | join('') | trim %}{%- endif %}{%- if '<|think_low|>' in content %}{%- set content = content.split('<|think_low|>') | join('') | trim %}{%- endif %}{%- if '<|think_minimal|>' in content %}{%- set content = content.split('<|think_minimal|>') | join('') | trim %}{%- endif %}{%- endif %}{%- if is_system %}{{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}{%- elif message.role == 'user' %}{%- set ns2.consecutive_failures = 0 %}{{- '<|im_start|>user\n' + content + '<|im_end|>\n' }}{%- elif message.role == 'assistant' %}{%- set reasoning_content = '' %}{%- set _explicit_reasoning = '' %}{%- if message.reasoning_content is defined and message.reasoning_content is not none %}{%- if message.reasoning_content is string %}{%- set _explicit_reasoning = message.reasoning_content %}{%- else %}{%- set _explicit_reasoning = message.reasoning_content | string %}{%- endif %}{%- elif message.thinking is defined and message.thinking is not none %}{%- if message.thinking is string %}{%- set _explicit_reasoning = message.thinking %}{%- else %}{%- set _explicit_reasoning = message.thinking | string %}{%- endif %}{%- elif message.reasoning is defined and message.reasoning is not none %}{%- if message.reasoning is string %}{%- set _explicit_reasoning = message.reasoning %}{%- else %}{%- set _explicit_reasoning = message.reasoning | string %}{%- endif %}{%- endif %}{%- if _explicit_reasoning %}{%- set _lead_end = '' %}{%- if content.startswith('<think>') and '</think>' in content %}{%- set _lead_end = '</think>' %}{%- elif content.startswith('<thinking>') and '</thinking>' in content %}{%- set _lead_end = '</thinking>' %}{%- elif content.startswith('</think>') %}{%- set _lead_end = '</think>' %}{%- elif content.startswith('</thinking>') %}{%- set _lead_end = '</thinking>' %}{%- endif %}{%- if _lead_end %}{%- set content = content.split(_lead_end)[-1].lstrip('\n') %}{%- endif %}{%- set reasoning_content = _explicit_reasoning %}{%- else %}{%- set _think_end = '' %}{%- if content.startswith('</think>') %}{%- set _think_end = '</think>' %}{%- elif content.startswith('</thinking>') %}{%- set _think_end = '</thinking>' %}{%- elif '\n</think>' in content %}{%- set _think_end = '\n</think>' %}{%- elif '\n</thinking>' in content %}{%- set _think_end = '\n</thinking>' %}{%- elif '\n</ think>' in content %}{%- set _think_end = '\n</ think>' %}{%- elif '\n</think >' in content %}{%- set _think_end = '\n</think >' %}{%- elif content.startswith('<think>') and '</think>' in content %}{%- set _think_end = '</think>' %}{%- elif content.startswith('<thinking>') and '</thinking>' in content %}{%- set _think_end = '</thinking>' %}{%- endif %}{%- if _think_end %}{%- if 'thinking' in _think_end %}{%- set _think_start = '<thinking>' %}{%- else %}{%- set _think_start = '<think>' %}{%- endif %}{%- set reasoning_content = content.split(_think_end)[0].rstrip('\n') %}{%- if _think_start in reasoning_content %}{%- set reasoning_content = reasoning_content.split(_think_start)[-1].lstrip('\n') %}{%- endif %}{%- set content = content.split(_think_end)[-1].lstrip('\n') %}{%- endif %}{%- endif %}{%- set reasoning_content = reasoning_content | trim %}{%- if (_preserve_thinking or loop.index0 > ns.last_query_index) %}{{- '<|im_start|>assistant\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}{%- else %}{{- '<|im_start|>assistant\n' + content }}{%- endif %}{%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}{%- for tool_call in message.tool_calls %}{%- if tool_call.function is defined and tool_call.function is not none %}{%- set tc = tool_call.function %}{%- else %}{%- set tc = tool_call %}{%- endif %}{%- set tc_name = tc.name if (tc.name is defined and tc.name is not none) else '' %}{%- if _tool_format == 'json' %}{%- if loop.first %}{%- if content | trim %}{{- '\n\n' }}{%- endif %}{%- else %}{{- '\n' }}{%- endif %}{%- set _args = '{}' %}{%- if tc.arguments is defined and tc.arguments is not none %}{%- if tc.arguments is mapping %}{%- set _args = tc.arguments | tojson %}{%- elif tc.arguments is string %}{%- if tc.arguments %}{%- set _args = tc.arguments %}{%- endif %}{%- else %}{%- set _args = tc.arguments | tojson %}{%- endif %}{%- endif %}{{- '<tool_call>\n{"name": ' }}{{- tc_name | tojson }}{{- ', "arguments": ' }}{{- _args }}{{- '}\n</tool_call>' }}{%- else %}{%- if loop.first %}{%- if content | trim %}{{- '\n\n<tool_call>\n<function=' + tc_name + '>\n' }}{%- else %}{{- '<tool_call>\n<function=' + tc_name + '>\n' }}{%- endif %}{%- else %}{{- '\n<tool_call>\n<function=' + tc_name + '>\n' }}{%- endif %}{%- if tc.arguments is defined and tc.arguments is not none %}{%- if tc.arguments is mapping %}{%- for args_name, args_value in tc.arguments.items() %}{{- '<parameter=' + args_name + '>\n' }}{%- if args_value is string %}{%- set _av = args_value %}{%- else %}{%- set _av = args_value | tojson %}{%- endif %}{%- if max_tool_arg_chars > 0 and _av | length > max_tool_arg_chars %}{{- _av[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_av | length | string) ~ ' chars]' }}{%- else %}{{- _av }}{%- endif %}{{- '\n</parameter>\n' }}{%- endfor %}{%- else %}{%- if tc.arguments is string %}{%- set _raw_args = tc.arguments %}{%- else %}{%- set _raw_args = tc.arguments | tojson %}{%- endif %}{%- if _raw_args %}{%- if max_tool_arg_chars > 0 and _raw_args | length > max_tool_arg_chars %}{{- _raw_args[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_raw_args | length | string) ~ ' chars]' }}{%- else %}{{- _raw_args }}{%- endif %}{%- endif %}{%- endif %}{%- endif %}{{- '</function>\n</tool_call>' }}{%- endif %}{%- endfor %}{%- endif %}{{- '<|im_end|>\n' }}{%- elif message.role == 'tool' %}{%- set _content_lower = content | lower %}{%- set _content_head = _content_lower[:120] %}{%- set _is_code_or_grep = ('throw new ' in _content_lower or 'throw error' in _content_lower or 'console.error' in _content_lower or 'logger.error' in _content_lower or 'logging.error' in _content_lower or 'import ' in _content_head or 'def ' in _content_head or 'function ' in _content_head) %}{%- set _exit_code_zero = ('exit code: 0' in _content_head or 'process exited with code 0' in _content_head) %}{%- set _error_field_ok = ('"error": null' in _content_head or '"error":null' in _content_head or '"error": false' in _content_head or '"error":false' in _content_head or '"error": ""' in _content_head or '"error":""' in _content_head) %}{%- set _strong_error = (('"error":' in _content_head and not _error_field_ok) or '"status": "error"' in _content_head or '"status":"error"' in _content_head or 'traceback (most recent call last):' in _content_head or 'command not found' in _content_head or 'invalid syntax' in _content_head or 'fatal:' in _content_head or (('exit code: ' in _content_head or 'process exited with code' in _content_head) and not _exit_code_zero) or _content_head.startswith('exception:') or _content_head.startswith('failed to ')) %}{%- set _weak_error = ('error:' in _content_head or 'err!' in _content_head) %}{%- set _weak_suppressed = ('$ ' in _content_head or 'took ' in _content_head or content | length >= 600) %}{%- if not _is_code_or_grep and (_strong_error or (_weak_error and not _weak_suppressed)) %}{%- set ns2.consecutive_failures = ns2.consecutive_failures + 1 %}{%- else %}{%- set ns2.consecutive_failures = 0 %}{%- endif %}{%- if ns2.prev_role != 'tool' %}{{- '<|im_start|>user' }}{%- endif %}{%- if _tool_format != 'json' and max_tool_response_chars > 0 and content | length > max_tool_response_chars %}{%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED - original length ' ~ (content | length | string) ~ ' chars]' %}{%- endif %}{{- '\n<tool_response>\n' + content }}{%- if ns2.consecutive_failures >= 2 %}{{- '\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.' }}{%- elif ns2.consecutive_failures == 1 %}{{- '\n\n⚠️ SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with completely corrected arguments.' }}{%- endif %}{{- '\n</tool_response>' }}{%- if loop.last %}{{- '<|im_end|>\n' }}{%- else %}{%- set _next_role = _msgs[loop.index0 + 1].role %}{%- if _next_role != 'tool' %}{{- '<|im_end|>\n' }}{%- endif %}{%- endif %}{%- else %}{{- '<|im_start|>user\n[' + message.role + ']: ' + content + '<|im_end|>\n' }}{%- endif %}{%- set ns2.prev_role = message.role %}{%- endfor %}{%- if add_generation_prompt %}{{- '<|im_start|>assistant\n' }}{%- if not ns_state.thinking %}{{- '<think>\n\n</think>\n\n' }}{%- else %}{{- '<think>\n' }}{%- endif %}{%- endif %}
|
chat_template.jinja
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
{%- set template_version = "qwen3.8-froggeric-v22.
|
| 2 |
{%- set _tool_format = tool_call_format if tool_call_format is defined else 'xml' %}
|
| 3 |
{%- set image_count = namespace(value=0) %}
|
| 4 |
{%- set video_count = namespace(value=0) %}
|
|
@@ -103,7 +103,7 @@
|
|
| 103 |
{{- 'Picture ' ~ image_count.value ~ ': ' }}
|
| 104 |
{%- endif %}
|
| 105 |
{{- '<|vision_start|><|image_pad|><|vision_end|>' }}
|
| 106 |
-
{%- elif item.type == 'video' or 'video' in item %}
|
| 107 |
{%- if is_system_content %}
|
| 108 |
{{- raise_exception('System message cannot contain videos.') }}
|
| 109 |
{%- endif %}
|
|
@@ -176,9 +176,37 @@
|
|
| 176 |
{%- endfor %}
|
| 177 |
{{- '\n</tools>' }}
|
| 178 |
{%- if _tool_format == 'json' %}
|
| 179 |
-
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
{%- else %}
|
| 181 |
-
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
{%- endif %}
|
| 183 |
{%- if _sc %}
|
| 184 |
{{- '\n\n' + _sc }}
|
|
@@ -398,7 +426,8 @@
|
|
| 398 |
{%- if ns2.prev_role != 'tool' %}
|
| 399 |
{{- '<|im_start|>user' }}
|
| 400 |
{%- endif %}
|
| 401 |
-
{%-
|
|
|
|
| 402 |
{%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED - original length ' ~ (content | length | string) ~ ' chars]' %}
|
| 403 |
{%- endif %}
|
| 404 |
{{- '\n<tool_response>\n' + content }}
|
|
|
|
| 1 |
+
{%- set template_version = "qwen3.8-froggeric-v22.5" %}
|
| 2 |
{%- set _tool_format = tool_call_format if tool_call_format is defined else 'xml' %}
|
| 3 |
{%- set image_count = namespace(value=0) %}
|
| 4 |
{%- set video_count = namespace(value=0) %}
|
|
|
|
| 103 |
{{- 'Picture ' ~ image_count.value ~ ': ' }}
|
| 104 |
{%- endif %}
|
| 105 |
{{- '<|vision_start|><|image_pad|><|vision_end|>' }}
|
| 106 |
+
{%- elif item.type == 'video' or 'video' in item or 'video_url' in item %}
|
| 107 |
{%- if is_system_content %}
|
| 108 |
{{- raise_exception('System message cannot contain videos.') }}
|
| 109 |
{%- endif %}
|
|
|
|
| 176 |
{%- endfor %}
|
| 177 |
{{- '\n</tools>' }}
|
| 178 |
{%- if _tool_format == 'json' %}
|
| 179 |
+
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}
|
| 180 |
+
{%- if ns_state.thinking %}
|
| 181 |
+
{{- '<think>\nBrief explanation of tool call\n</think>\n' }}
|
| 182 |
+
{%- endif %}
|
| 183 |
+
{{- '<tool_call>\n{"name": "example_function_name", "arguments": {"example_parameter_1": "value_1", "example_parameter_2": "This is the value for the second parameter"}}\n</tool_call>\n\n<IMPORTANT>\nReminder:\n' }}
|
| 184 |
+
{%- if ns_state.thinking %}
|
| 185 |
+
{{- '- 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.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n' }}
|
| 186 |
+
{%- endif %}
|
| 187 |
+
{{- '- Function calls MUST follow the specified format: a single JSON object with "name" and "arguments" keys inside <tool_call></tool_call> XML tags.\n' }}
|
| 188 |
+
{%- if ns_state.thinking %}
|
| 189 |
+
{{- '- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY after thinking, with NO conversational text before it.\n' }}
|
| 190 |
+
{%- else %}
|
| 191 |
+
{{- '- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY, with NO conversational text before it.\n' }}
|
| 192 |
+
{%- endif %}
|
| 193 |
+
{{- '- The <tool_call> tag MUST be at the very beginning of a new line, with NO spaces or indentation before it.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}
|
| 194 |
{%- else %}
|
| 195 |
+
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}
|
| 196 |
+
{%- if ns_state.thinking %}
|
| 197 |
+
{{- '<think>\nBrief explanation of tool call\n</think>\n' }}
|
| 198 |
+
{%- endif %}
|
| 199 |
+
{{- '<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n' }}
|
| 200 |
+
{%- if ns_state.thinking %}
|
| 201 |
+
{{- '- 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.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n' }}
|
| 202 |
+
{%- endif %}
|
| 203 |
+
{{- '- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags.\n' }}
|
| 204 |
+
{%- if ns_state.thinking %}
|
| 205 |
+
{{- '- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY after thinking, with NO conversational text before it.\n' }}
|
| 206 |
+
{%- else %}
|
| 207 |
+
{{- '- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY, with NO conversational text before it.\n' }}
|
| 208 |
+
{%- endif %}
|
| 209 |
+
{{- '- The <tool_call> and <function> tags MUST be at the very beginning of a new line, with NO spaces or indentation before them.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}
|
| 210 |
{%- endif %}
|
| 211 |
{%- if _sc %}
|
| 212 |
{{- '\n\n' + _sc }}
|
|
|
|
| 426 |
{%- if ns2.prev_role != 'tool' %}
|
| 427 |
{{- '<|im_start|>user' }}
|
| 428 |
{%- endif %}
|
| 429 |
+
{%- set _is_json_payload = (_tool_format == 'json' and content | trim | length > 0 and (content | trim)[:1] in ('{', '[')) %}
|
| 430 |
+
{%- if not _is_json_payload and max_tool_response_chars > 0 and content | length > max_tool_response_chars %}
|
| 431 |
{%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED - original length ' ~ (content | length | string) ~ ' chars]' %}
|
| 432 |
{%- endif %}
|
| 433 |
{{- '\n<tool_response>\n' + content }}
|
chat_template_oneline.txt
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
{%- set template_version = "qwen3.8-froggeric-v22.4" %}{%- set _tool_format = tool_call_format if tool_call_format is defined else 'xml' %}{%- set image_count = namespace(value=0) %}{%- set video_count = namespace(value=0) %}{%- set add_vision_id = add_vision_id if add_vision_id is defined else false %}{%- set enable_thinking = enable_thinking if enable_thinking is defined else true %}{%- set auto_disable_thinking_with_tools = auto_disable_thinking_with_tools if auto_disable_thinking_with_tools is defined else false %}{%- if preserve_reasoning is defined and preserve_reasoning is not none %}{%- set _preserve_thinking = preserve_reasoning %}{%- elif preserve_thinking is defined and preserve_thinking is not none %}{%- set _preserve_thinking = preserve_thinking %}{%- else %}{%- set _preserve_thinking = true %}{%- endif %}{%- set max_tool_arg_chars = max_tool_arg_chars if max_tool_arg_chars is defined else 0 %}{%- set max_tool_response_chars = max_tool_response_chars if max_tool_response_chars is defined else 0 %}{%- set _default_reasoning_effort = 'medium' %}{%- set _has_tools = (tools is defined and tools and tools is iterable and tools is not mapping) %}{%- set _effort_raw = (reasoning_effort | string | lower) if reasoning_effort is defined and reasoning_effort is not none else _default_reasoning_effort %}{%- set _initial_thinking = enable_thinking %}{%- if _effort_raw in ('none', 'off') %}{%- set _initial_thinking = false %}{%- set _initial_effort = 'medium' %}{%- elif _effort_raw in ('minimal', 'low') %}{%- set _initial_effort = 'low' %}{%- elif _effort_raw in ('high', 'xhigh', 'max', 'ultracode', 'extreme') %}{%- set _initial_effort = 'xhigh' %}{%- else %}{%- set _initial_effort = 'medium' %}{%- endif %}{%- set ns_state = namespace(thinking=_initial_thinking, effort=_initial_effort) %}{%- if auto_disable_thinking_with_tools and _has_tools %}{%- set ns_state.thinking = false %}{%- endif %}{%- for msg in messages %}{%- if msg.role == 'system' or msg.role == 'developer' or msg.role == 'user' %}{%- if msg.content is string %}{%- if '<|think_off|>' in msg.content %}{%- set ns_state.thinking = false %}{%- elif '<|think_on|>' in msg.content %}{%- set ns_state.thinking = true %}{%- elif '<|think_xhigh|>' in msg.content or '<|think_high|>' in msg.content or '<|think_ultracode|>' in msg.content or '<|think_extreme|>' in msg.content or '<|think_max|>' in msg.content %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'xhigh' %}{%- elif '<|think_low|>' in msg.content or '<|think_minimal|>' in msg.content %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'low' %}{%- elif '<|think_medium|>' in msg.content %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'medium' %}{%- endif %}{%- elif msg.content is iterable and msg.content is not mapping %}{%- for item in msg.content %}{%- if item is string %}{%- set _item_text = item %}{%- elif item is mapping and 'text' in item and item.text is string %}{%- set _item_text = item.text %}{%- else %}{%- set _item_text = '' %}{%- endif %}{%- if _item_text %}{%- if '<|think_off|>' in _item_text %}{%- set ns_state.thinking = false %}{%- elif '<|think_on|>' in _item_text %}{%- set ns_state.thinking = true %}{%- elif '<|think_xhigh|>' in _item_text or '<|think_high|>' in _item_text or '<|think_ultracode|>' in _item_text or '<|think_extreme|>' in _item_text or '<|think_max|>' in _item_text %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'xhigh' %}{%- elif '<|think_low|>' in _item_text or '<|think_minimal|>' in _item_text %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'low' %}{%- elif '<|think_medium|>' in _item_text %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'medium' %}{%- endif %}{%- endif %}{%- endfor %}{%- endif %}{%- endif %}{%- endfor %}{%- set reasoning_instructions = '' %}{%- if ns_state.thinking %}{%- if ns_state.effort == 'xhigh' %}{%- set reasoning_instructions = 'Reasoning effort is set to xhigh. Please think carefully through the task, validate key assumptions, consider plausible alternatives, and prioritize correctness, consistency, and clarity in the final answer.' %}{%- elif ns_state.effort == 'low' %}{%- set reasoning_instructions = 'Reasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration.' %}{%- endif %}{%- endif %}{%- macro render_content(content, do_vision_count, is_system_content=false) %}{%- if content is string %}{{- content }}{%- elif content is iterable and content is not mapping %}{%- for item in content %}{%- if item is mapping %}{%- if item.type == 'image' or 'image' in item or 'image_url' in item %}{%- if is_system_content %}{{- raise_exception('System message cannot contain images.') }}{%- endif %}{%- if do_vision_count %}{%- set image_count.value = image_count.value + 1 %}{%- endif %}{%- if add_vision_id %}{{- 'Picture ' ~ image_count.value ~ ': ' }}{%- endif %}{{- '<|vision_start|><|image_pad|><|vision_end|>' }}{%- elif item.type == 'video' or 'video' in item %}{%- if is_system_content %}{{- raise_exception('System message cannot contain videos.') }}{%- endif %}{%- if do_vision_count %}{%- set video_count.value = video_count.value + 1 %}{%- endif %}{%- if add_vision_id %}{{- 'Video ' ~ video_count.value ~ ': ' }}{%- endif %}{{- '<|vision_start|><|video_pad|><|vision_end|>' }}{%- elif 'text' in item %}{{- item.text }}{%- else %}{{- raise_exception('Unexpected item type in content.') }}{%- endif %}{%- else %}{{- item | string }}{%- endif %}{%- endfor %}{%- elif content is none or content is undefined %}{{- '' }}{%- else %}{{- raise_exception('Unexpected content type.') }}{%- endif %}{%- endmacro %}{%- if not messages %}{{- raise_exception('No messages provided.') }}{%- endif %}{%- set head = namespace(count=0, seen_non_system=false) %}{%- for message in messages %}{%- set _is_sys = (message.role == 'system' or message.role == 'developer') %}{%- if _is_sys and not head.seen_non_system %}{%- set head.count = head.count + 1 %}{%- else %}{%- set head.seen_non_system = true %}{%- endif %}{%- endfor %}{%- set sys_state = namespace(content='') %}{%- for message in messages[:head.count] %}{%- set _part = render_content(message.content, false, true) | trim %}{%- if '<|think_off|>' in _part %}{%- set _part = _part.split('<|think_off|>') | join('') | trim %}{%- endif %}{%- if '<|think_on|>' in _part %}{%- set _part = _part.split('<|think_on|>') | join('') | trim %}{%- endif %}{%- if '<|think_xhigh|>' in _part %}{%- set _part = _part.split('<|think_xhigh|>') | join('') | trim %}{%- endif %}{%- if '<|think_high|>' in _part %}{%- set _part = _part.split('<|think_high|>') | join('') | trim %}{%- endif %}{%- if '<|think_ultracode|>' in _part %}{%- set _part = _part.split('<|think_ultracode|>') | join('') | trim %}{%- endif %}{%- if '<|think_extreme|>' in _part %}{%- set _part = _part.split('<|think_extreme|>') | join('') | trim %}{%- endif %}{%- if '<|think_max|>' in _part %}{%- set _part = _part.split('<|think_max|>') | join('') | trim %}{%- endif %}{%- if '<|think_medium|>' in _part %}{%- set _part = _part.split('<|think_medium|>') | join('') | trim %}{%- endif %}{%- if '<|think_low|>' in _part %}{%- set _part = _part.split('<|think_low|>') | join('') | trim %}{%- endif %}{%- if '<|think_minimal|>' in _part %}{%- set _part = _part.split('<|think_minimal|>') | join('') | trim %}{%- endif %}{%- if _part %}{%- if sys_state.content %}{%- set sys_state.content = sys_state.content ~ '\n\n' ~ _part %}{%- else %}{%- set sys_state.content = _part %}{%- endif %}{%- endif %}{%- endfor %}{%- set _sc = sys_state.content %}{%- set _msgs = messages[head.count:] %}{%- if _has_tools %}{{- '<|im_start|>system\n' }}{%- if reasoning_instructions %}{{- reasoning_instructions + '\n\n' }}{%- endif %}{{- '# Tools\n\nYou have access to the following functions:\n\n<tools>' }}{%- for tool in tools %}{{- '\n' }}{{- tool | tojson }}{%- endfor %}{{- '\n</tools>' }}{%- if _tool_format == 'json' %}{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<think>\nBrief explanation of tool call\n</think>\n<tool_call>\n{"name": "example_function_name", "arguments": {"example_parameter_1": "value_1", "example_parameter_2": "This is the value for the second parameter"}}\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- 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.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n- Function calls MUST follow the specified format: a single JSON object with "name" and "arguments" keys inside <tool_call></tool_call> XML tags.\n- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY after thinking, with NO conversational text before it.\n- The <tool_call> tag MUST be at the very beginning of a new line, with NO spaces or indentation before it.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}{%- else %}{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<think>\nBrief explanation of tool call\n</think>\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- 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.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags.\n- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY after thinking, with NO conversational text before it.\n- The <tool_call> and <function> tags MUST be at the very beginning of a new line, with NO spaces or indentation before them.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}{%- endif %}{%- if _sc %}{{- '\n\n' + _sc }}{%- endif %}{{- '<|im_end|>\n' }}{%- else %}{%- if _sc %}{{- '<|im_start|>system\n' + (reasoning_instructions + '\n\n' if reasoning_instructions else '') + _sc + '<|im_end|>\n' }}{%- elif reasoning_instructions %}{{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }}{%- endif %}{%- endif %}{%- set _last_idx = _msgs | length - 1 %}{%- set ns = namespace(multi_step_tool=true, last_query_index=_last_idx) %}{%- for message in _msgs[::-1] %}{%- set index = (_msgs | length - 1) - loop.index0 %}{%- if ns.multi_step_tool and message.role == 'user' %}{%- set _rc = render_content(message.content, false) | trim %}{%- if not (_rc.startswith('<tool_response>') and _rc.endswith('</tool_response>')) %}{%- set ns.multi_step_tool = false %}{%- set ns.last_query_index = index %}{%- endif %}{%- endif %}{%- endfor %}{%- if ns.multi_step_tool %}{%- if _last_idx > 50 %}{%- set ns.last_query_index = _last_idx %}{%- else %}{%- set ns.last_query_index = 0 %}{%- endif %}{%- endif %}{%- set ns2 = namespace(prev_role='', consecutive_failures=0) %}{%- for message in _msgs %}{%- set is_system = (message.role == "system" or message.role == "developer") %}{%- set content = render_content(message.content, true, is_system) | trim %}{%- if is_system or message.role == 'user' %}{%- if '<|think_off|>' in content %}{%- set content = content.split('<|think_off|>') | join('') | trim %}{%- endif %}{%- if '<|think_on|>' in content %}{%- set content = content.split('<|think_on|>') | join('') | trim %}{%- endif %}{%- if '<|think_xhigh|>' in content %}{%- set content = content.split('<|think_xhigh|>') | join('') | trim %}{%- endif %}{%- if '<|think_high|>' in content %}{%- set content = content.split('<|think_high|>') | join('') | trim %}{%- endif %}{%- if '<|think_ultracode|>' in content %}{%- set content = content.split('<|think_ultracode|>') | join('') | trim %}{%- endif %}{%- if '<|think_extreme|>' in content %}{%- set content = content.split('<|think_extreme|>') | join('') | trim %}{%- endif %}{%- if '<|think_max|>' in content %}{%- set content = content.split('<|think_max|>') | join('') | trim %}{%- endif %}{%- if '<|think_medium|>' in content %}{%- set content = content.split('<|think_medium|>') | join('') | trim %}{%- endif %}{%- if '<|think_low|>' in content %}{%- set content = content.split('<|think_low|>') | join('') | trim %}{%- endif %}{%- if '<|think_minimal|>' in content %}{%- set content = content.split('<|think_minimal|>') | join('') | trim %}{%- endif %}{%- endif %}{%- if is_system %}{{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}{%- elif message.role == 'user' %}{%- set ns2.consecutive_failures = 0 %}{{- '<|im_start|>user\n' + content + '<|im_end|>\n' }}{%- elif message.role == 'assistant' %}{%- set reasoning_content = '' %}{%- set _explicit_reasoning = '' %}{%- if message.reasoning_content is defined and message.reasoning_content is not none %}{%- if message.reasoning_content is string %}{%- set _explicit_reasoning = message.reasoning_content %}{%- else %}{%- set _explicit_reasoning = message.reasoning_content | string %}{%- endif %}{%- elif message.thinking is defined and message.thinking is not none %}{%- if message.thinking is string %}{%- set _explicit_reasoning = message.thinking %}{%- else %}{%- set _explicit_reasoning = message.thinking | string %}{%- endif %}{%- elif message.reasoning is defined and message.reasoning is not none %}{%- if message.reasoning is string %}{%- set _explicit_reasoning = message.reasoning %}{%- else %}{%- set _explicit_reasoning = message.reasoning | string %}{%- endif %}{%- endif %}{%- if _explicit_reasoning %}{%- set _lead_end = '' %}{%- if content.startswith('<think>') and '</think>' in content %}{%- set _lead_end = '</think>' %}{%- elif content.startswith('<thinking>') and '</thinking>' in content %}{%- set _lead_end = '</thinking>' %}{%- elif content.startswith('</think>') %}{%- set _lead_end = '</think>' %}{%- elif content.startswith('</thinking>') %}{%- set _lead_end = '</thinking>' %}{%- endif %}{%- if _lead_end %}{%- set content = content.split(_lead_end)[-1].lstrip('\n') %}{%- endif %}{%- set reasoning_content = _explicit_reasoning %}{%- else %}{%- set _think_end = '' %}{%- if content.startswith('</think>') %}{%- set _think_end = '</think>' %}{%- elif content.startswith('</thinking>') %}{%- set _think_end = '</thinking>' %}{%- elif '\n</think>' in content %}{%- set _think_end = '\n</think>' %}{%- elif '\n</thinking>' in content %}{%- set _think_end = '\n</thinking>' %}{%- elif '\n</ think>' in content %}{%- set _think_end = '\n</ think>' %}{%- elif '\n</think >' in content %}{%- set _think_end = '\n</think >' %}{%- elif content.startswith('<think>') and '</think>' in content %}{%- set _think_end = '</think>' %}{%- elif content.startswith('<thinking>') and '</thinking>' in content %}{%- set _think_end = '</thinking>' %}{%- endif %}{%- if _think_end %}{%- if 'thinking' in _think_end %}{%- set _think_start = '<thinking>' %}{%- else %}{%- set _think_start = '<think>' %}{%- endif %}{%- set reasoning_content = content.split(_think_end)[0].rstrip('\n') %}{%- if _think_start in reasoning_content %}{%- set reasoning_content = reasoning_content.split(_think_start)[-1].lstrip('\n') %}{%- endif %}{%- set content = content.split(_think_end)[-1].lstrip('\n') %}{%- endif %}{%- endif %}{%- set reasoning_content = reasoning_content | trim %}{%- if (_preserve_thinking or loop.index0 > ns.last_query_index) %}{{- '<|im_start|>assistant\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}{%- else %}{{- '<|im_start|>assistant\n' + content }}{%- endif %}{%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}{%- for tool_call in message.tool_calls %}{%- if tool_call.function is defined and tool_call.function is not none %}{%- set tc = tool_call.function %}{%- else %}{%- set tc = tool_call %}{%- endif %}{%- set tc_name = tc.name if (tc.name is defined and tc.name is not none) else '' %}{%- if _tool_format == 'json' %}{%- if loop.first %}{%- if content | trim %}{{- '\n\n' }}{%- endif %}{%- else %}{{- '\n' }}{%- endif %}{%- set _args = '{}' %}{%- if tc.arguments is defined and tc.arguments is not none %}{%- if tc.arguments is mapping %}{%- set _args = tc.arguments | tojson %}{%- elif tc.arguments is string %}{%- if tc.arguments %}{%- set _args = tc.arguments %}{%- endif %}{%- else %}{%- set _args = tc.arguments | tojson %}{%- endif %}{%- endif %}{{- '<tool_call>\n{"name": ' }}{{- tc_name | tojson }}{{- ', "arguments": ' }}{{- _args }}{{- '}\n</tool_call>' }}{%- else %}{%- if loop.first %}{%- if content | trim %}{{- '\n\n<tool_call>\n<function=' + tc_name + '>\n' }}{%- else %}{{- '<tool_call>\n<function=' + tc_name + '>\n' }}{%- endif %}{%- else %}{{- '\n<tool_call>\n<function=' + tc_name + '>\n' }}{%- endif %}{%- if tc.arguments is defined and tc.arguments is not none %}{%- if tc.arguments is mapping %}{%- for args_name, args_value in tc.arguments.items() %}{{- '<parameter=' + args_name + '>\n' }}{%- if args_value is string %}{%- set _av = args_value %}{%- else %}{%- set _av = args_value | tojson %}{%- endif %}{%- if max_tool_arg_chars > 0 and _av | length > max_tool_arg_chars %}{{- _av[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_av | length | string) ~ ' chars]' }}{%- else %}{{- _av }}{%- endif %}{{- '\n</parameter>\n' }}{%- endfor %}{%- else %}{%- if tc.arguments is string %}{%- set _raw_args = tc.arguments %}{%- else %}{%- set _raw_args = tc.arguments | tojson %}{%- endif %}{%- if _raw_args %}{%- if max_tool_arg_chars > 0 and _raw_args | length > max_tool_arg_chars %}{{- _raw_args[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_raw_args | length | string) ~ ' chars]' }}{%- else %}{{- _raw_args }}{%- endif %}{%- endif %}{%- endif %}{%- endif %}{{- '</function>\n</tool_call>' }}{%- endif %}{%- endfor %}{%- endif %}{{- '<|im_end|>\n' }}{%- elif message.role == 'tool' %}{%- set _content_lower = content | lower %}{%- set _content_head = _content_lower[:120] %}{%- set _is_code_or_grep = ('throw new ' in _content_lower or 'throw error' in _content_lower or 'console.error' in _content_lower or 'logger.error' in _content_lower or 'logging.error' in _content_lower or 'import ' in _content_head or 'def ' in _content_head or 'function ' in _content_head) %}{%- set _exit_code_zero = ('exit code: 0' in _content_head or 'process exited with code 0' in _content_head) %}{%- set _error_field_ok = ('"error": null' in _content_head or '"error":null' in _content_head or '"error": false' in _content_head or '"error":false' in _content_head or '"error": ""' in _content_head or '"error":""' in _content_head) %}{%- set _strong_error = (('"error":' in _content_head and not _error_field_ok) or '"status": "error"' in _content_head or '"status":"error"' in _content_head or 'traceback (most recent call last):' in _content_head or 'command not found' in _content_head or 'invalid syntax' in _content_head or 'fatal:' in _content_head or (('exit code: ' in _content_head or 'process exited with code' in _content_head) and not _exit_code_zero) or _content_head.startswith('exception:') or _content_head.startswith('failed to ')) %}{%- set _weak_error = ('error:' in _content_head or 'err!' in _content_head) %}{%- set _weak_suppressed = ('$ ' in _content_head or 'took ' in _content_head or content | length >= 600) %}{%- if not _is_code_or_grep and (_strong_error or (_weak_error and not _weak_suppressed)) %}{%- set ns2.consecutive_failures = ns2.consecutive_failures + 1 %}{%- else %}{%- set ns2.consecutive_failures = 0 %}{%- endif %}{%- if ns2.prev_role != 'tool' %}{{- '<|im_start|>user' }}{%- endif %}{%- if _tool_format != 'json' and max_tool_response_chars > 0 and content | length > max_tool_response_chars %}{%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED - original length ' ~ (content | length | string) ~ ' chars]' %}{%- endif %}{{- '\n<tool_response>\n' + content }}{%- if ns2.consecutive_failures >= 2 %}{{- '\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.' }}{%- elif ns2.consecutive_failures == 1 %}{{- '\n\n⚠️ SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with completely corrected arguments.' }}{%- endif %}{{- '\n</tool_response>' }}{%- if loop.last %}{{- '<|im_end|>\n' }}{%- else %}{%- set _next_role = _msgs[loop.index0 + 1].role %}{%- if _next_role != 'tool' %}{{- '<|im_end|>\n' }}{%- endif %}{%- endif %}{%- else %}{{- '<|im_start|>user\n[' + message.role + ']: ' + content + '<|im_end|>\n' }}{%- endif %}{%- set ns2.prev_role = message.role %}{%- endfor %}{%- if add_generation_prompt %}{{- '<|im_start|>assistant\n' }}{%- if not ns_state.thinking %}{{- '<think>\n\n</think>\n\n' }}{%- else %}{{- '<think>\n' }}{%- endif %}{%- endif %}
|
|
|
|
| 1 |
+
{%- set template_version = "qwen3.8-froggeric-v22.5" %}{%- set _tool_format = tool_call_format if tool_call_format is defined else 'xml' %}{%- set image_count = namespace(value=0) %}{%- set video_count = namespace(value=0) %}{%- set add_vision_id = add_vision_id if add_vision_id is defined else false %}{%- set enable_thinking = enable_thinking if enable_thinking is defined else true %}{%- set auto_disable_thinking_with_tools = auto_disable_thinking_with_tools if auto_disable_thinking_with_tools is defined else false %}{%- if preserve_reasoning is defined and preserve_reasoning is not none %}{%- set _preserve_thinking = preserve_reasoning %}{%- elif preserve_thinking is defined and preserve_thinking is not none %}{%- set _preserve_thinking = preserve_thinking %}{%- else %}{%- set _preserve_thinking = true %}{%- endif %}{%- set max_tool_arg_chars = max_tool_arg_chars if max_tool_arg_chars is defined else 0 %}{%- set max_tool_response_chars = max_tool_response_chars if max_tool_response_chars is defined else 0 %}{%- set _default_reasoning_effort = 'medium' %}{%- set _has_tools = (tools is defined and tools and tools is iterable and tools is not mapping) %}{%- set _effort_raw = (reasoning_effort | string | lower) if reasoning_effort is defined and reasoning_effort is not none else _default_reasoning_effort %}{%- set _initial_thinking = enable_thinking %}{%- if _effort_raw in ('none', 'off') %}{%- set _initial_thinking = false %}{%- set _initial_effort = 'medium' %}{%- elif _effort_raw in ('minimal', 'low') %}{%- set _initial_effort = 'low' %}{%- elif _effort_raw in ('high', 'xhigh', 'max', 'ultracode', 'extreme') %}{%- set _initial_effort = 'xhigh' %}{%- else %}{%- set _initial_effort = 'medium' %}{%- endif %}{%- set ns_state = namespace(thinking=_initial_thinking, effort=_initial_effort) %}{%- if auto_disable_thinking_with_tools and _has_tools %}{%- set ns_state.thinking = false %}{%- endif %}{%- for msg in messages %}{%- if msg.role == 'system' or msg.role == 'developer' or msg.role == 'user' %}{%- if msg.content is string %}{%- if '<|think_off|>' in msg.content %}{%- set ns_state.thinking = false %}{%- elif '<|think_on|>' in msg.content %}{%- set ns_state.thinking = true %}{%- elif '<|think_xhigh|>' in msg.content or '<|think_high|>' in msg.content or '<|think_ultracode|>' in msg.content or '<|think_extreme|>' in msg.content or '<|think_max|>' in msg.content %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'xhigh' %}{%- elif '<|think_low|>' in msg.content or '<|think_minimal|>' in msg.content %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'low' %}{%- elif '<|think_medium|>' in msg.content %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'medium' %}{%- endif %}{%- elif msg.content is iterable and msg.content is not mapping %}{%- for item in msg.content %}{%- if item is string %}{%- set _item_text = item %}{%- elif item is mapping and 'text' in item and item.text is string %}{%- set _item_text = item.text %}{%- else %}{%- set _item_text = '' %}{%- endif %}{%- if _item_text %}{%- if '<|think_off|>' in _item_text %}{%- set ns_state.thinking = false %}{%- elif '<|think_on|>' in _item_text %}{%- set ns_state.thinking = true %}{%- elif '<|think_xhigh|>' in _item_text or '<|think_high|>' in _item_text or '<|think_ultracode|>' in _item_text or '<|think_extreme|>' in _item_text or '<|think_max|>' in _item_text %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'xhigh' %}{%- elif '<|think_low|>' in _item_text or '<|think_minimal|>' in _item_text %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'low' %}{%- elif '<|think_medium|>' in _item_text %}{%- set ns_state.thinking = true %}{%- set ns_state.effort = 'medium' %}{%- endif %}{%- endif %}{%- endfor %}{%- endif %}{%- endif %}{%- endfor %}{%- set reasoning_instructions = '' %}{%- if ns_state.thinking %}{%- if ns_state.effort == 'xhigh' %}{%- set reasoning_instructions = 'Reasoning effort is set to xhigh. Please think carefully through the task, validate key assumptions, consider plausible alternatives, and prioritize correctness, consistency, and clarity in the final answer.' %}{%- elif ns_state.effort == 'low' %}{%- set reasoning_instructions = 'Reasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration.' %}{%- endif %}{%- endif %}{%- macro render_content(content, do_vision_count, is_system_content=false) %}{%- if content is string %}{{- content }}{%- elif content is iterable and content is not mapping %}{%- for item in content %}{%- if item is mapping %}{%- if item.type == 'image' or 'image' in item or 'image_url' in item %}{%- if is_system_content %}{{- raise_exception('System message cannot contain images.') }}{%- endif %}{%- if do_vision_count %}{%- set image_count.value = image_count.value + 1 %}{%- endif %}{%- if add_vision_id %}{{- 'Picture ' ~ image_count.value ~ ': ' }}{%- endif %}{{- '<|vision_start|><|image_pad|><|vision_end|>' }}{%- elif item.type == 'video' or 'video' in item or 'video_url' in item %}{%- if is_system_content %}{{- raise_exception('System message cannot contain videos.') }}{%- endif %}{%- if do_vision_count %}{%- set video_count.value = video_count.value + 1 %}{%- endif %}{%- if add_vision_id %}{{- 'Video ' ~ video_count.value ~ ': ' }}{%- endif %}{{- '<|vision_start|><|video_pad|><|vision_end|>' }}{%- elif 'text' in item %}{{- item.text }}{%- else %}{{- raise_exception('Unexpected item type in content.') }}{%- endif %}{%- else %}{{- item | string }}{%- endif %}{%- endfor %}{%- elif content is none or content is undefined %}{{- '' }}{%- else %}{{- raise_exception('Unexpected content type.') }}{%- endif %}{%- endmacro %}{%- if not messages %}{{- raise_exception('No messages provided.') }}{%- endif %}{%- set head = namespace(count=0, seen_non_system=false) %}{%- for message in messages %}{%- set _is_sys = (message.role == 'system' or message.role == 'developer') %}{%- if _is_sys and not head.seen_non_system %}{%- set head.count = head.count + 1 %}{%- else %}{%- set head.seen_non_system = true %}{%- endif %}{%- endfor %}{%- set sys_state = namespace(content='') %}{%- for message in messages[:head.count] %}{%- set _part = render_content(message.content, false, true) | trim %}{%- if '<|think_off|>' in _part %}{%- set _part = _part.split('<|think_off|>') | join('') | trim %}{%- endif %}{%- if '<|think_on|>' in _part %}{%- set _part = _part.split('<|think_on|>') | join('') | trim %}{%- endif %}{%- if '<|think_xhigh|>' in _part %}{%- set _part = _part.split('<|think_xhigh|>') | join('') | trim %}{%- endif %}{%- if '<|think_high|>' in _part %}{%- set _part = _part.split('<|think_high|>') | join('') | trim %}{%- endif %}{%- if '<|think_ultracode|>' in _part %}{%- set _part = _part.split('<|think_ultracode|>') | join('') | trim %}{%- endif %}{%- if '<|think_extreme|>' in _part %}{%- set _part = _part.split('<|think_extreme|>') | join('') | trim %}{%- endif %}{%- if '<|think_max|>' in _part %}{%- set _part = _part.split('<|think_max|>') | join('') | trim %}{%- endif %}{%- if '<|think_medium|>' in _part %}{%- set _part = _part.split('<|think_medium|>') | join('') | trim %}{%- endif %}{%- if '<|think_low|>' in _part %}{%- set _part = _part.split('<|think_low|>') | join('') | trim %}{%- endif %}{%- if '<|think_minimal|>' in _part %}{%- set _part = _part.split('<|think_minimal|>') | join('') | trim %}{%- endif %}{%- if _part %}{%- if sys_state.content %}{%- set sys_state.content = sys_state.content ~ '\n\n' ~ _part %}{%- else %}{%- set sys_state.content = _part %}{%- endif %}{%- endif %}{%- endfor %}{%- set _sc = sys_state.content %}{%- set _msgs = messages[head.count:] %}{%- if _has_tools %}{{- '<|im_start|>system\n' }}{%- if reasoning_instructions %}{{- reasoning_instructions + '\n\n' }}{%- endif %}{{- '# Tools\n\nYou have access to the following functions:\n\n<tools>' }}{%- for tool in tools %}{{- '\n' }}{{- tool | tojson }}{%- endfor %}{{- '\n</tools>' }}{%- if _tool_format == 'json' %}{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}{%- if ns_state.thinking %}{{- '<think>\nBrief explanation of tool call\n</think>\n' }}{%- endif %}{{- '<tool_call>\n{"name": "example_function_name", "arguments": {"example_parameter_1": "value_1", "example_parameter_2": "This is the value for the second parameter"}}\n</tool_call>\n\n<IMPORTANT>\nReminder:\n' }}{%- if ns_state.thinking %}{{- '- 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.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n' }}{%- endif %}{{- '- Function calls MUST follow the specified format: a single JSON object with "name" and "arguments" keys inside <tool_call></tool_call> XML tags.\n' }}{%- if ns_state.thinking %}{{- '- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY after thinking, with NO conversational text before it.\n' }}{%- else %}{{- '- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY, with NO conversational text before it.\n' }}{%- endif %}{{- '- The <tool_call> tag MUST be at the very beginning of a new line, with NO spaces or indentation before it.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}{%- else %}{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}{%- if ns_state.thinking %}{{- '<think>\nBrief explanation of tool call\n</think>\n' }}{%- endif %}{{- '<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n' }}{%- if ns_state.thinking %}{{- '- 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.\n- ALL explanation and reasoning MUST be placed strictly inside the <think></think> block.\n' }}{%- endif %}{{- '- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags.\n' }}{%- if ns_state.thinking %}{{- '- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY after thinking, with NO conversational text before it.\n' }}{%- else %}{{- '- If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY, with NO conversational text before it.\n' }}{%- endif %}{{- '- The <tool_call> and <function> tags MUST be at the very beginning of a new line, with NO spaces or indentation before them.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}{%- endif %}{%- if _sc %}{{- '\n\n' + _sc }}{%- endif %}{{- '<|im_end|>\n' }}{%- else %}{%- if _sc %}{{- '<|im_start|>system\n' + (reasoning_instructions + '\n\n' if reasoning_instructions else '') + _sc + '<|im_end|>\n' }}{%- elif reasoning_instructions %}{{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }}{%- endif %}{%- endif %}{%- set _last_idx = _msgs | length - 1 %}{%- set ns = namespace(multi_step_tool=true, last_query_index=_last_idx) %}{%- for message in _msgs[::-1] %}{%- set index = (_msgs | length - 1) - loop.index0 %}{%- if ns.multi_step_tool and message.role == 'user' %}{%- set _rc = render_content(message.content, false) | trim %}{%- if not (_rc.startswith('<tool_response>') and _rc.endswith('</tool_response>')) %}{%- set ns.multi_step_tool = false %}{%- set ns.last_query_index = index %}{%- endif %}{%- endif %}{%- endfor %}{%- if ns.multi_step_tool %}{%- if _last_idx > 50 %}{%- set ns.last_query_index = _last_idx %}{%- else %}{%- set ns.last_query_index = 0 %}{%- endif %}{%- endif %}{%- set ns2 = namespace(prev_role='', consecutive_failures=0) %}{%- for message in _msgs %}{%- set is_system = (message.role == "system" or message.role == "developer") %}{%- set content = render_content(message.content, true, is_system) | trim %}{%- if is_system or message.role == 'user' %}{%- if '<|think_off|>' in content %}{%- set content = content.split('<|think_off|>') | join('') | trim %}{%- endif %}{%- if '<|think_on|>' in content %}{%- set content = content.split('<|think_on|>') | join('') | trim %}{%- endif %}{%- if '<|think_xhigh|>' in content %}{%- set content = content.split('<|think_xhigh|>') | join('') | trim %}{%- endif %}{%- if '<|think_high|>' in content %}{%- set content = content.split('<|think_high|>') | join('') | trim %}{%- endif %}{%- if '<|think_ultracode|>' in content %}{%- set content = content.split('<|think_ultracode|>') | join('') | trim %}{%- endif %}{%- if '<|think_extreme|>' in content %}{%- set content = content.split('<|think_extreme|>') | join('') | trim %}{%- endif %}{%- if '<|think_max|>' in content %}{%- set content = content.split('<|think_max|>') | join('') | trim %}{%- endif %}{%- if '<|think_medium|>' in content %}{%- set content = content.split('<|think_medium|>') | join('') | trim %}{%- endif %}{%- if '<|think_low|>' in content %}{%- set content = content.split('<|think_low|>') | join('') | trim %}{%- endif %}{%- if '<|think_minimal|>' in content %}{%- set content = content.split('<|think_minimal|>') | join('') | trim %}{%- endif %}{%- endif %}{%- if is_system %}{{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}{%- elif message.role == 'user' %}{%- set ns2.consecutive_failures = 0 %}{{- '<|im_start|>user\n' + content + '<|im_end|>\n' }}{%- elif message.role == 'assistant' %}{%- set reasoning_content = '' %}{%- set _explicit_reasoning = '' %}{%- if message.reasoning_content is defined and message.reasoning_content is not none %}{%- if message.reasoning_content is string %}{%- set _explicit_reasoning = message.reasoning_content %}{%- else %}{%- set _explicit_reasoning = message.reasoning_content | string %}{%- endif %}{%- elif message.thinking is defined and message.thinking is not none %}{%- if message.thinking is string %}{%- set _explicit_reasoning = message.thinking %}{%- else %}{%- set _explicit_reasoning = message.thinking | string %}{%- endif %}{%- elif message.reasoning is defined and message.reasoning is not none %}{%- if message.reasoning is string %}{%- set _explicit_reasoning = message.reasoning %}{%- else %}{%- set _explicit_reasoning = message.reasoning | string %}{%- endif %}{%- endif %}{%- if _explicit_reasoning %}{%- set _lead_end = '' %}{%- if content.startswith('<think>') and '</think>' in content %}{%- set _lead_end = '</think>' %}{%- elif content.startswith('<thinking>') and '</thinking>' in content %}{%- set _lead_end = '</thinking>' %}{%- elif content.startswith('</think>') %}{%- set _lead_end = '</think>' %}{%- elif content.startswith('</thinking>') %}{%- set _lead_end = '</thinking>' %}{%- endif %}{%- if _lead_end %}{%- set content = content.split(_lead_end)[-1].lstrip('\n') %}{%- endif %}{%- set reasoning_content = _explicit_reasoning %}{%- else %}{%- set _think_end = '' %}{%- if content.startswith('</think>') %}{%- set _think_end = '</think>' %}{%- elif content.startswith('</thinking>') %}{%- set _think_end = '</thinking>' %}{%- elif '\n</think>' in content %}{%- set _think_end = '\n</think>' %}{%- elif '\n</thinking>' in content %}{%- set _think_end = '\n</thinking>' %}{%- elif '\n</ think>' in content %}{%- set _think_end = '\n</ think>' %}{%- elif '\n</think >' in content %}{%- set _think_end = '\n</think >' %}{%- elif content.startswith('<think>') and '</think>' in content %}{%- set _think_end = '</think>' %}{%- elif content.startswith('<thinking>') and '</thinking>' in content %}{%- set _think_end = '</thinking>' %}{%- endif %}{%- if _think_end %}{%- if 'thinking' in _think_end %}{%- set _think_start = '<thinking>' %}{%- else %}{%- set _think_start = '<think>' %}{%- endif %}{%- set reasoning_content = content.split(_think_end)[0].rstrip('\n') %}{%- if _think_start in reasoning_content %}{%- set reasoning_content = reasoning_content.split(_think_start)[-1].lstrip('\n') %}{%- endif %}{%- set content = content.split(_think_end)[-1].lstrip('\n') %}{%- endif %}{%- endif %}{%- set reasoning_content = reasoning_content | trim %}{%- if (_preserve_thinking or loop.index0 > ns.last_query_index) %}{{- '<|im_start|>assistant\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}{%- else %}{{- '<|im_start|>assistant\n' + content }}{%- endif %}{%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}{%- for tool_call in message.tool_calls %}{%- if tool_call.function is defined and tool_call.function is not none %}{%- set tc = tool_call.function %}{%- else %}{%- set tc = tool_call %}{%- endif %}{%- set tc_name = tc.name if (tc.name is defined and tc.name is not none) else '' %}{%- if _tool_format == 'json' %}{%- if loop.first %}{%- if content | trim %}{{- '\n\n' }}{%- endif %}{%- else %}{{- '\n' }}{%- endif %}{%- set _args = '{}' %}{%- if tc.arguments is defined and tc.arguments is not none %}{%- if tc.arguments is mapping %}{%- set _args = tc.arguments | tojson %}{%- elif tc.arguments is string %}{%- if tc.arguments %}{%- set _args = tc.arguments %}{%- endif %}{%- else %}{%- set _args = tc.arguments | tojson %}{%- endif %}{%- endif %}{{- '<tool_call>\n{"name": ' }}{{- tc_name | tojson }}{{- ', "arguments": ' }}{{- _args }}{{- '}\n</tool_call>' }}{%- else %}{%- if loop.first %}{%- if content | trim %}{{- '\n\n<tool_call>\n<function=' + tc_name + '>\n' }}{%- else %}{{- '<tool_call>\n<function=' + tc_name + '>\n' }}{%- endif %}{%- else %}{{- '\n<tool_call>\n<function=' + tc_name + '>\n' }}{%- endif %}{%- if tc.arguments is defined and tc.arguments is not none %}{%- if tc.arguments is mapping %}{%- for args_name, args_value in tc.arguments.items() %}{{- '<parameter=' + args_name + '>\n' }}{%- if args_value is string %}{%- set _av = args_value %}{%- else %}{%- set _av = args_value | tojson %}{%- endif %}{%- if max_tool_arg_chars > 0 and _av | length > max_tool_arg_chars %}{{- _av[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_av | length | string) ~ ' chars]' }}{%- else %}{{- _av }}{%- endif %}{{- '\n</parameter>\n' }}{%- endfor %}{%- else %}{%- if tc.arguments is string %}{%- set _raw_args = tc.arguments %}{%- else %}{%- set _raw_args = tc.arguments | tojson %}{%- endif %}{%- if _raw_args %}{%- if max_tool_arg_chars > 0 and _raw_args | length > max_tool_arg_chars %}{{- _raw_args[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_raw_args | length | string) ~ ' chars]' }}{%- else %}{{- _raw_args }}{%- endif %}{%- endif %}{%- endif %}{%- endif %}{{- '</function>\n</tool_call>' }}{%- endif %}{%- endfor %}{%- endif %}{{- '<|im_end|>\n' }}{%- elif message.role == 'tool' %}{%- set _content_lower = content | lower %}{%- set _content_head = _content_lower[:120] %}{%- set _is_code_or_grep = ('throw new ' in _content_lower or 'throw error' in _content_lower or 'console.error' in _content_lower or 'logger.error' in _content_lower or 'logging.error' in _content_lower or 'import ' in _content_head or 'def ' in _content_head or 'function ' in _content_head) %}{%- set _exit_code_zero = ('exit code: 0' in _content_head or 'process exited with code 0' in _content_head) %}{%- set _error_field_ok = ('"error": null' in _content_head or '"error":null' in _content_head or '"error": false' in _content_head or '"error":false' in _content_head or '"error": ""' in _content_head or '"error":""' in _content_head) %}{%- set _strong_error = (('"error":' in _content_head and not _error_field_ok) or '"status": "error"' in _content_head or '"status":"error"' in _content_head or 'traceback (most recent call last):' in _content_head or 'command not found' in _content_head or 'invalid syntax' in _content_head or 'fatal:' in _content_head or (('exit code: ' in _content_head or 'process exited with code' in _content_head) and not _exit_code_zero) or _content_head.startswith('exception:') or _content_head.startswith('failed to ')) %}{%- set _weak_error = ('error:' in _content_head or 'err!' in _content_head) %}{%- set _weak_suppressed = ('$ ' in _content_head or 'took ' in _content_head or content | length >= 600) %}{%- if not _is_code_or_grep and (_strong_error or (_weak_error and not _weak_suppressed)) %}{%- set ns2.consecutive_failures = ns2.consecutive_failures + 1 %}{%- else %}{%- set ns2.consecutive_failures = 0 %}{%- endif %}{%- if ns2.prev_role != 'tool' %}{{- '<|im_start|>user' }}{%- endif %}{%- set _is_json_payload = (_tool_format == 'json' and content | trim | length > 0 and (content | trim)[:1] in ('{', '[')) %}{%- if not _is_json_payload and max_tool_response_chars > 0 and content | length > max_tool_response_chars %}{%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED - original length ' ~ (content | length | string) ~ ' chars]' %}{%- endif %}{{- '\n<tool_response>\n' + content }}{%- if ns2.consecutive_failures >= 2 %}{{- '\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.' }}{%- elif ns2.consecutive_failures == 1 %}{{- '\n\n⚠️ SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with completely corrected arguments.' }}{%- endif %}{{- '\n</tool_response>' }}{%- if loop.last %}{{- '<|im_end|>\n' }}{%- else %}{%- set _next_role = _msgs[loop.index0 + 1].role %}{%- if _next_role != 'tool' %}{{- '<|im_end|>\n' }}{%- endif %}{%- endif %}{%- else %}{{- '<|im_start|>user\n[' + message.role + ']: ' + content + '<|im_end|>\n' }}{%- endif %}{%- set ns2.prev_role = message.role %}{%- endfor %}{%- if add_generation_prompt %}{{- '<|im_start|>assistant\n' }}{%- if not ns_state.thinking %}{{- '<think>\n\n</think>\n\n' }}{%- else %}{{- '<think>\n' }}{%- endif %}{%- endif %}
|
scripts/check_applied.py
CHANGED
|
@@ -124,11 +124,11 @@ def main():
|
|
| 124 |
sys.exit(1)
|
| 125 |
else:
|
| 126 |
active_ver = list(unique_versions)[0]
|
| 127 |
-
if "froggeric-v22.
|
| 128 |
-
print(f"{GREEN}✅ SUCCESS: Verified v22.
|
| 129 |
sys.exit(0)
|
| 130 |
elif "froggeric" in active_ver:
|
| 131 |
-
print(f"{CYAN}ℹ️ INFO: Found earlier fixed template version ({active_ver}). Upgrade to v22.
|
| 132 |
sys.exit(0)
|
| 133 |
else:
|
| 134 |
print(f"{YELLOW}⚠️ Stock / unpatched template detected.{RESET}\n")
|
|
|
|
| 124 |
sys.exit(1)
|
| 125 |
else:
|
| 126 |
active_ver = list(unique_versions)[0]
|
| 127 |
+
if "froggeric-v22.5" in active_ver:
|
| 128 |
+
print(f"{GREEN}✅ SUCCESS: Verified v22.5 template is cleanly installed!{RESET}\n")
|
| 129 |
sys.exit(0)
|
| 130 |
elif "froggeric" in active_ver:
|
| 131 |
+
print(f"{CYAN}ℹ️ INFO: Found earlier fixed template version ({active_ver}). Upgrade to v22.5 recommended.{RESET}\n")
|
| 132 |
sys.exit(0)
|
| 133 |
else:
|
| 134 |
print(f"{YELLOW}⚠️ Stock / unpatched template detected.{RESET}\n")
|
scripts/test_v22.py
CHANGED
|
@@ -1696,7 +1696,54 @@ execute_test(
|
|
| 1696 |
)
|
| 1697 |
|
| 1698 |
# ==========================================
|
| 1699 |
-
# 14.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1700 |
# ==========================================
|
| 1701 |
|
| 1702 |
def run_fuzz_property_test(name, cases, seed):
|
|
@@ -1725,9 +1772,9 @@ def execute_fuzz_property_test(*args, **kwargs):
|
|
| 1725 |
if run_fuzz_property_test(*args, **kwargs):
|
| 1726 |
tests_passed += 1
|
| 1727 |
|
| 1728 |
-
#
|
| 1729 |
execute_fuzz_property_test(
|
| 1730 |
-
"
|
| 1731 |
cases=300,
|
| 1732 |
seed=20260820
|
| 1733 |
)
|
|
|
|
| 1696 |
)
|
| 1697 |
|
| 1698 |
# ==========================================
|
| 1699 |
+
# 14. v22.5 Enhancements & Coherence Fixes
|
| 1700 |
+
# ==========================================
|
| 1701 |
+
|
| 1702 |
+
# 102. Non-thinking tool prompt coherence (omits <think> block from instructions)
|
| 1703 |
+
execute_test(
|
| 1704 |
+
"102. Non-thinking tool prompt coherence (omits <think> block from instructions)",
|
| 1705 |
+
messages=[{"role": "user", "content": "Call weather tool"}],
|
| 1706 |
+
tools=tools_sample,
|
| 1707 |
+
kwargs={"enable_thinking": False},
|
| 1708 |
+
expected_in=[
|
| 1709 |
+
"If you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>",
|
| 1710 |
+
"If you choose to call a tool, you MUST output the <tool_call> block IMMEDIATELY, with NO conversational text before it."
|
| 1711 |
+
],
|
| 1712 |
+
expected_not_in=[
|
| 1713 |
+
"<think>\nBrief explanation of tool call\n</think>",
|
| 1714 |
+
"You can use the <think></think> block to plan your next tool call",
|
| 1715 |
+
"ALL explanation and reasoning MUST be placed strictly inside the <think></think> block"
|
| 1716 |
+
]
|
| 1717 |
+
)
|
| 1718 |
+
|
| 1719 |
+
# 103. Multi-part video with 'video_url' dictionary format
|
| 1720 |
+
execute_test(
|
| 1721 |
+
"103. Multi-part video with 'video_url' dictionary format",
|
| 1722 |
+
messages=[{"role": "user", "content": [
|
| 1723 |
+
{"type": "video_url", "video_url": {"url": "https://example.com/demo.mp4"}},
|
| 1724 |
+
{"type": "text", "text": "summarize this video"}
|
| 1725 |
+
]}],
|
| 1726 |
+
expected_in=[
|
| 1727 |
+
"<|vision_start|><|video_pad|><|vision_end|>summarize this video"
|
| 1728 |
+
]
|
| 1729 |
+
)
|
| 1730 |
+
|
| 1731 |
+
# 104. Plain-text tool response truncation in JSON mode
|
| 1732 |
+
execute_test(
|
| 1733 |
+
"104. Plain-text tool response truncation in JSON mode",
|
| 1734 |
+
messages=[
|
| 1735 |
+
{"role": "user", "content": "Run bash"},
|
| 1736 |
+
{"role": "assistant", "content": "", "tool_calls": [{"type": "function", "function": {"name": "bash", "arguments": {}}}]},
|
| 1737 |
+
{"role": "tool", "content": "log line entry\n" * 100}
|
| 1738 |
+
],
|
| 1739 |
+
kwargs={"tool_call_format": "json", "max_tool_response_chars": 50},
|
| 1740 |
+
expected_in=[
|
| 1741 |
+
"[TRUNCATED - original length"
|
| 1742 |
+
]
|
| 1743 |
+
)
|
| 1744 |
+
|
| 1745 |
+
# ==========================================
|
| 1746 |
+
# 15. Property-Based Fuzzing (v22.3)
|
| 1747 |
# ==========================================
|
| 1748 |
|
| 1749 |
def run_fuzz_property_test(name, cases, seed):
|
|
|
|
| 1772 |
if run_fuzz_property_test(*args, **kwargs):
|
| 1773 |
tests_passed += 1
|
| 1774 |
|
| 1775 |
+
# 105. Property fuzz: generated conversations uphold all structural invariants
|
| 1776 |
execute_fuzz_property_test(
|
| 1777 |
+
"105. Property fuzz: generated conversations uphold all structural invariants",
|
| 1778 |
cases=300,
|
| 1779 |
seed=20260820
|
| 1780 |
)
|