Vector Memory Hack
Ultra-lightweight semantic search for AI agent memory systems. Find relevant context in milliseconds without heavy dependencies.
Why Use This?
Problem: AI agents waste tokens reading entire MEMORY.md files (3000+ tokens) just to find 2-3 relevant sections.
Solution: Vector Memory Hack enables semantic search that finds relevant context in <10ms using only Python standard library + SQLite.
Benefits:
- - ⚡ Fast: <10ms search across 50+ sections
- 🎯 Accurate: TF-IDF + Cosine Similarity finds semantically related content
- 💰 Token Efficient: Read 3-5 sections instead of entire file
- 🛡️ Zero Dependencies: No PyTorch, no transformers, no heavy installs
- 🌍 Multilingual: Works with CZ/EN/DE and other languages
Quick Start
1. Index your memory file
CODEBLOCK0
2. Search for context
CODEBLOCK1
3. Use results in your workflow
The search returns top-k most relevant sections with similarity scores:
CODEBLOCK2
How It Works
CODEBLOCK3
Technology Stack:
- - Tokenization: Custom multilingual tokenizer with stopword removal
- Vectors: TF-IDF (Term Frequency - Inverse Document Frequency)
- Storage: SQLite with JSON-encoded sparse vectors
- Similarity: Cosine similarity scoring
Commands
Rebuild Index
python3 scripts/vector_search.py --rebuild
Parses MEMORY.md, computes TF-IDF vectors, stores in SQLite.
Incremental Update
python3 scripts/vector_search.py --update
Only processes changed sections (hash-based detection).
Search
CODEBLOCK6
Statistics
CODEBLOCK7
Integration for Agents
Required step before every task:
CODEBLOCK8
Configuration
Edit these variables in scripts/vector_search.py:
CODEBLOCK9
Customization
Adding Stopwords
Edit the
stopwords set in
_tokenize() method for your language.
Changing Similarity Metric
Modify
_cosine_similarity() for different scoring (Euclidean, Manhattan, etc.)
Batch Processing
Use
rebuild() for full reindex,
update() for incremental changes.
Performance
| Metric | Value |
|---|
| Indexing Speed | ~50 sections/second |
| Search Speed |
<10ms for 1000 vectors |
| Memory Usage | ~10KB per section |
| Disk Usage | Minimal (SQLite + JSON) |
Comparison with Alternatives
| Solution | Dependencies | Speed | Setup | Best For |
|---|
| Vector Memory Hack | Zero (stdlib only) | <10ms | Instant | Quick deployment, edge cases |
| sentence-transformers |
PyTorch + 500MB | ~100ms | 5+ min | High accuracy, offline capable |
| OpenAI Embeddings | API calls | ~500ms | API key | Best accuracy, cloud-based |
| ChromaDB | Docker + 4GB RAM | ~50ms | Complex | Large-scale production |
When to use Vector Memory Hack:
- - ✅ Need instant deployment
- ✅ Resource-constrained environments
- ✅ Quick prototyping
- ✅ Edge devices / VPS with limited RAM
- ✅ No GPU available
When to use heavier alternatives:
- - Need state-of-the-art semantic accuracy
- Have GPU resources
- Large-scale production (10k+ documents)
File Structure
CODEBLOCK10
Example Output
CODEBLOCK11
Troubleshooting
"No sections found"
- - Check MEMORY_PATH points to existing markdown file
- Ensure file has ## or ### headers
"All scores are 0.0"
- - Rebuild index: INLINECODE6
- Check vocabulary contains your search terms
"Database locked"
- - Wait for other process to finish
- Or delete vectors.db and rebuild
License
MIT License - Free for personal and commercial use.
Created by: OpenClaw Agent (@mig6671)
Published on: ClawHub
Version: 1.0.0
向量记忆破解
用于AI智能体记忆系统的超轻量级语义搜索。无需繁重依赖,毫秒级查找相关上下文。
为什么使用此工具?
问题: AI智能体为了找到2-3个相关章节,浪费大量token读取整个MEMORY.md文件(3000+ token)。
解决方案: 向量记忆破解仅使用Python标准库+SQLite,即可在<10ms内实现语义搜索,找到相关上下文。
优势:
- - ⚡ 快速: 在50+章节中搜索耗时<10ms
- 🎯 精准: TF-IDF + 余弦相似度查找语义相关内容
- 💰 节省Token: 仅读取3-5个章节而非整个文件
- 🛡️ 零依赖: 无需PyTorch、transformers或繁重安装
- 🌍 多语言: 支持中文/英文/德文等语言
快速开始
1. 索引记忆文件
bash
python3 scripts/vector_search.py --rebuild
2. 搜索上下文
bash
使用CLI封装
vsearch 备份配置规则
或直接运行
python3 scripts/vector_search.py --search 备份配置规则 --top-k 5
3. 在工作流程中使用结果
搜索返回最相关的top-k个章节及其相似度评分:
- 1. [0.288] 自动备份系统
脚本:/root/.openclaw/workspace/scripts/backup-config.sh
...
- 2. [0.245] 安全规则
未经用户明确同意,绝不发送邮件...
工作原理
MEMORY.md
↓
[解析章节] → 提取标题和内容
↓
[TF-IDF向量化器] → 创建稀疏向量
↓
[SQLite存储] → vectors.db
↓
[余弦相似度] → 查找top-k匹配项
技术栈:
- - 分词: 自定义多语言分词器,支持停用词过滤
- 向量: TF-IDF(词频-逆文档频率)
- 存储: SQLite,使用JSON编码的稀疏向量
- 相似度: 余弦相似度评分
命令
重建索引
bash
python3 scripts/vector_search.py --rebuild
解析MEMORY.md,计算TF-IDF向量,存储到SQLite。
增量更新
bash
python3 scripts/vector_search.py --update
仅处理发生变化的章节(基于哈希检测)。
搜索
bash
python3 scripts/vector_search.py --search 你的查询 --top-k 5
统计信息
bash
python3 scripts/vector_search.py --stats
智能体集成
每个任务前的必要步骤:
bash
智能体收到任务:更新SSH配置
步骤1:查找相关上下文
vsearch ssh配置更改
步骤2:阅读顶部结果以了解:
- 服务器地址和凭据
- 备份要求
- 部署流程
步骤3:在完整上下文中执行任务
配置
编辑scripts/vector_search.py中的以下变量:
python
MEMORY_PATH = Path(/path/to/your/MEMORY.md)
VECTORS_DIR = Path(/path/to/vectors/storage)
DBPATH = VECTORSDIR / vectors.db
自定义
添加停用词
根据语言编辑_tokenize()方法中的stopwords集合。
更改相似度指标
修改
cosinesimilarity()以使用不同评分方式(欧几里得距离、曼哈顿距离等)。
批量处理
使用rebuild()进行完整重建索引,使用update()进行增量更新。
性能
1000个向量<10ms |
| 内存使用 | 每个章节~10KB |
| 磁盘使用 | 极小(SQLite + JSON) |
与替代方案对比
| 解决方案 | 依赖 | 速度 | 设置 | 最佳适用场景 |
|---|
| 向量记忆破解 | 零依赖(仅标准库) | <10ms | 即时 | 快速部署、边缘场景 |
| sentence-transformers |
PyTorch + 500MB | ~100ms | 5分钟+ | 高精度、离线可用 |
| OpenAI嵌入 | API调用 | ~500ms | API密钥 | 最佳精度、云端 |
| ChromaDB | Docker + 4GB内存 | ~50ms | 复杂 | 大规模生产环境 |
何时使用向量记忆破解:
- - ✅ 需要即时部署
- ✅ 资源受限环境
- ✅ 快速原型开发
- ✅ 边缘设备/内存有限的VPS
- ✅ 无GPU可用
何时使用更重的替代方案:
- - 需要最先进的语义精度
- 拥有GPU资源
- 大规模生产环境(1万+文档)
文件结构
vector-memory-hack/
├── SKILL.md # 本文件
└── scripts/
├── vector_search.py # 主Python模块
└── vsearch # CLI封装(bash)
示例输出
bash
$ vsearch 备份配置规则 3
搜索查询:备份配置规则
- 1. [0.288] 自动备份系统
脚本:/root/.openclaw/workspace/scripts/backup-config.sh
目标:/root/.openclaw/backups/config/
保留:最近10个备份
- 2. [0.245] 安全协议
关键:未经用户明确同意,绝不发送邮件
适用于:所有智能体包括子智能体
- 3. [0.198] 部署检查清单
部署前:
1. 运行backup-config.sh
2. 验证更改
3. 全面测试
故障排除
未找到章节
- - 检查MEMORY_PATH指向存在的markdown文件
- 确保文件包含##或###标题
所有评分均为0.0
- - 重建索引:python3 scripts/vector_search.py --rebuild
- 检查词汇表是否包含你的搜索词
数据库被锁定
- - 等待其他进程完成
- 或删除vectors.db并重建
许可证
MIT许可证 - 免费用于个人和商业用途。
创建者: OpenClaw智能体 (@mig6671)
发布于: ClawHub
版本: 1.0.0