Keep model reasoning out of inserted polish text
Thinking-capable OpenAI-compatible models can return tagged reasoning before the final polished answer. The cleanup layer now strips only explicit think-tag blocks after response parsing so existing provider requests, UI, and fallback behavior stay unchanged. Constraint: Issue #25 asks to adapt thinking output rather than disabling model thinking. Rejected: Add a provider-specific thinking toggle | broader UI and settings change than needed for the bug. Rejected: Strip localized heading text | too likely to remove normal user-facing content without a stable provider contract. Confidence: medium Scope-risk: narrow Tested: cargo test polish::tests -- --nocapture Tested: cargo check
This commit is contained in:
1 parent
ac611d5151
commit
2e63c42840
1 file changed
+35
-1
@@ -223,7 +223,8 @@ fn extract_assistant_content(body: &str) -> Result<String, LLMError> {
|
||||
/// an iterative trim — if the model stacks two boilerplate sentences we'll still
|
||||
/// strip both.
|
||||
fn clean_polish_output(content: &str) -> String {
|
||||
let trimmed = content.trim();
|
||||
let without_thinking = strip_thinking_blocks(content);
|
||||
let trimmed = without_thinking.trim();
|
||||
let stripped = strip_markdown_fence(trimmed);
|
||||
let mut output = stripped.to_string();
|
||||
|
||||
@@ -239,6 +240,26 @@ fn clean_polish_output(content: &str) -> String {
|
||||
output.trim().to_string()
|
||||
}
|
||||
|
||||
/// Strip model reasoning blocks so only the final polished text is inserted.
|
||||
///
|
||||
/// Thinking-capable OpenAI-compatible models commonly return their reasoning in
|
||||
/// `<think>...</think>` before the final answer, so keep this as a
|
||||
/// conservative cleanup layer after parsing `message.content` instead of
|
||||
/// provider-specific handling.
|
||||
fn strip_thinking_blocks(text: &str) -> String {
|
||||
let mut output = text.to_string();
|
||||
|
||||
while let Some(start) = output.find("<think>") {
|
||||
let Some(end_from_start) = output[start..].find("</think>") else {
|
||||
break;
|
||||
};
|
||||
let end = start + end_from_start + "</think>".len();
|
||||
output.replace_range(start..end, "");
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
fn strip_markdown_fence(text: &str) -> &str {
|
||||
if !(text.starts_with("```") && text.ends_with("```")) {
|
||||
return text;
|
||||
@@ -348,3 +369,16 @@ pub mod prompts {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn clean_polish_output_strips_think_tag_block() {
|
||||
let content =
|
||||
"<think>先分析用户意图。\n这里可能很长。</think>\n\n请明天上午十点提醒我开会。";
|
||||
|
||||
assert_eq!(clean_polish_output(content), "请明天上午十点提醒我开会。");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user