Memory System Design
\nHuman memory is divided into short-term memory and long-term memory, and the memory system of AI Agents adopts a similar design.
\nUnderstanding the differences and uses of these two types of memory is the foundation for building intelligent Agents.
\nShort-Term Memory: Agent's Working Memory
\nShort-term memory is like the Agent's workbench, storing the context of the current conversation and temporary information.
\nFeatures:
\n- \n
- Limited capacity: Usually can only save the most recent few conversations (e.g., the last 10-20 turns) \n
- Fast access: Reading and writing speeds are very fast \n
- Temporary: Usually cleared or compressed after the conversation ends \n
- Context-dependent: Directly affects current response generation \n
Main Uses:
\n- \n
- Maintaining conversation coherence \n
- Storing intermediate results of current tasks \n
- Remembering user preferences mentioned in the current conversation \n
Technical Implementation:
\nclass ShortTermMemory:\n """Short-term memory implementation"""\n \n def __init__(self, max_turns=10):\n self.max_turns = max_turns # Maximum conversation turns\n self.conversation_history = [] # Conversation history\n self.temporary_data = {} # Temporary data storage\n \n def add_message(self, role, content):\n """Add message to conversation history"""\n message = {"role": role, "content": content, "timestamp": time.time()}\n self.conversation_history.append(message)\n \n # Keep history within maximum length\n if len(self.conversation_history) > self.max_turns:\n self.conversation_history.pop(0)\n \n def get_context(self):\n """Get conversation context (for sending to LLM)"""\n return self.conversation_history[-self.max_turns:]\n \n def store_temp(self, key, value):\n """Store temporary data"""\n self.temporary_data = value\n \n def get_temp(self, key, default=None):\n """Get temporary data"""\n return self.temporary_data.get(key, default)\n \n def clear_temp(self):\n """Clear temporary data"""\n self.temporary_data.clear()\n\nLong-Term Memory: Agent's Knowledge Base
\nLong-term memory is like the Agent's archive room, storing important information that needs to be retained long-term.
\nFeatures:
\n- \n
- Large capacity: Can store a massive amount of information \n
- Persistent: Information is saved long-term and will not be automatically cleared \n
- Retrieval-based access: Retrieves relevant information through queries, rather than sequential reading \n
- Structured storage: Information is usually stored in a structured way for easy retrieval \n
Main Uses:
\n- \n
- Storing users' personal information and preferences \n
- Accumulating knowledge and experience \n
- Remembering important conversation content \n
- Saving task execution results \n
Technical Implementation:
\nclass LongTermMemory:\n """Long-term memory base class"""\n \n def __init__(self):\n self.memories = [] # Memory entry list\n \n def add_memory(self, content, metadata=None):\n """Add memory"""\n memory = {\n "id": str(uuid.uuid4()),\n "content": content,\n "metadata": metadata or {},\n "timestamp": time.time(),\n "importance": 0.5 # Default importance\n }\n self.memories.append(memory)\n return memory\n \n def search_memories(self, query, limit=5):\n """Search memory (subclasses must implement specific search logic)"""\n raise NotImplementedError\n \n def get_memory(self, memory_id\n
YouTip