Skip to content

feat(memory): L2 支持关键词与混合检索,嵌入模型改为可选 - #11

Merged
JohnRichard4096 merged 6 commits into
mainfrom
feat/hybrid-retrieval
Nov 23, 2026
Merged

JohnRichard4096 merged 6 commits into
mainfrom
feat/hybrid-retrieval

Conversation

@AmriaLin

@AmriaLin AmriaLin commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

L2 之前读写都直接 call_embedding,没有嵌入服务的部署连最小可用都做不到。现在检索分三模式,读工具与潜意识工具都能指定:

  • keyword:本地 BM25,只走 collection.get,不碰嵌入服务
  • vector:只走向量,模型缺席时报错,不静默换语义
  • hybrid:两路各排序后按 vector_weight(默认 0.6)加权 RRF 融合

auto 为默认:模型可用就 hybrid,不可用退 keyword。探测结果缓存在进程内,一次失败后不再反复撞连不上的服务。写入侧没有模型时写占位向量(宽度沿用库里已有向量),保证仅关键词部署也能增删改。

关键词分词不引入依赖:英文按词、中文二元组,单字只用于单词查询。RRF 的 k 取 20 而非 60——候选只有十几条时 k=60 会把名次差压平,权重翻不动顺序。

配套改动:embedding_available() 供指纹重映射判断,模型不可用时跳过;ambot memory reindex 在 EMBEDDING_ENABLED=false 时直接拒绝。

验证:ruff check/format 通过,pyright 零 error,py_compile 通过;query_notes 三模式用临时脚本跑过(BM25 排序、权重翻转、衰减重排、空结果、降级、占位向量写入),脚本未提交。

影响面:read_memory 多两个可选参数;配了模型的环境默认从纯向量变为 hybrid,排序会有变化,这是本次要的行为;配置新增 [retrieval] 与 EMBEDDING_ENABLED,不配置时旧部署照常可用。

Summary by Sourcery

Make L2 memory usable without embeddings while adding configurable keyword, vector, and hybrid retrieval.

New Features:

  • Add keyword, vector, hybrid, and automatic fallback modes for L2 memory retrieval, with configurable fusion weighting.
  • Allow L2 memory operation without an embedding service by using local keyword search and placeholder vectors for writes.

Bug Fixes:

  • Avoid repeated embedding-service failures by caching unavailability and prevent unavailable embeddings from blocking startup remapping checks.

Enhancements:

  • Expose retrieval mode and vector-weight controls through memory tools and configuration.
  • Support BM25 keyword ranking, weighted reciprocal-rank fusion, and decay-aware reranking across retrieval modes.

Build:

  • Bump the project version to 0.10.0.

Documentation:

  • Update documentation and configuration examples to describe optional embeddings and the new L2 retrieval modes.

Chores:

  • Reject manual memory reindexing when embeddings are explicitly disabled.

之前读写都直接 call_embedding,没有嵌入服务的部署连最小可用都做不到。
现在检索分三种模式,读工具与潜意识工具都能按需指定:

- keyword:本地 BM25,只走 collection.get,不碰嵌入服务
- vector:只走向量,模型缺席时报错而不是静默换语义
- hybrid:两路各排序后按 vector_weight(默认 0.6)做加权 RRF 融合

auto 作为默认值:模型可用就 hybrid,不可用退到 keyword。探测结果缓存在
进程内,第一次失败后不再反复撞连不上的服务。

写入侧没有模型时写占位向量(宽度沿用库里已有向量,避免维度冲突),
保证仅关键词部署也能正常增删改;有模型时行为不变。

关键词分词不引入依赖:英文按词、中文二元组,单字只用于单词查询。
RRF 的 k 取 20 而不是 60,候选只有十几条时 k=60 会让名次差失去意义,
权重翻不动顺序。

另外两处配套:embedding_available() 供指纹重映射判断,模型不可用时跳过;
ambot memory reindex 在 EMBEDDING_ENABLED=false 时直接拒绝。

