Skip to main content

VecRecall

PyPI CI Python 许可证

**本地优先的 AI 记忆库。**给 Agent 和 AI 应用存长期记忆:存原文、按需召回、只把相关的几百 token 放进 prompt。默认零依赖,数据全部在本机 SQLite 里;需要多 Agent 隔离、不可变审计记录时也能直接用。

English: README_EN.md · 版本记录:CHANGELOG.md

零依赖默认配置 装上语义模型
LongMemEval-S 召回(R_any@5,470 题) 94.7% 98.3%
每次召回放进 prompt 的上下文(top-5) 约 1,100 token 约 1,200 token
额外依赖 无 sentence-transformers

数字可复现,方法见 召回率。


安装

pip install vecrecall                    # 零依赖:词法嵌入 + BM25 + SQLite
pip install "vecrecall[sentence]"        # 加多语言语义模型(召回更高,写入时每条多一次编码)
pip install "vecrecall[crypto]"          # 加静态加密
pip install "vecrecall[full]"            # 以上全部 + ChromaDB

从源码安装:git clone https://github.com/snowfoxHQ/vecRecall && cd vecRecall && pip install -e .

装了 sentence-transformers 时,VecRecall() 会自动用语义模型;没装就用零依赖的词法嵌入。也可以用 embedding_backend= 显式指定,见后端。


5 分钟上手

from vecrecall import VecRecall

with VecRecall(base_dir="~/.vr", wing="my-project") as palace:
    # 写入:原文逐字保存
    palace.add("决定用 PostgreSQL 替代 MySQL,原因是 PostgreSQL 的 JSON 支持更好",
               topic="database", importance=0.9)

    # 存档一段对话:默认每轮一条记忆,共用同一个 session_id
    palace.archive_session([
        {"role": "user", "content": "我刚领养了一只金毛,叫 Max"},
        {"role": "assistant", "content": "恭喜!先从笼内训练开始"},
    ], session_id="chat-0525")
    palace.search("我的狗叫什么名字", n=1)   # 命中「我刚领养了一只金毛,叫 Max」那一轮

    # 检索:默认 rrf(向量 + BM25 按名次融合)
    for r in palace.search("为什么用 PostgreSQL 不用 MySQL", n=5):
        print(r.score, r.memory.content)

    # 构建要放进 prompt 的上下文
    ctx = palace.build_context(current_query="PostgreSQL 的 JSON 支持怎么样", load_l3=True)
    print(ctx.total_tokens_estimate)

命令行:

vr init --dir ~/.vr --wing my-project

# 写入两条记忆
vr add "登录接口的 JWT 有效期从 1 小时改成 24 小时,refresh token 保持 30 天" --topic auth
vr add "订单服务的数据库连接池上限调到 100" --topic database

# 存档一段对话(文件格式见下方),默认每轮对话一条记忆
vr archive chat.json

# 检索:默认 rrf;可用 --mode vector|bm25|rrf 切换
vr search "JWT 有效期是多少"

# 生成要放进 prompt 的上下文:L1 关键记忆 + 与问题相关的记忆
vr context "JWT 有效期多长,要不要改" --l3

chat.json 是一个消息列表:

[
  {"role": "user", "content": "测试环境的 Redis 地址改成 10.0.0.12 了"},
  {"role": "assistant", "content": "好的,已更新配置说明"}
]

接到 Claude Code 等 MCP 客户端:

{ "mcpServers": { "vecrecall": { "command": "vr-mcp", "args": ["--dir", "~/.vr", "--wing", "my-project"] } } }

核心设计

检索和组织分开。wing(项目)、topic(话题)只用来浏览和整理,不参与检索过滤;检索永远在全部记忆上做。按目录过滤检索的做法,会把放错目录的相关记忆直接漏掉。

**存原文,摘要只给人看。**写入的内容逐字保存;ui_summary 等摘要只在列表里展示,不参与检索。

**按轮存储,按需召回。**会话默认每轮对话存一条记忆。和整段会话存一条相比,命中的那一轮不会被长会话里的其他内容稀释,召回的上下文也只有原来的约十二分之一。

**四层上下文。**一次唤醒只加载需要的部分:

L0  身份      ~50 token    每次都加载
L1  关键记忆  ~600 token   按重要性取 top-15;immutable 记忆始终包含
L2  语义触发  按需          与当前话题相似度 ≥ 0.55 才加载
L3  深度检索  显式请求      默认 rrf 检索

