From d1171020882a1e10fcb0239f34f8c7d2e0d041f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=80=A1?= Date: Sun, 25 Oct 2026 23:57:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(recall):=20t=5Fstanding=5Frecall=20?= =?UTF-8?q?=E6=94=B9=E4=B8=A5=E6=A0=BC=E7=9B=B8=E7=AD=89=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=EF=BC=88=E4=B8=8A=E7=BA=BF=E5=8D=B3=E6=8A=93=E5=88=B0=E6=8D=A2?= =?UTF-8?q?=E4=B8=BB=E8=AF=AD=E8=AF=AF=E5=91=BD=E4=B8=AD=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 留出集实测:「织忆的服务跑在哪个端口」被判为「出图的服务跑在哪个端口」的匹配 (Dice 0.82,仅主语两字之差),把 ComfyUI 8188 置顶成织忆端口——模板句换主语必错。 改为归一化后完全相等才返回;近似 ≥0.78 只打 near-miss 日志用于补变体;加回归测试。 --- go/internal/storage/standing_match.go | 50 +++++++++++----------- go/internal/storage/standing_match_test.go | 10 +++-- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/go/internal/storage/standing_match.go b/go/internal/storage/standing_match.go index 4ef72d2..1440a24 100644 --- a/go/internal/storage/standing_match.go +++ b/go/internal/storage/standing_match.go @@ -1,6 +1,7 @@ package storage import ( + "log" "os" "strings" "sync" @@ -31,10 +32,9 @@ func StandingAnswerEnabled() bool { return os.Getenv("ZHIYI_STANDING_ANSWER") == "1" } -// standingMatchThreshold 归一化 bigram Dice 系数阈值。 -// 取 0.78:实测「cron 任务的调度者是谁」vs 同句 = 1.0;vs「cron 调度者是谁」≈ 0.86; -// vs「织忆的向量维度是多少」≈ 0.0。0.78 能容纳语序微调、挡住跨主题误命中。 -const standingMatchThreshold = 0.78 +// nearMissLogThreshold 近似命中日志阈值(**只用于日志**,不参与是否返回的决策)。 +// 取 0.78:近似到这个程度说明"很像同一句但不是",正是该补 question 变体的信号。 +const nearMissLogThreshold = 0.78 var ( standingCacheMu sync.RWMutex @@ -124,7 +124,14 @@ func loadStandingAnswers() []StandingAnswer { return all } -// LookupStandingAnswer 返回与 query 最匹配的常设答案;无匹配返回 nil。 +// LookupStandingAnswer 返回与 query 严格匹配的常设答案;无匹配返回 nil。 +// +// 匹配口径 = **归一化后完全相等**(2026-10-25 实测修正): +// 先前用 bigram Dice 模糊匹配(阈值 0.78),上线后留出集立刻抓到真实误命中—— +// 「织忆的服务跑在哪个端口」被判为「出图的服务跑在哪个端口」的匹配(仅主语两字之差, +// Dice≈0.82),把 ComfyUI 的 8188 当成织忆端口置顶。模板句 + 换主语 = 必错。 +// 结论:常设答案只认**同一句话**;同义问法靠多行变体(question_id 不同)覆盖。 +// 近似命中(Dice ≥ nearMissLogThreshold)只打日志不返回,用来发现该补哪些变体。 func LookupStandingAnswer(query string) *StandingAnswer { q := normalizeForMatch(query) if len([]rune(q)) < 2 { @@ -134,36 +141,27 @@ func LookupStandingAnswer(query string) *StandingAnswer { if len(all) == 0 { return nil } - var best *StandingAnswer - bestScore := 0.0 + var near *StandingAnswer + nearScore := 0.0 for i := range all { cand := normalizeForMatch(all[i].Question) if cand == "" { continue } - var score float64 - switch { - case cand == q: - score = 1.0 - case strings.Contains(q, cand) || strings.Contains(cand, q): - // 包含关系:短句 vs 长句。按长度比折算,避免「是谁」这种短语命中一切。 - ratio := float64(len([]rune(cand))) / float64(len([]rune(q))) - if ratio > 1 { - ratio = 1 / ratio - } - score = 0.85 * ratio - default: - score = diceSimilarity(q, cand) + if cand == q { + return &all[i] } - if score > bestScore { - bestScore = score - best = &all[i] + if s := diceSimilarity(q, cand); s > nearScore { + nearScore = s + near = &all[i] } } - if best == nil || bestScore < standingMatchThreshold { - return nil + // 近似但不等:只记日志(便于补变体),绝不返回——错答比不答更贵。 + if near != nil && nearScore >= nearMissLogThreshold { + log.Printf("[standing] near-miss %.2f(不返回)query=%q 最接近=%q id=%s", + nearScore, query, near.Question, near.QuestionID) } - return best + return nil } // PrependStandingAnswer 把常设答案置顶注入召回结果(去重 + 遵守 topK)。 diff --git a/go/internal/storage/standing_match_test.go b/go/internal/storage/standing_match_test.go index 7ae15c7..a6ea6ae 100644 --- a/go/internal/storage/standing_match_test.go +++ b/go/internal/storage/standing_match_test.go @@ -49,9 +49,13 @@ func TestLookupStandingAnswerThreshold(t *testing.T) { if sa := LookupStandingAnswer("cron 任务的调度者是谁?"); sa == nil || sa.QuestionID != "cron-scheduler" { t.Fatalf("同句应命中 cron-scheduler,实得 %+v", sa) } - // 命中:语序微调 - if sa := LookupStandingAnswer("织忆向量维度是多少"); sa == nil || sa.QuestionID != "zhiyi-dim" { - t.Fatalf("近义句应命中 zhiyi-dim,实得 %+v", sa) + // 不命中:仅语序微调也不行(严格相等口径,防"模板句换主语"误命中) + if sa := LookupStandingAnswer("织忆向量维度是多少"); sa != nil { + t.Fatalf("非同一句不应命中(应靠变体行覆盖),实得 %+v", sa) + } + // 高相似但换主语 ⇒ 必须不命中(真实事故回归测试) + if sa := LookupStandingAnswer("织忆的服务跑在哪个端口"); sa != nil { + t.Fatalf("换主语的高相似句不得命中,实得 %+v", sa) } // 不命中:跨主题(防止"是谁"类短语乱命中) if sa := LookupStandingAnswer("今天的天气怎么样"); sa != nil {