memory-systems

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Memory System Design

内存系统设计

Memory provides persistence that allows agents to maintain continuity across sessions and reason over accumulated knowledge.
内存提供持久化能力,让Agent能够在跨会话场景下保持连续性,并基于累积的知识进行推理。

Memory Architecture Spectrum

内存架构范围

LayerLatencyPersistenceUse Case
Working MemoryZeroVolatileContext window
Short-TermLowSessionSession state
Long-TermMediumPersistentCross-session knowledge
Entity MemoryMediumPersistentEntity tracking
Temporal KGMediumPersistentTime-aware queries
层级延迟持久化能力适用场景
工作内存零延迟易失性上下文窗口
短期内存低延迟会话级会话状态
长期内存中等延迟持久化跨会话知识
实体内存中等延迟持久化实体跟踪
时序知识图谱(Temporal KG)中等延迟持久化时间感知查询

Memory System Performance

内存系统性能

SystemDMR AccuracyRetrieval Latency
Zep (Temporal KG)94.8%2.58s
MemGPT93.4%Variable
GraphRAG75-85%Variable
Vector RAG60-70%Fast
Recursive Summary35.3%Low
系统DMR准确率检索延迟
Zep(时序知识图谱)94.8%2.58秒
MemGPT93.4%可变
GraphRAG75-85%可变
向量RAG60-70%快速
递归摘要35.3%

Why Vector Stores Fall Short

向量存储的局限性

Vector stores lose relationship information:
  • Can retrieve "Customer X purchased Product Y"
  • Cannot answer "What did customers who bought Y also buy?"
  • Cannot distinguish current vs outdated facts
向量存储会丢失关系信息:
  • 可以检索到「客户X购买了产品Y」
  • 无法回答「购买Y的客户还买了什么?」
  • 无法区分当前事实与过时事实

Memory Implementation Patterns

内存实现模式

Pattern 1: File-System-as-Memory

模式1:文件系统作为内存

python
undefined
python
undefined

Simple, no infrastructure needed

Simple, no infrastructure needed

def store_fact(entity_id, fact): path = f"memory/{entity_id}.json" facts = load_json(path, default=[]) facts.append({"fact": fact, "timestamp": now()}) save_json(path, facts)
undefined
def store_fact(entity_id, fact): path = f"memory/{entity_id}.json" facts = load_json(path, default=[]) facts.append({"fact": fact, "timestamp": now()}) save_json(path, facts)
undefined

Pattern 2: Vector RAG with Metadata

模式2:带元数据的向量RAG

python
undefined
python
undefined

Embed facts with rich metadata

Embed facts with rich metadata

vector_store.add( embedding=embed(fact), metadata={ "entity_id": entity_id, "valid_from": now(), "source": "conversation", "confidence": 0.95 } )
undefined
vector_store.add( embedding=embed(fact), metadata={ "entity_id": entity_id, "valid_from": now(), "source": "conversation", "confidence": 0.95 } )
undefined

Pattern 3: Knowledge Graph

模式3:知识图谱

python
undefined
python
undefined

Preserve relationships

Preserve relationships

graph.create_relationship( from_entity="Customer_123", relationship="PURCHASED", to_entity="Product_456", properties={"date": "2024-01-15", "quantity": 2} )
undefined
graph.create_relationship( from_entity="Customer_123", relationship="PURCHASED", to_entity="Product_456", properties={"date": "2024-01-15", "quantity": 2} )
undefined

Pattern 4: Temporal Knowledge Graph

模式4:时序知识图谱

python
undefined
python
undefined

Time-travel queries

Time-travel queries

def query_address_at_time(user_id, query_time): return graph.query(""" MATCH (user)-[r:LIVES_AT]->(address) WHERE user.id = $user_id AND r.valid_from <= $query_time AND (r.valid_until IS NULL OR r.valid_until > $query_time) RETURN address """, {"user_id": user_id, "query_time": query_time})
undefined
def query_address_at_time(user_id, query_time): return graph.query(""" MATCH (user)-[r:LIVES_AT]->(address) WHERE user.id = $user_id AND r.valid_from <= $query_time AND (r.valid_until IS NULL OR r.valid_until > $query_time) RETURN address """, {"user_id": user_id, "query_time": query_time})
undefined

Entity Memory

实体内存

Track entities consistently across conversations:
  • Entity Identity: "John Doe" in one conversation = same person in another
  • Entity Properties: Facts discovered about entities over time
  • Entity Relationships: Relationships discovered between entities
python
def remember_entity(entity_id, properties):
    memory.store({
        "type": "entity",
        "id": entity_id,
        "properties": properties,
        "last_updated": now()
    })
跨会话一致跟踪实体:
  • 实体身份:一次对话中的「John Doe」等同于另一次对话中的同一个人
  • 实体属性:随时间推移发现的实体相关事实
  • 实体关系:发现的实体之间的关系
python
def remember_entity(entity_id, properties):
    memory.store({
        "type": "entity",
        "id": entity_id,
        "properties": properties,
        "last_updated": now()
    })

Memory Consolidation

内存整合

Trigger consolidation when:
  • Memory accumulates significantly
  • Retrieval returns too many outdated results
  • Periodically on schedule
  • Explicit request
Process:
  1. Identify outdated facts
  2. Merge related facts
  3. Update validity periods
  4. Archive/delete obsolete facts
  5. Rebuild indexes
在以下场景触发整合:
  • 内存累积量显著增加时
  • 检索返回过多过时结果时
  • 按计划定期执行
  • 收到明确请求时
流程:
  1. 识别过时事实
  2. 合并相关事实
  3. 更新有效期
  4. 归档/删除过时事实
  5. 重建索引

Choosing Memory Architecture

选择内存架构

RequirementArchitecture
Simple persistenceFile-system memory
Semantic searchVector RAG with metadata
Relationship reasoningKnowledge graph
Temporal validityTemporal knowledge graph
需求架构
简单持久化文件系统内存
语义搜索带元数据的向量RAG
关系推理知识图谱
时序有效性时序知识图谱

Best Practices

最佳实践

  1. Match architecture to query requirements
  2. Implement progressive disclosure for access
  3. Use temporal validity to prevent conflicts
  4. Consolidate periodically
  5. Design for retrieval failures gracefully
  6. Consider privacy implications
  7. Implement backup and recovery
  8. Monitor growth and performance
  1. 根据查询需求匹配架构
  2. 实现渐进式披露以控制访问
  3. 使用时序有效性避免冲突
  4. 定期执行整合操作
  5. 优雅设计检索失败的处理逻辑
  6. 考虑隐私影响
  7. 实现备份与恢复机制
  8. 监控内存增长与性能