**默认用 rrf 检索。**同时跑向量检索和 BM25 词法检索,只按名次合并(Reciprocal Rank Fusion),不直接比较两边的分数。语义模型擅长同义改写,BM25 擅长精确词匹配,两路结果互补。零依赖时比纯向量高 2.6 个百分点;装上语义模型后在各项指标上都超过单独的 BM25。mode="vector" 或 mode="bm25" 可以切换。


召回率

评测集是 LongMemEval(ICLR 2025)的 S 版:500 道题,每道题配一段约 48 个会话、约 490 轮对话的历史,并标注答案所在的会话。按官方做法跳过 30 道拒答题,评 470 道;只评检索,不评问答。

配置(每轮一条记忆) 嵌入 R_any@1 R_any@5 R_any@10 MRR 偏好类 R_any@5
rrf(默认) 语义 sentence 91.3% 98.3% 98.9% 0.944 96.7%
vector 语义 sentence 85.7% 96.4% 97.0% 0.904 90.0%
bm25 不需要 87.9% 96.6% 98.1% 0.914 80.0%
rrf(默认) 词法 bow(零依赖) 87.0% 94.7% 98.3% 0.907 66.7%
vector 词法 bow 77.9% 92.1% 94.3% 0.835 46.7%
vector,整段会话一条(v2.2 默认) 词法 bow 50.0% 75.3% 84.9% 0.611 26.7%
  • R_any@k:前 k 条结果里至少有一个答案会话。会话级召回,把检索到的记忆换算成所属会话再去重。
  • 语义模型是默认的 paraphrase-multilingual-MiniLM-L12-v2。在普通桌面 CPU 上编码约 14 ms/条,就是写入时多出来的时间。
  • 偏好类只有 30 题,误差约 ±4 个百分点;其余题型见原始报告。

复现:

python benchmarks/longmemeval.py --data benchmarks/data/longmemeval_s_cleaned.json \
    --granularity turn --configs vector,bm25,rrf [--embedder sentence]

下载数据、全部参数和按题型的结果见 benchmarks/README.md,原始结果在 benchmarks/results/。


规模与性能

默认配置(SQLite 向量后端 + 词法嵌入)在不同记忆数下的延迟,单位毫秒,p50:

记忆数 rrf 检索(默认) bm25 检索 vector 检索 写入(不去重) 写入(add() 默认去重) 磁盘
1,000 44 0.3 45 4.3 47 6 MB
10,000 449 2.2 454 4.5 427 60 MB
50,000 2,303 12 2,267 4.4 2,152 300 MB
100,000 4,907 30 4,631 4.5 4,324 600 MB
  • 几千条以内(个人日常使用的常见规模)各项操作都在几百毫秒以内。
  • 瓶颈是默认的 SQLite 向量后端:它用纯 Python 逐条计算相似度,vector / rrf 检索和开启去重的写入都随记忆数线性变慢,约 0.045 ms/条。BM25 本身很快,10 万条时 p50 仍只有 30 ms。
  • 首次 rrf / bm25 检索要先建 BM25 索引(1 万条约 1.3 秒,10 万条约 14 秒)。MCP / REST 这类常驻服务只付一次;vr 命令行每次都是新进程,每次都要重建。

记忆较多时可以这样缓解:

  • 用 archive_session() 写入(逐轮模式默认不去重),或 add(..., dedup=False),写入就不随库变大而变慢。
  • 换成 Chroma 后端(pip install "vecrecall[chroma]",vector_backend=ChromaVectorBackend(...)),它用 ANN 索引,不做全量扫描。
  • 只需要词法检索时用 mode="bm25"。

默认后端的 ANN / 向量化计算计划在 v2.4 实现。数据来自 LongMemEval-S 的真实对话轮次,测试机为普通 Windows 桌面 CPU、Python 3.14,完整报告见 benchmarks/results/scale_bow.md,复现:python benchmarks/bench_scale.py --data benchmarks/data/longmemeval_s_cleaned.json。


功能一览

