Use this file to discover all available pages before exploring further.
Long-term memory in AG-Kit enables agents to remember information across conversations and sessions. It provides persistent storage with semantic search, automatic memory extraction, and intelligent consolidation capabilities.
Persistent facts - Information that should be remembered indefinitely
User preferences - Personal settings and choices
Historical context - Past interactions and decisions
Domain knowledge - Learned information and insights
Relationships - Connections between concepts and entities
Unlike short-term memory (conversation history), long-term memory focuses on extracting and storing meaningful information that transcends individual conversations.
from agkit.agents import Mem0LongTermMemory# Initialize Mem0 memorymemory = Mem0LongTermMemory( api_key=os.getenv('MEM0_API_KEY'), user_id='user-123', agent_id='assistant-v1', app_id='my-app')# Test connectionawait memory.ping()# Extract and record memories from conversationmessages = [ {'role': 'user', 'content': 'My name is Alice and I love pizza'}, {'role': 'assistant', 'content': 'Nice to meet you, Alice! I\'ll remember that you love pizza.'}]extracted_memories = await memory.extract_and_record(messages, { 'user_id': 'user-123'})print('Extracted memories:', extracted_memories)
Find memories using natural language queries with semantic understanding.
# Search for memories about user preferencesresults = await memory.semantic_search('What does the user like?', limit=5, threshold=0.7 # Minimum similarity score)# Search within specific strategyfood_preferences = await memory.semantic_search('favorite foods', strategy='preferences', limit=10)# Search with custom filtersrecent_facts = await memory.semantic_search('user information', strategy='facts', filters={'verified': True}, limit=20)print(results)
# Delete by IDawait memory.delete('mem-001')# Delete by query (all matching memories)await memory.delete({ 'strategy': 'temporary', 'filters': {'expired': True}})# Clear all memories with specific strategyawait memory.clear('temporary')# Clear all memories (use with caution!)await memory.clear()
Mem0 automatically extracts meaningful information from conversations.
# Extract memories from conversationconversation = [ {'role': 'user', 'content': 'I just moved to New York and started working at Google'}, {'role': 'assistant', 'content': 'Congratulations on your new job and move!'}, {'role': 'user', 'content': 'Thanks! I\'m excited but also nervous about the change'}]extracted_memories = await memory.extract_and_record(conversation, { 'user_id': 'user-123', 'metadata': { 'conversation_id': 'conv-456', 'extraction_date': datetime.now().isoformat() }})
# Efficient: Use limits and filtersmemories = await memory.retrieve( strategy='preferences', limit=10, filters={'category': 'food'})# Inefficient: Retrieve all and filter in codeall_memories = await memory.retrieve()filtered = [m for m in all_memories if m.strategy == 'preferences']