common/autoparser : detect reasoning markers when enable_thinking changes system prompt (#20859)
This commit is contained in:
parent
07ff000551
commit
7a0b6a635e
|
|
@ -348,6 +348,34 @@ void analyze_reasoning::compare_thinking_enabled() {
|
|||
mode = reasoning_mode::TAG_BASED;
|
||||
}
|
||||
}
|
||||
} else if (!left_trimmed.empty() && !right_trimmed.empty()) {
|
||||
// Full-output diff is noisy (e.g., SmolLM3 changes the system message when enable_thinking flips).
|
||||
// Try to find reasoning markers by tail-anchoring:
|
||||
// one output's generation prompt tail may appear in the other with extra reasoning markers appended.
|
||||
const auto & output_A = comparison->output_A;
|
||||
const auto & output_B = comparison->output_B;
|
||||
const size_t anchor_len = 64;
|
||||
|
||||
for (int dir = 0; dir < 2; dir++) {
|
||||
const auto & base = dir == 0 ? output_B : output_A;
|
||||
const auto & extended = dir == 0 ? output_A : output_B;
|
||||
|
||||
size_t len = std::min(base.size(), anchor_len);
|
||||
std::string anchor = base.substr(base.size() - len);
|
||||
auto pos = extended.rfind(anchor);
|
||||
if (pos == std::string::npos || pos + len >= extended.size()) continue;
|
||||
|
||||
std::string extra = trim_whitespace(extended.substr(pos + len));
|
||||
if (extra.empty()) continue;
|
||||
|
||||
auto seg = prune_whitespace_segments(segmentize_markers(extra));
|
||||
if (seg.size() == 2 && seg[0].type == segment_type::MARKER && seg[1].type == segment_type::MARKER) {
|
||||
if (start.empty()) start = seg[0].value;
|
||||
if (end.empty()) end = seg[1].value;
|
||||
mode = reasoning_mode::TAG_BASED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (mode == reasoning_mode::NONE && start.empty() && !end.empty()) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
{#- Copyright 2025-present the Unsloth team. All rights reserved. #}
|
||||
{#- Licensed under the Apache License, Version 2.0 (the "License") #}
|
||||
{#- Edits made by Unsloth to make it work for most inference engines #}
|
||||
{# ───── defaults ───── #}
|
||||
{%- if enable_thinking is not defined -%}
|
||||
{%- set enable_thinking = true -%}
|
||||
{%- endif -%}
|
||||
{# ───── reasoning mode ───── #}
|
||||
{%- if enable_thinking -%}
|
||||
{%- set reasoning_mode = "/think" -%}
|
||||
{%- else -%}
|
||||
{%- set reasoning_mode = "/no_think" -%}
|
||||
{%- endif -%}
|
||||
{# ───── header (system message) ───── #}
|
||||
{{- "<|im_start|>system\n" -}}
|
||||
{%- if messages[0].role == "system" -%}
|
||||
{%- set system_message = messages[0].content -%}
|
||||
{%- if "/no_think" in system_message -%}
|
||||
{%- set reasoning_mode = "/no_think" -%}
|
||||
{%- elif "/think" in system_message -%}
|
||||
{%- set reasoning_mode = "/think" -%}
|
||||
{%- endif -%}
|
||||
{%- set custom_instructions = system_message.replace("/no_think", "") -%}
|
||||
{%- set custom_instructions = custom_instructions.replace("/think", "") -%}
|
||||
{%- set custom_instructions = custom_instructions.rstrip() -%}
|
||||
{%- endif -%}
|
||||
{{- "## Metadata\n\n" -}}
|
||||
{{- "Knowledge Cutoff Date: June 2025\n" -}}
|
||||
{{- "Reasoning Mode: " + reasoning_mode + "\n\n" -}}
|
||||
{{- "## Custom Instructions\n\n" -}}
|
||||
{%- if custom_instructions -%}
|
||||
{{- custom_instructions + "\n\n" -}}
|
||||
{%- elif reasoning_mode == "/think" -%}
|
||||
{{- "You are a helpful AI assistant named SmolLM, trained by Hugging Face. Your role as an assistant involves thoroughly exploring questions through a systematic thinking process before providing the final precise and accurate solutions. This requires engaging in a comprehensive cycle of analysis, summarizing, exploration, reassessment, reflection, backtracking, and iteration to develop well-considered thinking process. Please structure your response into two main sections: Thought and Solution using the specified format: <think> Thought section </think> Solution section. In the Thought section, detail your reasoning process in steps. Each step should include detailed considerations such as analysing questions, summarizing relevant findings, brainstorming new ideas, verifying the accuracy of the current steps, refining any errors, and revisiting previous steps. In the Solution section, based on various attempts, explorations, and reflections from the Thought section, systematically present the final solution that you deem correct. The Solution section should be logical, accurate, and concise and detail necessary steps needed to reach the conclusion.\n\n" -}}
|
||||
{%- else -%}
|
||||
{{- "You are a helpful AI assistant named SmolLM, trained by Hugging Face.\n\n" -}}
|
||||
{%- endif -%}
|
||||
{{- "<|im_end|>\n" -}}
|
||||
{# ───── main loop ───── #}
|
||||
{%- for message in messages -%}
|
||||
{%- set content = message.content if message.content is string else "" -%}
|
||||
{%- if message.role == "user" -%}
|
||||
{{ "<|im_start|>" + message.role + "\n" + content + "<|im_end|>\n" }}
|
||||
{%- elif message.role == "assistant" -%}
|
||||
{%- if reasoning_mode == "/think" -%}
|
||||
{{ "<|im_start|>assistant\n" + content.lstrip("\n") + "<|im_end|>\n" }}
|
||||
{%- else -%}
|
||||
{{ "<|im_start|>assistant\n" + "<think>\n\n</think>\n" + content.lstrip("\n") + "<|im_end|>\n" }}
|
||||
{%- endif -%}
|
||||
{%- elif message.role == "tool" -%}
|
||||
{{ "<|im_start|>" + "user\n" + content + "<|im_end|>\n" }}
|
||||
{%- endif -%}
|
||||
{%- endfor -%}
|
||||
{# ───── generation prompt ───── #}
|
||||
{%- if add_generation_prompt -%}
|
||||
{%- if reasoning_mode == "/think" -%}
|
||||
{{ "<|im_start|>assistant\n" }}
|
||||
{%- else -%}
|
||||
{{ "<|im_start|>assistant\n" + "<think>\n\n</think>\n" }}
|
||||
{%- endif -%}
|
||||
{%- endif -%}
|
||||
|
|
@ -62,6 +62,9 @@ static void test_nemotron_tool_format(testing & t);
|
|||
static void test_cohere_reasoning_detection(testing & t);
|
||||
static void test_cohere_analysis(testing & t);
|
||||
|
||||
// SmolLM3 template analysis tests
|
||||
static void test_smollm3_analysis(testing & t);
|
||||
|
||||
// Marker separation
|
||||
static void test_marker_separation(testing & t);
|
||||
|
||||
|
|
@ -96,6 +99,7 @@ int main(int argc, char * argv[]) {
|
|||
t.test("seed_oss_diffs", test_seed_oss_tool_analysis);
|
||||
t.test("cohere", test_cohere_analysis);
|
||||
t.test("nemotron", test_nemotron_analysis);
|
||||
t.test("smollm3", test_smollm3_analysis);
|
||||
t.test("standard_json_tools", test_standard_json_tools_formats);
|
||||
t.test("normalize_quotes_to_json", test_normalize_quotes_to_json);
|
||||
t.test("tagged_args_embedded_quotes", test_tagged_args_with_embedded_quotes);
|
||||
|
|
@ -1448,6 +1452,47 @@ static void test_tool_format_cohere(testing & t) {
|
|||
t.assert_true("tools_array_wrapped should be true", analysis.tools.format.tools_array_wrapped);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// SmolLM3 Template Analysis Tests
|
||||
// Tests for templates that change system message when enable_thinking flips
|
||||
// and prefill an empty <think></think> block in no-think mode.
|
||||
// ============================================================================
|
||||
static common_chat_template load_smollm3_template(testing & t) {
|
||||
return load_template(t, "models/templates/HuggingFaceTB-SmolLM3-3B.jinja");
|
||||
}
|
||||
|
||||
static void test_smollm3_reasoning_detection(testing & t);
|
||||
|
||||
static void test_smollm3_analysis(testing & t) {
|
||||
t.test("SmolLM3 reasoning detection", test_smollm3_reasoning_detection);
|
||||
}
|
||||
|
||||
static void test_smollm3_reasoning_detection(testing & t) {
|
||||
common_chat_template tmpl = load_smollm3_template(t);
|
||||
|
||||
// Run differential analysis
|
||||
struct autoparser analysis;
|
||||
analysis.analyze_template(tmpl);
|
||||
|
||||
// SmolLM3 uses <think>/<think> reasoning tags.
|
||||
// The template changes the entire system message when enable_thinking flips,
|
||||
// so the analyzer must compare isolated generation prompts (not full outputs).
|
||||
t.assert_equal("reasoning_start should be '<think>'", "<think>", analysis.reasoning.start);
|
||||
t.assert_equal("reasoning_end should be '</think>'", "</think>", analysis.reasoning.end);
|
||||
t.assert_equal("reasoning should be TAG_BASED", reasoning_mode::TAG_BASED, analysis.reasoning.mode);
|
||||
|
||||
// Content should remain plain (no wrappers)
|
||||
t.assert_equal("content start should be empty", "", analysis.content.start);
|
||||
t.assert_equal("content end should be empty", "", analysis.content.end);
|
||||
t.assert_equal("content should be PLAIN", content_mode::PLAIN, analysis.content.mode);
|
||||
|
||||
// Preserved tokens should include the reasoning markers
|
||||
bool has_think_start = std::find(analysis.preserved_tokens.begin(), analysis.preserved_tokens.end(), "<think>") != analysis.preserved_tokens.end();
|
||||
bool has_think_end = std::find(analysis.preserved_tokens.begin(), analysis.preserved_tokens.end(), "</think>") != analysis.preserved_tokens.end();
|
||||
t.assert_true("preserved_tokens should contain '<think>'", has_think_start);
|
||||
t.assert_true("preserved_tokens should contain '</think>'", has_think_end);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// standard_json_tools Format Tests
|
||||
// ============================================================================
|
||||
|
|
|
|||
Loading…
Reference in New Issue