内存管理
存储用户偏好和反馈,让你的 LLM 在不同会话间记住这些信息。按用户和团队进行范围界定,并内置访问控制。
要求: LiteLLM v1.83.10+ 并连接 PostgreSQL。无需更改配置。
创建
- curl
- Python
curl -X POST "https://:4000/v1/memory" \
-H "Authorization: Bearer sk-1234" \
-H "Content-Type: application/json" \
-d '{
"key": "user:preferences",
"value": "Prefers concise responses. Timezone: PST.",
"metadata": {"version": 1}
}'
import httpx
client = httpx.Client(
base_url="https://:4000",
headers={"Authorization": "Bearer sk-1234"},
)
client.post("/v1/memory", json={
"key": "user:preferences",
"value": "Prefers concise responses. Timezone: PST.",
"metadata": {"version": 1},
})
读取
curl "https://:4000/v1/memory/user:preferences" \
-H "Authorization: Bearer sk-1234"
更新
curl -X PUT "https://:4000/v1/memory/user:preferences" \
-H "Authorization: Bearer sk-1234" \
-H "Content-Type: application/json" \
-d '{"value": "Prefers concise responses. Timezone: EST."}'
列表
# All entries
curl "https://:4000/v1/memory" \
-H "Authorization: Bearer sk-1234"
# By prefix
curl "https://:4000/v1/memory?key_prefix=user:" \
-H "Authorization: Bearer sk-1234"
删除
curl -X DELETE "https://:4000/v1/memory/user:preferences" \
-H "Authorization: Bearer sk-1234"
访问控制
基于 API 密钥自动进行范围界定。
| 角色 | 读取 | 写入 |
|---|---|---|
| 用户 | 自身 + 团队条目 | 仅限自身条目 |
| 团队管理员 | 自身 + 团队条目 | 自身 + 团队条目 |
| 代理(Proxy)管理员 | 全部 | 全部 |
键命名
键(Key)是全局唯一的。使用前缀进行命名空间划分和查询
user:preferences → per-user settings
team:playbook:onboarding → shared team resources
agent:memory:scratchpad → agent working memory
示例:Slack 机器人中的按用户内存
按 Slack 工作区和用户划分内存,从而隔离每个人的偏好。
键格式: slack:{team_id}:{user_id}
import httpx
LITELLM_BASE = "https://:4000"
LITELLM_KEY = "sk-1234"
def memory_key(team_id: str, user_id: str) -> str:
return f"slack:{team_id}:{user_id}"
async def get_preferences(team_id: str, user_id: str) -> str:
"""Read saved preferences. Returns "" if none exist."""
key = memory_key(team_id, user_id)
async with httpx.AsyncClient() as client:
r = await client.get(
f"{LITELLM_BASE}/v1/memory/{key}",
headers={"Authorization": f"Bearer {LITELLM_KEY}"},
)
if r.status_code == 404:
return ""
return r.json().get("value", "")
async def save_preference(team_id: str, user_id: str, note: str):
"""Append a preference. PUT upserts — creates or updates."""
key = memory_key(team_id, user_id)
existing = await get_preferences(team_id, user_id)
# Store as bullet list
bullets = [b for b in existing.split("\n") if b.strip()]
bullets.append(f"- {note}")
async with httpx.AsyncClient() as client:
await client.put(
f"{LITELLM_BASE}/v1/memory/{key}",
headers={"Authorization": f"Bearer {LITELLM_KEY}"},
json={"value": "\n".join(bullets)},
)
在每一轮对话中注入到你的系统提示词(System Prompt)中
prefs = await get_preferences(team_id, user_id)
messages = [
{"role": "system", "content": f"""You are a helpful assistant.
SAVED USER PREFERENCES:
{prefs}
Follow these unless the current message contradicts them."""},
{"role": "user", "content": user_message},
]
查询工作区的所有偏好设置
curl "https://:4000/v1/memory?key_prefix=slack:T024BE7LD:" \
-H "Authorization: Bearer sk-1234"
元数据
为条目附加任何 JSON 数据
{
"key": "agent:findings",
"value": "Q1 API usage up 15%...",
"metadata": {"tags": ["research"], "confidence": 0.92}
}
API 参考
完整的请求/响应架构、参数和错误代码:/memory 端点参考。