Files
memoryweave/go/internal/api/routes/entity_index_test.go
T
xiaoyi 46eaf1a35d feat(graph/notes): 整句→实体兜底拆解(EntityIndex)
问题:调用方(hermes 织忆插件)把整句用户提问直接当 entity 传给
/api/v1/graph/notes,NormalizeEntity 后整句匹配不到图谱节点 → 0 篇笔记,
Obsidian 笔记层在真实对话里几乎不生效。

改动(纯服务端,调用方无需改,也就无需重启 hermes 网关):
- 新增 routes.EntityIndex:节点名词表(PageRank,TTL 10min,后台 45s 预热)
  + 最长/权重匹配把整句拆成候选实体;候选需过 相对权重比(0.15) 与 图谱度数(>=3)
  两道过滤,压掉通用词/自动切词噪声节点。
- /api/v1/graph/notes:entity 直查 0 篇笔记时自动兜底拆实体重查;
  新增 query 参数;响应新增 mode(direct|nl_extract|none) 与 entities_used。
  兜底路径 graph_paths 截断(单实体 20 条、合计 50 条)、参与匹配实体上限 8。
- 覆盖测试 10 个:真实句子拆解/大小写/包含冗余/权重比/度数过滤/TTL缓存与失败退避/nil 安全。

实测(生产 7821):单词实体 entity=织忆 仍 mode=direct 3 篇(回归不变);
整句 "织忆召回会不会给出obsidian实体所在的笔记位置呢" → mode=nl_extract
used=[Obsidian 织忆] 3 篇真路径 0.29s;无意义句 "完全不相干的一句话天气不错"
→ mode=none 0 篇(噪声已过滤)。
2026-09-27 02:19:13 +08:00

