dsh-rag-memory
给 QQ 机器人用的本地 RAG 长期记忆插件:把群聊/私聊消息、事实、摘要存进 SQLite,检索时用「中文分词 BM25 + Embedding 向量召回」混合排序,再可选 Rerank 精排,最后通过 HTTP 提供给机器人调用。
English: Local RAG long-term memory for DeepSeek Harness (DSH) QQ bots. Stores chat messages, facts and notes in SQLite, retrieves with Chinese-friendly BM25 plus optional embedding vectors and rerank, and serves everything over a local HTTP API. No native build step; works on desktop and Android.
- 数据全在本地,不上传聊天内容
- 电脑 / 手机(Android + Node)通用:只用 Node 内置
node:sqlite,无原生编译依赖 - 没配 Embedding / Rerank 时自动降级为纯 BM25,插件仍然可用
安装
从 npm(发布后):
dsh plugin --profile web add dsh-rag-memory
本地目录:
dsh plugin --profile web add ./dsh-rag-memory
装完接口在 http://127.0.0.1:39001/rag-memory(独立端口,不经过 DSH Web 的登录认证)。装上即生效,不用重启引擎。
配置
在 cordis.patch.yml 的 config 里写:
config:
dbPath: '' # 空 = $DSH_HOME/dsh-rag-memory/memory.db
token: '' # 非空时,请求需带 x-memory-token 头
defaultK: 8
embedding:
enabled: true
baseURL: 'https://api.siliconflow.cn/v1'
apiKey: 'sk-...'
model: 'BAAI/bge-m3'
rerank:
enabled: true
baseURL: 'https://api.siliconflow.cn/v1'
apiKey: 'sk-...'
model: 'BAAI/bge-reranker-v2-m3'
embedding / rerank 都兼容 OpenAI 风格的 /v1/embeddings 与 /v1/rerank(硅基流动、Jina、Cohere 等)。
API Key 会明文存在配置文件里,请勿把配置提交到公开仓库。
HTTP 接口
前缀:/rag-memory,插件自起独立 HTTP 服务(默认 http://127.0.0.1:39001,端口被占会自动 +1 到 39020)。
| 方法 | 路径 | 作用 |
|---|---|---|
| GET | /health |
健康检查与能力状态 |
| GET | /stats |
各 scope 条数、向量条数 |
| POST | /ingest |
批量写入消息 |
| POST | /search |
混合检索 |
| POST | /context |
检索并拼好可注入的文本块 |
| POST | /save |
直接存一条事实/画像/笔记 |
| GET | /profile?scope= |
列出事实类记忆 |
| POST | /delete |
按 id 删除 |
| POST | /clear |
按 scope 清空(需 confirm: true) |
写入
POST /ingest
{
"scope": "group:123456",
"items": [
{ "text": "小明说他下周三去杭州出差", "speaker": "小明", "ts": 1700000000000 },
{ "text": "群里在讨论周末去爬山", "kind": "message" }
]
}
scope 必填,建议用 group:<群号> / private:<QQ号>;kind 可为 message(默认)、fact、profile、note、event。
检索
POST /search
{ "scope": "group:123456", "query": "小明什么时候去杭州", "k": 8 }
返回按相关度排序的条目,sources 标明命中的通道(bm25 / vector / rerank),degraded 列出降级原因。
直接拿注入文本
POST /context
{ "scope": "group:123456", "query": "小明出差", "k": 6, "maxChars": 2000 }
返回的 context 形如:
【相关记忆 · 共 3 条】
1. 2024-01-02 10:30 小明:下周三去杭州出差
2. ...
直接拼进机器人这轮的 system prompt 即可。
机器人接入示例(Node)
const MEMORY = 'http://127.0.0.1:39001/rag-memory'
const scope = `group:${groupId}`
// 收到消息:先入库(不阻塞回复)
fetch(`${MEMORY}/ingest`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ scope, items: [{ text, speaker: nickname, ts: Date.now() }] }),
}).catch(() => {})
// 调模型前:取相关记忆
const res = await fetch(`${MEMORY}/context`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ scope, query: userText, k: 8 }),
})
const { context } = await res.json()
const system = context ? `${basePrompt}\n\n${context}` : basePrompt
不配 token 时直接调用即可;配了就在请求头加 x-memory-token。
发布到插件市场
DSH 的「插件 → 发现」是索引源机制,上架实质是「发布 npm 包 + 提交到市场索引」。
发布 npm(先确认
package.json的author/repository换成你的):npm pack --dry-run # 检查包内容 npm publish建 GitHub 仓库,推源码(README、LICENSE 已就绪)
到 dsh.so 提交插件 填 npm 包名或仓库地址,自动校验 + 安全扫描后生成市场条目
用户侧:DSH →「插件」→「发现」→「添加索引源」→ 填对应市场地址(如
https://cdn.dsh.so/market/index.json)
其他可选市场:AAAMECHANIC/dsh-plugin-market、DeepSeekBox。
数据与备份
- 默认数据库:
$DSH_HOME/dsh-rag-memory/memory.db(WAL 模式) - 备份:直接复制整个目录,或
sqlite3 memory.db "VACUUM INTO 'backup.db'" - 删除 scope 前建议先
GET /stats确认条数
No comments yet. Be the first to write one.