From 73010a1fc6ab401e435598db05025a090a97c2ba Mon Sep 17 00:00:00 2001 From: ExtReMLapin <3909752+ExtReMLapin@users.noreply.github.com> Date: Tue, 4 Nov 2025 14:15:41 +0100 Subject: [PATCH] Applied @ochafik 's suggested code after testing locally, no regression Co-authored-by: Olivier Chafik --- common/chat.cpp | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/common/chat.cpp b/common/chat.cpp index 0887593744..93eadfdb19 100644 --- a/common/chat.cpp +++ b/common/chat.cpp @@ -2339,30 +2339,26 @@ static common_chat_params common_chat_params_init_hermes_2_pro(const common_chat "( \"```\\n\" | \"```json\\n\" | \"```xml\\n\" ) space " + wrappable_tool_call + " space \"```\" space "); auto tool_call = builder.add_rule("tool_call", string_join(tool_call_alts, " | ")); - builder.add_rule("thinking-start", "\"\""); - builder.add_rule("thinking-content", "( [^<] | \"<\" [^/] | \"] )*"); - builder.add_rule("thinking-end", "\"\" space"); - - //thinking grammar logic depending on if thinking_forced_open was to true (so already opened (and maybe closed)) and if thinking is even allowed - std::string thinking_grammar_logic = ""; // thinking tag was closed or not supported/wanted + // thinking grammar logic depending on if thinking_forced_open was to true (so already opened (and maybe closed)) and if thinking is even allowed if (extra_context["enable_thinking"]) { data.grammar_triggers.push_back({ COMMON_GRAMMAR_TRIGGER_TYPE_WORD, data.thinking_forced_open ? "" : "" }); - if (data.thinking_forced_open) { - //thinking tag was already opened by used so we don't need to add it again - thinking_grammar_logic = "(thinking-content thinking-end) "; - } - else - { - thinking_grammar_logic = "(thinking-start thinking-content thinking-end) "; + std::string prelude = ""; + if (!data.thinking_forced_open) { + prelude = builder.add_rule("think-start", "\"\""); } + prelude += " "; + prelude += builder.add_rule("think-content", "( [^<] | \"<\" [^/] | \"] )*"); + prelude += " "; + prelude += builder.add_rule("think-end", "\"\" space"); + prelude += " "; + builder.add_rule("root", prelude + "(" + tool_call + ")" + (inputs.parallel_tool_calls ? "*" : "?")); + } else { + builder.add_rule("root", inputs.parallel_tool_calls ? "(" + tool_call + ")+" : tool_call); } - - builder.add_rule("root", thinking_grammar_logic + (inputs.parallel_tool_calls ? "(" + tool_call + ")+" : tool_call)); - // Trigger on some common known "good bad" outputs (only from the start and with a json that's about a specific argument name to avoid false positives) data.grammar_triggers.push_back({ COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN_FULL,