fix(recall): t_standing_recall 改严格相等匹配(上线即抓到换主语误命中)

留出集实测:「织忆的服务跑在哪个端口」被判为「出图的服务跑在哪个端口」的匹配
(Dice 0.82,仅主语两字之差),把 ComfyUI 8188 置顶成织忆端口——模板句换主语必错。
改为归一化后完全相等才返回;近似 ≥0.78 只打 near-miss 日志用于补变体;加回归测试。
This commit is contained in:
小怡
2026-10-25 23:57:23 +08:00
parent 1aaab764ba
commit d117102088
2 changed files with 31 additions and 29 deletions
+24 -26
View File
@@ -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)。
+7 -3
View File
@@ -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 {