🧠 Memory System
A flexible memory system for AI agents with optional embedding support and multiple storage backends.
Features
- - Private & Shared Memories - Private by default, shared memories for multi-agent collaboration
- Embedding Search - Semantic search using sentence-transformers
- Multiple Backends - Local file / SQLite / GitHub / Gitee
- LLM Summarization - Auto-extract key info from conversations
- Memory Maintenance - Review, consolidate, tag suggestions
- Templates - Quick memory creation with templates
Installation
CODEBLOCK0
Quick Start
Python API
CODEBLOCK1
CLI
CODEBLOCK2
Private vs Shared Memory
| Type | Storage | Access | Use Case |
|---|
| Private | INLINECODE0 | Current agent only | User preferences, personal notes |
| Shared |
./shared_memory/ | All agents | Team decisions, collaboration |
Default: All memories are private. Use shared add only when other agents need to know.
CODEBLOCK3
Storage Backends
Local (Default)
CODEBLOCK4
SQLite (High Performance)
CODEBLOCK5
GitHub
export GITHUB_TOKEN="your_token"
CODEBLOCK7
Gitee
export GITEE_TOKEN="your_token"
CODEBLOCK9
Embedding & Semantic Search
Embedding is optional and auto-downloads on first use.
CODEBLOCK10
CLI with embedding:
CODEBLOCK11
Shared Memory (Multi-Agent)
CODEBLOCK12
CLI:
CODEBLOCK13
Conversation Summarization
Auto-extract key information from conversation history.
CODEBLOCK14
CLI:
CODEBLOCK15
Memory Maintenance
CODEBLOCK16
Templates
Predefined formats for quick memory creation.
CODEBLOCK17
Memory Groups
Organize memories into groups.
CODEBLOCK18
Batch Operations
CODEBLOCK19
API Reference
MemoryManager
| Method | Description |
|---|
| INLINECODE3 | Add memory |
| INLINECODE4 |
Get by ID |
|
delete(id) | Delete |
|
list(tags, limit, offset) | List with pagination |
|
search(query, tags, top_k, threshold) | Search |
|
batch_delete(ids) | Batch delete |
|
list_groups() | List groups |
|
export_json(filepath) | Export JSON |
SharedMemoryManager
| Method | Description |
|---|
| INLINECODE11 | Add shared memory |
| INLINECODE12 |
List shared |
|
get_by_agent(agent_id) | By agent |
|
search(query) | Search shared |
Files Structure
CODEBLOCK20
Configuration
INLINECODE15 :
CODEBLOCK21
License
MIT
🧠 记忆系统
一个灵活的AI智能体记忆系统,支持可选的嵌入功能和多种存储后端。
特性
- - 私有与共享记忆 - 默认为私有记忆,共享记忆用于多智能体协作
- 嵌入搜索 - 使用sentence-transformers进行语义搜索
- 多种后端 - 本地文件 / SQLite / GitHub / Gitee
- LLM摘要 - 从对话中自动提取关键信息
- 记忆维护 - 回顾、整合、标签建议
- 模板 - 使用模板快速创建记忆
安装
bash
pip install sentence-transformers scikit-learn pyyaml numpy
快速开始
Python API
python
from memory_system import MemoryManager
初始化(本地存储)
mm = MemoryManager(backend=local)
添加
mm.add(用户偏好深色主题, tags=[偏好])
搜索
results = mm.search(深色主题偏好)
列表
entries = mm.list(tags=[偏好])
CLI
bash
添加
python3 memory_cli.py add 用户反馈:页面加载缓慢 --tags bug,性能
列表
python3 memory_cli.py list
搜索
python3 memory_cli.py -e search 性能问题
语义搜索(带嵌入)
python3 memory_cli.py -e search 深色模式
私有记忆 vs 共享记忆
| 类型 | 存储位置 | 访问权限 | 使用场景 |
|---|
| 私有 | ./memorydata/ | 仅当前智能体 | 用户偏好、个人笔记 |
| 共享 |
./sharedmemory/ | 所有智能体 | 团队决策、协作 |
默认:所有记忆均为私有。仅当其他智能体需要知晓时使用 shared add。
python
私有记忆 - 用户说记住...
mm.add(用户姓名是张三)
共享记忆 - 用户说告诉其他智能体...
smm.add(团队决策:使用React, agent
id=agenta)
存储后端
本地(默认)
python
mm = MemoryManager(backend=local)
SQLite(高性能)
python
mm = MemoryManager(backend=sqlite, base_path=./memory.db)
GitHub
bash
export GITHUB
TOKEN=yourtoken
python
mm = MemoryManager(
backend=github,
repo=owner/repo,
branch=main
)
Gitee
bash
export GITEE
TOKEN=yourtoken
python
mm = MemoryManager(
backend=gitee,
repo=owner/repo,
branch=master
)
嵌入与语义搜索
嵌入功能为可选,首次使用时自动下载。
python
启用嵌入
mm = MemoryManager(backend=local, use_embedding=True)
添加(自动生成向量)
mm.add(用户工作时间为上午9点到下午6点)
语义搜索 - 查找相似内容
results = mm.search(用户什么时间工作)
带嵌入的CLI:
bash
python3 memory_cli.py -e search 工作时间
共享记忆(多智能体)
python
from memory_system import SharedMemoryManager
初始化
smm = SharedMemoryManager(backend=local, shared
path=./sharedmemory)
添加共享记忆(来自某个智能体)
smm.add(Bug #123已修复, agent
id=agentb)
列出共享记忆
shared = smm.list()
按智能体查询
by
agent = smm.getby
agent(agentb)
CLI:
bash
添加共享记忆
python3 memory
cli.py shared add 团队决策:使用Vue --agent agenta
列表
python3 memory_cli.py shared list
搜索
python3 memory_cli.py -e shared search Vue决策
对话摘要
从对话历史中自动提取关键信息。
python
from memory_system import MemoryManager, MemorySummarizer, ConversationMemoryProcessor
mm = MemoryManager(use_embedding=True)
summarizer = MemorySummarizer() # 自动检测OpenClaw模型
processor = ConversationMemoryProcessor(mm, summarizer, auto_save=True)
conversation =
用户:我偏好深色主题
助手:已切换为深色主题
用户:页面加载缓慢
助手:已优化图片
memories = processor.process(conversation)
CLI:
bash
python3 memory_cli.py summarize --file conversation.txt --save
记忆维护
bash
生成报告
python3 memory_cli.py maintenance report
回顾旧记忆
python3 memory_cli.py maintenance review --days 7
查找相似记忆
python3 memory_cli.py maintenance consolidate
为未标记的记忆建议标签
python3 memory_cli.py maintenance suggest-tags
标记为过时
python3 memory_cli.py maintenance outdated --mark
--reason 已过期
模板
用于快速创建记忆的预定义格式。
bash
列出模板
python3 memory_cli.py template list
显示模板
python3 memory_cli.py template show task
使用模板
python3 memory_cli.py template use task \
--field title=完成报告 \
--field priority=高
记忆分组
将记忆组织到分组中。
bash
添加到分组
python3 memory_cli.py add 工作任务 --tags 工作 --group 工作
列出分组
python3 memory_cli.py group list
显示分组
python3 memory_cli.py group show 工作
批量操作
bash
批量添加标签
python3 memory_cli.py batch-add-tags id1,id2 --tags 重要,优先
批量删除(需要确认)
python3 memory_cli.py batch-delete id1,id2 --force
API参考
MemoryManager
| 方法 | 描述 |
|---|
| add(content, tags, metadata, group) | 添加记忆 |
| get(id) |
按ID获取 |
| delete(id) | 删除 |
| list(tags, limit, offset) | 分页列表 |
| search(query, tags, top_k, threshold) | 搜索 |
| batch_delete(ids) | 批量删除 |
| list_groups() | 列出分组 |
| export_json(filepath) | 导出JSON |
SharedMemoryManager
| 方法 | 描述 |
|---|
| add(content, agent_id, tags) | 添加共享记忆 |
| list(tags) |
列出共享记忆 |
| getbyagent(agent_id) | 按智能体查询 |
| search(query) | 搜索共享记忆 |
文件结构
memory_system/
├── memory_manager.py # 核心管理器
├── shared_memory.py # 共享记忆
├── summarizer.py # LLM摘要
├── maintenance.py # 维护工具
├── templates.py # 模板
├── embedding.py # 嵌入处理器
├── storage/ # 存储后端
│ ├── local.py
│ ├── sqlite.py
│ ├── github.py
│ └── gitee.py
└── memory_cli.py # CLI入口(使用python3运行)
配置
config.yaml:
yaml
STORAGE_BACKEND: local
USE_EMBEDDING: false
EMBEDDING_MODEL: sentence-transformers/all-MiniLM-L6-v2
storage:
local:
basepath: ./memorydata
sqlite:
base_path: ./memory.db
github:
repo: owner/repo
tokenenv: GITHUBTOKEN
gitee:
repo: owner/repo
tokenenv: GITEETOKEN
许可证
MIT