feat(doc): dense vector index for unified text+media retrieval

文档层引入稠密向量索引,与媒体检索共享同一多模态空间:
- Doc 加 DenseVec 字段(json:-,运行时计算)
- Consume/QueryScored 优先使用 denseSearchScored(brute-force cosine),
  未配置时退化到 TF-IDF 倒排检索
- buildDenseIndex 在 Agent 启动时为全部文档一次性计算稠密向量
- L0 RelevanceContext 支持 denseSpace(Prune 使用稠密余弦),
  退化到 fastText 稀疏余弦

vector 包新增 DenseCosine([]float64 brute-force cosine)。

验证:492 篇文档 brute-force ~300ms,全部测试通过。
This commit is contained in:
JianFeeeee
2026-09-09 18:56:44 +08:00
parent 6c2039f5c9
commit 580d5f5501
4 changed files with 239 additions and 25 deletions

View File

@ -304,6 +304,21 @@ func CosineSimilarity(a, b Vector) float64 {
return dot / (math.Sqrt(normA) * math.Sqrt(normB))
}
// DenseCosine 计算两个 []float64 稠密向量的余弦相似度。
// 与 CosineSimilarity(稀疏 map)数学等价,但面向稠密多模态向量。
func DenseCosine(a, b []float64) float64 {
var dot, na, nb float64
for i := range a {
dot += a[i] * b[i]
na += a[i] * a[i]
nb += b[i] * b[i]
}
if na == 0 || nb == 0 {
return 0
}
return dot / math.Sqrt(na*nb)
}
// InvertedIndex 倒排索引,加速向量搜索
type InvertedIndex struct {
mu sync.RWMutex