功能 说明 文档
认知层 写入时自动抽取话题、重要性、摘要、实体与关系;启发式零依赖,或接 Ollama / OpenAI 兼容 LLM 指南
语义去重 相似度 ≥ 0.92 时合并到已有记忆,计数加一 指南
时序知识图谱 记录实体、关系和属性随时间的变化(「谁在什么时候是什么状态」) 指南
多跳检索 向量命中后沿实体关系扩展,返回带路径的证据 指南
遗忘曲线与蒸馏 记忆强度随时间衰减;碎片记忆可压缩成一条长期记忆 指南
类型化记忆 episodic / semantic / procedural;procedural 按场景标签 + 版本精确匹配 指南
多 Agent 隔离 owner × visibility(private / shared);private 只有写入者能看到 指南
不可变记忆与哈希链 retention_policy="immutable" 不去重、不遗忘、不被清理;哈希链按 topic_key 分链校验 指南、区块链存档
静态加密 记忆原文、摘要、元数据加密落盘 指南
写入治理 写入前脱敏、幂等写回队列、检索决策、仓库知识映射 指南
推理链与反思 记录观察 / 假设 / 决策 / 行动,事后用结果评分,把有效决策写回长期记忆 指南
扩展钩子 before_write / after_write / recall_filter / on_delete,插件不用改核心 指南
插件 Trace 本地事件日志;记忆卡片门槛(未验证的经验不能以「已验证」入库) 指南
后端可插拔 向量:SQLite(默认)/ Chroma / 内存;嵌入:词法 / sentence-transformers / Ollama / OpenAI 兼容 API 指南

接入方式

方式 入口 说明
Python from vecrecall import VecRecall 全部功能
MCP vr-mcp / vr mcp 34 个工具,适用于 Claude Code、Gemini CLI 等 MCP 客户端
REST vr-api / vr api 零依赖 HTTP JSON 接口,默认 127.0.0.1:8791
CLI vr 29 个子命令

完整的工具列表、端点和命令见 docs/interfaces.md。


安全与隐私

  • 全部本地运行,核心功能不需要任何 API Key;数据默认在 ~/.vecrecall。
  • 默认不加密,原文以明文写入 knowledge.db。存放敏感内容时请开启静态加密(vr init --encrypt),或写入前脱敏(redact_payloads=True)。python scripts/audit_secrets.py 可以只读扫描已入库的记忆里有没有混入密钥。
  • 静态加密只覆盖 knowledge.db,向量索引、知识图谱、哈希链数据库目前不加密。
  • REST:设置 --api-key 后必须带 Bearer token;监听非本机地址时强制要求 token;默认不发 CORS 头。
  • MCP 的导入导出路径限制在数据目录内;全部 SQL 参数化。

开发

pip install -e ".[crypto]"
for t in core blockchain governance recall_policy repo_map lexical trace reasoning memory_card; do
  python tests/test_$t.py
done
python benchmarks/benchmark_recall.py    # 内置冒烟评测,几秒钟
  • 每次推送到 main 和每个 PR,GitHub Actions 会在 Linux(Python 3.10–3.12)和 Windows(3.12)上跑全部测试,并做打包检查。
  • 发版:推送 v* 标签后自动构建并发布到 PyPI(Trusted Publishing),流程见 RELEASING.md。
  • 已知问题与改进计划:IMPROVEMENT_PLAN.md。

版本记录

每个版本的改动见 CHANGELOG.md,早期版本的详细升级说明见 docs/upgrade-notes.md。

许可证

MIT

Metadata

Release files for vecrecall 2.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vecrecall 2.3.0
File Size Uploaded Provenance Actions
vecrecall-2.3.0.tar.gz 155.1 kB With provenance Details for vecrecall-2.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for vecrecall 2.3.0
File Interpreter ABI Platform Actions
vecrecall-2.3.0-py3-none-any.whl Python 3 none any Details for vecrecall-2.3.0-py3-none-any.whl

Total release size: 280.6 kB

Release files / vecrecall-2.3.0.tar.gz

Download URL vecrecall-2.3.0.tar.gz
Size 155.1 kB
Tags Source
SHA-256 checksum
How to use checksums
0d1c12052c9f3b0ea4831b9051a65e00b2d9d8855a99e09c6ee6e4765f153cae
BLAKE2b-256 checksum
How to use checksums
c7ef6e3d5c653555acef244a87eed96fc8552c6cbb1a7662a1a1146d51ec0da9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / vecrecall-2.3.0-py3-none-any.whl

Download URL vecrecall-2.3.0-py3-none-any.whl
Size 125.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e6f6182f2367c15b582440911fa4cefba1bd0dd5d9831fb83de5405bcd49fe7
BLAKE2b-256 checksum
How to use checksums
9e2d108d9d6299378e60f615592e733d1d3831146bd2c5374281ac538d39b88b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for vecrecall 2.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for vecrecall-2.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page