Persistent Context, Knowledge Storage, and State Persistence
Memory systems enable agents to maintain context across interactions, learn from experience, and access relevant information efficiently for better decision-making and continuity.
class HybridMemorySystem: def __init__(self): # Short-term: Conversation buffer self.conversation_memory = ConversationBufferWindowMemory(k=10) # Long-term: Vector store self.vector_memory = Chroma(embedding_function=OpenAIEmbeddings()) # Structured: Knowledge graph self.knowledge_graph = KnowledgeGraphMemory() # User preferences self.user_preferences = UserPreferenceStore() def store_interaction(self, user_input, agent_response, context=None): # Store in conversation buffer self.conversation_memory.save_context( {"input": user_input}, {"output": agent_response} ) # Extract and store important information if self.is_important(user_input, agent_response): self.vector_memory.add_texts([f"{user_input} -> {agent_response}"]) # Update knowledge graph entities = self.extract_entities(user_input, agent_response) self.knowledge_graph.update_from_entities(entities) def retrieve_relevant_context(self, query): # Get recent conversation recent_context = self.conversation_memory.load_memory_variables({}) # Search long-term memory relevant_memories = self.vector_memory.similarity_search(query, k=3) # Query knowledge graph related_facts = self.knowledge_graph.get_related_facts(query) return { "recent": recent_context, "memories": relevant_memories, "facts": related_facts }