Skip to main content

DNA Memory — 多链 DNA 记忆匹配引擎

零依赖 · 零 Token · 亚毫秒级匹配

pip install dna-memory

这是一个基于多链 DNA 编码 + 加权投票 + 反链负反馈抑制的记忆匹配引擎。 源自在 Hermes Agent 中实战验证的神经蛊阵算法,抽离为独立包。


为什么需要它?

现有的记忆检索方案(Mem0、Hindsight 等)都需要 Agent 显式 API 调用才能存取记忆。 Agent 必须"知道自己需要记忆"——但这恰恰是最难的问题。

DNA Memory 的核心思想:每个实体自带多链 DNA,不是后贴的标签。匹配时各链加权投票,反链抑制假阳性,分歧驱动自动进化。

  • 自动注入 — 无需 Agent 主动调用,消息经过时自动匹配
  • 零 Token — 全部本地关键词匹配,0.02-0.06ms/次
  • 反链免疫 — 错误匹配自动产生抗体,下次不再犯
  • 可解释 — 每步可追踪(DNA→投票→反链→结果)

快速开始

from dna_memory import Config, DNAEncoder, DNAMatcher, AntiChain

# 1. 配置 5 条链
config = Config()
config.add_strand("domain", {
    "tech": ["python", "docker", "server", "技术", "服务器", "系统"],
    "video": ["视频", "生成", "渲染", "动画", "工作流"],
    "memory": ["记忆", "hindsight", "recall", "虫洞", "生态", "框架"],
}, weight=0.30)
config.add_strand("intent", {
    "deploy": ["部署", "安装", "启动"],
    "fix": ["修复", "修改", "更新"],
    "query": ["查询", "查看", "检查"],
}, weight=0.20)

# 2. 编码器 + 匹配器
encoder = DNAEncoder(config.strands)
matcher = DNAMatcher(config.weights())
matcher.mark_entity_strand("entity")  # 标记实体链

# 3. 实体库
entities = [
    {"id": "devops", "dna": {"domain": ["tech"], "intent": ["deploy", "fix"]}},
    {"id": "video_tool", "dna": {"domain": ["video"], "intent": ["build"]}},
    {"id": "memory_sys", "dna": {"domain": ["memory"], "intent": ["query"]}},
]

# 4. 匹配
dna = encoder.encode("帮我修复服务器部署")
result = matcher.match(dna, "帮我修复服务器部署", entities)
print(result.entity_id)  # → "devops"
print(result.score)      # → 0.50

反链免疫系统

from dna_memory import AntiChain, record_miss

anti = AntiChain()

# 错误匹配后自动学习
record_miss(
    query="帮我部署视频工作流",
    wrong_id="memory_sys",  # 匹配错了
    anti_chain=anti,
    strand_keywords=config.strands,
    wrong_dna={"domain": ["memory"], "intent": ["query"]},
)

# "视频" 和 "工作流" 被加入 memory_sys 的反链
# 下次 "视频工作流" 不会错误匹配到 memory_sys

anti.save("my_antibodies.json")

基准性能

操作 耗时
DNA 五链编码 0.02-0.06ms
磁吸匹配(10 实体) 0.08ms
反链扣分 0.01ms
全链路 < 0.2ms
Token 消耗 0

设计文档

完整算法设计见:神经蛊阵论文 v3

许可证

MIT License — 自由使用、修改、商用。

Metadata

Release files for dna-memory 0.1.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 dna-memory 0.1.0
File Size Uploaded Provenance Actions
dna_memory-0.1.0.tar.gz 8.2 kB Without provenance Details for dna_memory-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dna-memory 0.1.0
File Interpreter ABI Platform Actions
dna_memory-0.1.0-py3-none-any.whl Python 3 none any Details for dna_memory-0.1.0-py3-none-any.whl

Total release size: 17.5 kB

Release files / dna_memory-0.1.0.tar.gz

Download URL dna_memory-0.1.0.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
651fad8ba24309e2a36cb94d156640415376a5a69529629054205ac8c5455abb
BLAKE2b-256 checksum
How to use checksums
6bff02855caed047d6e3df630782f81530ba0a4e71e70bbf4b86c81337ee0d27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / dna_memory-0.1.0-py3-none-any.whl

Download URL dna_memory-0.1.0-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
43feda40d1bec306bdbf911faefec143ea88d3ab31885898ba7ca38b3b8d6a75
BLAKE2b-256 checksum
How to use checksums
51fb0ab2c5f86412b3924580aed9d3babfc2a16132d6f93da44bfabbcf5e8edb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for dna-memory 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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