|
| 1 | +--- |
| 2 | +title: AgentOps |
| 3 | +--- |
| 4 | +<Snippet file="paper-release.mdx" /> |
| 5 | + |
| 6 | +Integrate [**Mem0**](https://github.com/mem0ai/mem0) with [AgentOps](https://agentops.ai), a comprehensive monitoring and analytics platform for AI agents. This integration enables automatic tracking and analysis of memory operations, providing insights into agent performance and memory usage patterns. |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +1. Automatic monitoring of Mem0 operations and performance metrics |
| 11 | +2. Real-time tracking of memory add, search, and retrieval operations |
| 12 | +3. Analytics dashboard with memory usage patterns and insights |
| 13 | +4. Error tracking and debugging capabilities for memory operations |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +Before setting up Mem0 with AgentOps, ensure you have: |
| 18 | + |
| 19 | +1. Installed the required packages: |
| 20 | +```bash |
| 21 | +pip install mem0ai agentops |
| 22 | +``` |
| 23 | + |
| 24 | +2. Valid API keys: |
| 25 | + - [AgentOps API Key](https://app.agentops.ai/dashboard/api-keys) |
| 26 | + - OpenAI API Key (for LLM operations) |
| 27 | + - [Mem0 API Key](https://app.mem0.ai/dashboard/api-keys) (optional, for cloud operations) |
| 28 | + |
| 29 | +## Basic Integration Example |
| 30 | + |
| 31 | +The following example demonstrates how to integrate Mem0 with AgentOps monitoring for comprehensive memory operation tracking: |
| 32 | + |
| 33 | +```python |
| 34 | +#Import the required libraries for local memory management with Mem0 |
| 35 | +from mem0 import Memory, AsyncMemory |
| 36 | +import os |
| 37 | +import asyncio |
| 38 | +import logging |
| 39 | +from dotenv import load_dotenv |
| 40 | +import agentops |
| 41 | + |
| 42 | +#Set up environment variables for API keys |
| 43 | +os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY") |
| 44 | +os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") |
| 45 | + |
| 46 | +#Set up the configuration for local memory storage and define sample user data. |
| 47 | +local_config = { |
| 48 | + "llm": { |
| 49 | + "provider": "openai", |
| 50 | + "config": { |
| 51 | + "model": "gpt-4o-mini", |
| 52 | + "temperature": 0.1, |
| 53 | + "max_tokens": 2000, |
| 54 | + }, |
| 55 | + } |
| 56 | +} |
| 57 | +user_id = "alice_demo" |
| 58 | +agent_id = "assistant_demo" |
| 59 | +run_id = "session_001" |
| 60 | + |
| 61 | +sample_messages = [ |
| 62 | + {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, |
| 63 | + {"role": "assistant", "content": "How about a thriller? They can be quite engaging."}, |
| 64 | + {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, |
| 65 | + { |
| 66 | + "role": "assistant", |
| 67 | + "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future.", |
| 68 | + }, |
| 69 | +] |
| 70 | + |
| 71 | +sample_preferences = [ |
| 72 | + "I prefer dark roast coffee over light roast", |
| 73 | + "I exercise every morning at 6 AM", |
| 74 | + "I'm vegetarian and avoid all meat products", |
| 75 | + "I love reading science fiction novels", |
| 76 | + "I work in software engineering", |
| 77 | +] |
| 78 | + |
| 79 | +#This function demonstrates sequential memory operations using the synchronous Memory class |
| 80 | +def demonstrate_sync_memory(local_config, sample_messages, sample_preferences, user_id): |
| 81 | + """ |
| 82 | + Demonstrate synchronous Memory class operations. |
| 83 | + """ |
| 84 | + |
| 85 | + agentops.start_trace("mem0_memory_example", tags=["mem0_memory_example"]) |
| 86 | + try: |
| 87 | + |
| 88 | + memory = Memory.from_config(local_config) |
| 89 | + |
| 90 | + result = memory.add( |
| 91 | + sample_messages, user_id=user_id, metadata={"category": "movie_preferences", "session": "demo"} |
| 92 | + ) |
| 93 | + |
| 94 | + for i, preference in enumerate(sample_preferences): |
| 95 | + result = memory.add(preference, user_id=user_id, metadata={"type": "preference", "index": i}) |
| 96 | + |
| 97 | + search_queries = [ |
| 98 | + "What movies does the user like?", |
| 99 | + "What are the user's food preferences?", |
| 100 | + "When does the user exercise?", |
| 101 | + ] |
| 102 | + |
| 103 | + for query in search_queries: |
| 104 | + results = memory.search(query, user_id=user_id) |
| 105 | + |
| 106 | + if results and "results" in results: |
| 107 | + for j, result in enumerate(results): |
| 108 | + print(f"Result {j+1}: {result.get('memory', 'N/A')}") |
| 109 | + else: |
| 110 | + print("No results found") |
| 111 | + |
| 112 | + all_memories = memory.get_all(user_id=user_id) |
| 113 | + if all_memories and "results" in all_memories: |
| 114 | + print(f"Total memories: {len(all_memories['results'])}") |
| 115 | + |
| 116 | + delete_all_result = memory.delete_all(user_id=user_id) |
| 117 | + print(f"Delete all result: {delete_all_result}") |
| 118 | + |
| 119 | + agentops.end_trace(end_state="success") |
| 120 | + except Exception as e: |
| 121 | + agentops.end_trace(end_state="error") |
| 122 | + |
| 123 | +# Execute sync demonstrations |
| 124 | +demonstrate_sync_memory(local_config, sample_messages, sample_preferences, user_id) |
| 125 | + |
| 126 | +``` |
| 127 | + |
| 128 | +For detailed information on this integration, refer to the official [Agentops Mem0 integration documentation](https://docs.agentops.ai/v2/integrations/mem0). |
| 129 | + |
| 130 | + |
| 131 | +## Key Features |
| 132 | + |
| 133 | +### 1. Automatic Operation Tracking |
| 134 | + |
| 135 | +AgentOps automatically monitors all Mem0 operations: |
| 136 | + |
| 137 | +- **Memory Operations**: Track add, search, get_all, delete operations and much more |
| 138 | +- **Performance Metrics**: Monitor response times and success rates |
| 139 | +- **Error Tracking**: Capture and analyze operation failures |
| 140 | + |
| 141 | +### 2. Real-time Analytics Dashboard |
| 142 | + |
| 143 | +Access comprehensive analytics through the AgentOps dashboard: |
| 144 | + |
| 145 | +- **Usage Patterns**: Visualize memory usage trends over time |
| 146 | +- **User Behavior**: Analyze how different users interact with memory |
| 147 | +- **Performance Insights**: Identify bottlenecks and optimization opportunities |
| 148 | + |
| 149 | +### 3. Session Management |
| 150 | + |
| 151 | +Organize your monitoring with structured sessions: |
| 152 | + |
| 153 | +- **Session Tracking**: Group related operations into logical sessions |
| 154 | +- **Success/Failure Rates**: Track session outcomes for reliability monitoring |
| 155 | +- **Custom Metadata**: Add context to sessions for better analysis |
| 156 | + |
| 157 | +## Best Practices |
| 158 | + |
| 159 | +1. **Initialize Early**: Always initialize AgentOps before importing Mem0 classes |
| 160 | +2. **Session Management**: Use meaningful session names and end sessions appropriately |
| 161 | +3. **Error Handling**: Wrap operations in try-catch blocks and report failures |
| 162 | +4. **Tagging**: Use tags to organize different types of memory operations |
| 163 | +5. **Environment Separation**: Use different projects or tags for dev/staging/prod |
| 164 | + |
| 165 | +## Help & Resources |
| 166 | + |
| 167 | +- [AgentOps Documentation](https://docs.agentops.ai/) |
| 168 | +- [AgentOps Dashboard](https://app.agentops.ai/) |
| 169 | +- [Mem0 Platform](https://app.mem0.ai/) |
| 170 | + |
| 171 | +<Snippet file="get-help.mdx" /> |
| 172 | + |
0 commit comments