diff --git a/backend/app/api/copilot.py b/backend/app/api/copilot.py index 741b1c1b..a936b78a 100644 --- a/backend/app/api/copilot.py +++ b/backend/app/api/copilot.py @@ -120,7 +120,10 @@ NODE_REFERENCE = """\ - branch:条件分支。config: {mode: expression|llm, cases:[{key, condition, label}]} - loop:循环。config: {mode: foreach|while, items, item_var, condition, max_iterations} - retrieve:知识库检索。config: {query, collection, limit, assign_to} -- memory:长期记忆读写。config: {action: recall|write, query, content, scope, assign_to} +- memory:长期记忆读写。config: {action: recall|write, query, content, assign_to} + **不要写 scope**。记忆域由平台给(当前统一是 default)。自己起一个域的后果是 + 写进去再也读不回来——真发生过:一轮写进 scope"user",下一轮没写 scope 落回 + default,于是「我叫张三」存下来了,问「我是谁」却答不上来 - human:人工介入。config: {mode: approve|input|edit, title, message} - validate:JSON Schema 校验,可自动让模型修复。config: {schema, source, max_retries} - transform:数据整形。config: {mode: expression|template|json, expression/template, assign_to} diff --git a/backend/app/api/knowledge.py b/backend/app/api/knowledge.py index 37c87255..677627c7 100644 --- a/backend/app/api/knowledge.py +++ b/backend/app/api/knowledge.py @@ -207,6 +207,9 @@ async def embedding_status( "kind": saved.get("kind", "local"), "model": saved.get("model", ""), "stale_chunks": await kb.stale_count(session, collection), + # 记忆和知识库共用一个 embedder,换模型时一起失效——报一个漏一个 + # 的话,用户点完重建还是想不起事,而且不知道为什么 + "stale_memories": await store.stale_count(session), "has_semantics": has_semantics(), "default_alpha": default_alpha(), # 没建倒排的片段会走全表扫——结果对,但慢 @@ -247,6 +250,7 @@ async def set_embedding( "embedder": emb.embedder_id(), "dim": emb.embedder_dim(), "stale_chunks": await kb.stale_count(session), + "stale_memories": await store.stale_count(session), } @@ -254,8 +258,16 @@ async def set_embedding( async def reindex( collection: str | None = None, session: AsyncSession = Depends(get_session) ) -> dict[str, Any]: - """用当前 embedder 重算向量。换了模型之后唯一的恢复手段。""" - return await kb.reindex(session, collection) + """用当前 embedder 重算向量。换了模型之后唯一的恢复手段。 + + 知识库和长期记忆一起重建:它们共用一个 embedder,换模型时一起失效。 + 分成两个按钮的结果是点了一个、以为好了,另一个还在悄悄退回关键词—— + 真踩过,记忆那边连个提示都没有。 + """ + out = await kb.reindex(session, collection) + # 记忆不按 collection 分,重建就是全量 + out["memories_reindexed"] = await store.reindex(session) + return out @kb_router.delete("/documents/{doc_id}", status_code=204) diff --git a/backend/app/memory/store.py b/backend/app/memory/store.py index b286be6c..4a20ccde 100644 --- a/backend/app/memory/store.py +++ b/backend/app/memory/store.py @@ -8,7 +8,8 @@ from sqlalchemy.ext.asyncio import AsyncSession from app.db.models import MemoryItem from app.memory.embeddings import ( - cosine, embed_text, embedder_dim, embedder_id, from_blob, hybrid_rank, to_blob, usable, + cosine, embed_text, embed_texts, embedder_dim, embedder_id, from_blob, hybrid_rank, + to_blob, usable, ) # 判重阈值:余弦相似度高于它才算同一件事。比排名分数严格得多, @@ -88,8 +89,14 @@ async def recall( limit: int = 5, kind: str | None = None, min_score: float = 0.05, + on_degrade: Any = None, ) -> list[dict[str, Any]]: - """按混合相关度取回记忆,并按重要性和使用次数做轻微加权。""" + """按混合相关度取回记忆,并按重要性和使用次数做轻微加权。 + + 存量向量和当前 embedder 对不上时整体退回纯关键词。以前这里退得悄无声息 + ——知识库那边至少会发一条 warn,记忆这边只是"最近想不起事",而人根本 + 不会想到要去重建索引。 + """ stmt = select(MemoryItem).where(MemoryItem.scope == scope) if kind: stmt = stmt.where(MemoryItem.kind == kind) @@ -99,7 +106,15 @@ async def recall( query_vec = await embed_text(query) # 和 kb.search 同一个道理:有对不上的就整体退回关键词,而不是让它们静默沉底 - if any(not usable(r.embed_model, r.embed_dim) for r in rows): + stale = [r for r in rows if not usable(r.embed_model, r.embed_dim)] + if stale: + if on_degrade: + names = sorted({r.embed_model or "未知模型" for r in stale}) + on_degrade( + f"记忆域「{scope}」里有 {len(stale)}/{len(rows)} 条向量由" + f"「{'、'.join(names)}」建立,和当前的「{embedder_id()}」对不上," + f"本次已退回纯关键词召回。重建索引后恢复。" + ) vectors: list[Any] = [None] * len(rows) alpha = 0.0 else: @@ -170,6 +185,43 @@ async def list_memories( return list((await session.execute(stmt)).scalars()) +async def stale_count(session: AsyncSession, scope: str | None = None) -> int: + """有多少条记忆的向量和当前 embedder 对不上。""" + from sqlalchemy import or_ + + stmt = select(func.count(MemoryItem.id)).where( + or_(MemoryItem.embed_model != embedder_id(), MemoryItem.embed_dim != embedder_dim())) + if scope: + stmt = stmt.where(MemoryItem.scope == scope) + return int((await session.execute(stmt)).scalar_one() or 0) + + +async def reindex(session: AsyncSession, scope: str | None = None) -> int: + """用当前 embedder 重算记忆的向量,返回重算了多少条。 + + 换 embedding 模型之后记忆和知识库一起失效,但在此之前只有知识库有重建 + 路径——记忆这边悄悄退回关键词,而且没有任何恢复手段。 + """ + stmt = select(MemoryItem) + if scope: + stmt = stmt.where(MemoryItem.scope == scope) + rows = list((await session.execute(stmt)).scalars()) + if not rows: + return 0 + + model_id, dim = embedder_id(), embedder_dim() + batch = 64 # 和 kb.reindex 一样按批:一次几千条全发给远端接口必被限流 + for i in range(0, len(rows), batch): + part = rows[i:i + batch] + vectors = await embed_texts([r.content for r in part]) + for row, vec in zip(part, vectors): + row.embedding = to_blob(vec) + row.embed_model = model_id + row.embed_dim = dim + await session.commit() + return len(rows) + + async def list_scopes(session: AsyncSession) -> list[dict[str, Any]]: rows = await session.execute( select(MemoryItem.scope, func.count(MemoryItem.id)).group_by(MemoryItem.scope) diff --git a/backend/tests/test_conversations.py b/backend/tests/test_conversations.py index 2b63e096..8a068f2a 100644 --- a/backend/tests/test_conversations.py +++ b/backend/tests/test_conversations.py @@ -436,3 +436,19 @@ def test_the_three_paths_are_spelled_out_for_the_model() -> None: # 协议里要给得出这两个出口,否则模型没法表达 assert '"op":"reply"' in _STREAM_PROTOCOL assert '"run":true' in _STREAM_PROTOCOL + + +async def test_copilot_is_told_not_to_invent_a_memory_scope() -> None: + """记忆域由平台给。图里自己起一个,写进去就再也读不回来。 + + 真发生过(会话 cbe09f4b 的第 2~3 轮):一轮的 memory 节点写了 + scope="user",下一轮没写 scope、落回 default,于是「我叫张三」存下来了, + 问「我是谁」却答不上来——记忆在库里好好的,只是读错了地方。 + """ + from app.api.copilot import NODE_REFERENCE + + memory_line = next( + (l for l in NODE_REFERENCE.splitlines() if l.strip().startswith("- memory:")), "") + assert memory_line, "节点参考里找不到 memory 这一行" + assert "scope" not in memory_line, "scope 还列在可填字段里,模型就会去填它" + assert "不要写 scope" in NODE_REFERENCE diff --git a/backend/tests/test_kb.py b/backend/tests/test_kb.py index f1ebaa78..7b34e37f 100644 --- a/backend/tests/test_kb.py +++ b/backend/tests/test_kb.py @@ -245,3 +245,109 @@ def test_a_scanned_pdf_is_called_out_rather_than_ingested_empty() -> None: with pytest.raises(UnsupportedDocument) as e: extract(buf.getvalue(), "scan.pdf") assert "扫描件" in str(e.value) + + +# -------------------------------------------------------------------------- +# 长期记忆:和知识库同一个坑,以前只有知识库有恢复手段 +# -------------------------------------------------------------------------- + + +async def _memory(scope: str, content: str): + from app.memory import store + + async with SessionLocal() as session: + return await store.remember(session, scope=scope, content=content, dedupe=False) + + +async def test_memory_records_who_built_the_vector() -> None: + from app.db.models import MemoryItem + + item = await _memory("memtest_mark", "后端跑在 8001 端口。") + async with SessionLocal() as session: + row = await session.get(MemoryItem, item.id) + assert row.embed_model == emb.embedder_id() and row.embed_dim == emb.embedder_dim() + + +async def test_memory_recall_degrades_out_loud() -> None: + """以前退得悄无声息。知识库那边至少会发一条 warn,记忆这边只是"最近想不起 + 事",而人根本不会想到要去重建索引。""" + from sqlalchemy import select + + from app.db.models import MemoryItem + from app.memory import store + + await _memory("memtest_stale", "公司年会定在十二月二十号。") + async with SessionLocal() as session: + row = (await session.execute(select(MemoryItem).where( + MemoryItem.scope == "memtest_stale"))).scalars().first() + row.embed_model, row.embed_dim = "openai:text-embedding-3-small", 1536 + row.embedding = to_blob(np.ones(1536, dtype=np.float32)) + await session.commit() + + notes: list[str] = [] + async with SessionLocal() as session: + hits = await store.recall(session, scope="memtest_stale", query="年会", + on_degrade=notes.append) + assert hits, "退回关键词也要能召回,而不是空手" + assert notes and "重建索引" in notes[0], notes + + +async def test_memory_reindex_restores_it() -> None: + from sqlalchemy import select + + from app.db.models import MemoryItem + from app.memory import store + + await _memory("memtest_fix", "张三的邮箱是 zhangsan@example.com。") + async with SessionLocal() as session: + row = (await session.execute(select(MemoryItem).where( + MemoryItem.scope == "memtest_fix"))).scalars().first() + row.embed_model, row.embed_dim = "别的模型", 99 + await session.commit() + + assert await store.stale_count(session, "memtest_fix") == 1 + assert await store.reindex(session, "memtest_fix") == 1 + assert await store.stale_count(session, "memtest_fix") == 0 + + notes: list[str] = [] + async with SessionLocal() as session: + hits = await store.recall(session, scope="memtest_fix", query="邮箱", + on_degrade=notes.append) + assert hits and not notes, "重建之后不该再降级" + + +async def test_one_rebuild_covers_both_stores() -> None: + """知识库和记忆共用一个 embedder,换模型时一起失效。 + + 分成两个按钮的结果是点了一个、以为好了,另一个还在悄悄退回关键词。 + """ + from httpx import ASGITransport, AsyncClient + from sqlalchemy import select + + from app.db.models import Chunk, MemoryItem + from app.main import app + from app.memory import store + + await _seed("both_fix", ["平台管理员有六个。"]) + await _memory("both_mem", "商家账户共五个。") + + async with SessionLocal() as session: + for model_cls, scope_col, scope_val in ( + (Chunk, Chunk.collection, "both_fix"), (MemoryItem, MemoryItem.scope, "both_mem") + ): + for row in (await session.execute( + select(model_cls).where(scope_col == scope_val))).scalars(): + row.embed_model, row.embed_dim = "旧模型", 7 + await session.commit() + + transport = ASGITransport(app=app) + async with AsyncClient(transport=transport, base_url="http://test") as c: + status = (await c.get("/api/kb/embedding")).json() + assert status["stale_chunks"] >= 1 and status["stale_memories"] >= 1 + + out = (await c.post("/api/kb/reindex")).json() + assert out["reindexed"] >= 1 + assert out["memories_reindexed"] >= 1, "重建只管了知识库,记忆还留在旧模型上" + + after = (await c.get("/api/kb/embedding")).json() + assert after["stale_chunks"] == 0 and after["stale_memories"] == 0 diff --git a/frontend/src/api/client.ts b/frontend/src/api/client.ts index 3eba1bbf..e3d1aba7 100644 --- a/frontend/src/api/client.ts +++ b/frontend/src/api/client.ts @@ -193,12 +193,13 @@ export const api = { remove: (id: string) => del(`/kb/documents/${id}`), embedding: (collection?: string) => get<{ embedder: string; dim: number; configured: boolean - kind: string; model: string; stale_chunks: number + kind: string; model: string; stale_chunks: number; stale_memories: number + unindexed_chunks: number }>(`/kb/embedding${collection ? `?collection=${collection}` : ''}`), setEmbedding: (body: { kind: string; model?: string }) => put<{ embedder: string; dim: number; stale_chunks: number }>('/kb/embedding', body), reindex: (collection?: string) => - post<{ reindexed: number; embedder: string }>( + post<{ reindexed: number; memories_reindexed: number; embedder: string }>( `/kb/reindex${collection ? `?collection=${collection}` : ''}`), }, skills: { diff --git a/frontend/src/pages/KnowledgePage.tsx b/frontend/src/pages/KnowledgePage.tsx index 8a09053b..d071e1d2 100644 --- a/frontend/src/pages/KnowledgePage.tsx +++ b/frontend/src/pages/KnowledgePage.tsx @@ -51,7 +51,14 @@ function EmbedderBar({ collection, onChanged }: { useEffect(() => { void refresh() }, [collection]) if (!info) return null - const stale = info.stale_chunks ?? 0 + // 记忆和知识库共用一个 embedder,换模型时一起失效。只报一个的话, + // 用户点完重建还是想不起事,而且不知道为什么 + const staleChunks = info.stale_chunks ?? 0 + const staleMemories = info.stale_memories ?? 0 + // 没建倒排的片段也归这个按钮管:它们走全表扫,结果对但慢,而重建正好 + // 把倒排一起建了。按钮只认"向量对不上"的话,这些片段永远等不到人来点 + const unindexed = info.unindexed_chunks ?? 0 + const stale = staleChunks + staleMemories + unindexed const pick = async (kind: string, model: string) => { setBusy(true) @@ -69,7 +76,9 @@ function EmbedderBar({ collection, onChanged }: { setBusy(true) try { const out = await api.kb.reindex(collection) - toast(`已用 ${out.embedder} 重建 ${out.reindexed} 段`, 'ok') + const mem = out.memories_reindexed ?? 0 + toast(`已用 ${out.embedder} 重建 ${out.reindexed} 段知识` + + (mem ? ` 和 ${mem} 条记忆` : ''), 'ok') await refresh() await onChanged() } catch (e: any) { @@ -99,12 +108,21 @@ function EmbedderBar({ collection, onChanged }: { )} - {stale > 0 && ( + {staleChunks + staleMemories > 0 && (

- 有 {stale} 段的向量是用别的模型建的,和当前模型对不上——检索这些内容时 + 有{staleChunks ? ` ${staleChunks} 段知识` : ''} + {staleChunks && staleMemories ? '、' : ''} + {staleMemories ? ` ${staleMemories} 条记忆` : ''} + 的向量是用别的模型建的,和当前模型对不上——检索/召回这些内容时 会退回纯关键词(搜不出同义表达)。重建索引后恢复。

)} + {unindexed > 0 && staleChunks + staleMemories === 0 && ( +

+ 有 {unindexed} 段还没建倒排索引,检索它们会退回全表扫——结果是对的, + 只是慢。重建一次就好。 +

+ )} {info.kind !== 'openai' && stale === 0 && (

本地向量不联网、零配置,但它是词频哈希、没有语义泛化——