167 lines
4.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package routes
import (
"reflect"
"testing"
"time"
)
func fakeVocab(m map[string]float64) (NodeVocabularyFunc, *int) {
calls := 0
return func() map[string]float64 {
calls++
return m
}, &calls
}
// 真实场景:hermes 织忆插件把整句提问当 entity 传过来,服务端要能拆出实体。
func TestExtractNLSentence(t *testing.T) {
load, _ := fakeVocab(map[string]float64{
"织忆": 3.386e-06,
"位置": 1.510e-06,
"小怡": 3.134e-06,
"牧尘": 2.491e-06,
})
idx := NewEntityIndex(load)
got := idx.Extract("现在织忆recall带obsidian实体位置么", 2)
want := []string{"织忆", "位置"}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %v want %v", got, want)
}
// 只取 1 个时必须是权重最高的那个
if got := idx.Extract("现在织忆recall带obsidian实体位置么", 1); !reflect.DeepEqual(got, []string{"织忆"}) {
t.Fatalf("top1 got %v", got)
}
}
func TestExtractASCIIKeepsOriginalSpelling(t *testing.T) {
load, _ := fakeVocab(map[string]float64{
"Obsidian": 2.0e-06,
"CLI": 1.0e-06,
})
idx := NewEntityIndex(load)
got := idx.Extract("obsidian 和 cli 怎么配", 5)
want := []string{"Obsidian", "CLI"}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %v want %v", got, want)
}
}
func TestExtractDropsContainedShorterName(t *testing.T) {
load, _ := fakeVocab(map[string]float64{
"织忆系统": 3.0e-06,
"织忆": 2.0e-06,
})
idx := NewEntityIndex(load)
if got := idx.Extract("织忆系统的笔记", 5); !reflect.DeepEqual(got, []string{"织忆系统"}) {
t.Fatalf("got %v", got)
}
}
func TestExtractWeightRatioFilter(t *testing.T) {
load, _ := fakeVocab(map[string]float64{"织忆": 3.0e-06, "位置": 1.0e-06})
idx := NewEntityIndex(load)
if got := idx.Extract("织忆和位置", 5); len(got) != 2 {
t.Fatalf("默认阈值应保留两个,got %v", got)
}
idx.MinWeightRatio = 0.5
if got := idx.Extract("织忆和位置", 5); !reflect.DeepEqual(got, []string{"织忆"}) {
t.Fatalf("阈值 0.5 应只剩织忆,got %v", got)
}
}
func TestExtractSkipsTooShortAndNonLetterNames(t *testing.T) {
load, _ := fakeVocab(map[string]float64{"忆": 5.0e-06, "12": 4.0e-06})
idx := NewEntityIndex(load)
if got := idx.Extract("织忆12", 5); got != nil {
t.Fatalf("expected nil, got %v", got)
}
}
func TestExtractNoHitAndEmptyQuery(t *testing.T) {
load, _ := fakeVocab(map[string]float64{"织忆": 3.0e-06})
idx := NewEntityIndex(load)
if got := idx.Extract("今天天气不错", 3); got != nil {
t.Fatalf("expected nil, got %v", got)
}
if got := idx.Extract(" ", 3); got != nil {
t.Fatalf("expected nil, got %v", got)
}
if got := idx.Extract("织忆", 0); got != nil {
t.Fatalf("maxEntities=0 应返回 nil, got %v", got)
}
}
func TestExtractCachesVocabularyAndBacksOffOnFailure(t *testing.T) {
load, calls := fakeVocab(map[string]float64{"织忆": 3.0e-06})
idx := NewEntityIndex(load)
now := time.Unix(1700000000, 0)
idx.now = func() time.Time { return now }
idx.Extract("织忆", 1)
idx.Extract("织忆", 1)
if *calls != 1 {
t.Fatalf("TTL 内应只加载一次,calls=%d", *calls)
}
now = now.Add(11 * time.Minute)
idx.Extract("织忆", 1)
if *calls != 2 {
t.Fatalf("TTL 过期应重新加载,calls=%d", *calls)
}
// 加载失败(空词表)→ 退避期内不重复打 DB,且不 panic
failCalls := 0
bad := NewEntityIndex(func() map[string]float64 { failCalls++; return nil })
bad.now = func() time.Time { return now }
if got := bad.Extract("织忆", 1); got != nil {
t.Fatalf("空词表应返回 nil, got %v", got)
}
bad.Extract("织忆", 1)
if failCalls != 1 {
t.Fatalf("退避期内应只尝试一次,failCalls=%d", failCalls)
}
now = now.Add(2 * time.Minute)
bad.Extract("织忆", 1)
if failCalls != 2 {
t.Fatalf("退避期过后应重试,failCalls=%d", failCalls)
}
}
func TestExtractDegreeFilterDropsNoiseNode(t *testing.T) {
load, _ := fakeVocab(map[string]float64{
"天气": 3.0e-06,
"一句话": 1.6e-06,
})
idx := NewEntityIndex(load)
idx.Degree = func(name string) int {
if name == "一句话" {
return 2 // 自动切词噪声节点(实测 graph.db:一句话 deg=2)
}
return 50
}
got := idx.Extract("完全不相干的一句话天气不错", 3)
if !reflect.DeepEqual(got, []string{"天气"}) {
t.Fatalf("got %v want [天气]", got)
}
// 度数过滤把候选清空 → 返回 nil(宁可不给,也别给噪声笔记)
idx2 := NewEntityIndex(load)
idx2.Degree = func(string) int { return 0 }
if got := idx2.Extract("完全不相干的一句话天气不错", 3); got != nil {
t.Fatalf("got %v want nil", got)
}
}
func TestNilEntityIndexIsSafe(t *testing.T) {
var idx *EntityIndex
idx.Warm(0)
if got := idx.Extract("织忆", 3); got != nil {
t.Fatalf("expected nil, got %v", got)
}
noLoad := NewEntityIndex(nil)
if got := noLoad.Extract("织忆", 3); got != nil {
t.Fatalf("expected nil, got %v", got)
}
}