验证:ruff check/format、pyright 零 error、py_compile;纯逻辑与
query_notes 三模式用临时脚本跑过(BM25 排序、权重翻转、衰减重排、
空结果、降级、占位向量写入),脚本未提交。
原文把插件写成"基于向量数据库"、L2 叫"向量记忆层",与现在的实际行为不
符:嵌入可以完全不配。另外时间退化的公式只写了向量距离那一路,混合模式
用融合分,补上两种来源。

配置表标注 default_mode 与 vector_weight 可被工具参数覆盖。
@sourcery-ai

sourcery-ai Bot commented Nov 23, 2026

Copy link
Copy Markdown

Sorry @AmriaLin, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 4 days and 20 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Data structures

新增 BM25 分词与打分、加权 RRF 融合等检索基础逻辑,供 L2 检索实现使用。

  • amrita_plugin_memory/retrieval.py L1-142

API

扩展记忆读取工具参数以支持检索模式与向量权重,并更新 CLI 重映射入口以拒绝显式禁用嵌入时的操作。

  • amrita_plugin_memory/cli.py L26, L121-123
  • amrita_plugin_memory/tools.py L116-133 (+3 small edits)

为潜意识记忆读取工具暴露检索模式与向量权重参数。

  • amrita_plugin_memory/rethinking/schemas.py L28-41

Uses of the API

将潜意识读取工具传入的检索模式与权重转交给记忆检索接口。

  • amrita_plugin_memory/rethinking/tools.py L114-115

Implementation

在 L2 存储层实现关键词、向量与混合检索、无嵌入时的占位向量写入,以及与时间衰减重排的衔接。

  • amrita_plugin_memory/vector.py L55-131, L435-493, L541-669 (+7 small edits)

让衰减重排可接收调用方提供的相关度分数,并统一距离检索与融合分数检索的衰减排序。

  • amrita_plugin_memory/decay.py L141-163, L177-180 (+3 small edits)

将嵌入可用性接入指纹重映射流程,在模型不可用时跳过重映射并记录失败状态。

  • amrita_plugin_memory/embedding.py L32-37, L391-394, L409

Configuration

新增检索模式、向量权重、关键词扫描上限及可选嵌入开关的配置定义,并升级项目版本。

  • amrita_plugin_memory/config.py L181-204, L223-225, L256-262
  • pyproject.toml L4

调整多个模块中与本次改动无关的注释标记,统一由 Sphinx 风格的 #: 改为普通注释。

  • amrita_plugin_memory/permissions.py L21-23
  • amrita_plugin_memory/profile_store.py L27
  • amrita_plugin_memory/rethinking/heatmap.py L31, L54
  • amrita_plugin_memory/rethinking/runner.py L51-53
  • amrita_plugin_memory/rethinking/schedule.py L30-32, L210, L216-219
  • amrita_plugin_memory/rethinking/tools.py L64-68
  • amrita_plugin_memory/tasks.py L13

Documentation

更新 README,说明 L2 检索模式、可选嵌入部署、衰减行为及新增配置项。

  • README.md L125-142, L162-167, L351-356 (+11 small edits)

Lock files and vendored code

同步更新依赖锁文件。

  • uv.lock L280

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

#: 是 Sphinx 的变量文档标记,仓库里没有文档构建消费它,读起来只是多两个
字符。统一成 #,注释内容不变。
上一笔只清了本次改动碰到的地方,仓库里还散着 17 处。统一成 #,
注释内容和位置不变。
L2 多出检索模式、嵌入模型转为可选,都是行为变更,按 0.x 的 minor 走。
之前 embedding_available() 会先拿 "ping" 发一次探测请求,每个进程开局都
多一次无用调用。改成状态只由真实请求给出:成功置 True,异常置 False 并
告警一次,之后不再尝试。

auto/hybrid 在请求失败后就地降级成 keyword,结果里标 degraded;
显式 vector 仍然报错,不静默换语义。
@JohnRichard4096
JohnRichard4096 merged commit 0867498 into main Nov 23, 2026
3 checks passed
@JohnRichard4096
JohnRichard4096 deleted the feat/hybrid-retrieval branch November 23, 2026 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants