|
| 1 | +# LangGraph Subgraphs Demo: Travel Planning Assistant ✈️ |
| 2 | + |
| 3 | +This demo showcases **LangGraph subgraphs** through an interactive travel planning assistant. Watch as specialized AI agents collaborate to plan your perfect trip! |
| 4 | + |
| 5 | +## What are LangGraph Subgraphs? 🤖 |
| 6 | + |
| 7 | +**Subgraphs** are the key to building modular, scalable AI systems in LangGraph. A subgraph is essentially "a graph that is used as a node in another graph" - enabling powerful encapsulation and reusability. |
| 8 | + |
| 9 | +### Key Concepts |
| 10 | + |
| 11 | +- **Encapsulation**: Each subgraph handles a specific domain with its own expertise |
| 12 | +- **Modularity**: Subgraphs can be developed, tested, and maintained independently |
| 13 | +- **Reusability**: The same subgraph can be used across multiple parent graphs |
| 14 | +- **State Communication**: Subgraphs can share state or use different schemas with transformations |
| 15 | + |
| 16 | +## Demo Architecture 🗺️ |
| 17 | + |
| 18 | +This travel planner demonstrates both **shared state** and **specialized routing** patterns: |
| 19 | + |
| 20 | +### Parent Graph: Trip Coordinator |
| 21 | +- **Role**: Analyzes user requests and routes to appropriate travel specialists |
| 22 | +- **Intelligence**: Uses keyword analysis to determine which specialist to activate |
| 23 | +- **State Management**: Maintains the complete travel plan across all subgraphs |
| 24 | + |
| 25 | +### Subgraph 1: 🚗 Transportation Specialist |
| 26 | +- **Specialization**: Flights, trains, car rentals, buses, cruises, and travel logistics |
| 27 | +- **Triggers**: "flight", "train", "car rental", "transportation", "departure", "arrival" |
| 28 | +- **Tools**: `update_transportation()` with booking tips and cost optimization |
| 29 | + |
| 30 | +### Subgraph 2: 🏨 Accommodation Expert |
| 31 | +- **Specialization**: Hotels, Airbnb, resorts, hostels, camping, and lodging options |
| 32 | +- **Triggers**: "hotel", "accommodation", "stay", "room", "airbnb", "resort" |
| 33 | +- **Tools**: `update_accommodation()` with location and amenities optimization |
| 34 | + |
| 35 | +### Subgraph 3: 🎯 Activities Planner |
| 36 | +- **Specialization**: Attractions, restaurants, tours, cultural experiences, entertainment |
| 37 | +- **Triggers**: "activities", "attractions", "restaurant", "sightseeing", "tour", "experience" |
| 38 | +- **Tools**: `update_activities()` with local insights and time optimization |
| 39 | + |
| 40 | +## How It Works 🔄 |
| 41 | + |
| 42 | +1. **User Input**: "Plan a 5-day trip to Paris for 2 people" |
| 43 | +2. **Coordinator Analysis**: Determines this requires all three subgraphs |
| 44 | +3. **Intelligent Routing**: Distributes specialized requests to each subgraph: |
| 45 | + - Transportation: "Find flights to Paris and local transport" |
| 46 | + - Accommodation: "Find hotels in central Paris for 2 guests" |
| 47 | + - Activities: "Plan 5 days of Paris attractions and dining" |
| 48 | +4. **Parallel Processing**: All subgraphs work simultaneously |
| 49 | +5. **State Synchronization**: Each subgraph updates its portion of the travel plan |
| 50 | +6. **Visual Feedback**: UI shows which subgraphs are active and updating |
| 51 | + |
| 52 | +## State Communication Patterns 📊 |
| 53 | + |
| 54 | +### Shared State Schema |
| 55 | +```typescript |
| 56 | +interface TravelPlan { |
| 57 | + destination: string; |
| 58 | + travel_dates: string; |
| 59 | + duration: string; |
| 60 | + travelers: number; |
| 61 | + budget: string; |
| 62 | + transportation: Transportation; // Owned by Transportation subgraph |
| 63 | + accommodation: Accommodation; // Owned by Accommodation subgraph |
| 64 | + activities: Activity[]; // Owned by Activities subgraph |
| 65 | + travel_notes: string[]; // Shared across all |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Domain-Specific Updates |
| 70 | +Each subgraph only modifies its own domain while preserving the complete travel plan: |
| 71 | + |
| 72 | +- **Transportation Subgraph**: Updates `travel_plan.transportation` object |
| 73 | +- **Accommodation Subgraph**: Updates `travel_plan.accommodation` object |
| 74 | +- **Activities Subgraph**: Updates `travel_plan.activities` array |
| 75 | + |
| 76 | +## Try These Examples! 💡 |
| 77 | + |
| 78 | +### Transportation Focus |
| 79 | +- "Find me flights from New York to Tokyo" |
| 80 | +- "I want to take the train through Europe" |
| 81 | +- "Plan a road trip from Los Angeles to San Francisco" |
| 82 | + |
| 83 | +### Accommodation Focus |
| 84 | +- "Find luxury hotels in downtown Manhattan" |
| 85 | +- "I want to camp in Yosemite National Park" |
| 86 | +- "Book an Airbnb near the Eiffel Tower" |
| 87 | + |
| 88 | +### Activities Focus |
| 89 | +- "What are the must-see attractions in Rome?" |
| 90 | +- "Find the best restaurants in Tokyo" |
| 91 | +- "Plan adventure activities in Costa Rica" |
| 92 | + |
| 93 | +### Multi-Domain Requests |
| 94 | +- "Plan a romantic weekend in Santorini" |
| 95 | +- "Organize a family vacation to Disney World" |
| 96 | +- "Create a backpacking itinerary through Southeast Asia" |
| 97 | + |
| 98 | +## Visual Features 👁️ |
| 99 | + |
| 100 | +### Real-Time Subgraph Activity |
| 101 | +- **Glowing indicators** show which subgraph is currently active |
| 102 | +- **Animated routing paths** display request flow from coordinator to subgraphs |
| 103 | +- **Live travel plan updates** as each subgraph contributes |
| 104 | + |
| 105 | +### Travel Plan Visualization |
| 106 | +- **Dynamic travel overview** with destination and traveler indicators |
| 107 | +- **Comprehensive travel details** showing transportation, accommodation, and activities |
| 108 | +- **Smart destination emojis** that match popular travel destinations |
| 109 | +- **Planning progress tracking** across all travel aspects |
| 110 | + |
| 111 | +### Mobile Responsive |
| 112 | +- **Collapsible chat interface** for mobile users |
| 113 | +- **Touch-friendly controls** with gesture support |
| 114 | +- **Optimized layouts** for different screen sizes |
| 115 | + |
| 116 | +## Technical Implementation ⚙️ |
| 117 | + |
| 118 | +### Backend (Python) |
| 119 | +- **LangGraph workflow** with embedded subgraphs |
| 120 | +- **Smart routing logic** using travel-specific keyword analysis |
| 121 | +- **State management** with shared travel plan object |
| 122 | +- **Tool definitions** for each travel domain specialist |
| 123 | +- **Event streaming** for real-time UI updates |
| 124 | + |
| 125 | +### Frontend (React + TypeScript) |
| 126 | +- **CopilotKit integration** for seamless chat experience |
| 127 | +- **Real-time state synchronization** with `useCoAgent` |
| 128 | +- **Animated UI components** showing subgraph activity |
| 129 | +- **Travel-themed design** with destination recognition |
| 130 | +- **Custom travel data models** for type safety |
| 131 | + |
| 132 | +## Learning Outcomes 🎯 |
| 133 | + |
| 134 | +After exploring this demo, you'll understand: |
| 135 | + |
| 136 | +1. **Subgraph Architecture**: How to structure travel planning with specialized components |
| 137 | +2. **State Patterns**: When to use shared travel state vs. different schemas |
| 138 | +3. **Routing Logic**: How to intelligently direct travel requests to appropriate subgraphs |
| 139 | +4. **Visual Representation**: Making AI travel planning visible to users |
| 140 | +5. **Real-World Applications**: Practical patterns for multi-specialist systems |
| 141 | + |
| 142 | +## Why Travel Planning Works So Well 🌟 |
| 143 | + |
| 144 | +Travel planning is perfect for demonstrating subgraphs because: |
| 145 | + |
| 146 | +- **Clear Domain Separation**: Everyone understands transportation ≠ accommodation ≠ activities |
| 147 | +- **Real-World Relevance**: Everyone has planned a trip and can relate to the process |
| 148 | +- **Complex Coordination**: Shows how specialists must work together for optimal results |
| 149 | +- **Visual Appeal**: Travel plans are naturally visual and engaging |
| 150 | +- **Immediate Value**: Users can actually use this for real travel planning! |
| 151 | + |
| 152 | +## Next Steps 🚀 |
| 153 | + |
| 154 | +Ready to build your own subgraph systems? Consider these patterns: |
| 155 | + |
| 156 | +- **E-commerce**: Product Finder → Inventory Checker → Price Calculator → Order Processor |
| 157 | +- **Healthcare**: Symptom Analyzer → Treatment Planner → Medication Manager → Appointment Scheduler |
| 158 | +- **Education**: Course Planner → Content Creator → Assessment Designer → Progress Tracker |
| 159 | +- **Content Creation**: Research → Writing → Editing → Publishing |
| 160 | + |
| 161 | +The possibilities are endless when you can compose specialized AI agents into powerful workflows! ⚡ |
| 162 | + |
| 163 | +## Example Conversations 💬 |
| 164 | + |
| 165 | +### Complete Trip Planning |
| 166 | +**User**: "Plan a 7-day trip to Japan for my honeymoon" |
| 167 | + |
| 168 | +**Trip Coordinator**: *Routes to Transportation Specialist* |
| 169 | +**🚗 Transportation**: "I'll find flights to Tokyo and plan your internal travel" |
| 170 | + |
| 171 | +**Trip Coordinator**: *Routes to Accommodation Specialist* |
| 172 | +**🏨 Accommodation**: "Let me find romantic hotels and ryokans for your stay" |
| 173 | + |
| 174 | +**Trip Coordinator**: *Routes to Activities Specialist* |
| 175 | +**🎯 Activities**: "I'll curate amazing experiences from temples to cuisine" |
| 176 | + |
| 177 | +### Specific Domain Updates |
| 178 | +**User**: "Actually, let's fly business class" |
| 179 | +**Trip Coordinator**: *Routes to Transportation Specialist* |
| 180 | +**🚗 Transportation**: "Upgrading your flights to business class with better timing" |
| 181 | + |
| 182 | +**User**: "Find activities near our hotel" |
| 183 | +**Trip Coordinator**: *Routes to Activities Specialist* |
| 184 | +**🎯 Activities**: "Adding nearby attractions and restaurants to your itinerary" |
| 185 | + |
| 186 | +This natural conversation flow demonstrates how subgraphs enable truly intelligent, specialized assistance! 🎉 |
0 commit comments