Memlayer 的显著性过滤与记忆模式设计
Memlayer 是面向上下文智能体的可插拔记忆层,支持多种大模型。它通过显著性门筛选对话中值得保存的信息,并提供 LOCAL、ONLINE、LIGHTWEIGHT 三种模式,同时按延迟需求划分搜索层级。
# 计数器 **用于智能和上下文代理的 plug-and-play 内存层** Memlayer在仅3代码行中为任何LLM添加了持久,智能的内存,使能代理在对话中回想起上下文,提取结构化知识,并在重要时提取表面相关信息. **<100ms快速搜索 • 噪音-智能记忆门 • 多端检索模式 • 100% 本地版 • 零配置** [](https://www.python.org/downloads/) [](LICENSE) [](https://pypi.org/project/memlayer/) [快速启动](#quick-start) • [文档](https://divagr18.github.io/memlayer/) • [实例](https://github.com/divagr18/memlayer/tree/main/examples) • [推特( 推特)](https://x.com/getmemlayer) Memlayer Overview ## 目录 - [特征](#features) - [快速启动](#quick-start) - [关键概念](#key-concepts) - [内存模式](#memory-modes) - [搜索级别](#search-tiers) - [供应商](#providers) - [高级特性](#advanced-features) - [实例](#examples) - [业绩](#performance) - [文档](#documentation) - [捐款](#contributing) ## 特征 - **通用 LLM 支持**: 与OpenAI,克劳德,双子座,奥拉马模型合作. - **插座与游戏**: 安装方式 `pip install memlayer` 几分钟后开始——需要最小的设置. - **智能内存过滤**:三种操作模式(LOCAL / ONLINE / LIGHTWEIGHT) 自动过滤重要信息 - **混合搜索**: 结合矢量相似性+知识图 转录以准确检索 - **三层搜索**: 快速( < 100ms), 平衡( < 500ms), 深( < 2s) 优化用于不同用途 - **知识图**: 从对话中自动提取实体、关系和事实 - **主动提醒**: 安排任务, 并在任务到期时获得自动提醒 - **内建可观测性**: 以详细的性能衡量标准追踪每个搜索操作 - **弹性存储**: ChromaDB (vector) + NetworkX (graph) 或 仅图模式 - **生产准备**: 快速冷却的无服务器友好型开始使用在线模式 ## 快速启动 ### 安装 ```bash pip install memlayer ``` ### 基本用途 ```python from memlayer import OpenAI # Initialize with memory capabilities client = OpenAI( model="gpt-4.1-mini", storage_path="./memories", user_id="user_123" ) # Store information automatically client.chat([ {"role": "user", "content": "My name is Alice and I work at TechCorp"} ]) # Retrieve information automatically (no manual prompting needed!) response = client.chat([ {"role": "user", "content": "Where do I work?"} ]) # Response: "You work at TechCorp." ``` 就是这样,自动计数器: 1. 使用基于 ML 的分类过滤显著信息 2. * 摘录结构化的事实、实体和关系 3. QQ 在混合矢量+图存储中存储记忆 4. QQ 为每个查询检索相关上下文 5. QQ 将记忆无缝地输入到 LLM 上下文 ## 关键概念 ### 突出过滤 并非所有的对话内容都值得存储。 Memlayer 使用 **显要门** 到智能过滤器 : - ✅ **保存**: 事实、偏好、用户信息、决定、关系 - ❌ **跳过**:问候、承认、填词、元交谈 ### 混合存储 记忆存储在两个互补系统中: - **矢量存储器( ChromaDB )**: 语义相似 寻找事实 - **知识图 (NetworkX).**:实体关系和结构化知识 ### 自动合并 每次交谈后,背景线程: 1. 使用 LLM 提取事实、实体和关系 2. 将事实存储在嵌入的矢量数据库中 3. 与实体和关系建立知识图 4. 输入所有快速检索文件 ## 内存模式 Memlayer 提供了控制两种模式的三种模式 **内存过滤( 显著)** 财务报告和财务报告 **存储**: ### 1. LOCAL 模式( 默认) ```python client = Ollama(operation_mode="local") ``` - **过滤**: 句式转换器 ML模型(高精度) - **储存**: ChromaDB (载体) + NetworkX (图) - **启动**:~10s(型号加载) - **最好还是**: 大量生产,离线应用 - **费用**: 免费(无 API 呼叫) ### 2. ONLINE 模式( 默认) ```python client = OpenAI(operation_mode="online") ``` - **过滤**: OpenAI 嵌入 API (高精度) - **储存**: ChromaDB (载体) + NetworkX (图) - **启动**:~2s(无型号加载!). - **最好还是**: 无服务器,云函数,快速冷启动 - **费用**:~ $0.0001 每次操作 ### 3. LIGHTWEIGHT 模式 ```python client = OpenAI(operation_mode="lightweight") ``` - **过滤**: 关键词(中等准确性) - **储存**: 仅限NetworkX(无向量存储!) - **启动**: < 1s(即时) - **最好还是**: 原型、测试、低资源环境 - **费用**: 免费( 完全没有嵌入) **性能比较 :** ``` Mode Startup Time Accuracy API Cost Storage ────────────────────────────────────────────────────────────── LOCAL ~10s High Free Vector+Graph ONLINE ~2s High $0.0001/op Vector+Graph LIGHTWEIGHT <1s Medium Free Graph-only ``` ## 搜索级别 Memlayer 提供了三个搜索级,优化了对不同延迟要求的搜索: ### 快分级( < 100ms) ```python # Automatic - LLM chooses based on query complexity response = client.chat([{"role": "user", "content": "What's my name?"}]) ``` - 2 向量搜索结果 - 无图向反转 - 完全适合:实时聊天,简单真实的回忆 ### 平衡级( < 500ms) DEFAULT ```python # Automatic - handles most queries well response = client.chat([{"role": "user", "content": "Tell me about my projects"}]) ``` - 5 向量搜索结果 - 无图向反转 - 完全适合: 一般性对话,大多数使用案例 ### 深层( < 2s) ```python # Explicit request or auto-detected for complex queries response = client.chat([{ "role": "user", "content": "Use deep search: Tell me everything about Alice and her relationships" }]) ``` - 10 向量搜索结果 - 图形可转动( 实体提取 + 1 - hop 关系) - 非常适合: 研究,"告诉我一切",多跳推理 ## * 供应商 Memlayer与所有主要的LLM提供商合作: ### OpenAI ```python from memlayer import OpenAI client = OpenAI( model="gpt-4.1-mini", # or gpt-4.1, gpt-5, etc. storage_path="./memories", user_id="user_123" ) ``` ### 克劳德(雅典) ```python from memlayer import Claude client = Claude( model="claude-4-sonnet", storage_path="./memories", user_id="user_123" ) ``` ### 谷歌双子座 ```python from memlayer import Gemini client = Gemini( model="gemini-2.5-flash", storage_path="./memories", user_id="user_123" ) ``` ### Ollama(当地) ```python from memlayer import Ollama client = Ollama( host="http://localhost:11434", model="qwen3:14b", # or llama3.2, mistral, etc. storage_path="./memories", user_id="user_123", operation_mode="local" # Run 100% offline! ) ``` ### LMStudio(当地) ```python from memlayer import LMStudio client = LMStudio( host="http://localhost:11434/v1", model="qwen/qwen3-14b", storage_path="./memories", user_id="user_123", ) ``` **所有供应商共享相同的API** - 在他们之间无缝的切换! ## 高级特性 ### 主动任务提醒 ```python # User schedules a task client.chat([{ "role": "user", "content": "Remind me to submit the report next Friday at 9am" }]) # Later, when the task is due, Memlayer automatically injects it response = client.chat([{"role": "user", "content": "What should I do today?"}]) # Response includes: "Don't forget to submit the report - it's due today at 9am!" ``` ### 观察和追踪 ```python response = client.chat(messages) # Inspect search performance if client.last_trace: print(f"Search tier: {client.last_trace.events[0].metadata.get('tier')}") print(f"Total time: {client.last_trace.total_duration_ms}ms") for event in client.last_trace.events: print(f" {event.event_type}: {event.duration_ms}ms") ``` ### 自定义亮度阈值 ```python # Control memory filtering strictness client = OpenAI( salience_threshold=-0.1 # Permissive (saves more) # salience_threshold=0.0 # Balanced (default) # salience_threshold=0.1 # Strict (saves less) ) ``` ### 知识图提取 ```python # Manually extract structured knowledge kg = client.analyze_and_extract_knowledge( "Alice leads Project Phoenix in the London office. The project uses Python and React." ) print(kg["facts"]) # ["Alice leads Project Phoenix", ...] print(kg["entities"]) # [{"name": "Alice", "type": "Person"}, ...] print(kg["relationships"]) # [{"subject": "Alice", "predicate": "leads", "object": "Project Phoenix"}] ``` ## 实例 探索 `examples/` 综合实例目录: ### 基本情况 ```bash # Getting started python examples/01_basics/getting_started.py ``` ### 搜索级别 ```bash # Try all three search tiers python examples/02_search_tiers/fast_tier_example.py python examples/02_search_tiers/balanced_tier_example.py python examples/02_search_tiers/deep_tier_example.py # Compare them side-by-side python examples/02_search_tiers/tier_comparison.py ``` ### 高级特性 ```bash # Proactive task reminders python examples/03_features/task_reminders.py # Knowledge graph visualization python examples/03_features/test_knowledge_graph.py ``` ### 基准 ```bash # Compare salience modes python examples/04_benchmarks/compare_operation_modes.py ``` ### 供应商 ```bash # Try different LLM providers python examples/05_providers/openai_example.py python examples/05_providers/claude_example.py python examples/05_providers/gemini_example.py python examples/05_providers/ollama_example.py ``` 见 [examples/README.md](<https://github.com/divagr18/memlayer/blob/main/examples/README.md>) 用于完整文档。 ## 业绩 ### 显著模式比较 从基准开始的现实世界启动时间: ``` Mode First Use Memory Savings Trade-off ───────────────────────────────────────────────────────── LIGHTWEIGHT ~5s No embeddings No semantic search ONLINE ~5s 5s faster Small API cost LOCAL ~10s No API cost 11s model loading ``` ### 搜索级别间隔 典型查询延迟 : ``` Tier Latency Vector Results Graph Use Case ──────────────────────────────────────────────────────────── Fast 50-150ms 2 No Real-time chat Balanced 200-600ms 5 No General use Deep 800-2500ms 10 Yes Research queries ``` ### 内存整合 背景处理( 非屏蔽) : ``` Step Time Async ────────────────────────────────────────────── Salience filtering ~10ms Yes Knowledge extraction ~1-2s Yes (background thread) Vector storage ~50ms Yes Graph storage ~20ms Yes Total (non-blocking) ~0ms User doesn't wait! ``` ## 文档 ### 开始 - **[基本情况概述](<https://github.com/divagr18/memlayer/blob/main/docs/basics/overview.md>)** - 建筑、构件和元件设计方法 - **[快速启动指南](<https://github.com/divagr18/memlayer/blob/main/docs/basics/quickstart.md>)** - 起来运行在 5 分钟内 - **[流线模式](<https://github.com/divagr18/memlayer/blob/main/docs/basics/streaming.md>)** - 完整流程回复指南 - **[API 参考文献](<https://github.com/divagr18/memlayer/blob/main/docs/API_REFERENCE.md>)** - 完成包含所有方法和参数的 API 文件 ### 提供者设置 - **[供应商概览](<https://github.com/divagr18/memlayer/blob/main/docs/providers/README.md>)** - 比较所有提供者, 选择正确的一个 - **[Ollama 设置](<https://github.com/divagr18/memlayer/blob/main/docs/providers/ollama.md>)** - 用本地模式完全下线运行 - **[OpenAI](<https://github.com/divagr18/memlayer/blob/main/docs/providers/openai.md>)** - OpenAI 配置 - **[克劳德](<https://github.com/divagr18/memlayer/blob/main/docs/providers/claude.md>)** - Anthropic 克劳德设置 - **[双子座](<https://github.com/divagr18/memlayer/blob/main/docs/providers/gemini.md>)** - 谷歌双子座配置 ### 实例 - **[实例索引](<https://github.com/divagr18/memlayer/blob/main/examples/README.md>)** - 按类别分列的综合实例 - **[提供方实例](<https://github.com/divagr18/memlayer/blob/main/examples/05_providers/README.md>)** - 供应商的比较和使用 ## 可金枪鱼特征(快速指数) 该项目暴露了您可以调制的几个运行时/配置的节点,以匹配空闲度、成本和准确度的权衡。 `docs/` 文件夹 : - **[docs/tuning/operation_mode.md](<https://github.com/divagr18/memlayer/blob/main/docs/tuning/operation_mode.md>)** — **建筑深潜**: 如何选择 `online`, `local`,以及 `lightweight` 模式、性能影响、储存组成和部署战略。 - **[docs/tuning/intervals.md](<https://github.com/divagr18/memlayer/blob/main/docs/tuning/intervals.md>)** - 排程和编程间隔配置(`scheduler_interval_seconds`, `curation_interval_seconds`)和实用指导。 - **[docs/tuning/salience_threshold.md](<https://github.com/divagr18/memlayer/blob/main/docs/tuning/salience_threshold.md>)** ——如何调整. `salience_threshold` 和预期的行为。 - **[docs/services/consolidation.md](<https://github.com/divagr18/memlayer/blob/main/docs/services/consolidation.md>)** - 整合管道内部以及如何在方案上称之为管道(包括: `update_from_text`). - **[docs/services/curation.md](<https://github.com/divagr18/memlayer/blob/main/docs/services/curation.md>)** ——内存编译工作如何,存档规则如何,如何运行/停止编译服务. - **[docs/storage/chroma.md](<https://github.com/divagr18/memlayer/blob/main/docs/storage/chroma.md>)** ——ChromaDB注释:元数据类型,连接处理,以及Windows文件锁指导. - **[docs/storage/networkx.md](<https://github.com/divagr18/memlayer/blob/main/docs/storage/networkx.md>)** ——知识图的持久性,预期节点计划,以及备份/存储提示. 调制时使用文件 。 `docs/` 文件已添加到此存储库,并提供详细、实用的指导。 ## 发展 ### 设置 ```bash # Clone repository git clone https://github.com/divagr18/memlayer.git cd memlayer # Install dependencies pip install -e . # Run tests python -m pytest tests/ # Run examples python examples/01_basics/getting_started.py ``` ### 项目结构 ``` memlayer/ ├── memlayer/ # Core library │ ├── wrappers/ # LLM provider wrappers │ ├── storage/ # Storage backends (ChromaDB, NetworkX) │ ├── services.py # Search & consolidation services │ ├── ml_gate.py # Salience filtering │ └── embedding_models.py # Embedding model implementations ├── examples/ # Organized examples by category │ ├── 01_basics/ │ ├── 02_search_tiers/ │ ├── 03_features/ │ ├── 04_benchmarks/ │ └── 05_providers/ ├── tests/ # Tests and benchmarks ├── docs/ # Documentation └── README.md # This file ``` ## 捐款 捐献是欢迎的! 1. **报告错误** - 打开复制步骤的问题 2. **建议的特性** - 分享您的使用大小写和要求 3. **提交 PR** - 修复错误, 添加特性, 改进文件 4. **共享实例** - 告诉我们你建造了什么! 请保持PR的焦点,并包括新功能的测试. ## 联系人支持( S) - **Author/Maintainer**: 迪维扬什·阿格拉瓦尔 - **电子邮件**: kessav.r.1925@gmail.com (中文(简体) ). - **GitHub**: [divagr18 (英语).](https://github.com/divagr18) - **问题**: 报告错误或请求特性通过 [GitHub 问题](https://github.com/divagr18/memlayer/issues) 关于安全漏洞,请直接与电子邮件联系。 `SECURITY` 在主题栏中,而不是打开一个公开的问题。 ## 许可证 MIT 许可证 - 见 [LICENSE](<https://github.com/divagr18/memlayer/blob/main/LICENSE>) 详细情况。 ## 承认 - 以下列方式建造 [ChromaDB](https://www.trychroma.com/) 用于矢量存储 - 用途 [NetworkX](https://networkx.org/) 用于知识图操作 - 动力来自 [sentence-transformers](https://www.sbert.net/) 用于本地嵌入 - 支助 [OpenAI](https://openai.com/), [人类](https://www.anthropic.com/), [谷歌双子座](https://ai.google.dev/),以及 [奥拉玛](https://ollama.ai/) --- **用QQ为AI社区制作** 记得你的LLMS,今天试试Memlayer!
查看原始来源