Implement hierarchical three-tier memory:
Tiers:
- Working Memory: Size 5, immediate, critical items
- Short-Term Memory: Size 50, recent context
- Long-Term Memory: Vector store, 10K capacity
Operations:
add(item, priority): Add to appropriate tier- 'critical' → working
- 'normal' → stm
promote(item_id, to_tier): Move up hierarchyretrieve(query, max_tokens): Fill context window- Take all from working
- Fill remainder from stm
- If still space, query ltm
consolidate(): Move old items down tiersestimate_tokens(items): Count tokens
Eviction:
- Working full → oldest to stm
- Stm full → oldest to ltm
- Ltm full → vector eviction
Examples
Example 1:
Input:
hm = HierarchicalMemory(working_size=2); hm.add('critical info', 'critical'); len(hm.working)Output:
1Explanation: Critical item added to working memory
Starter Code
class HierarchicalMemory:
"""
Three-tier hierarchical memory: Working -> Short-term -> Long-term
"""
def __init__(self, working_size=5, stm_size=50, ltm_capacity=10000):
self.working_size = working_size
self.stm_size = stm_size
self.ltm_capacity = ltm_capacity
self.working = [] # Highest priority, immediate access
self.stm = [] # Recent context
self.ltm = None # Vector store for long-term
self.promotion_stats = {'stm_to_working': 0, 'ltm_to_stm': 0}
def add(self, item, priority='normal'):
"""
Add item to appropriate memory tier based on priority.
Priority: 'critical' -> working, 'normal' -> stm
"""
# Your implementation here
pass
def promote(self, item_id, to_tier):
"""Promote item to higher tier (stm->working, ltm->stm)"""
# Your implementation here
pass
def retrieve(self, query, max_tokens=1000):
"""
Retrieve context filling up to max_tokens.
Priority: working > stm > ltm
"""
# Your implementation here
pass
def consolidate(self):
"""
Move old working items to stm, old stm to ltm.
Simulates sleep/consolidation.
"""
# Your implementation here
pass
def estimate_tokens(self, items):
"""Estimate token count for items"""
# Your implementation here
passPython3
ReadyLines: 1Characters: 0
Ready