
关于
LLM 对话的持久化记忆系统,包括上下文管理和长期记忆存储
name: conversation-memory description: LLM 对话的持久化记忆系统,包括短期记忆、长期记忆和实体记忆 risk: unknown source: vibeship-spawner-skills (Apache 2.0) date_added: 2026-02-27
对话记忆
LLM 对话的持久化记忆系统,包括短期记忆、长期记忆和实体记忆
能力
- 短期记忆
- 长期记忆
- 实体记忆
- 记忆持久化
- 记忆检索
- 记忆整合
前置条件
- 知识:LLM 对话模式、数据库基础、键值存储
- 推荐技能:context-window-management、rag-implementation
范围
- 不涵盖:知识图谱构建、语义搜索实现、数据库管理
- 边界:专注于 LLM 的记忆模式,涵盖存储和检索策略
生态系统
主要工具
- Mem0 - AI 应用的记忆层
- LangChain Memory - LangChain 中的记忆工具
- Redis - 用于会话记忆的内存数据存储
模式
分层记忆系统
针对不同用途的不同记忆层级
适用场景:构建任何对话式 AI
interface MemorySystem {
// 缓冲区:当前对话(在上下文中)
buffer: ConversationBuffer;
// 短期:最近的交互(会话)
shortTerm: ShortTermMemory;
// 长期:跨会话持久化
longTerm: LongTermMemory;
// 实体:关于人物、地点、事物的事实
entity: EntityMemory;
}
class TieredMemory implements MemorySystem {
async addMessage(message: Message): Promise<void> {
// 始终添加到缓冲区
this.buffer.add(message);
// 提取实体
const entities = await extractEntities(message);
for (const entity of entities) {
await this.entity.upsert(entity);
}
// 检查是否值得记忆
if (await isMemoryWorthy(message)) {
await this.shortTerm.add({
content: message.content,
timestamp: Date.now(),
importance: await scoreImportance(message)
});
}
}
async consolidate(): Promise<void> {
// 将重要的短期记忆移至长期
const memories = await this.shortTerm.getOld(24 * 60 * 60 * 1000);
for (const memory of memories) {
if (memory.importance > 0.7 || memory.referenced > 2) {
await this.longTerm.add(memory);
}
await this.shortTerm.remove(memory.id);
}
}
async buildContext(query: string): Promise<string> {
const parts: string[] = [];
// 相关的长期记忆
const longTermRelevant = await this.longTerm.search(query, 3);
if (longTermRelevant.length) {
parts.push('## 相关记忆\n' +
longTermRelevant.map(m => `- ${m.content}`).join('\n'));
}
// 相关实体
const entities = await this.entity.getRelevant(query);
if (entities.length) {
parts.push('## 已知实体\n' +
entities.map(e => `- ${e.name}: ${e.facts.join(', ')}`).join('\n'));
}
// 最近对话
const recent = this.buffer.getRecent(10);
parts.push('## 最近对话\n' + formatMessages(recent));
return parts.join('\n\n');
}
}
实体记忆
存储和更新关于实体的事实
适用场景:需要记住关于人物、地点、事物的详细信息
interface Entity {
id: string;
name: string;
type: 'person' | 'place' | 'thing' | 'concept';
facts: Fact[];
lastMentioned: number;
mentionCount: number;
}
interface Fact {
content: string;
confidence: number;
source: string; // 来自哪条消息
timestamp: number;
}
class EntityMemory {
async extractAndStore(message: Message): Promise<void> {
// 使用 LLM 提取实体和事实
const extraction = await llm.complete(`
从这条消息中提取实体和事实。
返回 JSON: { "entities": [
{ "name": "...", "type": "...", "facts": ["..."] }
]}
消息: "${message.content}"
`);
const { entities } = JSON.parse(extraction);
for (const entity of entities) {
await this.upsert(entity, message.id);
}
}
async upsert(entity: ExtractedEntity, sourceId: string): Promise<void> {
const existing = await this.store.get(entity.name.toLowerCase());
if (existing) {
// 合并事实,避免重复
for (const fact of entity.facts) {
if (!this.hasSimilarFact(existing.facts, fact)) {
existing.facts.push({
content: fact,
confidence: 0.9,
source: sourceId,
timestamp: Date.now()
});
}
}
}
}
}
兼容工具
Claude CodeCursor
标签
AI与机器学习