|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +--- |
| 4 | + |
| 5 | +# Your First Workflow |
| 6 | + |
| 7 | +Build a complete customer service workflow using multiple AI frameworks. |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +We'll create a workflow that: |
| 12 | +1. Analyzes customer sentiment (LangChain) |
| 13 | +2. Checks customer history (Zep Memory) |
| 14 | +3. Creates support ticket (MCP Tools) |
| 15 | +4. Starts resolution workflow (Temporal) |
| 16 | + |
| 17 | +## Step 1: Define the Business Rules |
| 18 | + |
| 19 | +Create `rules/customer_service.yaml`: |
| 20 | + |
| 21 | +```yaml |
| 22 | +rule_set: customer_service |
| 23 | +rules: |
| 24 | + - rule: analyze_sentiment |
| 25 | + description: Analyze customer message sentiment |
| 26 | + priority: high |
| 27 | + conditions: |
| 28 | + - message.text != null |
| 29 | + actions: |
| 30 | + - framework: langchain |
| 31 | + action: sentiment_analysis |
| 32 | + output: sentiment_score |
| 33 | + |
| 34 | + - rule: check_escalation |
| 35 | + description: Determine if escalation needed |
| 36 | + priority: high |
| 37 | + conditions: |
| 38 | + - sentiment_score < 0.3 |
| 39 | + - customer.tier == "premium" |
| 40 | + actions: |
| 41 | + - framework: zep |
| 42 | + action: get_customer_history |
| 43 | + - framework: temporal |
| 44 | + action: start_escalation_workflow |
| 45 | + params: |
| 46 | + priority: high |
| 47 | + |
| 48 | + - rule: create_ticket |
| 49 | + description: Create support ticket |
| 50 | + priority: medium |
| 51 | + conditions: |
| 52 | + - sentiment_score < 0.7 |
| 53 | + actions: |
| 54 | + - framework: mcp |
| 55 | + action: create_ticket |
| 56 | + params: |
| 57 | + type: support |
| 58 | + auto_assign: true |
| 59 | +``` |
| 60 | +
|
| 61 | +## Step 2: Initialize the Frameworks |
| 62 | +
|
| 63 | +```python |
| 64 | +from bizy.core import MetaOrchestrator |
| 65 | +from bizy.adapters import ( |
| 66 | + LangChainAdapter, |
| 67 | + TemporalAdapter, |
| 68 | + MCPAdapter, |
| 69 | + ZepAdapter |
| 70 | +) |
| 71 | + |
| 72 | +async def setup_orchestrator(): |
| 73 | + orchestrator = MetaOrchestrator() |
| 74 | + |
| 75 | + # Configure adapters |
| 76 | + adapters = { |
| 77 | + "langchain": LangChainAdapter({"api_key": "..."}), |
| 78 | + "temporal": TemporalAdapter({"host": "localhost"}), |
| 79 | + "mcp": MCPAdapter({"server_url": "http://localhost:8080"}), |
| 80 | + "zep": ZepAdapter({"api_url": "http://localhost:8000"}) |
| 81 | + } |
| 82 | + |
| 83 | + # Register all adapters |
| 84 | + for name, adapter in adapters.items(): |
| 85 | + await adapter.connect() |
| 86 | + orchestrator.register_adapter(name, adapter) |
| 87 | + |
| 88 | + # Load rules |
| 89 | + orchestrator.load_rules("rules/customer_service.yaml") |
| 90 | + |
| 91 | + return orchestrator |
| 92 | +``` |
| 93 | + |
| 94 | +## Step 3: Create the Workflow Handler |
| 95 | + |
| 96 | +```python |
| 97 | +class CustomerServiceHandler: |
| 98 | + def __init__(self, orchestrator): |
| 99 | + self.orchestrator = orchestrator |
| 100 | + |
| 101 | + async def handle_customer_message(self, customer_id: str, message: str): |
| 102 | + # Prepare context |
| 103 | + context = { |
| 104 | + "customer": await self.get_customer_info(customer_id), |
| 105 | + "message": {"text": message} |
| 106 | + } |
| 107 | + |
| 108 | + # Execute rules |
| 109 | + result = await self.orchestrator.execute( |
| 110 | + rule_set="customer_service", |
| 111 | + context=context |
| 112 | + ) |
| 113 | + |
| 114 | + return { |
| 115 | + "ticket_id": result.get("ticket_id"), |
| 116 | + "workflow_id": result.get("workflow_id"), |
| 117 | + "sentiment": result.get("sentiment_score"), |
| 118 | + "actions_taken": result.get("executed_actions") |
| 119 | + } |
| 120 | +``` |
| 121 | + |
| 122 | +## Step 4: Run the Workflow |
| 123 | + |
| 124 | +```python |
| 125 | +async def main(): |
| 126 | + # Setup |
| 127 | + orchestrator = await setup_orchestrator() |
| 128 | + handler = CustomerServiceHandler(orchestrator) |
| 129 | + |
| 130 | + # Handle a customer message |
| 131 | + result = await handler.handle_customer_message( |
| 132 | + customer_id="cust_123", |
| 133 | + message="I'm very frustrated with the service!" |
| 134 | + ) |
| 135 | + |
| 136 | + print(f"Ticket created: {result['ticket_id']}") |
| 137 | + print(f"Sentiment score: {result['sentiment']}") |
| 138 | + print(f"Actions taken: {result['actions_taken']}") |
| 139 | + |
| 140 | +# Run |
| 141 | +import asyncio |
| 142 | +asyncio.run(main()) |
| 143 | +``` |
| 144 | + |
| 145 | +## Expected Output |
| 146 | + |
| 147 | +``` |
| 148 | +Ticket created: TICK-2024-001 |
| 149 | +Sentiment score: 0.2 |
| 150 | +Actions taken: [ |
| 151 | + "langchain.sentiment_analysis", |
| 152 | + "zep.get_customer_history", |
| 153 | + "temporal.start_escalation_workflow", |
| 154 | + "mcp.create_ticket" |
| 155 | +] |
| 156 | +``` |
| 157 | + |
| 158 | +## Next Steps |
| 159 | + |
| 160 | +- Add error handling and retries |
| 161 | +- Implement custom actions |
| 162 | +- Create workflow monitoring |
| 163 | +- Scale with multiple workers |
| 164 | + |
| 165 | +## Learn More |
| 166 | + |
| 167 | +- [Business Rule Patterns](../rules/best-practices) |
| 168 | +- [Error Handling](../tutorials/error-handling) |
| 169 | +- [Production Deployment](../deployment/requirements) |
0 commit comments