From c4b4aad8e48140ce097b99851b83cdfd7c9fdc2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=94=AF?= Date: Tue, 6 Oct 2026 17:36:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(distill):=20=E4=BA=8C=E6=AC=A1=E4=BF=AE=20p?= =?UTF-8?q?rompt=20=E9=94=9A=E7=82=B9=20+=20=E5=8E=9F=E5=A7=8B=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E6=8E=A2=E9=92=88=EF=BC=8C=E6=9B=B4=E6=AD=A3=E3=80=8C?= =?UTF-8?q?2B=20=E6=97=A0=E5=8C=BA=E5=88=86=E8=83=BD=E5=8A=9B=E3=80=8D?= =?UTF-8?q?=E7=9A=84=E9=94=99=E8=AF=AF=E7=BB=93=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - prompt: 彻底移除评分段的示例小数(此前写「例如 0.8/0.7/0.6/0.9/0.7」, 模型转而锚定 0.6 ⇒ 评分从恒定 0.755 变成恒定 0.600,仍不可用) - 新增 rawscore_liveprobe_test.go:直取 LLM 原始返回 + 异质输入 + 同输入重复, 用于判定模型能力(此前仅看解析后分数、样本同质,证据不足) - 更正 IsDegenerateScore 注释:实测模型**具备**区分能力(垃圾→0.0,有价值→0.8), 五维塌成同值是常见但非必然的输出形态;该函数仅作观测,不得单独用作丢弃判据 --- go/internal/distill/engine.go | 26 ++++- go/internal/distill/engine_echo_test.go | 53 +++++++-- go/internal/distill/quality_liveprobe_test.go | 7 +- .../distill/rawscore_liveprobe_test.go | 107 ++++++++++++++++++ 4 files changed, 180 insertions(+), 13 deletions(-) create mode 100644 go/internal/distill/rawscore_liveprobe_test.go diff --git a/go/internal/distill/engine.go b/go/internal/distill/engine.go index 96f67ef..72e87c1 100644 --- a/go/internal/distill/engine.go +++ b/go/internal/distill/engine.go @@ -119,6 +119,22 @@ func IsEchoScore(s FiveDScore) bool { return false } +// IsDegenerateScore 判断评分向量是否「退化」:五个维度完全相同(含全零)。 +// +// ⚠️ 用途仅限**观测**,不得单独用作丢弃判据。 +// +// 实测(2026-10-06,local/minicpm5-2b,异质输入 × 同输入重复): +// - 模型**具备**区分能力:垃圾内容→0.0×5,有价值内容→0.8×5; +// - 且**能**给出真正分维度的向量(实测到 is0.8/su0.8/pa0.7/vd0.6/ru0.5); +// - 但同一输入重复调用时结果**不稳定**:3 次全给 0.8×5,而另一次给了分维度向量。 +// +// 结论:五维塌成同一值是**常见但非必然**的输出形态(说明该维度信息不可信), +// 不等于"模型没有区分能力" —— 曾据此下过该结论,属证据不足(样本同质),已更正。 +// 全零同样是模型真实输出(原始 JSON 里字段齐全),不是 Go 零值兜底。 +func IsDegenerateScore(s FiveDScore) bool { + return s.IS == s.SU && s.SU == s.PA && s.PA == s.VD && s.VD == s.RU +} + // scoreShadowMode 影子模式:true(默认)= 只在日志里记录"本该被丢",不真丢。 // 设环境变量 ZHIYI_SCORE_SHADOW=0 关闭影子(= 恢复真拦)。 var scoreShadowMode = os.Getenv("ZHIYI_SCORE_SHADOW") != "0" @@ -452,8 +468,9 @@ func (e *Engine) distillOne(input DistillInput) DistillResult { // 影子模式(2026-10-06):闸门此前因 echo bug 从未真正生效, // 修好 prompt 后它会首次开始真拦 —— 先只记录"本该被丢",观察样本分布, // 避免一次性批量误杀。ZHIYI_SCORE_SHADOW=0 可切回真拦。 - log.Printf("[distill][SHADOW] would-drop episode=%s overall=%.3f vd=%.2f echo=%v content=%q", - input.EpisodeID, overall, score.VD, IsEchoScore(score), truncate(input.Content, 80)) + log.Printf("[distill][SHADOW] would-drop episode=%s overall=%.3f vd=%.2f echo=%v degenerate=%v content=%q", + input.EpisodeID, overall, score.VD, IsEchoScore(score), IsDegenerateScore(score), + truncate(input.Content, 80)) } else { log.Printf("[distill] score below threshold for %s, skip", input.EpisodeID) // 即使阈值未通过,仍然用 LLM 实体提取(如果有的话) @@ -571,8 +588,9 @@ func (e *Engine) callLLM5D(content string) (LLMResponse, error) { {"facts": ["独立事实1", "独立事实2"], "decisions": ["决定1"], "conclusions": ["结论1"], "actions_taken": ["行动1"], "open_questions": ["问题1"], "entities": ["entity1", "entity2"], "is": <小数>, "su": <小数>, "pa": <小数>, "vd": <小数>, "ru": <小数>} 评分说明:is/su/pa/vd/ru 必须是 0.0 到 1.0 之间的浮点数(越高越好),不得用 0-10 整数。 -🔴 上面五个 <小数> 是**类型占位符,不是示例数值**。你必须依据**本次内容**独立打分,各维度可以不同。 - 严禁沿用固定的数值组合(例如 0.8/0.7/0.6/0.9/0.7)——照抄示例分数会被系统判为无效评分并整条丢弃。 +🔴 这五个分数必须依据**本次内容**独立判断:不同内容要给出**不同**的分数,各维度之间也应有差异。 + 禁止给出固定不变的分数组合 —— 那会被系统判为无效评分并整条丢弃。 + 输出里不要出现任何示例数值,直接写你本次的判断。 要求:除非消息完全无意义,否则提取并输出为事实。facts 要详尽,不要只给1条摘要。 内容: diff --git a/go/internal/distill/engine_echo_test.go b/go/internal/distill/engine_echo_test.go index d505df5..248b519 100644 --- a/go/internal/distill/engine_echo_test.go +++ b/go/internal/distill/engine_echo_test.go @@ -45,8 +45,38 @@ func TestEchoVectorMatchesWeightedOverall(t *testing.T) { } } -// TestPromptHasNoEchoLiterals 回归守卫:prompt 里不得再出现具体的示例评分数字。 +// TestIsDegenerateScore 退化向量检测(阳性 + 阴性对照)。 +// +// ⚠️ 本函数只做**观测标记**,不得单独用作丢弃判据 —— 见 engine.go 中 IsDegenerateScore 注释。 +func TestIsDegenerateScore(t *testing.T) { + // 阳性:五维相同(含全零) + for name, s := range map[string]FiveDScore{ + "全零(可能字段缺失)": {IS: 0, SU: 0, PA: 0, VD: 0, RU: 0}, + "全0.5": {IS: 0.5, SU: 0.5, PA: 0.5, VD: 0.5, RU: 0.5}, + "全0.8": {IS: 0.8, SU: 0.8, PA: 0.8, VD: 0.8, RU: 0.8}, + "全一": {IS: 1, SU: 1, PA: 1, VD: 1, RU: 1}, + } { + if !IsDegenerateScore(s) { + t.Errorf("阳性对照失败[%s]:五维相同的 %+v 未被识别为退化评分", name, s) + } + } + // 阴性:至少一维不同 → 不算退化 + for name, s := range map[string]FiveDScore{ + "真实分维度(实测样本)": {IS: 0.8, SU: 0.8, PA: 0.7, VD: 0.6, RU: 0.5}, + "仅一位不同": {IS: 0.8, SU: 0.8, PA: 0.8, VD: 0.8, RU: 0.7}, + "历史echo向量": {IS: 0.8, SU: 0.7, PA: 0.6, VD: 0.9, RU: 0.7}, + } { + if IsDegenerateScore(s) { + t.Errorf("阴性对照失败[%s]:%+v 被误判为退化评分(滥报)", name, s) + } + } +} + +// TestPromptHasNoEchoLiterals 回归守卫:**prompt 评分段**不得再出现具体示例小数。 +// // 起因:示例写成具体数值 ⇒ 模型照抄 ⇒ 每条 overall 恒为 0.755 ⇒ 评分闸门形同虚设。 +// 二次踩坑:改成"禁止沿用(例如 0.8/0.7/0.6/0.9/0.7)"后,模型转而锚定 0.6 ⇒ 恒定 0.600。 +// 因此守卫必须**只扫 prompt 模板段**(文件别处的 echoScoreVectors 注释本就需要记录历史值)。 func TestPromptHasNoEchoLiterals(t *testing.T) { src, err := os.ReadFile("engine.go") if err != nil { @@ -54,14 +84,23 @@ func TestPromptHasNoEchoLiterals(t *testing.T) { } s := string(src) - if !strings.Contains(s, `"is": <小数>`) { + start := strings.Index(s, "输出格式(严格JSON)") + end := strings.Index(s, "内容:\n%s") + if start < 0 || end < 0 || end <= start { + t.Fatal("无法定位 prompt 评分段(模板被重构?请更新本测试的锚点)") + } + prompt := s[start:end] + + if !strings.Contains(prompt, `"is": <小数>`) { t.Fatal("回归:prompt 里已找不到 <小数> 占位符 —— 示例可能又被写成具体数字") } - if strings.Contains(s, `"is": 0.8, "su": 0.7, "pa": 0.6, "vd": 0.9, "ru": 0.7`) { - t.Fatal("回归:prompt 里重新出现了具体示例评分(echo 源)") + if !strings.Contains(prompt, "禁止给出固定不变的分数组合") { + t.Error("prompt 缺少「禁止固定数值组合」的约束句") } - // 占位符必须带"禁止沿用固定数值"的显式约束,否则模型仍会编造固定值 - if !strings.Contains(s, "严禁沿用固定的数值组合") { - t.Error("prompt 缺少「禁止沿用固定数值组合」的约束句") + // 评分段不得出现任何具体小数(0.0/1.0 范围端点除外) + for _, bad := range []string{"0.8", "0.9", "0.7", "0.6", "0.5"} { + if strings.Contains(prompt, bad) { + t.Fatalf("回归:prompt 评分段出现具体示例小数 %q —— 会变成锚点(实测会把评分钉成常数)", bad) + } } } diff --git a/go/internal/distill/quality_liveprobe_test.go b/go/internal/distill/quality_liveprobe_test.go index 0e48417..af95062 100644 --- a/go/internal/distill/quality_liveprobe_test.go +++ b/go/internal/distill/quality_liveprobe_test.go @@ -66,8 +66,11 @@ func TestLiveQualityGateOnRealEpisodes(t *testing.T) { t.Logf("\n════ %s ════", ep.ID) t.Logf(" 原文(%d字): %s", utf8.RuneCountInString(ep.Content), ep.Content) - t.Logf(" 评分(应恒为 prompt 示例): is=%.2f su=%.2f pa=%.2f vd=%.2f ru=%.2f", - resp.IS, resp.SU, resp.PA, resp.VD, resp.RU) + t.Logf(" 评分(2026-10-06 前应恒为 0.755;修 prompt 后应为按内容变化):"+ + "is=%.2f su=%.2f pa=%.2f vd=%.2f ru=%.2f | echo=%v degenerate=%v", + resp.IS, resp.SU, resp.PA, resp.VD, resp.RU, + IsEchoScore(FiveDScore{IS: resp.IS, SU: resp.SU, PA: resp.PA, VD: resp.VD, RU: resp.RU}), + IsDegenerateScore(FiveDScore{IS: resp.IS, SU: resp.SU, PA: resp.PA, VD: resp.VD, RU: resp.RU})) t.Logf(" 提取 %d 条 → 闸门放行 %d 条(丢弃 %d)", len(facts), len(kept), len(facts)-len(kept)) for k, f := range facts { rl := utf8.RuneCountInString(f) diff --git a/go/internal/distill/rawscore_liveprobe_test.go b/go/internal/distill/rawscore_liveprobe_test.go new file mode 100644 index 0000000..13e0033 --- /dev/null +++ b/go/internal/distill/rawscore_liveprobe_test.go @@ -0,0 +1,107 @@ +//go:build liveprobe + +package distill + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "strings" + "testing" + "time" +) + +// TestRawLLMScoreBehavior 直接观测模型**原始输出**,回答「2B 是否具备 5 维区分能力」。 +// +// 前一次结论("不具备")建立在 6 条**同质**样本 + 只看解析后分数之上,不成立: +// 输入几乎无差异时,评分相同可能是**正确**行为;且"全零"到底是模型真给 0, +// 还是模型没输出该字段(Go 零值),只看解析结果无法区分。 +// +// 本测试补齐三件事: +// +// ① 原始返回:模型到底输出了 5 个不同 key,还是一个标量复制 5 份? +// ② 异质输入:明显有价值 vs 明显垃圾,分数是否随质量变化? +// ③ 确定性:同一输入重复 3 次,分数是否稳定? +// +// go test ./internal/distill/ -tags liveprobe -run TestRawLLMScoreBehavior -v +func TestRawLLMScoreBehavior(t *testing.T) { + tmpl := extractPromptTemplate(t) + client := &http.Client{Timeout: 180 * time.Second} + + cases := []struct{ name, content string }{ + {"高价值-决策", "[2026-10-06, 周二] 牧尘: 决定把蒸馏评分模型从本地 2B 换成更强的模型,因为评分区分度不够。"}, + {"垃圾-纯问候", "[2026-10-06, 周二] 牧尘: 你好\n[2026-10-06, 周二] 小怡: 哈哈\n[2026-10-06, 周二] 牧尘: 谢谢"}, + {"中等-状态", "[2026-10-06, 周二] 小怡: 服务已重启完成。"}, + {"长文-细节", "[2026-10-06, 周二] 牧尘: 我在淇县负责生物制造产业专班和卫都街道,设备是小米笔记本,平时喜欢马哲经济学和剪辑。"}, + {"短事实", "[2026-10-06, 周二] 牧尘: 我不在招商局工作。"}, + } + + for _, c := range cases { + raw := callRaw(t, client, fmt.Sprintf(tmpl, c.content)) + t.Logf("\n══════ %s ══════", c.name) + t.Logf("输入: %s", strings.ReplaceAll(c.content, "\n", " ⏎ ")) + t.Logf("原始返回: %s", raw) + } + + t.Logf("\n══════ 确定性检验(同一输入 ×3,temperature=0.2)══════") + prompt := fmt.Sprintf(tmpl, cases[0].content) + for i := 1; i <= 3; i++ { + t.Logf("第%d次: %s", i, callRaw(t, client, prompt)) + } +} + +// extractPromptTemplate 从 engine.go 就地提取生产 prompt 模板(含单个 %s), +// 保证本测试跑的是**与生产逐字相同**的 prompt,而不是我手抄的近似版。 +func extractPromptTemplate(t *testing.T) string { + t.Helper() + src, err := os.ReadFile("engine.go") + if err != nil { + t.Fatalf("读 engine.go: %v", err) + } + s := string(src) + const startMark = "prompt := fmt.Sprintf(`" + i := strings.Index(s, startMark) + if i < 0 { + t.Fatal("找不到 prompt 模板起点(模板被重构?更新本测试锚点)") + } + rest := s[i+len(startMark):] + const endMark = "`, truncate(content, 1000))" + j := strings.Index(rest, endMark) + if j < 0 { + t.Fatal("找不到 prompt 模板终点(模板被重构?更新本测试锚点)") + } + return rest[:j] +} + +// callRaw 直接打本地 LLM 端点,返回**未经解析**的 message.content 原文。 +func callRaw(t *testing.T, c *http.Client, prompt string) string { + t.Helper() + body, _ := json.Marshal(map[string]interface{}{ + "model": "local/minicpm5-2b", + "messages": []map[string]string{{"role": "user", "content": prompt}}, + "temperature": 0.2, + "max_tokens": 2400, + }) + resp, err := c.Post("http://127.0.0.1:8081/v1/chat/completions", + "application/json", bytes.NewReader(body)) + if err != nil { + t.Fatalf("请求失败: %v", err) + } + defer resp.Body.Close() + b, _ := io.ReadAll(resp.Body) + + var out struct { + Choices []struct { + Message struct { + Content string `json:"content"` + } `json:"message"` + } `json:"choices"` + } + if err := json.Unmarshal(b, &out); err != nil || len(out.Choices) == 0 { + return fmt.Sprintf("<外层解析失败 status=%d body=%s>", resp.StatusCode, truncate(string(b), 300)) + } + return out.Choices[0].Message.Content +}