mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-25 03:18:08 +00:00
fix(knowledge): 覆盖同名条目时摘掉旧向量
从一次真实的知识库更新里发现:在线实例更新一个已有条目之后, knowledge_count=32 而 vector_count=33——多出来的那一条是上一版的副本。 成因:vector.Store.Insert 是**追加**语义(s.docs = append + index.Add),不按 id 去重; 而 Store.Add 走的是「写 content.md + 覆盖 items[id] + Insert 向量」。 文件与内存条目都被正确替换了,只有向量索引多留了一份。 危害不在于多占内存:**检索可能命中已被替换掉的旧内容**,而且完全静默—— 条目数看起来是对的,只有向量数比条目数多。 修法:Insert 之前先 s.vec.Remove(id)(Remove 已按 id 过滤 docs 与倒排索引)。 回归测试 TestAddOverwriteReplacesVector 钉住 knowledge_count / vector_count / content.md 三者都必须只剩新版。 注:该文件在 origin/main 上本就有 32 行 gofmt 差异(结构体字段注释对齐), 不属本次改动,按纪律不做整体重排。
This commit is contained in:
@ -201,6 +201,13 @@ func (s *Store) Add(name, content string) error {
|
||||
}
|
||||
s.items[id] = k
|
||||
|
||||
// 覆盖同名条目时必须先摘掉旧向量。
|
||||
//
|
||||
// vector.Store.Insert 是**追加**语义(s.docs = append + index.Add),不按 id
|
||||
// 去重。少了这一步,更新一条知识会在向量索引里留下上一版的副本:条目数看起来
|
||||
// 是对的,只有向量数比条目数多——而检索可能因此命中已被替换掉的旧内容。
|
||||
s.vec.Remove(id)
|
||||
|
||||
vec := s.vectorize(name + " " + content)
|
||||
s.vec.Insert(id, name+": "+content, vec, map[string]string{
|
||||
"name": name, "path": path,
|
||||
|
||||
Reference in New Issue
Block a user