Files
AgentLab/backend/app/memory/store.py
T
ThearchyHelios 6a46e4b55c 大文件传得进来了:分批算向量、上传转后台、边读边数大小
压测量出来的三个问题,10MB 文本(切 7914 段):

    上传耗时  150s(本地哈希)   换成 Qwen3-Embedding-4B 要 40 分钟以上
    而这 150 秒是压在一个 HTTP 请求里同步跑完的

浏览器和反向代理的默认超时都在 60 秒上下,也就是 5MB 以上的文件用户必然
看到超时——而后端其实还在跑,跑完文档也确实入了库。界面显示失败、数据其实
成功,比直接拒绝还糟。

**分批算向量。** 原来 ingest 是 embed_texts(pieces) 一次全发。本地哈希只是慢,
换成远端模型就是必挂:近八千段一次请求塞不下(OpenAI 单次上限 2048)。
reindex 那边早就分批了,ingest 这条一直漏着——而我上一轮刚把"换真模型"做成
设置页里点两下的事。

**上传转后台。** create_document / process_document 拆开,上传立刻返回
status=processing,切块和算向量在后台跑,进度写回文档行,前端轮询显示
"正在切块并算向量… 4288/7914 段"。实测上传返回 0.07 秒。

进度这件事返工过一次:先用另一个 session 单独写,结果整个过程界面上都是空的、
只在结束时闪一下——SQLite 只允许一个写者,主事务开着的时候那次写被堵住了。
改成主事务自己按批提交,顺手把进度写进去。

失败要写回 failed,否则永远停在"处理中"转圈。服务重启时也把残留的
processing 收尾成 failed——和 runner 对 running 的 run 是同一个道理。

**边读边数大小。** 原来是 raw = await file.read() 读完再判断,传个 1GB 的
文件内存在报 413 之前就吃掉了——那个检查拦的是"入库",不是"占内存"。改成
按块读、超了立刻停。上限从写死的 10MB 变成配置项,默认 50MB。

端到端验过:10MB 上传返回 0.07s,后台进度 768→1472→…→7914 实时可见,
最终 ready / 7914 段。
2026-09-20 09:47:02 +08:00

232 lines
8.3 KiB
Python
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.
from __future__ import annotations
from datetime import datetime, timezone
from typing import Any
from sqlalchemy import delete, func, select
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.models import MemoryItem
from app.memory.embeddings import (
cosine, embed_text, embed_texts, embedder_dim, embedder_id, from_blob, hybrid_rank,
to_blob, usable,
)
# 判重阈值:余弦相似度高于它才算同一件事。比排名分数严格得多,
# 因为这里比的是真实语义距离,不是"谁排第一"。
_DEDUPE_THRESHOLD = 0.92
async def remember(
session: AsyncSession,
*,
scope: str,
content: str,
kind: str = "fact",
meta: dict[str, Any] | None = None,
importance: float = 0.5,
dedupe: bool = True,
) -> MemoryItem:
"""写一条长期记忆。
dedupe 会先看同 scope 下有没有近乎重复的内容 —— agent 很容易把同一件事
反复记下来,不去重的话记忆库几轮就被同义句灌满了。
"""
content = content.strip()
vec = await embed_text(content)
if dedupe:
# 判重必须看**真实相似度**,不能用 recall 的分数。recall 走 hybrid_rank,
# 那里的分数是 min-max 排名归一化的结果:排第一的那条在两路信号上都会
# 被归成 1.0,与它和查询像不像毫无关系。于是 scope 里只要已有两条记忆,
# 新内容随便撞上一个字排到第一,就会被判成"重复"而丢弃,接口还返回 201
# 并把那条不相关的旧记忆当成"刚保存的"交回去——静默丢数据。
stmt = select(MemoryItem).where(MemoryItem.scope == scope)
if kind:
stmt = stmt.where(MemoryItem.kind == kind)
candidates = list((await session.execute(stmt)).scalars())
best, best_score = None, 0.0
for row in candidates:
# 对不上当前 embedder 的直接跳过:维度不同 cosine 会抛,维度碰巧
# 相同但模型不同则更糟——两个空间之间的相似度是个随机数,
# 拿它判重会把不相关的旧记忆认成"重复"然后丢掉新内容
if not usable(row.embed_model, row.embed_dim):
continue
other = from_blob(row.embedding, embedder_dim())
if other is None:
continue
score = cosine(vec, other)
if score > best_score:
best, best_score = row, score
if best is not None and best_score > _DEDUPE_THRESHOLD:
best.importance = max(best.importance, importance)
best.use_count += 1
await session.commit()
await session.refresh(best)
return best
item = MemoryItem(
scope=scope,
kind=kind,
content=content,
meta=meta or {},
importance=importance,
embedding=to_blob(vec),
embed_model=embedder_id(),
embed_dim=embedder_dim(),
)
session.add(item)
await session.commit()
await session.refresh(item)
return item
async def recall(
session: AsyncSession,
*,
scope: str,
query: str,
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)
rows = list((await session.execute(stmt)).scalars())
if not rows:
return []
query_vec = await embed_text(query)
# 和 kb.search 同一个道理:有对不上的就整体退回关键词,而不是让它们静默沉底
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:
vectors = [from_blob(r.embedding, embedder_dim()) for r in rows]
alpha = 0.5
ranked = hybrid_rank(
query,
[r.content for r in rows],
vectors,
query_vec,
alpha=alpha,
)
results: list[dict[str, Any]] = []
now = datetime.now(timezone.utc)
for idx, score, parts in ranked:
row = rows[idx]
# 重要的、被用得多的记忆略微上浮
boosted = score * (0.8 + 0.4 * row.importance) * (1 + min(row.use_count, 10) * 0.01)
if boosted < min_score:
continue
results.append(
{
"id": row.id,
"content": row.content,
"kind": row.kind,
"meta": row.meta,
"importance": row.importance,
"score": round(boosted, 4),
"signals": {k: round(v, 4) for k, v in parts.items()},
"created_at": row.created_at.isoformat() if row.created_at else None,
}
)
if len(results) >= limit:
break
# 记一下被召回过,作为下次排序的信号
for r in results:
item = await session.get(MemoryItem, r["id"])
if item:
item.use_count += 1
item.last_used_at = now
await session.commit()
return results
async def forget(session: AsyncSession, memory_id: str) -> bool:
item = await session.get(MemoryItem, memory_id)
if not item:
return False
await session.delete(item)
await session.commit()
return True
async def clear_scope(session: AsyncSession, scope: str) -> int:
result = await session.execute(delete(MemoryItem).where(MemoryItem.scope == scope))
await session.commit()
return result.rowcount or 0
async def list_memories(
session: AsyncSession, *, scope: str | None = None, limit: int = 200
) -> list[MemoryItem]:
stmt = select(MemoryItem).order_by(MemoryItem.created_at.desc()).limit(limit)
if scope:
stmt = stmt.where(MemoryItem.scope == scope)
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()
from app.memory.kb import _EMBED_BATCH
batch = _EMBED_BATCH # 和 kb 那边统一:一次几千条全发给远端接口必被限流
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)
)
return [{"scope": scope, "count": count} for scope, count in rows]