package com.custom.webui.util; public final class ReasoningLeakFilter { private ReasoningLeakFilter() { } /** 明确的推理标记短语,出现即高度可疑 */ private static final String[] MARKERS = { "Thinking Process", "Let me ", "Let's ", "Wait,", "Analyze the Request", "one last check", "Let me double-check", "Hmm,", "Okay, the ", "*Wait", "Final Decision", "Evaluate Constraints" }; /** * 判断整段文本是否属于推理泄漏。 * * @param text 完整正文 * @return true 表示判定为泄漏,调用方应丢弃 */ public static boolean looksLikeReasoning(String text) { if (text == null || text.isBlank()) { return false; } int markerHits = 0; for (String marker : MARKERS) { if (text.contains(marker)) { markerHits++; } } if (markerHits == 0) { return false; } // 中文占比高时基本可排除:这些标记词都是英文, // 中文正常回答不会出现,出现了多半是模型串了语言 long chinese = text.chars().filter(c -> c >= 0x4E00 && c <= 0x9FFF).count(); double chineseRatio = (double) chinese / Math.max(1, text.length()); // 至少命中 2 个标记,或命中 1 个且几乎全英文 return markerHits >= 2 || chineseRatio < 0.2; } }