From a6ea0316b6913b247fe4a744d0ebbfa152088a2d Mon Sep 17 00:00:00 2001 From: leehack Date: Mon, 9 Nov 2026 20:26:09 -0500 Subject: [PATCH] Take a thought only at the start of a reply after a prompt that opens no thought, as llama.cpp does (#1865) After a prompt that opens no thought, llama.cpp (cd959bae9) takes a thought only where the reply starts with the start tag; every other thinking tag is content. llamadart's handlers extracted thoughts anywhere (extractThinking), so a bare ``, a `` after text, or a second `` after a leading thought became reasoning, and the stream, which split tags as they arrived, could disagree with the final parse and repeat text. - `ChatFormatTraits.takesThoughtOnlyAtReplyStart` marks the formats verified against the oracle; their rendered parse runs inside `withThoughtOnlyAtReplyStart`, which switches `extractThinking` to the leading-thought rule for every handler call site. - The stream ends such a leading thought at its first end tag and streams the rest as content, as it already did for Seed-OSS and PEG replies. - `closed_thought_upstream.json` gains oracle cases for one vendored template per format (five templates vendored at b10549, identical at cd959bae9). Refs #1874 --- CHANGELOG.md | 2 + .../engine/chat_completion_stream_parser.dart | 22 +- .../core/template/chat_format_registry.dart | 26 +- .../core/template/chat_template_engine.dart | 53 +- lib/src/core/template/thinking_utils.dart | 61 +- skills/llamadart-chat-streaming/SKILL.md | 5 +- test/fixtures/closed_thought_upstream.json | 3385 +++++++++++++++++ test/fixtures/closed_thought_upstream.md | 16 + .../deepseek-ai-DeepSeek-V3.2.jinja | 141 + .../deepseek-ai-DeepSeek-V4.jinja | 137 + .../ibm-granite-granite-3.3-2B-Instruct.jinja | 59 + ...mistralai-Mistral-Nemo-Instruct-2407.jinja | 87 + .../poolside-Laguna-XS.2.jinja | 132 + .../engine/closed_thought_upstream_test.dart | 105 +- .../lfm2_reasoning_boundary_test.dart | 10 +- .../chat_completion_stream_parser_test.dart | 2 +- .../template/forced_open_grammar_test.dart | 3 + .../template_caps_cache_fixtures_test.dart | 9 + .../core/template/thinking_utils_test.dart | 63 + website/docs/changelog/recent-releases.md | 2 + .../docs/guides/chat-template-and-parsing.md | 15 +- .../docs/guides/template-engine-internals.md | 17 +- 22 files changed, 4282 insertions(+), 70 deletions(-) create mode 100644 test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-V3.2.jinja create mode 100644 test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-V4.jinja create mode 100644 test/fixtures/llama_cpp_templates/ibm-granite-granite-3.3-2B-Instruct.jinja create mode 100644 test/fixtures/llama_cpp_templates/mistralai-Mistral-Nemo-Instruct-2407.jinja create mode 100644 test/fixtures/llama_cpp_templates/poolside-Laguna-XS.2.jinja diff --git a/CHANGELOG.md b/CHANGELOG.md index 68fd84b4a..04832f9a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## Unreleased +- When the prompt opened no thought, take a thought only at the start of the reply, as llama.cpp does: any other thinking tag, such as a `` before any ``, a `` later in the reply or a second ``, is now content in generic, Llama 3, GLM 4.5, Kimi K2, LFM2, Laguna, Granite, MiniCPM5, Mistral Nemo, Apertus, Hunyuan V3 and DeepSeek R1, V3.2 and V4 replies, and after the start of a Hermes or Qwen3-Coder XML reply. The stream used to depend on how the reply was chunked and could repeat text as both content and reasoning ([#1865](https://github.com/leehack/llamadart/issues/1865)). + ## 0.19.2 - Keep the default WebGPU bridge assets at `v0.1.83` (unchanged from 0.19.1). diff --git a/lib/src/core/engine/chat_completion_stream_parser.dart b/lib/src/core/engine/chat_completion_stream_parser.dart index d68dd0291..908763107 100644 --- a/lib/src/core/engine/chat_completion_stream_parser.dart +++ b/lib/src/core/engine/chat_completion_stream_parser.dart @@ -199,14 +199,20 @@ class ChatCompletionStreamParser { final verbatimThoughtOpenings = verbatimForcedThought ? traits.forcedThoughtContentOpenings : const []; - // After a prompt that opens no thought, a Seed-OSS or PEG-parsed reply - // has one only at its start, kept as generated, as the parse takes it. - final leadingThoughtOnly = + final promptOpensNoThought = + !templateResult.thinkingForcedOpen && !thoughtClosedForThinkingOff; + // After a prompt that opens no thought, a reply has a thought only at its + // start in these formats. A Seed-OSS or PEG-parsed reply keeps it as + // generated, as the parse takes it. + final verbatimLeadingThought = (templateResult.format == ChatFormat.seedOss.index || pegParsedWithoutTools) && - !templateResult.thinkingForcedOpen && - !thoughtClosedForThinkingOff; - final verbatimThought = verbatimForcedThought || leadingThoughtOnly; + promptOpensNoThought; + final leadingThoughtOnly = + verbatimLeadingThought || + (promptOpensNoThought && traits.takesThoughtOnlyAtReplyStart); + final verbatimThought = verbatimForcedThought || verbatimLeadingThought; + final endsAtFirstEndTag = verbatimThought || leadingThoughtOnly; // After a prompt that opens no thought, the parse keeps an end tag before // any start tag as content, with every thinking tag after it. final unopenedEndTagIsContent = @@ -407,13 +413,13 @@ class ChatCompletionStreamParser { startTag: startTag, endTag: endTag, tagState: thoughtTags, - endsAtFirstEndTag: verbatimThought, + endsAtFirstEndTag: endsAtFirstEndTag, thoughtContentOpenings: verbatimThoughtOpenings, bodyOpenings: bodyOpenings, unopenedEndTagIsContent: unopenedEndTagIsContent, ); if (split.bodyStarted || - (verbatimThought && + (endsAtFirstEndTag && split.emissions.any((emission) => emission.endsThinking))) { verbatimThoughtEnded = true; } diff --git a/lib/src/core/template/chat_format_registry.dart b/lib/src/core/template/chat_format_registry.dart index 29b813c3a..e3c8492a7 100644 --- a/lib/src/core/template/chat_format_registry.dart +++ b/lib/src/core/template/chat_format_registry.dart @@ -88,6 +88,7 @@ final class ChatFormatTraits { this.parsesCallsInsideThoughts = false, this.keepsForcedThoughtVerbatim = false, this.closedThoughtKeepsTagsAsContent = false, + this.takesThoughtOnlyAtReplyStart = false, this.forcedThoughtContentOpenings = const [], this.mayEmbedToolEnvelopeAfterContent = false, this.hasRepeatedContentChannels = false, @@ -126,6 +127,10 @@ final class ChatFormatTraits { /// extracts no reasoning, so thinking tags are content. final bool closedThoughtKeepsTagsAsContent; + /// Whether, after a prompt that opens no thought, the parse takes a thought + /// only at the start of the reply, so other thinking tags are content. + final bool takesThoughtOnlyAtReplyStart; + /// Markers that, with [keepsForcedThoughtVerbatim], also end a forced-open /// thought when they come before its end tag, and start the content. final List forcedThoughtContentOpenings; @@ -213,6 +218,7 @@ chatFormatRegistrations = List.unmodifiable([ detect: (s) => _hasAll(s, const ['dsml_token', 'DSML', 'function_calls']), traits: ChatFormatTraits( qualifiesJsonSchemaTools: false, + takesThoughtOnlyAtReplyStart: true, mayEmbedToolEnvelopeAfterContent: true, ), ), @@ -222,6 +228,7 @@ chatFormatRegistrations = List.unmodifiable([ detect: (s) => _hasAll(s, const ['dsml_token', 'DSML', 'tool_calls']), traits: ChatFormatTraits( qualifiesJsonSchemaTools: false, + takesThoughtOnlyAtReplyStart: true, mayEmbedToolEnvelopeAfterContent: true, ), ), @@ -253,6 +260,7 @@ chatFormatRegistrations = List.unmodifiable([ ]), traits: ChatFormatTraits( qualifiesJsonSchemaTools: true, + takesThoughtOnlyAtReplyStart: true, mayEmbedToolEnvelopeAfterContent: true, leavesIncompleteToolCall: _contentContainsAny(const ['']), ), @@ -277,6 +285,7 @@ chatFormatRegistrations = List.unmodifiable([ detect: (s) => s.contains('<|tool▁calls▁begin|>'), traits: ChatFormatTraits( qualifiesJsonSchemaTools: false, + takesThoughtOnlyAtReplyStart: true, toolCallOpening: DeepseekR1Handler.toolCallOpening, disabledByResponseSchema: true, ), @@ -306,6 +315,7 @@ chatFormatRegistrations = List.unmodifiable([ ]), traits: ChatFormatTraits( qualifiesJsonSchemaTools: false, + takesThoughtOnlyAtReplyStart: true, toolCallOpening: XmlToolCallFormat.minicpm5.toolCallOpening, ), ), @@ -325,6 +335,7 @@ chatFormatRegistrations = List.unmodifiable([ detect: (s) => _hasAll(s, const ['elif thinking', '<|tool_call|>']), traits: ChatFormatTraits( qualifiesJsonSchemaTools: false, + takesThoughtOnlyAtReplyStart: true, toolCallOpening: GraniteHandler.toolCallOpening, ), ), @@ -335,6 +346,7 @@ chatFormatRegistrations = List.unmodifiable([ _hasAll(s, const ['[gMASK]', '', '']), traits: ChatFormatTraits( qualifiesJsonSchemaTools: true, + takesThoughtOnlyAtReplyStart: true, mayEmbedToolEnvelopeAfterContent: true, disabledByResponseSchema: true, leavesIncompleteToolCall: _contentContainsAny(const ['']), @@ -347,6 +359,7 @@ chatFormatRegistrations = List.unmodifiable([ _hasAll(s, const ['<|hy_User{}|>', '', '']), traits: ChatFormatTraits( qualifiesJsonSchemaTools: true, + takesThoughtOnlyAtReplyStart: true, toolCallOpening: HunyuanV3Handler.toolCallOpening, leavesIncompleteToolCall: _contentContainsAny(const [ '', @@ -367,6 +380,7 @@ chatFormatRegistrations = List.unmodifiable([ s.contains('Function calls MUST follow the specified format')), traits: ChatFormatTraits( qualifiesJsonSchemaTools: false, + takesThoughtOnlyAtReplyStart: true, toolCallOpening: XmlToolCallFormat.qwen3Coder.toolCallOpening, closedThoughtKeepsTagsAsContent: true, keepsForcedThoughtVerbatim: true, @@ -407,6 +421,7 @@ chatFormatRegistrations = List.unmodifiable([ detect: (s) => s.contains(''), traits: ChatFormatTraits( qualifiesJsonSchemaTools: true, + takesThoughtOnlyAtReplyStart: true, toolCallOpening: HermesHandler.toolCallOpening, keepsForcedThoughtVerbatim: true, disabledByResponseSchema: true, @@ -451,6 +466,7 @@ chatFormatRegistrations = List.unmodifiable([ detect: (s) => _hasAll(s, const ['<|system_start|>', '<|tools_prefix|>']), traits: ChatFormatTraits( qualifiesJsonSchemaTools: false, + takesThoughtOnlyAtReplyStart: true, toolCallOpening: ApertusHandler.toolCallOpening, requiredKeepsLazyGrammar: true, ), @@ -465,6 +481,7 @@ chatFormatRegistrations = List.unmodifiable([ (s.contains('List of tools: [') && !s.contains('<|tool_list_start|>')), traits: ChatFormatTraits( qualifiesJsonSchemaTools: false, + takesThoughtOnlyAtReplyStart: true, toolCallOpening: Lfm2Handler.toolCallOpening, requiredKeepsLazyGrammar: true, ), @@ -488,6 +505,7 @@ chatFormatRegistrations = List.unmodifiable([ ]), traits: ChatFormatTraits( qualifiesJsonSchemaTools: false, + takesThoughtOnlyAtReplyStart: true, toolCallOpening: KimiK2Handler.toolCallOpening, parsesCallsInsideThoughts: true, ), @@ -576,7 +594,10 @@ chatFormatRegistrations = List.unmodifiable([ ChatFormat.llama3, Llama3Handler.new, detect: (s) => s.contains('<|start_header_id|>ipython<|end_header_id|>'), - traits: ChatFormatTraits(qualifiesJsonSchemaTools: false), + traits: ChatFormatTraits( + qualifiesJsonSchemaTools: false, + takesThoughtOnlyAtReplyStart: true, + ), ), // Ministral templates also carry [THINK] and [TOOL_CALLS], so Ministral // precedes Magistral and Mistral Nemo. @@ -602,6 +623,7 @@ chatFormatRegistrations = List.unmodifiable([ detect: (s) => s.contains('[TOOL_CALLS]'), traits: ChatFormatTraits( qualifiesJsonSchemaTools: true, + takesThoughtOnlyAtReplyStart: true, toolCallOpening: MistralHandler.toolCallOpening, toolChoiceNoneRendersContentOnly: true, leavesIncompleteToolCall: _contentContainsAny(const ['[TOOL_CALLS]']), @@ -652,6 +674,7 @@ chatFormatRegistrations = List.unmodifiable([ GenericHandler.new, traits: ChatFormatTraits( qualifiesJsonSchemaTools: true, + takesThoughtOnlyAtReplyStart: true, toolChoiceNoneRendersContentOnly: true, leavesIncompleteToolCall: _genericLeavesIncompleteToolCall, ), @@ -661,6 +684,7 @@ chatFormatRegistrations = List.unmodifiable([ () => _withoutTemplateToolCalls(ChatFormat.contentOnly), traits: ChatFormatTraits( qualifiesJsonSchemaTools: false, + takesThoughtOnlyAtReplyStart: true, toolChoiceNoneRendersContentOnly: true, ), ), diff --git a/lib/src/core/template/chat_template_engine.dart b/lib/src/core/template/chat_template_engine.dart index 468f1f64e..573ce71a1 100644 --- a/lib/src/core/template/chat_template_engine.dart +++ b/lib/src/core/template/chat_template_engine.dart @@ -721,9 +721,10 @@ class ChatTemplateEngine { /// render left the prompt's thought, which a copy of the result loses. /// The reply then parses as llama.cpp parses it after that prompt, which /// [parse] cannot tell: thinking tags after the template's own empty thought - /// are content for DeepSeek V3, Hermes and Qwen3-Coder XML, as they are - /// after a prompt that opens no thought in a reply with an end tag before - /// any start tag, which [parse] takes as ending a thought; a Seed-OSS reply + /// are content for DeepSeek V3, Hermes and Qwen3-Coder XML; after a prompt + /// that opens no thought, most formats take a thought only at the start of + /// the reply, and DeepSeek V3 keeps an end tag before any start tag as + /// content, where [parse] takes them as thoughts; a Seed-OSS reply /// to a prompt that opens no thought has one only when it starts with /// ``; and a Seed-OSS or Cohere2 MoE thought closed because /// thinking is off still ends at a thinking tag or, for Cohere2 MoE, at the @@ -784,20 +785,26 @@ class ChatTemplateEngine { String? parser, List? tools, }) { + ChatParseResult parse() => _parse( + formatIndex, + output, + isPartial: isPartial, + cancelled: cancelled, + parseToolCalls: parseToolCalls, + thinkingForcedOpen: thinkingForcedOpen, + thinkingForcedClosed: thinkingForcedClosed, + promptOpensNoThought: promptOpensNoThought, + parser: parser, + tools: tools, + ); final ChatParseResult result; try { - result = _parse( - formatIndex, - output, - isPartial: isPartial, - cancelled: cancelled, - parseToolCalls: parseToolCalls, - thinkingForcedOpen: thinkingForcedOpen, - thinkingForcedClosed: thinkingForcedClosed, - promptOpensNoThought: promptOpensNoThought, - parser: parser, - tools: tools, - ); + result = + promptOpensNoThought && + !thinkingForcedOpen && + takesThoughtOnlyAtReplyStart(formatIndex) + ? withThoughtOnlyAtReplyStart(parse) + : parse(); } on StackOverflowError catch (_, stackTrace) { // PEG rules recurse once per nesting level of model-controlled output. Error.throwWithStackTrace( @@ -1129,6 +1136,15 @@ class ChatTemplateEngine { } } +/// Whether, after a prompt that opens no thought, the parse of [formatIndex] +/// takes a thought only at the start of the reply and keeps other thinking +/// tags as content, as llama.cpp does. +bool takesThoughtOnlyAtReplyStart(int formatIndex) => + formatIndex < ChatFormat.values.length && + chatFormatRegistration( + ChatFormat.values[formatIndex], + ).traits.takesThoughtOnlyAtReplyStart; + /// The markers that show where a reply ends a thought that a handler closed /// for thinking off: the format's thinking tags and, for Cohere2 MoE, where /// its text or action starts, as llama.cpp ends the thought it keeps open. @@ -1149,9 +1165,10 @@ List closedThoughtMarkers(int formatIndex) { /// for thinking off ([thoughtClosedForThinkingOff]), so a reply that shows one /// of [closedThoughtMarkers] parses as after a forced-open prompt. Without it /// or [thinkingForcedOpen] the prompt opened no thought, so a Seed-OSS end tag -/// in a reply that does not start with a thought is content, and a DeepSeek -/// V3, Hermes or Qwen3-Coder XML reply with an end tag before any start tag -/// keeps its thinking tags as content. +/// in a reply that does not start with a thought is content, a format with +/// [ChatFormatTraits.takesThoughtOnlyAtReplyStart] takes a thought only at the +/// start of the reply, and a DeepSeek V3 reply with an end tag before any +/// start tag keeps its thinking tags as content. ChatParseResult parseRenderedOutput( int formatIndex, String output, { diff --git a/lib/src/core/template/thinking_utils.dart b/lib/src/core/template/thinking_utils.dart index f80cc5626..b52ec36ba 100644 --- a/lib/src/core/template/thinking_utils.dart +++ b/lib/src/core/template/thinking_utils.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import '../models/chat/chat_template_result.dart'; import 'template_internal_metadata.dart'; @@ -12,6 +14,10 @@ typedef ThinkingExtraction = ({String content, String? reasoning}); /// If [startTag] is found without a matching [endTag], all text after /// [startTag] is treated as reasoning (model may still be thinking). /// +/// Inside [withThoughtOnlyAtReplyStart] and without [thinkingForcedOpen], +/// only a thought that starts [text] is reasoning, and the rest is content in +/// which thinking tags are text. +/// /// Escaped `\n` and `\r` in reasoning become a newline and a carriage return /// unless [unescape] is false. The content keeps the whitespace at its ends /// unless [trimContent] is true. @@ -23,13 +29,21 @@ ThinkingExtraction extractThinking( bool unescape = true, bool trimContent = true, }) { - final result = _extractThinking( - text, - startTag: startTag, - endTag: endTag, - thinkingForcedOpen: thinkingForcedOpen, - unescape: unescape, - ); + final result = + !thinkingForcedOpen && Zone.current[_thoughtOnlyAtReplyStart] == true + ? _extractLeadingThought( + text, + startTag: startTag, + endTag: endTag, + unescape: unescape, + ) + : _extractThinking( + text, + startTag: startTag, + endTag: endTag, + thinkingForcedOpen: thinkingForcedOpen, + unescape: unescape, + ); // Trim only the overall content ends here, once. The recursion keeps content // raw so whitespace between stripped thinking blocks (e.g. the space in // "Hellox worldy!") is preserved. @@ -108,6 +122,39 @@ String withoutTrailingTagPrefix(String text, Iterable tags) { return text.substring(0, text.length - cut); } +final Object _thoughtOnlyAtReplyStart = Object(); + +/// Runs [parse] with [extractThinking] taking a thought only where it starts +/// the text, as llama.cpp parses a reply to a prompt that opens no thought. +/// +/// Handlers extract thinking in many places and shapes, so the parse sets +/// this once around them instead of threading it through each one. +T withThoughtOnlyAtReplyStart(T Function() parse) => + runZoned(parse, zoneValues: {_thoughtOnlyAtReplyStart: true}); + +/// Splits off the thought that starts [text], after ASCII whitespace, up to +/// the first [endTag], or to the end when none follows. +ThinkingExtraction _extractLeadingThought( + String text, { + required String startTag, + required String endTag, + required bool unescape, +}) { + final rest = trimLeftAsciiSpace(text); + if (!rest.startsWith(startTag)) return (content: text, reasoning: null); + final thought = rest.substring(startTag.length); + final end = thought.indexOf(endTag); + var reasoning = end == -1 ? thought : thought.substring(0, end); + if (unescape) { + reasoning = reasoning.replaceAll(r'\n', '\n').replaceAll(r'\r', '\r'); + } + reasoning = reasoning.trim(); + return ( + content: end == -1 ? '' : thought.substring(end + endTag.length), + reasoning: reasoning.isEmpty ? null : reasoning, + ); +} + ThinkingExtraction _extractThinking( String text, { required String startTag, diff --git a/skills/llamadart-chat-streaming/SKILL.md b/skills/llamadart-chat-streaming/SKILL.md index 57465468b..afc72b4f2 100644 --- a/skills/llamadart-chat-streaming/SKILL.md +++ b/skills/llamadart-chat-streaming/SKILL.md @@ -141,7 +141,10 @@ modalities and speculative strategies before offering them. Nano v2) stream no `chunk.text` until `` or `` arrives or the reply ends, so a thought the model still writes never reaches it; Cohere2 MoE waits likewise for a thinking tag, `<|START_TEXT|>` or - `<|START_ACTION|>`. A boolean `enable_thinking` or `thinking` in + `<|START_ACTION|>`. When the prompt opens no thought, most formats stream + only a thought that starts the reply as `chunk.thinking`; a `` + before any `` and thinking tags later in the reply stay in + `chunk.text`, as in llama-server. A boolean `enable_thinking` or `thinking` in `chatTemplateKwargs` overrides it, as in llama-server, and every chat template receives the result as `enable_thinking`. A template's own setting in `chatTemplateKwargs`, such as `reasoning_effort` (Hy3, Cohere2 diff --git a/test/fixtures/closed_thought_upstream.json b/test/fixtures/closed_thought_upstream.json index 2c01afedc..e1dfa08bb 100644 --- a/test/fixtures/closed_thought_upstream.json +++ b/test/fixtures/closed_thought_upstream.json @@ -1146,6 +1146,58 @@ "reasoning_content": "", "tool_calls": [] } + }, + { + "name": "thought-after-text", + "output": "Answer X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } } ] }, @@ -1320,6 +1372,58 @@ "tool_calls": [] } }, + { + "name": "thought-after-text", + "output": "Answer X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + }, { "name": "thought", "output": "Plan.Answer.", @@ -1333,6 +1437,86 @@ "reasoning_content": "Plan.", "tool_calls": [] } + }, + { + "name": "end-tag-after-thought", + "output": "Plan.AB", + "upstream": { + "content": "AB", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "AB", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "thought-after-thought", + "output": "Plan.AXC", + "upstream": { + "content": "AXC", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "AXC", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "end-tag-after-thought-and-newlines", + "output": "\n\nPlan.\n\n\nA B", + "upstream": { + "content": "A B", + "reasoning_content": "Plan.\n", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "A B", + "reasoning_content": "Plan.\n", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end-after-thought", + "output": "Plan.A ", + "upstream": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_partial": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "end-tag-after-thought-then-call", + "output": "Plan.A\n{\"name\": \"weather\", \"arguments\": {\"city\": \"Paris\"}}\n", + "upstream": { + "content": "A", + "reasoning_content": "Plan.", + "tool_calls": [ + { + "name": "weather", + "arguments": "{\"city\": \"Paris\"}" + } + ] + }, + "upstream_without_tools": { + "content": "A\n{\"name\": \"weather\", \"arguments\": {\"city\": \"Paris\"}}\n", + "reasoning_content": "Plan.", + "tool_calls": [] + } } ] }, @@ -1505,6 +1689,3207 @@ "reasoning_content": "", "tool_calls": [] } + }, + { + "name": "thought-after-text", + "output": "Answer X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/google-gemma-2-2b-it.jinja", + "format": "generic", + "chat_template_kwargs": {}, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/HuggingFaceTB-SmolLM3-3B.jinja", + "format": "generic", + "chat_template_kwargs": {}, + "enable_thinking": true, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "thought", + "output": "Plan.Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "end-tag-after-thought", + "output": "Plan.AB", + "upstream": { + "content": "AB", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "AB", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "thought-after-thought", + "output": "Plan.AXC", + "upstream": { + "content": "AXC", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "AXC", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "end-tag-after-thought-and-newlines", + "output": "\n\nPlan.\n\n\nA B", + "upstream": { + "content": "\n\nA B", + "reasoning_content": "\nPlan.\n", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "\n\nA B", + "reasoning_content": "\nPlan.\n", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end-after-thought", + "output": "Plan.A ", + "upstream": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_partial": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/templates/Llama-3_2-3B-Instruct.jinja", + "format": "llama3", + "chat_template_kwargs": {}, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/GLM-4.6.jinja", + "format": "glm45", + "chat_template_kwargs": {}, + "enable_thinking": true, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "thought", + "output": "Plan.Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "end-tag-after-thought", + "output": "Plan.AB", + "upstream": { + "content": "AB", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "AB", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "thought-after-thought", + "output": "Plan.AXC", + "upstream": { + "content": "AXC", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "AXC", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "end-tag-after-thought-and-newlines", + "output": "\n\nPlan.\n\n\nA B", + "upstream": { + "content": "\nA B", + "reasoning_content": "\nPlan.\n", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "\nA B", + "reasoning_content": "\nPlan.\n", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end-after-thought", + "output": "Plan.A ", + "upstream": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_partial": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/poolside-Laguna-XS.2.jinja", + "format": "laguna", + "chat_template_kwargs": {}, + "enable_thinking": false, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "thought", + "output": "Plan.Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "end-tag-after-thought", + "output": "Plan.AB", + "upstream": { + "content": "AB", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "AB", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "thought-after-thought", + "output": "Plan.AXC", + "upstream": { + "content": "AXC", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "AXC", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "end-tag-after-thought-and-newlines", + "output": "\n\nPlan.\n\n\nA B", + "upstream": { + "content": "\n\nA B", + "reasoning_content": "\nPlan.\n", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "\n\nA B", + "reasoning_content": "\nPlan.\n", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end-after-thought", + "output": "Plan.A ", + "upstream": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_partial": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/LFM2-8B-A1B.jinja", + "format": "lfm2", + "chat_template_kwargs": {}, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/moonshotai-Kimi-K2.jinja", + "format": "kimiK2", + "chat_template_kwargs": {}, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "thought", + "output": "Plan.Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "end-tag-after-thought", + "output": "Plan.AB", + "upstream": { + "content": "AB", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "AB", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "thought-after-thought", + "output": "Plan.AXC", + "upstream": { + "content": "AXC", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "AXC", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end-after-thought", + "output": "Plan.A ", + "upstream": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_partial": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-R1-Distill-Qwen-32B.jinja", + "format": "deepseekR1", + "chat_template_kwargs": {}, + "enable_thinking": false, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-V3.2.jinja", + "format": "deepseekV32", + "chat_template_kwargs": {}, + "enable_thinking": false, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-V4.jinja", + "format": "deepseekV4", + "chat_template_kwargs": {}, + "enable_thinking": false, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/ibm-granite-granite-3.3-2B-Instruct.jinja", + "format": "granite", + "chat_template_kwargs": {}, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "thought", + "output": "Plan.Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "end-tag-after-thought", + "output": "Plan.AB", + "upstream": { + "content": "AB", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "AB", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "thought-after-thought", + "output": "Plan.AXC", + "upstream": { + "content": "AXC", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "AXC", + "reasoning_content": "Plan.", + "tool_calls": [] + } + }, + { + "name": "end-tag-after-thought-and-newlines", + "output": "\n\nPlan.\n\n\nA B", + "upstream": { + "content": "\n\nA B", + "reasoning_content": "\nPlan.\n", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "\n\nA B", + "reasoning_content": "\nPlan.\n", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end-after-thought", + "output": "Plan.A ", + "upstream": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + }, + "upstream_partial": { + "content": "A ", + "reasoning_content": "Plan.", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/openbmb-MiniCPM5-1B.jinja", + "format": "minicpm5", + "chat_template_kwargs": {}, + "enable_thinking": false, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/mistralai-Mistral-Nemo-Instruct-2407.jinja", + "format": "mistralNemo", + "chat_template_kwargs": {}, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/Apertus-8B-Instruct.jinja", + "format": "apertus", + "chat_template_kwargs": {}, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.<|inner_suffix|>Answer.", + "upstream": { + "content": "Plan.<|inner_suffix|>Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.<|inner_suffix|>Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.<|inner_suffix|>Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "<|inner_suffix|>Answer.", + "upstream": { + "content": "<|inner_suffix|>Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "<|inner_suffix|>Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n<|inner_suffix|>\n\nAnswer.", + "upstream": { + "content": "Plan\n<|inner_suffix|>\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n<|inner_suffix|>\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.<|inner_suffix|>Use 3 <", + "upstream": { + "content": "Plan.<|inner_suffix|>Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.<|inner_suffix|>Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.<|inner_suffix|>x <|inner_suffi", + "upstream": { + "content": "Plan.<|inner_suffix|>x <|inner_suffi", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.<|inner_suffix|>x <|inner_suffi", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.<|inner_suffix|>x <|inner_suffi", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "repeated-end-tag", + "output": "Plan.<|inner_suffix|>A<|inner_suffix|>B", + "upstream": { + "content": "Plan.<|inner_suffix|>A<|inner_suffix|>B", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.<|inner_suffix|>A<|inner_suffix|>B", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.<|inner_suffix|>A<|inner_prefix|>X<|inner_suffix|>C", + "upstream": { + "content": "Plan.<|inner_suffix|>A<|inner_prefix|>X<|inner_suffix|>C", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.<|inner_suffix|>A<|inner_prefix|>X<|inner_suffix|>C", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.<|inner_suffi", + "upstream": { + "content": "Plan.<|inner_suffi", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.<|inner_suffi", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.<|inner_suffi", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "thought-after-text", + "output": "Answer <|inner_prefix|>X<|inner_suffix|> more.", + "upstream": { + "content": "Answer <|inner_prefix|>X<|inner_suffix|> more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer <|inner_prefix|>X<|inner_suffix|> more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer <|inner_prefix|>X", + "upstream": { + "content": "Answer <|inner_prefix|>X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer <|inner_prefix|>X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer <|inner_prefix|>X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi <|inner_prefix|>", + "upstream": { + "content": "Hi <|inner_prefix|>", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi <|inner_prefix|>", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi <|inner_prefix|>", + "reasoning_content": "", + "tool_calls": [] + } + } + ] + }, + { + "template": "test/fixtures/llama_cpp_templates/tencent-Hy3.jinja", + "format": "hunyuanV3", + "chat_template_kwargs": {}, + "enable_thinking": false, + "cases": [ + { + "name": "end-tag-without-start-tag", + "output": "Plan.Answer.", + "upstream": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Plan.Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-first", + "output": "Answer.", + "upstream": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-around-newlines", + "output": "Plan\n\n\nAnswer.", + "upstream": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan\n\n\nAnswer.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-partial-tag", + "output": "Plan.Use 3 <", + "upstream": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.Use 3 <", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-unfinished-end-tag", + "output": "Plan.x x x x AB", + "upstream": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AB", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "end-tag-then-thought", + "output": "Plan.AXC", + "upstream": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Plan.AXC", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-end-tag", + "output": "Plan.X more.", + "upstream": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X more.", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "unfinished-thought-after-text", + "output": "Answer X", + "upstream": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Answer X", + "reasoning_content": "", + "tool_calls": [] + } + }, + { + "name": "start-tag-at-end", + "output": "Hi ", + "upstream": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_without_tools": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + }, + "upstream_partial": { + "content": "Hi ", + "reasoning_content": "", + "tool_calls": [] + } } ] } diff --git a/test/fixtures/closed_thought_upstream.md b/test/fixtures/closed_thought_upstream.md index 131b0a97b..5fef43f87 100644 --- a/test/fixtures/closed_thought_upstream.md +++ b/test/fixtures/closed_thought_upstream.md @@ -58,3 +58,19 @@ result for every case. `upstream_partial` is again `common_chat_peg_parse` with `is_partial = true`. The Hermes 2 Pro and Qwen3-Coder templates have no `thought` case: with no reasoning in the template, llama.cpp keeps even a leading thought as content, which llamadart does not. + +For issue #1865, `no_thought_templates` adds one vendored template per format +that takes a thought only at the start of the reply after a prompt that opens +no thought, recorded with the same probe at `cd959bae9`: Gemma 2 and SmolLM3 +(generic), Llama 3.2, GLM-4.6, Laguna XS.2, LFM2-8B-A1B, Kimi K2, DeepSeek R1 +Distill Qwen 32B, V3.2 and V4, Granite 3.3, MiniCPM5, Mistral Nemo, Apertus and +Hunyuan V3 (`tencent-Hy3`), with thinking flags under which llamadart opens no +thought. It also adds cases to the #1858 entries. Besides a thought that the +reply did not open, the cases put a start tag after text and tags after a +leading thought: llama.cpp takes a thought only where the reply starts with the +start tag and keeps every other thinking tag as content. The `thought`, +`*-after-thought*` cases appear only for templates whose reasoning llama.cpp +parses; with the others it keeps even a leading thought as content, which +llamadart does not. Two differences outside #1865 are left out: Kimi K2 with +the tool takes no thought after leading whitespace, and LFM2's partial parse +drops a trailing partial end tag after content. diff --git a/test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-V3.2.jinja b/test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-V3.2.jinja new file mode 100644 index 000000000..98d56589f --- /dev/null +++ b/test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-V3.2.jinja @@ -0,0 +1,141 @@ +{%- if not add_generation_prompt is defined -%} + {%- set add_generation_prompt = false -%} +{%- endif -%} +{%- if not thinking is defined -%} + {%- if enable_thinking is defined -%} + {%- set thinking = enable_thinking -%} + {%- else -%} + {%- set thinking = false -%} + {%- endif -%} +{%- endif -%} +{%- set dsml_token = '|DSML|' -%} +{%- set thinking_start_token = '' -%} +{%- set thinking_end_token = '' -%} +{%- set tools_header = '## Tools\n\nYou have access to a set of tools you can use to answer the user\'s question.\nYou can invoke functions by writing a "<' + dsml_token + 'function_calls>" block like the following as part of your reply to the user:\n<' + dsml_token + 'function_calls>\n<' + dsml_token + 'invoke name="$FUNCTION_NAME">\n<' + dsml_token + 'parameter name="$PARAMETER_NAME" string="true|false">$PARAMETER_VALUE\n...\n\n<' + dsml_token + 'invoke name="$FUNCTION_NAME2">\n...\n\n\n\nString and scalar parameters should be specified as is without any escaping or quotes, while lists and objects should use JSON format. The "string" attribute should be set to "true" for string type parameters and "false" for other types (numbers, booleans, arrays, objects).\n\nIf the thinking_mode is enabled, then after function results you should strongly consider outputting a thinking block. Here is an example:\n\n<' + dsml_token + 'function_calls>\n...\n\n\n\n...\n\n\n' + thinking_start_token + '...thinking about results' + thinking_end_token + '\n\nHere are the functions available in JSONSchema format:\n\n' -%} +{%- set tools_footer = '\n' -%} +{%- set ns = namespace(system_prompt='', is_first_sp=true) -%} +{%- for message in messages -%} + {%- if message['role'] == 'system' -%} + {%- if ns.is_first_sp -%} + {%- set ns.system_prompt = ns.system_prompt + (message['content'] or '') -%} + {%- set ns.is_first_sp = false -%} + {%- else -%} + {%- set ns.system_prompt = ns.system_prompt + '\n\n' + (message['content'] or '') -%} + {%- endif -%} + {%- endif -%} +{%- endfor -%} +{%- if tools is defined and tools -%} + {%- set ts = namespace(schemas='') -%} + {%- for tool in tools -%} + {%- if tool['type'] == 'function' -%} + {%- set ts.schemas = ts.schemas + (tool['function'] | tojson) + '\n' -%} + {%- endif -%} + {%- endfor -%} + {%- if ns.system_prompt -%} + {%- set ns.system_prompt = ns.system_prompt + '\n\n' + tools_header + ts.schemas + tools_footer -%} + {%- else -%} + {%- set ns.system_prompt = tools_header + ts.schemas + tools_footer -%} + {%- endif -%} +{%- endif -%} +{{- bos_token -}} +{{- ns.system_prompt -}} +{%- set last_user_idx = namespace(value=-1) -%} +{%- for message in messages -%} + {%- if message['role'] == 'user' or message['role'] == 'developer' -%} + {%- set last_user_idx.value = loop.index0 -%} + {%- endif -%} +{%- endfor -%} +{%- set state = namespace(pending_asst_marker=false, pending_tool_marker=false) -%} +{%- for message in messages -%} + {%- if message['role'] == 'user' -%} + {{- '<|User|>' + (message['content'] or '') -}} + {%- set state.pending_asst_marker = true -%} + {%- set state.pending_tool_marker = false -%} + {%- elif message['role'] == 'assistant' -%} + {%- set is_after_last_user = loop.index0 > last_user_idx.value -%} + {%- if state.pending_asst_marker -%} + {{- '<|Assistant|>' -}} + {%- if is_after_last_user and thinking -%} + {{- thinking_start_token -}} + {%- if message['reasoning_content'] is defined and message['reasoning_content'] -%} + {{- message['reasoning_content'] -}} + {%- endif -%} + {{- thinking_end_token -}} + {%- else -%} + {{- thinking_end_token -}} + {%- endif -%} + {%- elif state.pending_tool_marker -%} + {%- if is_after_last_user and thinking -%} + {{- '\n\n' + thinking_start_token -}} + {%- if message['reasoning_content'] is defined and message['reasoning_content'] -%} + {{- message['reasoning_content'] -}} + {%- endif -%} + {{- thinking_end_token -}} + {%- else -%} + {{- '\n\n' + thinking_end_token -}} + {%- endif -%} + {%- endif -%} + {%- set state.pending_asst_marker = false -%} + {%- set state.pending_tool_marker = false -%} + {%- if message['content'] is defined and message['content'] -%} + {{- message['content'] -}} + {%- endif -%} + {%- if message['tool_calls'] -%} + {{- '\n\n<' + dsml_token + 'function_calls>\n' -}} + {%- for tool in message['tool_calls'] -%} + {%- set func = tool['function'] -%} + {{- '<' + dsml_token + 'invoke name="' + func['name'] + '">\n' -}} + {%- set args = func['arguments'] -%} + {%- if args is string -%} + {%- set args = args | from_json -%} + {%- endif -%} + {%- for key, val in args.items() -%} + {%- if val is string -%} + {{- '<' + dsml_token + 'parameter name="' + key + '" string="true">' + val + '\n' -}} + {%- else -%} + {{- '<' + dsml_token + 'parameter name="' + key + '" string="false">' + (val | tojson) + '\n' -}} + {%- endif -%} + {%- endfor -%} + {{- '\n' -}} + {%- endfor -%} + {{- '' -}} + {%- endif -%} + {{- '<|end▁of▁sentence|>' -}} + {%- elif message['role'] == 'tool' -%} + {%- set outer_index = loop.index0 -%} + {%- set assistant_idx = namespace(value=-1) -%} + {%- for prev_msg in messages -%} + {%- if prev_msg['role'] == 'assistant' and prev_msg['tool_calls'] and loop.index0 < outer_index -%} + {%- set assistant_idx.value = loop.index0 -%} + {%- endif -%} + {%- endfor -%} + {%- set call_order = outer_index - assistant_idx.value -%} + {%- set assistant_msg = messages[assistant_idx.value] -%} + {%- set tool_call_count = assistant_msg['tool_calls'] | length -%} + {%- if call_order == 1 -%} + {{- '\n\n' -}} + {%- endif -%} + {{- '\n' + (message['content'] or '') + '' -}} + {%- if call_order == tool_call_count -%} + {{- '\n' -}} + {%- set state.pending_asst_marker = false -%} + {%- set state.pending_tool_marker = true -%} + {%- endif -%} + {%- endif -%} +{%- endfor -%} +{%- if add_generation_prompt -%} + {%- if state.pending_asst_marker -%} + {{- '<|Assistant|>' -}} + {%- if thinking -%} + {{- thinking_start_token -}} + {%- else -%} + {{- thinking_start_token + thinking_end_token -}} + {%- endif -%} + {%- elif state.pending_tool_marker -%} + {%- if thinking -%} + {{- '\n\n' + thinking_start_token -}} + {%- else -%} + {{- '\n\n' + thinking_start_token + thinking_end_token -}} + {%- endif -%} + {%- endif -%} +{%- endif -%} diff --git a/test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-V4.jinja b/test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-V4.jinja new file mode 100644 index 000000000..c7819149a --- /dev/null +++ b/test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-V4.jinja @@ -0,0 +1,137 @@ +{%- if not add_generation_prompt is defined -%} + {%- set add_generation_prompt = false -%} +{%- endif -%} +{%- if not thinking is defined -%} + {%- if enable_thinking is defined -%} + {%- set thinking = enable_thinking -%} + {%- else -%} + {%- set thinking = false -%} + {%- endif -%} +{%- endif -%} +{%- if not drop_thinking is defined -%} + {%- set drop_thinking = true -%} +{%- endif -%} +{%- set dsml_token = '|DSML|' -%} +{%- set thinking_start_token = '' -%} +{%- set thinking_end_token = '' -%} +{%- set reasoning_effort_max = 'Reasoning Effort: Absolute maximum with no shortcuts permitted.\nYou MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.\nExplicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.\n\n' -%} +{%- set response_format_template = '## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n' -%} +{%- set has_tools = false -%} +{%- set tools_header = '## Tools\n\nYou have access to a set of tools to help answer the user\'s question. You can invoke tools by writing a "<' + dsml_token + 'tool_calls>" block like the following:\n\n<' + dsml_token + 'tool_calls>\n<' + dsml_token + 'invoke name="$TOOL_NAME">\n<' + dsml_token + 'parameter name="$PARAMETER_NAME" string="true|false">$PARAMETER_VALUE\n...\n\n<' + dsml_token + 'invoke name="$TOOL_NAME2">\n...\n\n\n\nString parameters should be specified as is and set `string="true"`. For all other types (numbers, booleans, arrays, objects), pass the value in JSON format and set `string="false"`.\n\nIf thinking_mode is enabled (triggered by ' + thinking_start_token + '), you MUST output your complete reasoning inside ' + thinking_start_token + '...' + thinking_end_token + ' BEFORE any tool calls or final response.\n\nOtherwise, output directly after ' + thinking_end_token + ' with tool calls or final response.\n\n### Available Tool Schemas\n\n' -%} +{%- set tools_footer = '\nYou MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.\n' -%} +{%- set ns = namespace(system_prompt='', is_first_sp=true, has_tool_calls=false) -%} +{%- for message in messages -%} + {%- if message['role'] == 'system' -%} + {%- if ns.is_first_sp -%} + {%- set ns.system_prompt = ns.system_prompt + (message['content'] or '') -%} + {%- set ns.is_first_sp = false -%} + {%- else -%} + {%- set ns.system_prompt = ns.system_prompt + '\n\n' + (message['content'] or '') -%} + {%- endif -%} + {%- endif -%} +{%- endfor -%} +{%- if tools is defined and tools -%} + {%- set has_tools = true -%} + {%- set ts = namespace(schemas='') -%} + {%- for tool in tools -%} + {%- if tool['type'] == 'function' -%} + {%- set ts.schemas = ts.schemas + (tool['function'] | tojson) + '\n' -%} + {%- endif -%} + {%- endfor -%} + {%- if ns.system_prompt -%} + {%- set ns.system_prompt = ns.system_prompt + '\n\n' + tools_header + ts.schemas + tools_footer -%} + {%- else -%} + {%- set ns.system_prompt = tools_header + ts.schemas + tools_footer -%} + {%- endif -%} +{%- endif -%} +{%- if response_format is defined -%} + {%- if ns.system_prompt -%} + {%- set ns.system_prompt = ns.system_prompt + '\n\n' -%} + {%- endif -%} + {%- set ns.system_prompt = ns.system_prompt + response_format_template + (response_format | tojson) -%} +{%- endif -%} +{{- bos_token -}} +{%- if messages and thinking and reasoning_effort is defined and reasoning_effort == 'max' -%} + {{- reasoning_effort_max -}} +{%- endif -%} +{{- ns.system_prompt -}} +{%- set last_user_idx = namespace(value=-1) -%} +{%- for message in messages -%} + {%- if message['role'] == 'user' or message['role'] == 'developer' or message['role'] == 'tool' -%} + {%- set last_user_idx.value = loop.index0 -%} + {%- endif -%} +{%- endfor -%} +{%- set state = namespace(in_user=false) -%} +{%- for message in messages -%} + {%- if message['role'] == 'tool' -%} + {%- set ns.has_tool_calls = true -%} + {%- endif -%} +{%- endfor -%} +{%- for message in messages -%} + {%- if message['role'] == 'user' or message['role'] == 'developer' -%} + {%- if state.in_user -%} + {{- '\n\n' -}} + {%- else -%} + {{- '<|User|>' -}} + {%- set state.in_user = true -%} + {%- endif -%} + {{- message['content'] or '' -}} + {%- elif message['role'] == 'tool' -%} + {%- if state.in_user -%} + {{- '\n\n' -}} + {%- else -%} + {{- '<|User|>' -}} + {%- set state.in_user = true -%} + {%- endif -%} + {{- '' + (message['content'] or '') + '' -}} + {%- elif message['role'] == 'assistant' -%} + {%- set state.in_user = false -%} + {{- '<|Assistant|>' -}} + {%- set is_after_last_user = loop.index0 > last_user_idx.value -%} + {%- set keep_reasoning = thinking and ((not drop_thinking) or has_tools or is_after_last_user or ns.has_tool_calls) -%} + {%- if keep_reasoning -%} + {{- thinking_start_token -}} + {%- if message['reasoning_content'] is defined and message['reasoning_content'] -%} + {{- message['reasoning_content'] -}} + {%- endif -%} + {{- thinking_end_token -}} + {%- else -%} + {{- thinking_end_token -}} + {%- endif -%} + {%- if message['content'] is defined and message['content'] -%} + {{- message['content'] -}} + {%- endif -%} + {%- if message['tool_calls'] -%} + {{- '\n\n<' + dsml_token + 'tool_calls>\n' -}} + {%- for tool in message['tool_calls'] -%} + {%- set func = tool['function'] -%} + {{- '<' + dsml_token + 'invoke name="' + func['name'] + '">\n' -}} + {%- set args = func['arguments'] -%} + {%- if args is string -%} + {%- set args = args | from_json -%} + {%- endif -%} + {%- for key, val in args.items() -%} + {%- if val is string -%} + {{- '<' + dsml_token + 'parameter name="' + key + '" string="true">' + val + '\n' -}} + {%- else -%} + {{- '<' + dsml_token + 'parameter name="' + key + '" string="false">' + (val | tojson) + '\n' -}} + {%- endif -%} + {%- endfor -%} + {%- if not args -%} + {{- '\n' -}} + {%- endif -%} + {{- '\n' -}} + {%- endfor -%} + {{- '' -}} + {%- endif -%} + {{- '<|end▁of▁sentence|>' -}} + {%- endif -%} +{%- endfor -%} +{%- if add_generation_prompt -%} + {{- '<|Assistant|>' -}} + {%- if thinking -%} + {{- thinking_start_token -}} + {%- else -%} + {{- thinking_end_token -}} + {%- endif -%} +{%- endif -%} diff --git a/test/fixtures/llama_cpp_templates/ibm-granite-granite-3.3-2B-Instruct.jinja b/test/fixtures/llama_cpp_templates/ibm-granite-granite-3.3-2B-Instruct.jinja new file mode 100644 index 000000000..f50653609 --- /dev/null +++ b/test/fixtures/llama_cpp_templates/ibm-granite-granite-3.3-2B-Instruct.jinja @@ -0,0 +1,59 @@ +{# Alias tools -> available_tools #} +{%- if tools and not available_tools -%} + {%- set available_tools = tools -%} +{%- endif -%} +{%- if messages[0]['role'] == 'system' %} + {%- set system_message = messages[0]['content'] %} + {%- set loop_messages = messages[1:] %} + {%- else %} + {%- set system_message = "Knowledge Cutoff Date: April 2024. Today's Date: " + strftime_now('%B %d, %Y') + ". You are Granite, developed by IBM." %} + {%- if available_tools and documents %} + {%- set system_message = system_message + " You are a helpful assistant with access to the following tools. When a tool is required to answer the user's query, respond only with <|tool_call|> followed by a JSON list of tools used. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request. Write the response to the user's input by strictly aligning with the facts in the provided documents. If the information needed to answer the question is not available in the documents, inform the user that the question cannot be answered based on the available data." %} + {%- elif available_tools %} + {%- set system_message = system_message + " You are a helpful assistant with access to the following tools. When a tool is required to answer the user's query, respond only with <|tool_call|> followed by a JSON list of tools used. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request." %} + {%- elif documents %} + {%- set system_message = system_message + " Write the response to the user's input by strictly aligning with the facts in the provided documents. If the information needed to answer the question is not available in the documents, inform the user that the question cannot be answered based on the available data." %} + {%- elif thinking %} + {%- set system_message = system_message + " You are a helpful AI assistant. +Respond to every user query in a comprehensive and detailed way. You can write down your thoughts and reasoning process before responding. In the thought process, engage in a comprehensive cycle of analysis, summarization, exploration, reassessment, reflection, backtracing, and iteration to develop well-considered thinking process. In the response section, based on various attempts, explorations, and reflections from the thoughts section, systematically present the final solution that you deem correct. The response should summarize the thought process. Write your thoughts between and write your response between for each user query." %} + {%- else %} + {%- set system_message = system_message + " You are a helpful AI assistant." %} + {%- endif %} + {%- if 'citations' in controls and documents %} + {%- set system_message = system_message + ' +Use the symbols <|start_of_cite|> and <|end_of_cite|> to indicate when a fact comes from a document in the search result, e.g <|start_of_cite|> {document_id: 1}my fact <|end_of_cite|> for a fact from document 1. Afterwards, list all the citations with their corresponding documents in an ordered list.' %} + {%- endif %} + {%- if 'hallucinations' in controls and documents %} + {%- set system_message = system_message + ' +Finally, after the response is written, include a numbered list of sentences from the response with a corresponding risk value that are hallucinated and not based in the documents.' %} + {%- endif %} + {%- set loop_messages = messages %} + {%- endif %} + {{- '<|start_of_role|>system<|end_of_role|>' + system_message + '<|end_of_text|> +' }} + {%- if available_tools %} + {{- '<|start_of_role|>available_tools<|end_of_role|>' }} + {{- available_tools | tojson(indent=4) }} + {{- '<|end_of_text|> +' }} + {%- endif %} + {%- if documents %} + {%- for document in documents %} + {{- '<|start_of_role|>document {"document_id": "' + document['doc_id'] | string + '"}<|end_of_role|> +' }} + {{- document['text'] }} + {{- '<|end_of_text|> +' }} + {%- endfor %} + {%- endif %} + {%- for message in loop_messages %} + {{- '<|start_of_role|>' + message['role'] + '<|end_of_role|>' + message['content'] + '<|end_of_text|> +' }} + {%- if loop.last and add_generation_prompt %} + {{- '<|start_of_role|>assistant' }} + {%- if controls %} + {{- ' ' + controls | tojson()}} + {%- endif %} + {{- '<|end_of_role|>' }} + {%- endif %} + {%- endfor %} diff --git a/test/fixtures/llama_cpp_templates/mistralai-Mistral-Nemo-Instruct-2407.jinja b/test/fixtures/llama_cpp_templates/mistralai-Mistral-Nemo-Instruct-2407.jinja new file mode 100644 index 000000000..9c21a3f13 --- /dev/null +++ b/test/fixtures/llama_cpp_templates/mistralai-Mistral-Nemo-Instruct-2407.jinja @@ -0,0 +1,87 @@ +{%- if messages[0]["role"] == "system" %} + {%- set system_message = messages[0]["content"] %} + {%- set loop_messages = messages[1:] %} +{%- else %} + {%- set loop_messages = messages %} +{%- endif %} +{%- if not tools is defined %} + {%- set tools = none %} +{%- endif %} +{%- set user_messages = loop_messages | selectattr("role", "equalto", "user") | list %} + +{#- This block checks for alternating user/assistant messages, skipping tool calling messages #} +{%- set ns = namespace() %} +{%- set ns.index = 0 %} +{%- for message in loop_messages %} + {%- if not (message.role == "tool" or message.role == "tool_results" or (message.tool_calls is defined and message.tool_calls is not none)) %} + {%- if (message["role"] == "user") != (ns.index % 2 == 0) %} + {{- raise_exception("After the optional system message, conversation roles must alternate user/assistant/user/assistant/...") }} + {%- endif %} + {%- set ns.index = ns.index + 1 %} + {%- endif %} +{%- endfor %} + +{{- bos_token }} +{%- for message in loop_messages %} + {%- if message["role"] == "user" %} + {%- if tools is not none and (message == user_messages[-1]) %} + {{- "[AVAILABLE_TOOLS][" }} + {%- for tool in tools %} + {%- set tool = tool.function %} + {{- '{"type": "function", "function": {' }} + {%- for key, val in tool.items() if key != "return" %} + {%- if val is string %} + {{- '"' + key + '": "' + val + '"' }} + {%- else %} + {{- '"' + key + '": ' + val|tojson }} + {%- endif %} + {%- if not loop.last %} + {{- ", " }} + {%- endif %} + {%- endfor %} + {{- "}}" }} + {%- if not loop.last %} + {{- ", " }} + {%- else %} + {{- "]" }} + {%- endif %} + {%- endfor %} + {{- "[/AVAILABLE_TOOLS]" }} + {%- endif %} + {%- if loop.last and system_message is defined %} + {{- "[INST]" + system_message + "\n\n" + message["content"] + "[/INST]" }} + {%- else %} + {{- "[INST]" + message["content"] + "[/INST]" }} + {%- endif %} + {%- elif (message.tool_calls is defined and message.tool_calls is not none) %} + {{- "[TOOL_CALLS][" }} + {%- for tool_call in message.tool_calls %} + {%- set out = tool_call.function|tojson %} + {{- out[:-1] }} + {%- if not tool_call.id is defined or tool_call.id|length != 9 %} + {{- raise_exception("Tool call IDs should be alphanumeric strings with length 9!") }} + {%- endif %} + {{- ', "id": "' + tool_call.id + '"}' }} + {%- if not loop.last %} + {{- ", " }} + {%- else %} + {{- "]" + eos_token }} + {%- endif %} + {%- endfor %} + {%- elif message["role"] == "assistant" %} + {{- message["content"] + eos_token}} + {%- elif message["role"] == "tool_results" or message["role"] == "tool" %} + {%- if message.content is defined and message.content.content is defined %} + {%- set content = message.content.content %} + {%- else %} + {%- set content = message.content %} + {%- endif %} + {{- '[TOOL_RESULTS]{"content": ' + content|string + ", " }} + {%- if not message.tool_call_id is defined or message.tool_call_id|length != 9 %} + {{- raise_exception("Tool call IDs should be alphanumeric strings with length 9!") }} + {%- endif %} + {{- '"call_id": "' + message.tool_call_id + '"}[/TOOL_RESULTS]' }} + {%- else %} + {{- raise_exception("Only user and assistant roles are supported, with the exception of an initial optional system message!") }} + {%- endif %} +{%- endfor %} diff --git a/test/fixtures/llama_cpp_templates/poolside-Laguna-XS.2.jinja b/test/fixtures/llama_cpp_templates/poolside-Laguna-XS.2.jinja new file mode 100644 index 000000000..4baa3fded --- /dev/null +++ b/test/fixtures/llama_cpp_templates/poolside-Laguna-XS.2.jinja @@ -0,0 +1,132 @@ +{#- Iteration on laguna_glm_thinking_v5/chat_template.jinja -#} +{#- Adds a default system message (used when no system message is provided in `messages`). -#} +{{- "〈|EOS|〉" -}} +{%- set enable_thinking = enable_thinking | default(false) -%} +{%- set render_assistant_messages_raw = render_assistant_messages_raw | default(false) -%} +{%- set add_generation_prompt = add_generation_prompt | default(false) -%} + +{#- ───── header (system message) ───── -#} +{%- set system_message = "You are a helpful, conversationally-fluent assistant made by Poolside. You are here to be helpful to users through natural language conversations." -%} +{%- if messages and messages[0].role == "system" -%} + {%- set system_message = messages[0].content -%} +{%- endif -%} + +{%- if (system_message and system_message.strip()) or tools -%} + {{- "\n" -}} + + {%- if system_message and system_message.strip() -%} + {{- "\n" -}} + {{- system_message.rstrip() -}} + {%- endif -%} + + {%- if tools -%} + {{- "\n\n### Tools\n\n" -}} + {%- set ns = namespace(tool_string="You may call functions to assist with the user query.\n" + ~ "All available function signatures are listed below:\n" + ~ "\n") -%} + {%- for tool in tools -%} + {%- set ns.tool_string = ns.tool_string ~ (tool | tojson) ~ "\n" -%} + {%- endfor -%} + {%- if enable_thinking -%} + {%- set tool_string = ns.tool_string + "\n\n" ~ + "Wrap your thinking in '', '' tags, followed by a function call. For each function call, return an unescaped XML-like object with function name and arguments within '' and '' tags, like here:\n" ~ + " your thoughts here \n" ~ + "function-name\nargument-key\nvalue-of-argument-key\n" ~ + "" -%} + {%- else -%} + {%- set tool_string = ns.tool_string + "\n\n" ~ + "For each function call, return an unescaped XML-like object " ~ + "with function name and arguments within '' and '' tags, like here:\n" ~ + "function-name\nargument-key\nvalue-of-argument-key\n" ~ + "" -%} + {%- endif -%} + {{- tool_string -}} + {%- endif -%} + + {{- "\n\n" -}} +{%- endif -%} + +{#- ───── main loop ───── -#} +{%- for message in messages -%} + {%- set content = message.content if message.content is string else "" -%} + {%- if message.role == "user" -%} + {{- "\n" + content + "\n\n" -}} + {%- elif message.role == "assistant" -%} + {%- generation -%} + {{- "\n" -}} + {%- if render_assistant_messages_raw -%} + {#- Raw mode: prepend the generation prompt token, then dump content verbatim. -#} + {#- The generation prompt is when enable_thinking, otherwise. -#} + {#- Only prepend if content doesn't already start with it. -#} + {%- if enable_thinking -%} + {%- if not content.startswith('') -%} + {{- '' -}} + {%- endif -%} + {%- else -%} + {%- if not content.startswith('') -%} + {{- '' -}} + {%- endif -%} + {%- endif -%} + {{- content -}} + {#- Append closing tag if content doesn't already end with it. -#} + {%- if not content.endswith('\n') and not content.endswith('') -%} + {{- '\n' -}} + {%- endif -%} + {{- "\n" -}} + {%- else -%} + {#- Extract reasoning content from message.reasoning (vLLM field name) or message.reasoning_content, or from tags -#} + {%- set reasoning_content = '' %} + {%- if message.reasoning is string %} + {%- set reasoning_content = message.reasoning %} + {%- elif message.reasoning_content is string %} + {%- set reasoning_content = message.reasoning_content %} + {%- endif %} + {#- Always strip tags from content if present to avoid duplication -#} + {%- if '' in content %} + {%- if not reasoning_content %} + {%- set reasoning_content = content.split('')[0].rstrip('\n').split('')[-1].lstrip('\n') %} + {%- endif %} + {%- set content = content.split('')[-1].lstrip('\n') %} + {%- endif %} + {#- Display reasoning content for all messages -#} + {%- if reasoning_content -%} + {{- '\n' + reasoning_content.strip() + '\n\n' -}} + {%- else -%} + {{- '\n' -}} + {%- endif -%} + {#- Display main content -#} + {%- if content.strip() -%} + {{- content.strip() ~ "\n" -}} + {%- endif -%} + {%- if message.tool_calls -%} + {%- for tool_call in message.tool_calls -%} + {%- set function_data = tool_call.function -%} + {{- '' + function_data.name }} + {% set _args = function_data.arguments %} + {%- for k, v in _args.items() -%} + {{- "" ~ k ~ "\n" -}} + {{- ""}}{{ v | tojson(ensure_ascii=False) if v is not string else v }}{{ "\n" -}} + {%- endfor -%} + {{- "\n" -}} + {%- endfor -%} + {%- endif -%} + {{- "\n" -}} + {%- endif -%} + {%- endgeneration -%} + {%- elif message.role == "tool" -%} + {{- "\n" + content + "\n\n" -}} + {%- elif message.role == "system" and loop.index0 != 0 -%} + {#- Render additional system messages (skip the first one which is handled separately in the header) -#} + {{- "\n" + content + "\n\n" -}} + {%- endif -%} +{%- endfor -%} +{#- ───── generation prompt ───── -#} +{%- if add_generation_prompt -%} + {{- "\n" -}} + {#- ───── Include reasoning mode directive ───── -#} + {%- if not enable_thinking %} + {{- '' -}} + {%- else %} + {{- '' -}} + {%- endif %} +{%- endif -%} diff --git a/test/integration/core/engine/closed_thought_upstream_test.dart b/test/integration/core/engine/closed_thought_upstream_test.dart index 834ce14ae..a7c3d9f2f 100644 --- a/test/integration/core/engine/closed_thought_upstream_test.dart +++ b/test/integration/core/engine/closed_thought_upstream_test.dart @@ -542,30 +542,43 @@ void main() { in thinkingFlag == null ? const [false, true] : [thinkingFlag]) { group('${templatePath.split('/').last}, thinking: $enableThinking', () { late LlamaChatTemplateResult rendered; + // Without tools, as `upstream_without_tools` was recorded, so a + // generic template renders content-only. Hermes 2 Pro needs tools to + // render, and keeps its render with them. + late LlamaChatTemplateResult renderedWithoutTools; - setUpAll( - () => rendered = ChatTemplateEngine.render( - templateSource: File(templatePath).readAsStringSync(), - messages: [ - LlamaChatMessage.fromText( - role: LlamaChatRole.user, - text: _fixture['user_prompt'] as String, - ), - ], - metadata: const {}, - tools: [_weatherTool], - chatTemplateKwargs: kwargs, - enableThinking: enableThinking, - ), - ); + setUpAll(() { + LlamaChatTemplateResult render(List? tools) => + ChatTemplateEngine.render( + templateSource: File(templatePath).readAsStringSync(), + messages: [ + LlamaChatMessage.fromText( + role: LlamaChatRole.user, + text: _fixture['user_prompt'] as String, + ), + ], + metadata: const {}, + tools: tools, + chatTemplateKwargs: kwargs, + enableThinking: enableThinking, + ); + rendered = render([_weatherTool]); + try { + renderedWithoutTools = render(null); + } on LlamaException { + renderedWithoutTools = rendered; + } + }); test('leaves no thought open', () { expect( rendered.format, ChatFormat.values.byName(entry['format'] as String).index, ); - expect(rendered.thinkingForcedOpen, isFalse); - expect(thoughtClosedForThinkingOffOf(rendered), isFalse); + for (final result in [rendered, renderedWithoutTools]) { + expect(result.thinkingForcedOpen, isFalse); + expect(thoughtClosedForThinkingOffOf(result), isFalse); + } }); for (final c in (entry['cases'] as List).cast>()) { @@ -609,8 +622,9 @@ void main() { ]); final upstreamReasoning = (upstream['reasoning_content'] as String).trim(); + final render = withTools ? rendered : renderedWithoutTools; final parsed = parseRenderedOutput( - rendered.format, + render.format, output, parseToolCalls: withTools, thoughtClosedForThinkingOff: false, @@ -642,7 +656,7 @@ void main() { for (final tokens in _chunkings(output)) { final deltas = (await ChatCompletionStreamParser.parse( tokenStream: Stream.fromIterable(tokens), - templateResult: rendered, + templateResult: render, parseToolCallsEnabled: withTools, enableThinking: enableThinking, modelName: 'test-model', @@ -710,6 +724,59 @@ void main() { } }); + test( + 'only formats with the trait take a thought only at the reply start', + () { + for (final registration in chatFormatRegistrations) { + final name = registration.format.name; + final index = registration.format.index; + final tags = ChatTemplateEngine.thinkingTagsFor(index); + final output = 'Answer ${tags.startTag}X${tags.endTag} more.'; + final takesLeadingOnly = + registration.traits.takesThoughtOnlyAtReplyStart; + final ChatParseResult public; + try { + public = ChatTemplateEngine.parse( + index, + output, + parseToolCalls: false, + ); + } on StateError { + // PEG formats parse only with the parser their render returns. + expect(takesLeadingOnly, isFalse, reason: name); + continue; + } + ChatParseResult rendered({bool thinkingForcedOpen = false}) => + parseRenderedOutput( + index, + output, + parseToolCalls: false, + thinkingForcedOpen: thinkingForcedOpen, + thoughtClosedForThinkingOff: false, + ); + if (takesLeadingOnly) { + expect(public.reasoningContent, 'X', reason: name); + expect(rendered().reasoningContent, isNull, reason: name); + expect(rendered().content, output, reason: name); + } else if (registration.format != ChatFormat.seedOss) { + expect(rendered().content, public.content, reason: name); + expect(rendered().reasoningContent, public.reasoningContent); + } + final forced = ChatTemplateEngine.parse( + index, + output, + parseToolCalls: false, + thinkingForcedOpen: true, + ); + expect( + rendered(thinkingForcedOpen: true).reasoningContent, + forced.reasoningContent, + reason: name, + ); + } + }, + ); + // With thinking off, a handler closes a thought its template opened. The // stream then holds a reply's leading text where the parse may still end // that thought at an end tag. diff --git a/test/integration/core/template/lfm2_reasoning_boundary_test.dart b/test/integration/core/template/lfm2_reasoning_boundary_test.dart index 0a172529c..a35e30745 100644 --- a/test/integration/core/template/lfm2_reasoning_boundary_test.dart +++ b/test/integration/core/template/lfm2_reasoning_boundary_test.dart @@ -143,13 +143,15 @@ void main() { cities: [], ), ( - name: 'several thoughts and outside siblings', + // After a prompt that opens no thought, llama.cpp takes a thought only + // at the start of the reply; the calls in these stay inert text. + name: 'thoughts after the reply start and outside siblings', raw: 'Before$_outerA $_inner ' 'B $_inner$_outer After', forced: false, - content: 'Before After', - reasoning: 'A $_inner\nB $_inner', + content: 'BeforeA $_inner B $_inner After', + reasoning: null, cities: ['Rome', 'Rome'], ), ( @@ -203,7 +205,7 @@ void main() { cancelled: () => ending == 'cancel', ).collect(); expect(completion.text, scenario.content, reason: '$ending/$tokens'); - expect(completion.thinking, scenario.reasoning); + expect(completion.thinking, scenario.reasoning ?? ''); expect( completion.toolCalls.map((call) => call.arguments['city']), ending == 'length' ? isEmpty : scenario.cities, diff --git a/test/unit/core/engine/chat_completion_stream_parser_test.dart b/test/unit/core/engine/chat_completion_stream_parser_test.dart index 8954e62f3..e90c1ceda 100644 --- a/test/unit/core/engine/chat_completion_stream_parser_test.dart +++ b/test/unit/core/engine/chat_completion_stream_parser_test.dart @@ -2960,7 +2960,7 @@ void main() { ('Sunny.', '', 'Sunny.'), (' ', '', 'Sunny. '), - ]); + ], forcedOpen: true); }); test('every format streams a split end tag as its parse takes it ' diff --git a/test/unit/core/template/forced_open_grammar_test.dart b/test/unit/core/template/forced_open_grammar_test.dart index 106935e55..8d04704b9 100644 --- a/test/unit/core/template/forced_open_grammar_test.dart +++ b/test/unit/core/template/forced_open_grammar_test.dart @@ -48,7 +48,10 @@ const Set _closesThoughtItself = { // Required-call grammars that take any text before the calls, so a thought and // its end tag as well. const Set _takesTextBeforeCalls = { + ChatFormat.deepseekV32, + ChatFormat.deepseekV4, ChatFormat.kimiK3, + ChatFormat.laguna, ChatFormat.minimaxM3, }; diff --git a/test/unit/core/template/template_caps_cache_fixtures_test.dart b/test/unit/core/template/template_caps_cache_fixtures_test.dart index 04d53daf2..d91098abd 100644 --- a/test/unit/core/template/template_caps_cache_fixtures_test.dart +++ b/test/unit/core/template/template_caps_cache_fixtures_test.dart @@ -64,6 +64,15 @@ const Map _pinnedCaps = { 'test/fixtures/llama_cpp_templates/NousResearch-Hermes-2-Pro-Llama-3-8B-tool_use.jinja': '11111001', 'test/fixtures/llama_cpp_templates/Qwen-QwQ-32B.jinja': '11111011', + // The #1865 templates are pinned from Dart detection, without a server probe. + 'test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-V3.2.jinja': + '11111011', + 'test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-V4.jinja': '11111011', + 'test/fixtures/llama_cpp_templates/ibm-granite-granite-3.3-2B-Instruct.jinja': + '10101010', + 'test/fixtures/llama_cpp_templates/mistralai-Mistral-Nemo-Instruct-2407.jinja': + '11111001', + 'test/fixtures/llama_cpp_templates/poolside-Laguna-XS.2.jinja': '11111011', 'test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-R1-Distill-Qwen-32B.jinja': '11011011', 'test/fixtures/llama_cpp_templates/deepseek-ai-DeepSeek-V3.1.jinja': diff --git a/test/unit/core/template/thinking_utils_test.dart b/test/unit/core/template/thinking_utils_test.dart index da4276082..9e786fb7c 100644 --- a/test/unit/core/template/thinking_utils_test.dart +++ b/test/unit/core/template/thinking_utils_test.dart @@ -198,6 +198,69 @@ void main() { }); }); + group('withThoughtOnlyAtReplyStart', () { + ThinkingExtraction leading( + String text, { + bool thinkingForcedOpen = false, + String startTag = '', + String endTag = '', + }) => withThoughtOnlyAtReplyStart( + () => extractThinking( + text, + thinkingForcedOpen: thinkingForcedOpen, + startTag: startTag, + endTag: endTag, + ), + ); + + test('takes only a thought that starts the text', () { + for (final (text, content, reasoning) in const [ + ('Plan.Answer.', 'Answer.', 'Plan.'), + (' \n Plan. AB', 'AB', 'Plan.'), + ( + 'Plan.AXC', + 'AXC', + 'Plan.', + ), + ('Plan AB', 'AB', null), + ('Plan.Answer.', 'Plan.Answer.', null), + ( + 'Answer X more.', + 'Answer X more.', + null, + ), + ('Answer X', 'Answer X', null), + ('\u00a0XY', 'XY', null), + ]) { + final result = leading(text); + expect(result.content, content, reason: text); + expect(result.reasoning, reasoning, reason: text); + } + }); + + test('unescapes the thought and uses the given tags', () { + final result = leading( + r'<|inner_prefix|>a\nb<|inner_suffix|>c<|inner_suffix|>', + startTag: '<|inner_prefix|>', + endTag: '<|inner_suffix|>', + ); + expect(result.reasoning, 'a\nb'); + expect(result.content, 'c<|inner_suffix|>'); + }); + + test('leaves a forced-open thought and other parses as they were', () { + expect( + leading('Plan.AXC', thinkingForcedOpen: true), + (content: 'AC', reasoning: 'Plan.\nX'), + ); + expect(extractThinking('Answer X more.'), ( + content: 'Answer more.', + reasoning: 'X', + )); + }); + }); + group('endsUnopenedThought', () { test('is true for an end tag before any start tag', () { expect(endsUnopenedThought('Plan.Answer.'), isTrue); diff --git a/website/docs/changelog/recent-releases.md b/website/docs/changelog/recent-releases.md index 97a7e58fe..99f2d058f 100644 --- a/website/docs/changelog/recent-releases.md +++ b/website/docs/changelog/recent-releases.md @@ -13,6 +13,8 @@ validate against the exact release tag notes. ## Unreleased +- When the prompt opened no thought, take a thought only at the start of the reply, as llama.cpp does: any other thinking tag, such as a `` before any ``, a `` later in the reply or a second ``, is now content in generic, Llama 3, GLM 4.5, Kimi K2, LFM2, Laguna, Granite, MiniCPM5, Mistral Nemo, Apertus, Hunyuan V3 and DeepSeek R1, V3.2 and V4 replies, and after the start of a Hermes or Qwen3-Coder XML reply. The stream used to depend on how the reply was chunked and could repeat text as both content and reasoning ([#1865](https://github.com/leehack/llamadart/issues/1865)). + ## 0.19.2 - Keep default WebGPU bridge assets at `v0.1.83` (unchanged from 0.19.1). diff --git a/website/docs/guides/chat-template-and-parsing.md b/website/docs/guides/chat-template-and-parsing.md index e1499a4fa..f3ed66779 100644 --- a/website/docs/guides/chat-template-and-parsing.md +++ b/website/docs/guides/chat-template-and-parsing.md @@ -133,17 +133,22 @@ sets from `enableThinking`; their reply is held until a thinking tag, `<|START_TEXT|>` or `<|START_ACTION|>`, where llama-server ends the thought, and the text before it is reasoning. Seed-OSS templates open no thought: a reply has one only when it starts with ``, and other thinking tags -are content, as in llama-server. Likewise, when a Hermes-style, Qwen3-Coder or -DeepSeek V3 template opens no thought, as Qwen3's does with thinking on, a -`` before any `` is content, as are the thinking tags after it. +are content, as in llama-server. Likewise, when a template opens no thought, +as Qwen3's does with thinking on, most formats (Hermes-style, Qwen3-Coder, +generic, Llama 3, GLM 4.5, Kimi K2, LFM2, Granite, Mistral Nemo, the DeepSeek +R1, V3.2 and V4 families and others) take a thought only when the reply +starts with ``: a `` before any ``, a `` after +the start of the reply and every thinking tag after the first thought are +content. A DeepSeek V3 reply keeps a `` before any `` as content, +as are the thinking tags after it. To parse a reply as the engine does, pass the render to `ChatTemplateEngine.parseRendered(rendered, output)`, where `rendered` is the unchanged result of `ChatTemplateEngine.render` or `engine.chatTemplate`. `ChatTemplateEngine.parse`, which takes only the format and flags, cannot tell whether the template opened the thought or the render closed it, so it also ends a Seed-OSS thought at a `` in a -reply that did not open it, and a Hermes, Qwen3-Coder or DeepSeek V3 thought at -a `` before any ``. +reply that did not open it, ends a thought at a `` before any +``, and takes a thought from anywhere in the reply. Tencent Hunyuan V3 templates are also detected directly, including their namespaced reasoning tags and parallel `` envelopes. diff --git a/website/docs/guides/template-engine-internals.md b/website/docs/guides/template-engine-internals.md index c5cbc542a..3565286c0 100644 --- a/website/docs/guides/template-engine-internals.md +++ b/website/docs/guides/template-engine-internals.md @@ -165,12 +165,17 @@ sequenceDiagram render it is given, so it parses as the engine does. The public `ChatTemplateEngine.parse` cannot see them, so it also ends a thought at an end tag in a reply that does not start with the start tag, as after a - template that opens one. The same holds for a Hermes, Qwen3-Coder XML or - DeepSeek V3 reply with an end tag before any start tag: after a prompt that - opens no thought, llama.cpp keeps that end tag and every thinking tag after - it as content, and the stream streams them as content from that tag on, so - every chunking ends as the parse does. `ChatTemplateEngine.parse` ends a - thought at that end tag instead. Outside a thought, a Cohere2 MoE + template that opens one. Formats with the `takesThoughtOnlyAtReplyStart` + trait parse the same way after a prompt that opens no thought, as llama.cpp + (`cd959bae9`) does: only a thought that starts the reply is reasoning, and + any other thinking tag is content. Their handlers keep calling + `extractThinking`, which the parse switches to that rule for the whole parse + (`withThoughtOnlyAtReplyStart`), and the stream ends the thought at its first + end tag and streams the rest as content, so every chunking ends as the parse + does. A DeepSeek V3 reply with an end tag before any start tag keeps that + end tag and every thinking tag after it as content. `ChatTemplateEngine.parse` + ends a thought at such an end tag and takes thoughts from anywhere in the + reply instead. Outside a thought, a Cohere2 MoE `<|START_TEXT|>` or `<|START_ACTION|>` starts content in which thinking tags are text.