|
| 1 | +# Agent Examples |
| 2 | + |
| 3 | +This section provides comprehensive working examples that demonstrate real-world usage patterns of the Redis Agent Memory Server. Each example showcases different aspects of memory management, from basic conversation storage to advanced memory editing workflows. |
| 4 | + |
| 5 | +## 🧳 Travel Agent |
| 6 | + |
| 7 | +**File**: [`examples/travel_agent.py`](https://github.com/redis/agent-memory-server/blob/main/examples/travel_agent.py) |
| 8 | + |
| 9 | +A comprehensive travel assistant that demonstrates the most complete integration patterns. |
| 10 | + |
| 11 | +### Key Features |
| 12 | + |
| 13 | +- **Automatic Tool Discovery**: Uses `MemoryAPIClient.get_all_memory_tool_schemas()` to automatically discover and integrate all available memory tools |
| 14 | +- **Unified Tool Resolution**: Leverages `client.resolve_tool_call()` to handle all memory tool calls uniformly across different LLM providers |
| 15 | +- **Working Memory Management**: Session-based conversation state and structured memory storage |
| 16 | +- **Long-term Memory**: Persistent memory storage and semantic search capabilities |
| 17 | +- **Optional Web Search**: Cached web search using Tavily API with Redis caching |
| 18 | + |
| 19 | +### Available Tools |
| 20 | + |
| 21 | +The travel agent automatically discovers and uses all memory tools: |
| 22 | + |
| 23 | +1. **search_memory** - Search through previous conversations and stored information |
| 24 | +2. **get_working_memory** - Check current session state, stored memories, and data |
| 25 | +3. **add_memory_to_working_memory** - Store important information as structured memories |
| 26 | +4. **update_working_memory_data** - Store/update session-specific data like trip plans |
| 27 | +5. **web_search** (optional) - Search the internet for current travel information |
| 28 | + |
| 29 | +### Usage Examples |
| 30 | + |
| 31 | +```bash |
| 32 | +# Basic interactive usage |
| 33 | +cd examples |
| 34 | +python travel_agent.py |
| 35 | + |
| 36 | +# Automated demo showing capabilities |
| 37 | +python travel_agent.py --demo |
| 38 | + |
| 39 | +# With custom configuration |
| 40 | +python travel_agent.py --session-id my_trip --user-id john_doe --memory-server-url http://localhost:8001 |
| 41 | +``` |
| 42 | + |
| 43 | +### Environment Setup |
| 44 | + |
| 45 | +```bash |
| 46 | +# Required |
| 47 | +export OPENAI_API_KEY="your-openai-key" |
| 48 | + |
| 49 | +# Optional (for web search) |
| 50 | +export TAVILY_API_KEY="your-tavily-key" |
| 51 | +export REDIS_URL="redis://localhost:6379" |
| 52 | +``` |
| 53 | + |
| 54 | +### Key Implementation Patterns |
| 55 | + |
| 56 | +```python |
| 57 | +# Tool auto-discovery |
| 58 | +memory_tools = MemoryAPIClient.get_all_memory_tool_schemas() |
| 59 | + |
| 60 | +# Unified tool resolution for any provider |
| 61 | +result = await client.resolve_tool_call( |
| 62 | + tool_call=provider_tool_call, |
| 63 | + session_id=session_id |
| 64 | +) |
| 65 | + |
| 66 | +if result["success"]: |
| 67 | + print(result["formatted_response"]) |
| 68 | +``` |
| 69 | + |
| 70 | +## 🧠 Memory Prompt Agent |
| 71 | + |
| 72 | +**File**: [`examples/memory_prompt_agent.py`](https://github.com/redis/agent-memory-server/blob/main/examples/memory_prompt_agent.py) |
| 73 | + |
| 74 | +Demonstrates the simplified memory prompt feature for context-aware conversations without manual tool management. |
| 75 | + |
| 76 | +### Core Concept |
| 77 | + |
| 78 | +Uses `client.memory_prompt()` to automatically retrieve relevant memories and enrich prompts with contextual information. |
| 79 | + |
| 80 | +### How It Works |
| 81 | + |
| 82 | +1. **Store Messages**: All conversation messages stored in working memory |
| 83 | +2. **Memory Prompt**: `memory_prompt()` retrieves relevant context automatically |
| 84 | +3. **Enriched Context**: Memory context combined with system prompt |
| 85 | +4. **LLM Generation**: Enhanced context sent to LLM for personalized responses |
| 86 | + |
| 87 | +### Usage Examples |
| 88 | + |
| 89 | +```bash |
| 90 | +cd examples |
| 91 | +python memory_prompt_agent.py |
| 92 | + |
| 93 | +# With custom session |
| 94 | +python memory_prompt_agent.py --session-id my_session --user-id jane_doe |
| 95 | +``` |
| 96 | + |
| 97 | +### Key Implementation Pattern |
| 98 | + |
| 99 | +```python |
| 100 | +# Automatic memory retrieval and context enrichment |
| 101 | +context = await client.memory_prompt( |
| 102 | + query=user_message, |
| 103 | + session_id=session_id, |
| 104 | + long_term_search={ |
| 105 | + "text": user_message, |
| 106 | + "limit": 5, |
| 107 | + "user_id": user_id |
| 108 | + } |
| 109 | +) |
| 110 | + |
| 111 | +# Enhanced prompt with memory context |
| 112 | +response = await openai_client.chat.completions.create( |
| 113 | + model="gpt-4o", |
| 114 | + messages=context.messages |
| 115 | +) |
| 116 | +``` |
| 117 | + |
| 118 | +## ✏️ Memory Editing Agent |
| 119 | + |
| 120 | +**File**: [`examples/memory_editing_agent.py`](https://github.com/redis/agent-memory-server/blob/main/examples/memory_editing_agent.py) |
| 121 | + |
| 122 | +Demonstrates comprehensive memory editing capabilities through natural conversation patterns. |
| 123 | + |
| 124 | +### Core Features |
| 125 | + |
| 126 | +- **Memory Editing Workflow**: Complete lifecycle of creating, searching, editing, and deleting memories |
| 127 | +- **All Memory Tools**: Uses all available memory management tools including editing capabilities |
| 128 | +- **Realistic Scenarios**: Common patterns like corrections, updates, and information cleanup |
| 129 | +- **Interactive Demo**: Both automated demo and interactive modes |
| 130 | + |
| 131 | +### Memory Operations Demonstrated |
| 132 | + |
| 133 | +1. **search_memory** - Find existing memories using natural language |
| 134 | +2. **get_long_term_memory** - Retrieve specific memories by ID |
| 135 | +3. **add_memory_to_working_memory** - Store new information |
| 136 | +4. **edit_long_term_memory** - Update existing memories |
| 137 | +5. **delete_long_term_memories** - Remove outdated information |
| 138 | +6. **get_working_memory** - Check current session context |
| 139 | + |
| 140 | +### Common Editing Scenarios |
| 141 | + |
| 142 | +```python |
| 143 | +# Correction scenario |
| 144 | +"Actually, I work at Microsoft, not Google" |
| 145 | +# → Search for job memory, edit company name |
| 146 | + |
| 147 | +# Update scenario |
| 148 | +"I got promoted to Senior Engineer" |
| 149 | +# → Find job memory, update title and add promotion date |
| 150 | + |
| 151 | +# Preference change |
| 152 | +"I prefer tea over coffee now" |
| 153 | +# → Search beverage preferences, update from coffee to tea |
| 154 | + |
| 155 | +# Information cleanup |
| 156 | +"Delete that old job information" |
| 157 | +# → Search and remove outdated employment data |
| 158 | +``` |
| 159 | + |
| 160 | +### Usage Examples |
| 161 | + |
| 162 | +```bash |
| 163 | +cd examples |
| 164 | + |
| 165 | +# Interactive mode (explore memory editing) |
| 166 | +python memory_editing_agent.py |
| 167 | + |
| 168 | +# Automated demo (see complete workflow) |
| 169 | +python memory_editing_agent.py --demo |
| 170 | + |
| 171 | +# Custom configuration |
| 172 | +python memory_editing_agent.py --session-id alice_session --user-id alice |
| 173 | +``` |
| 174 | + |
| 175 | +### Demo Conversation Flow |
| 176 | + |
| 177 | +The automated demo shows a realistic conversation: |
| 178 | + |
| 179 | +1. **Initial Information**: User shares profile (name, job, preferences) |
| 180 | +2. **Corrections**: User corrects information (job company change) |
| 181 | +3. **Updates**: User provides updates (promotion, new title) |
| 182 | +4. **Multiple Changes**: User updates location and preferences |
| 183 | +5. **Information Retrieval**: User asks what agent remembers |
| 184 | +6. **Ongoing Updates**: Continued information updates |
| 185 | +7. **Memory Management**: Specific memory operations (show/delete) |
| 186 | + |
| 187 | +## 🏫 AI Tutor |
| 188 | + |
| 189 | +**File**: [`examples/ai_tutor.py`](https://github.com/redis/agent-memory-server/blob/main/examples/ai_tutor.py) |
| 190 | + |
| 191 | +A functional tutoring system that demonstrates episodic memory for learning tracking and semantic memory for concept management. |
| 192 | + |
| 193 | +### Core Features |
| 194 | + |
| 195 | +- **Quiz Management**: Runs interactive quizzes and stores results |
| 196 | +- **Learning Tracking**: Stores quiz results as episodic memories with timestamps |
| 197 | +- **Concept Tracking**: Tracks weak concepts as semantic memories |
| 198 | +- **Progress Analysis**: Provides summaries and personalized practice suggestions |
| 199 | + |
| 200 | +### Memory Patterns Used |
| 201 | + |
| 202 | +```python |
| 203 | +# Episodic: Per-question results with event dates |
| 204 | +{ |
| 205 | + "text": "User answered 'photosynthesis' question incorrectly", |
| 206 | + "memory_type": "episodic", |
| 207 | + "event_date": "2024-01-15T10:30:00Z", |
| 208 | + "topics": ["quiz", "biology", "photosynthesis"] |
| 209 | +} |
| 210 | + |
| 211 | +# Semantic: Weak concepts for targeted practice |
| 212 | +{ |
| 213 | + "text": "User struggles with photosynthesis concepts", |
| 214 | + "memory_type": "semantic", |
| 215 | + "topics": ["weak_concept", "biology", "photosynthesis"] |
| 216 | +} |
| 217 | +``` |
| 218 | + |
| 219 | +### Usage Examples |
| 220 | + |
| 221 | +```bash |
| 222 | +cd examples |
| 223 | + |
| 224 | +# Interactive tutoring session |
| 225 | +python ai_tutor.py |
| 226 | + |
| 227 | +# Demo with sample quiz flow |
| 228 | +python ai_tutor.py --demo |
| 229 | + |
| 230 | +# Custom student session |
| 231 | +python ai_tutor.py --user-id student123 --session-id bio_course |
| 232 | +``` |
| 233 | + |
| 234 | +### Key Commands |
| 235 | + |
| 236 | +- **Practice**: Start a quiz on specific topics |
| 237 | +- **Summary**: Get learning progress summary |
| 238 | +- **Practice-next**: Get personalized practice recommendations based on weak areas |
| 239 | + |
| 240 | +## Getting Started with Examples |
| 241 | + |
| 242 | +### 1. Prerequisites |
| 243 | + |
| 244 | +```bash |
| 245 | +# Install dependencies |
| 246 | +cd /path/to/agent-memory-server |
| 247 | +uv install --all-extras |
| 248 | + |
| 249 | +# Start memory server |
| 250 | +uv run agent-memory server |
| 251 | + |
| 252 | +# Set required API keys |
| 253 | +export OPENAI_API_KEY="your-openai-key" |
| 254 | +``` |
| 255 | + |
| 256 | +### 2. Run Examples |
| 257 | + |
| 258 | +```bash |
| 259 | +cd examples |
| 260 | + |
| 261 | +# Start with the travel agent (most comprehensive) |
| 262 | +python travel_agent.py --demo |
| 263 | + |
| 264 | +# Try memory editing workflows |
| 265 | +python memory_editing_agent.py --demo |
| 266 | + |
| 267 | +# Explore simplified memory prompts |
| 268 | +python memory_prompt_agent.py |
| 269 | + |
| 270 | +# Experience learning tracking |
| 271 | +python ai_tutor.py --demo |
| 272 | +``` |
| 273 | + |
| 274 | +### 3. Customize and Extend |
| 275 | + |
| 276 | +Each example is designed to be: |
| 277 | + |
| 278 | +- **Self-contained**: Runs independently with minimal setup |
| 279 | +- **Configurable**: Supports custom sessions, users, and server URLs |
| 280 | +- **Educational**: Well-commented code showing best practices |
| 281 | +- **Production-ready**: Robust error handling and logging |
| 282 | + |
| 283 | +### 4. Implementation Patterns |
| 284 | + |
| 285 | +Key patterns demonstrated across examples: |
| 286 | + |
| 287 | +```python |
| 288 | +# Memory client setup |
| 289 | +client = MemoryAPIClient( |
| 290 | + base_url="http://localhost:8000", |
| 291 | + default_namespace=namespace, |
| 292 | + user_id=user_id |
| 293 | +) |
| 294 | + |
| 295 | +# Tool integration |
| 296 | +tools = MemoryAPIClient.get_all_memory_tool_schemas() |
| 297 | +response = await openai_client.chat.completions.create( |
| 298 | + model="gpt-4o", |
| 299 | + messages=messages, |
| 300 | + tools=tools |
| 301 | +) |
| 302 | + |
| 303 | +# Tool resolution |
| 304 | +for tool_call in response.choices[0].message.tool_calls: |
| 305 | + result = await client.resolve_tool_call( |
| 306 | + tool_call=tool_call, |
| 307 | + session_id=session_id |
| 308 | + ) |
| 309 | +``` |
| 310 | + |
| 311 | +## Next Steps |
| 312 | + |
| 313 | +- **Start with Travel Agent**: Most comprehensive example showing all features |
| 314 | +- **Explore Memory Editing**: Learn advanced memory management patterns |
| 315 | +- **Study Code Patterns**: Each example demonstrates different architectural approaches |
| 316 | +- **Build Your Own**: Use examples as templates for your specific use case |
| 317 | + |
| 318 | +All examples include detailed inline documentation and can serve as starting points for building production memory-enhanced AI applications. |
0 commit comments