From 34b66c3a0ace23e29b247f4db98797511142aa3f Mon Sep 17 00:00:00 2001 From: Jhin Lee Date: Tue, 27 Oct 2026 23:40:24 -0400 Subject: [PATCH] Let a template's own thinking kwarg win over the value enableThinking sets --- CHANGELOG.md | 1 + lib/src/core/engine/engine.dart | 5 + .../core/template/chat_template_handler.dart | 6 +- .../handlers/command_r7b_handler.dart | 8 +- .../template/handlers/hunyuan_v3_handler.dart | 2 +- .../handlers/specialized_handler_utils.dart | 2 +- skills/llamadart-chat-streaming/SKILL.md | 4 +- test/fixtures/llama_cpp_templates/README.md | 1 + .../thinking_template_kwargs_upstream.json | 144 ++++++++++++++++++ .../thinking_template_kwargs_upstream.md | 17 +++ .../thinking_template_kwargs_parity_test.dart | 89 +++++++++++ website/docs/changelog/recent-releases.md | 1 + 12 files changed, 275 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/thinking_template_kwargs_upstream.json create mode 100644 test/fixtures/thinking_template_kwargs_upstream.md create mode 100644 test/unit/core/template/thinking_template_kwargs_parity_test.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c902d40c..c4bbb2e9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - Chat templates now receive `enableThinking` as `enable_thinking` on every request, including `responseFormat` and generic tool-call routes, as in llama.cpp, so templates such as GLM-4.6, GLM-4.7-Flash, GLM-OCR, SmolLM3, Apertus and DeepSeek-R1 distills render thinking as llama-server does for the same flag. `enableThinking` still defaults to `true`, so Apertus now deliberates and DeepSeek-R1 distills open a thought unless you pass `false` ([#1465](https://github.com/leehack/llamadart/issues/1465)). - A `responseFormat` or tool-call grammar after a thought the prompt leaves open now lets the model finish the thought first, as llama-server does, instead of returning the JSON as reasoning; a thought the template opens stays open with `enableThinking: false`, as in llama-server ([#1465](https://github.com/leehack/llamadart/issues/1465)). - A required tool call after a thought the prompt leaves open now lets Cohere2 MoE, Nemotron 3 Nano, MiniCPM5 and Hy3 models finish the thought first, as llama-server does; MiniCPM5 previously wrote the call inside the thought and returned no tool call. +- A template's own thinking setting passed in `chatTemplateKwargs`, such as `reasoning_effort` for Hy3 and Cohere2 MoE or `thinking_mode` for MiniMax M3, is no longer replaced by the value llamadart derives from `enableThinking`, so it renders as in llama-server ([#750](https://github.com/leehack/llamadart/issues/750)). ## 0.16.5 diff --git a/lib/src/core/engine/engine.dart b/lib/src/core/engine/engine.dart index 0ebca2cd1..af040e758 100644 --- a/lib/src/core/engine/engine.dart +++ b/lib/src/core/engine/engine.dart @@ -1631,6 +1631,11 @@ class LlamaEngine { /// A boolean `enable_thinking` in [chatTemplateKwargs], or failing that a /// boolean `thinking`, overrides [enableThinking] for rendering, parsing and /// streaming, as in llama-server; other values leave [enableThinking]. + /// A template's own thinking setting in [chatTemplateKwargs], such as the + /// `reasoning_effort` of Hy3 and Cohere2 MoE or the `thinking_mode` of + /// MiniMax M3, replaces the value llamadart derives from [enableThinking], + /// as in llama-server, while `enableThinking: false` still ends a thought + /// the prompt leaves open. /// Use [templateNow] to set deterministic template time context. /// /// Pass [responseFormat] to request strict structured output through diff --git a/lib/src/core/template/chat_template_handler.dart b/lib/src/core/template/chat_template_handler.dart index 627d03dc7..af21dedd2 100644 --- a/lib/src/core/template/chat_template_handler.dart +++ b/lib/src/core/template/chat_template_handler.dart @@ -180,16 +180,20 @@ abstract class ChatTemplateHandler { /// Renders [template] with [context] plus llama.cpp-style extra globals. /// /// This injects `chat_template_kwargs` values encoded by - /// [ChatTemplateEngine]/[LlamaEngine] through metadata. + /// [ChatTemplateEngine]/[LlamaEngine] through metadata. [context] overrides + /// them, and they override [defaults]: template settings derived from + /// `enableThinking` that a caller can set itself, as in llama-server. String renderTemplate( Template template, { required Map metadata, required Map context, + Map defaults = const {}, }) { final templateNow = DateTime.tryParse( metadata[internalTemplateNowMetadataKey] ?? '', ); return template.render({ + ...defaults, ...chatTemplateKwargsFromMetadata(metadata), ...context, if (templateNow != null) 'strftime_now': strftimeNow(templateNow), diff --git a/lib/src/core/template/handlers/command_r7b_handler.dart b/lib/src/core/template/handlers/command_r7b_handler.dart index 33345e616..481b97d1e 100644 --- a/lib/src/core/template/handlers/command_r7b_handler.dart +++ b/lib/src/core/template/handlers/command_r7b_handler.dart @@ -103,7 +103,13 @@ class CommandR7BHandler extends ChatTemplateHandler { 'eos_token': metadata['tokenizer.ggml.eos_token'] ?? '<|END_OF_TURN_TOKEN|>', 'enable_thinking': enableThinking, - 'reasoning': enableThinking, + }, + defaults: { + // Cohere2 MoE reads `reasoning_effort` only without `reasoning`. + if (!chatTemplateKwargsFromMetadata( + metadata, + ).containsKey('reasoning_effort')) + 'reasoning': enableThinking, 'skip_thinking': !enableThinking, }, ); diff --git a/lib/src/core/template/handlers/hunyuan_v3_handler.dart b/lib/src/core/template/handlers/hunyuan_v3_handler.dart index 6d98dfc88..c62e01f4f 100644 --- a/lib/src/core/template/handlers/hunyuan_v3_handler.dart +++ b/lib/src/core/template/handlers/hunyuan_v3_handler.dart @@ -104,10 +104,10 @@ class HunyuanV3Handler extends ChatTemplateHandler 'messages': templateMessages(messages, templateSource: templateSource), 'add_generation_prompt': addAssistant, 'tools': tools?.map((tool) => tool.toJson()).toList(), - 'reasoning_effort': enableThinking ? 'high' : 'no_think', 'bos_token': metadata['tokenizer.ggml.bos_token'] ?? '', 'eos_token': metadata['tokenizer.ggml.eos_token'] ?? _eos, }, + defaults: {'reasoning_effort': enableThinking ? 'high' : 'no_think'}, ); var thinkingForcedOpen = isThinkingForcedOpen( diff --git a/lib/src/core/template/handlers/specialized_handler_utils.dart b/lib/src/core/template/handlers/specialized_handler_utils.dart index 46eef5914..bb187236b 100644 --- a/lib/src/core/template/handlers/specialized_handler_utils.dart +++ b/lib/src/core/template/handlers/specialized_handler_utils.dart @@ -52,13 +52,13 @@ abstract class DirectJinjaHandler extends ChatTemplateHandler { 'tools': tools?.map((tool) => tool.toJson()).toList(), 'enable_thinking': enableThinking, 'thinking': enableThinking, - 'thinking_mode': enableThinking ? 'enabled' : 'disabled', 'bos_token': metadata['tokenizer.ggml.bos_token'] ?? defaultBosToken, 'eos_token': metadata['tokenizer.ggml.eos_token'] ?? defaultEosToken, 'current_date': resolveTemplateNow( metadata, ).toIso8601String().substring(0, 10), }, + defaults: {'thinking_mode': enableThinking ? 'enabled' : 'disabled'}, ); var thinkingForcedOpen = false; diff --git a/skills/llamadart-chat-streaming/SKILL.md b/skills/llamadart-chat-streaming/SKILL.md index e27f4565a..33451182b 100644 --- a/skills/llamadart-chat-streaming/SKILL.md +++ b/skills/llamadart-chat-streaming/SKILL.md @@ -107,7 +107,9 @@ description: >- `engine.create` when reasoning output is not wanted. A boolean `enable_thinking` or `thinking` in `chatTemplateKwargs` overrides it, as in llama-server, and every chat template receives the result as - `enable_thinking`. Native LiteRT-LM + `enable_thinking`. A template's own setting in `chatTemplateKwargs`, such as + `reasoning_effort` (Hy3, Cohere2 MoE) or `thinking_mode` (MiniMax M3), wins + over the value derived from `enableThinking`. Native LiteRT-LM recognizes renamed Qwen 3 bundles only from a Qwen 3 model-type declaration (field 6), or the complete canonical Qwen 3 template in field 7 or `ModelParams.chatTemplate`. Legacy litert-community Qwen3-0.6B bundles have diff --git a/test/fixtures/llama_cpp_templates/README.md b/test/fixtures/llama_cpp_templates/README.md index cc9dde572..f582c788c 100644 --- a/test/fixtures/llama_cpp_templates/README.md +++ b/test/fixtures/llama_cpp_templates/README.md @@ -8,6 +8,7 @@ that `test/unit/core/template/handlers/firefunction_v2_handler_test.dart`, `test/unit/core/template/tool_result_user_turn_render_test.dart`, `test/unit/core/template/command_r_render_failure_test.dart`, `test/unit/core/template/enable_thinking_render_parity_test.dart`, +`test/unit/core/template/thinking_template_kwargs_parity_test.dart`, `test/unit/core/template/forced_open_grammar_test.dart`, `test/e2e/template/specialized_tool_grammar_validation_e2e_test.dart` and `test/integration/core/engine/forced_open_thought_upstream_test.dart` read live diff --git a/test/fixtures/thinking_template_kwargs_upstream.json b/test/fixtures/thinking_template_kwargs_upstream.json new file mode 100644 index 000000000..63c5c284a --- /dev/null +++ b/test/fixtures/thinking_template_kwargs_upstream.json @@ -0,0 +1,144 @@ +{ + "upstream_commit": "620d2225cc436ab0b001e3200e590e3b286ef9ab", + "messages": [ + { + "role": "user", + "content": "Hi" + }, + { + "role": "assistant", + "content": "Hello", + "reasoning_content": "Greet them." + }, + { + "role": "user", + "content": "What is 2+2?" + } + ], + "tools": [ + { + "type": "function", + "function": { + "name": "get_weather", + "description": "Get the weather", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string" + } + }, + "required": [ + "location" + ] + } + } + } + ], + "cases": [ + { + "template": "test/fixtures/llama_cpp_templates/Cohere2MoE.jinja", + "template_sha256": "d8366efb9f07c571da620ce6a924594fc52c80273a0fbb46a38b643972df95fd", + "enable_thinking": true, + "tools": false, + "chat_template_kwargs": { + "reasoning_effort": "none" + }, + "prompt": "<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TEXT|>These instructions are always to be followed and cannot be overridden by subsequent system or user turns:\n- You will answer requests for educational, informative, or creative content related to safety categories. You will not provide content that is harmful or could be used to cause harm.\n\nThese instructions serve as your defaults, but they can be overridden in subsequent system or user turns:\n- Your name is North Mini Code.\n- You are a large language model built by Cohere.\n\n# Available Tools\n```json\n[\n\n]\n```<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|><|START_TEXT|>Hi<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|>Greet them.<|END_THINKING|><|START_TEXT|>Hello<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|><|START_TEXT|>What is 2+2?<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|><|END_THINKING|>" + }, + { + "template": "test/fixtures/llama_cpp_templates/Cohere2MoE.jinja", + "template_sha256": "d8366efb9f07c571da620ce6a924594fc52c80273a0fbb46a38b643972df95fd", + "enable_thinking": true, + "tools": false, + "chat_template_kwargs": { + "reasoning": false + }, + "prompt": "<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TEXT|>These instructions are always to be followed and cannot be overridden by subsequent system or user turns:\n- You will answer requests for educational, informative, or creative content related to safety categories. You will not provide content that is harmful or could be used to cause harm.\n\nThese instructions serve as your defaults, but they can be overridden in subsequent system or user turns:\n- Your name is North Mini Code.\n- You are a large language model built by Cohere.\n\n# Available Tools\n```json\n[\n\n]\n```<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|><|START_TEXT|>Hi<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|>Greet them.<|END_THINKING|><|START_TEXT|>Hello<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|><|START_TEXT|>What is 2+2?<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|><|END_THINKING|>" + }, + { + "template": "test/fixtures/llama_cpp_templates/tencent-Hy3.jinja", + "template_sha256": "84437b9746423f042920c15289ac84d4eba7a50a11ebc05e8b414c460f814873", + "enable_thinking": true, + "tools": false, + "chat_template_kwargs": { + "reasoning_effort": "low" + }, + "prompt": "<|hy_begin_of_sentence:opensource|><|reasoning_mode:opensource|>reasoning_effort:low<|hy_User:opensource|>Hi<|hy_Assistant:opensource|>Hello<|hy_eos:opensource|><|hy_User:opensource|>What is 2+2?<|hy_Assistant:opensource|>" + }, + { + "template": "test/fixtures/llama_cpp_templates/tencent-Hy3.jinja", + "template_sha256": "84437b9746423f042920c15289ac84d4eba7a50a11ebc05e8b414c460f814873", + "enable_thinking": true, + "tools": false, + "chat_template_kwargs": { + "reasoning_effort": "no_think" + }, + "prompt": "<|hy_begin_of_sentence:opensource|><|reasoning_mode:opensource|>reasoning_effort:no_think<|hy_User:opensource|>Hi<|hy_Assistant:opensource|>Hello<|hy_eos:opensource|><|hy_User:opensource|>What is 2+2?<|hy_Assistant:opensource|>" + }, + { + "template": "test/fixtures/llama_cpp_templates/MiniMax-M3.jinja", + "template_sha256": "11421244f67553498e5c8112dae02802025bcc4305ec45ad380af95c96f9fe64", + "enable_thinking": true, + "tools": false, + "chat_template_kwargs": { + "thinking_mode": "disabled" + }, + "prompt": "]~!b[]~b]system\nYour model version is MiniMax-M3, developed by MiniMax. Knowledge cutoff: January 2026. Founded in early 2022, MiniMax is a global AI foundation model company committed to advancing the frontiers of AI towards AGI.\n\n\nYou have a thinking capability that allows you to reason step by step before responding. When thinking is enabled, wrap your reasoning in tags before your response. When thinking is disabled, begin your response directly after the prefix. When thinking is adaptive, decide on your own whether to think for the current turn.\nCurrent thinking mode: disabled. Do not output any thinking process.\n[e~[\n]~b]developer\nYou are a helpful assistant.[e~[\n]~b]user\nHi[e~[\n]~b]ai\nGreet them.Hello[e~[\n]~b]user\nWhat is 2+2?[e~[\n]~b]ai\n" + }, + { + "template": "test/fixtures/llama_cpp_templates/MiniMax-M3.jinja", + "template_sha256": "11421244f67553498e5c8112dae02802025bcc4305ec45ad380af95c96f9fe64", + "enable_thinking": true, + "tools": false, + "chat_template_kwargs": { + "thinking_mode": "adaptive" + }, + "prompt": "]~!b[]~b]system\nYour model version is MiniMax-M3, developed by MiniMax. Knowledge cutoff: January 2026. Founded in early 2022, MiniMax is a global AI foundation model company committed to advancing the frontiers of AI towards AGI.\n\n\nYou have a thinking capability that allows you to reason step by step before responding. When thinking is enabled, wrap your reasoning in tags before your response. When thinking is disabled, begin your response directly after the prefix. When thinking is adaptive, decide on your own whether to think for the current turn.\nCurrent thinking mode: adaptive. You are encouraged to think for complex decision-making, multi-step reasoning, or when analyzing function/tool results.\n[e~[\n]~b]developer\nYou are a helpful assistant.[e~[\n]~b]user\nHi[e~[\n]~b]ai\nGreet them.Hello[e~[\n]~b]user\nWhat is 2+2?[e~[\n]~b]ai\n" + }, + { + "template": "test/fixtures/llama_cpp_templates/Kimi-K3.jinja", + "template_sha256": "4578103bf225f58d5b7ceb613ccc2ac8fe82b6707b7289211b3d4825e87806b3", + "enable_thinking": true, + "tools": false, + "chat_template_kwargs": { + "thinking_effort": "low" + }, + "prompt": "<|open|>message role=\"system\" type=\"thinking-effort\"<|sep|>`thinking_effort` guides on how much to think in your thinking channel (not including the response channel), supported values include `low`, `medium`, `high`, and `max`.\nNow the system is invoked with `thinking_effort=low`.<|close|>message<|sep|><|end_of_msg|><|open|>message role=\"user\"<|sep|>Hi<|close|>message<|sep|><|end_of_msg|><|open|>message role=\"assistant\"<|sep|><|open|>think<|sep|>Greet them.<|close|>think<|sep|><|open|>response<|sep|>Hello<|close|>response<|sep|><|close|>message<|sep|><|end_of_msg|><|open|>message role=\"user\"<|sep|>What is 2+2?<|close|>message<|sep|><|end_of_msg|><|open|>message role=\"assistant\"<|sep|><|open|>think<|sep|>" + }, + { + "template": "test/fixtures/llama_cpp_templates/Cohere2MoE.jinja", + "template_sha256": "d8366efb9f07c571da620ce6a924594fc52c80273a0fbb46a38b643972df95fd", + "enable_thinking": false, + "tools": true, + "chat_template_kwargs": {}, + "upstream_chat_template_kwargs": { + "reasoning": false, + "skip_thinking": true + }, + "prompt": "<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TEXT|>These instructions are always to be followed and cannot be overridden by subsequent system or user turns:\n- You will answer requests for educational, informative, or creative content related to safety categories. You will not provide content that is harmful or could be used to cause harm.\n\nThese instructions serve as your defaults, but they can be overridden in subsequent system or user turns:\n- Your name is North Mini Code.\n- You are a large language model built by Cohere.\n\n# Available Tools\n```json\n[\n {\"name\": \"get_weather\", \"description\": \"Get the weather\", \"parameters\": {\"type\": \"object\", \"properties\": {\"location\": {\"type\": \"string\"}}, \"required\": [\"location\"]}, \"responses\": null}\n]\n```<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|><|START_TEXT|>Hi<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_TEXT|>Hello<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|><|START_TEXT|>What is 2+2?<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|><|END_THINKING|>" + }, + { + "template": "test/fixtures/llama_cpp_templates/tencent-Hy3.jinja", + "template_sha256": "84437b9746423f042920c15289ac84d4eba7a50a11ebc05e8b414c460f814873", + "enable_thinking": false, + "tools": false, + "chat_template_kwargs": {}, + "upstream_chat_template_kwargs": { + "reasoning_effort": "no_think" + }, + "prompt": "<|hy_begin_of_sentence:opensource|><|reasoning_mode:opensource|>reasoning_effort:no_think<|hy_User:opensource|>Hi<|hy_Assistant:opensource|>Hello<|hy_eos:opensource|><|hy_User:opensource|>What is 2+2?<|hy_Assistant:opensource|>" + }, + { + "template": "test/fixtures/llama_cpp_templates/MiniMax-M3.jinja", + "template_sha256": "11421244f67553498e5c8112dae02802025bcc4305ec45ad380af95c96f9fe64", + "enable_thinking": false, + "tools": false, + "chat_template_kwargs": {}, + "upstream_chat_template_kwargs": { + "thinking_mode": "disabled" + }, + "prompt": "]~!b[]~b]system\nYour model version is MiniMax-M3, developed by MiniMax. Knowledge cutoff: January 2026. Founded in early 2022, MiniMax is a global AI foundation model company committed to advancing the frontiers of AI towards AGI.\n\n\nYou have a thinking capability that allows you to reason step by step before responding. When thinking is enabled, wrap your reasoning in tags before your response. When thinking is disabled, begin your response directly after the prefix. When thinking is adaptive, decide on your own whether to think for the current turn.\nCurrent thinking mode: disabled. Do not output any thinking process.\n[e~[\n]~b]developer\nYou are a helpful assistant.[e~[\n]~b]user\nHi[e~[\n]~b]ai\nGreet them.Hello[e~[\n]~b]user\nWhat is 2+2?[e~[\n]~b]ai\n" + } + ] +} diff --git a/test/fixtures/thinking_template_kwargs_upstream.md b/test/fixtures/thinking_template_kwargs_upstream.md new file mode 100644 index 000000000..b478f76aa --- /dev/null +++ b/test/fixtures/thinking_template_kwargs_upstream.md @@ -0,0 +1,17 @@ +# Template thinking kwargs upstream renders + +`thinking_template_kwargs_upstream.json` records the prompts llama.cpp renders, +for a conversation with an earlier assistant thought, when a chat template +kwarg sets a template's own thinking switch, for issue #750: `reasoning_effort` +or `reasoning` for Cohere2 MoE, `reasoning_effort` for Hy3, `thinking_mode` for +MiniMax M3 and `thinking_effort` for Kimi K3. The cases with +`enable_thinking: false` and no `chat_template_kwargs` pin that llamadart's +`enableThinking: false` renders as llama.cpp does with +`upstream_chat_template_kwargs`, the template's own off setting. + +`prompt` comes from a probe built from unchanged llama.cpp +`620d2225cc436ab0b001e3200e590e3b286ef9ab`. With no model loaded, it called +`common_chat_templates_init` on the vendored template with empty BOS and EOS +tokens, then `common_chat_templates_apply` with the messages, the tool when +`tools` is true, `enable_thinking`, the kwargs and +`COMMON_REASONING_FORMAT_AUTO`. diff --git a/test/unit/core/template/thinking_template_kwargs_parity_test.dart b/test/unit/core/template/thinking_template_kwargs_parity_test.dart new file mode 100644 index 000000000..593eccfe6 --- /dev/null +++ b/test/unit/core/template/thinking_template_kwargs_parity_test.dart @@ -0,0 +1,89 @@ +@TestOn('vm') +library; + +import 'dart:convert'; +import 'dart:io'; + +import 'package:crypto/crypto.dart'; +import 'package:llamadart/src/core/models/chat/chat_message.dart'; +import 'package:llamadart/src/core/models/chat/chat_role.dart'; +import 'package:llamadart/src/core/models/chat/content_part.dart'; +import 'package:llamadart/src/core/models/tools/tool_definition.dart'; +import 'package:llamadart/src/core/models/tools/tool_param.dart'; +import 'package:llamadart/src/core/template/chat_template_engine.dart'; +import 'package:llamadart/src/core/template/thinking_utils.dart'; +import 'package:path/path.dart' as p; +import 'package:test/test.dart'; + +final Map _fixture = + jsonDecode( + File( + 'test/fixtures/thinking_template_kwargs_upstream.json', + ).readAsStringSync(), + ) + as Map; + +final ToolDefinition _weatherTool = ToolDefinition( + name: 'get_weather', + description: 'Get the weather', + parameters: [ToolParam.string('location', required: true)], +); + +// A template's own thinking setting passed as a chat template kwarg wins over +// the value llamadart derives from `enableThinking`, as in llama-server, and +// `enableThinking: false` renders as that setting turned off. +void main() { + test('sends llama.cpp the tool the cases render', () { + expect(_fixture['tools'], [_weatherTool.toJson()]); + }); + + for (final entry + in (_fixture['cases'] as List).cast>()) { + final template = entry['template'] as String; + final kwargs = entry['chat_template_kwargs'] as Map; + final enableThinking = entry['enable_thinking'] as bool; + final withTools = entry['tools'] as bool; + + test('${p.basename(template)} with $kwargs, enableThinking: ' + '$enableThinking${withTools ? ' and tools' : ''} renders as llama.cpp ' + 'does with ${entry['upstream_chat_template_kwargs'] ?? kwargs}', () { + final source = File(template).readAsStringSync(); + expect( + sha256.convert(utf8.encode(source)).toString(), + entry['template_sha256'], + ); + + final result = ChatTemplateEngine.render( + templateSource: source, + messages: [ + for (final message + in (_fixture['messages'] as List).cast>()) + LlamaChatMessage.withContent( + role: LlamaChatRole.values.byName(message['role'] as String), + content: [ + if (message['reasoning_content'] case final String thought) + LlamaThinkingContent(thought), + LlamaTextContent(message['content'] as String), + ], + ), + ], + metadata: const { + 'tokenizer.ggml.bos_token': '', + 'tokenizer.ggml.eos_token': '', + }, + tools: withTools ? [_weatherTool] : null, + enableThinking: enableThinking, + chatTemplateKwargs: kwargs, + ); + + expect(result.prompt, entry['prompt']); + expect( + result.thinkingForcedOpen, + isThinkingForcedOpen( + entry['prompt'] as String, + startTag: ChatTemplateEngine.thinkingTagsFor(result.format).startTag, + ), + ); + }); + } +} diff --git a/website/docs/changelog/recent-releases.md b/website/docs/changelog/recent-releases.md index 67035f106..dd332969e 100644 --- a/website/docs/changelog/recent-releases.md +++ b/website/docs/changelog/recent-releases.md @@ -31,6 +31,7 @@ For canonical full release notes, use: - Chat templates now receive `enableThinking` as `enable_thinking` on every request, including `responseFormat` and generic tool-call routes, as in llama.cpp, so templates such as GLM-4.6, GLM-4.7-Flash, GLM-OCR, SmolLM3, Apertus and DeepSeek-R1 distills render thinking as llama-server does for the same flag. `enableThinking` still defaults to `true`, so Apertus now deliberates and DeepSeek-R1 distills open a thought unless you pass `false` ([#1465](https://github.com/leehack/llamadart/issues/1465)). - A `responseFormat` or tool-call grammar after a thought the prompt leaves open now lets the model finish the thought first, as llama-server does, instead of returning the JSON as reasoning; a thought the template opens stays open with `enableThinking: false`, as in llama-server ([#1465](https://github.com/leehack/llamadart/issues/1465)). - A required tool call after a thought the prompt leaves open now lets Cohere2 MoE, Nemotron 3 Nano, MiniCPM5 and Hy3 models finish the thought first, as llama-server does; MiniCPM5 previously wrote the call inside the thought and returned no tool call. +- A template's own thinking setting passed in `chatTemplateKwargs`, such as `reasoning_effort` for Hy3 and Cohere2 MoE or `thinking_mode` for MiniMax M3, is no longer replaced by the value llamadart derives from `enableThinking`, so it renders as in llama-server ([#750](https://github.com/leehack/llamadart/issues/750)). ## 0.16.5