附录 B API 速查表
公开 API 名称与参数速查。代码为概念性片段,不含仓库内路径。
B.1 MemoryService 初始化
from memu import MemoryService
service = MemoryService(
llm_profiles={...}, # 必填:LLM Profile 字典
database_config={...}, # 存储后端
retrieve_config={...}, # 可选:检索策略
memorize_config={...}, # 可选:写入流水线
user_config={...}, # 可选:作用域模型
memory_files_config={...}, # 可选:Markdown 导出
)
B.2 memorize()
签名
result = await service.memorize(
resource_url: str, # 必填
modality: str, # 必填
user: dict | None = None, # 作用域
**scope_fields
)
modality 枚举
| 值 |
含义 |
conversation |
对话 JSON / 消息列表 |
document |
文本、Markdown、日志 |
image |
图片 |
video |
视频 |
audio |
音频 |
返回字段
| 字段 |
类型 |
说明 |
resource |
dict |
本次 Resource |
items |
list[dict] |
新 MemoryItem |
categories |
list[dict] |
相关 MemoryCategory |
relations |
list[dict] |
CategoryItem 边 |
B.3 retrieve()
签名
context = await service.retrieve(
queries: list[dict], # 必填,消息格式
where: dict | None = None, # 作用域过滤
)
queries 消息格式
{"role": "user" | "assistant" | "system", "content": {"text": "..."}}
retrieve_config
| 键 |
值 |
默认 |
method |
"rag" / "llm" |
"rag" |
route_intention |
bool |
True(RAG) |
sufficiency_check |
bool |
True |
返回字段
| 字段 |
说明 |
needs_retrieval |
是否需要查记忆 |
original_query |
原始 query |
rewritten_query |
改写 query |
next_step_query |
建议下轮 query |
categories |
命中的 Category |
items |
命中的 Item |
resources |
命中的 Resource |
B.4 export_memory_files()
await service.export_memory_files(
user: dict | None = None,
)
需 memory_files_config.enabled=True。
B.5 llm_profiles 字段
default Profile
| 字段 |
说明 |
api_key |
API 密钥 |
base_url |
兼容端点 URL |
chat_model |
对话模型名 |
embed_model |
嵌入模型名(若未单独配 embedding Profile) |
client_backend |
sdk / httpx |
provider |
openai / openrouter / doubao / grok |
embedding Profile
| 字段 |
说明 |
api_key |
密钥 |
base_url |
端点 |
embed_model |
嵌入模型 |
B.6 database_config
# inmemory
{"metadata_store": {"provider": "inmemory"}}
# sqlite
{"metadata_store": {"provider": "sqlite", "path": "./memu.db"}}
# postgres
{"metadata_store": {"provider": "postgres", "dsn": "postgresql+psycopg://..."}}
B.7 memory_files_config
| 键 |
说明 |
enabled |
是否启用导出 |
output_dir |
输出目录 |
synthesize |
是否 LLM 合成 MEMORY 正文 |
B.8 Cloud API v3
| 方法 |
路径 |
说明 |
| POST |
/api/v3/memory/memorize |
异步摄入 |
| GET |
/api/v3/memory/memorize/status/{task_id} |
任务状态 |
| POST |
/api/v3/memory/categories |
列出类别 |
| POST |
/api/v3/memory/retrieve |
检索 |
Base URL: https://api.memu.so
Header: Authorization: Bearer TOKEN
| Tool |
映射 |
save_memory |
memorize |
search_memory |
retrieve |
安装:pip install "memu-py[langgraph]"
B.10 memory_type 枚举
| 值 |
用途 |
profile |
用户画像、偏好 |
event |
事件、决策 |
knowledge |
客观事实 |
behavior |
行为模式 |
skill |
方法论 |
tool |
工具经验 |
B.11 环境变量(常用)
| 变量 |
用途 |
OPENAI_API_KEY |
OpenAI 默认密钥 |
OPENROUTER_API_KEY |
OpenRouter 密钥 |
POSTGRES_DSN |
Postgres 连接串 |
参见:第 4 章配置详解;第 5、6 章 API 实战;附录 C 报错排查。