|
| 1 | +""" |
| 2 | +Complete customer service workflow example with handoffs. |
| 3 | +
|
| 4 | +This example demonstrates a realistic customer service scenario with: |
| 5 | +- Order status checking |
| 6 | +- Refund processing |
| 7 | +- Technical support |
| 8 | +- FAQ handling |
| 9 | +- Escalation management |
| 10 | +""" |
| 11 | + |
| 12 | +from praisonaiagents import Agent, handoff, RECOMMENDED_PROMPT_PREFIX |
| 13 | +import random |
| 14 | + |
| 15 | + |
| 16 | +# Mock functions for demonstration |
| 17 | +def check_order_status(order_id: str) -> str: |
| 18 | + """Check the status of an order""" |
| 19 | + statuses = ["shipped", "processing", "delivered", "pending"] |
| 20 | + return f"Order {order_id} is {random.choice(statuses)}" |
| 21 | + |
| 22 | + |
| 23 | +def process_refund(order_id: str, reason: str) -> str: |
| 24 | + """Process a refund request""" |
| 25 | + return f"Refund initiated for order {order_id}. Reason: {reason}. Expected in 3-5 business days." |
| 26 | + |
| 27 | + |
| 28 | +def get_faq_answer(question: str) -> str: |
| 29 | + """Get answer from FAQ database""" |
| 30 | + faqs = { |
| 31 | + "shipping": "Standard shipping takes 5-7 business days. Express shipping takes 2-3 business days.", |
| 32 | + "returns": "You can return items within 30 days of purchase in original condition.", |
| 33 | + "warranty": "All products come with a 1-year manufacturer warranty.", |
| 34 | + "payment": "We accept credit cards, debit cards, PayPal, and Apple Pay." |
| 35 | + } |
| 36 | + for key, answer in faqs.items(): |
| 37 | + if key in question.lower(): |
| 38 | + return answer |
| 39 | + return "I'll need to check our documentation for that specific question." |
| 40 | + |
| 41 | + |
| 42 | +# Create specialized agents with tools |
| 43 | +order_agent = Agent( |
| 44 | + name="Order Specialist", |
| 45 | + role="Order Management Specialist", |
| 46 | + goal="Handle all order-related inquiries including tracking, modifications, and status updates", |
| 47 | + backstory="I specialize in order management and have access to the order tracking system.", |
| 48 | + tools=[check_order_status], |
| 49 | + instructions=f"""{RECOMMENDED_PROMPT_PREFIX} |
| 50 | +
|
| 51 | +I can help with: |
| 52 | +- Checking order status |
| 53 | +- Tracking shipments |
| 54 | +- Order modifications |
| 55 | +- Delivery issues |
| 56 | +
|
| 57 | +For refunds, I'll transfer you to our Refund Specialist. |
| 58 | +For technical issues, I'll connect you with Technical Support.""" |
| 59 | +) |
| 60 | + |
| 61 | +refund_agent = Agent( |
| 62 | + name="Refund Specialist", |
| 63 | + role="Refund and Returns Specialist", |
| 64 | + goal="Process refunds and handle return requests efficiently", |
| 65 | + backstory="I'm authorized to process refunds and handle all return-related matters.", |
| 66 | + tools=[process_refund], |
| 67 | + instructions="""I process refunds and returns. I need the order ID and reason for the refund. |
| 68 | + I ensure all refunds are processed according to our policy.""" |
| 69 | +) |
| 70 | + |
| 71 | +faq_agent = Agent( |
| 72 | + name="FAQ Assistant", |
| 73 | + role="Knowledge Base Specialist", |
| 74 | + goal="Provide quick answers to frequently asked questions", |
| 75 | + backstory="I have access to our comprehensive FAQ database.", |
| 76 | + tools=[get_faq_answer], |
| 77 | + instructions="I provide answers to common questions about shipping, returns, warranty, and payments." |
| 78 | +) |
| 79 | + |
| 80 | +technical_agent = Agent( |
| 81 | + name="Technical Support", |
| 82 | + role="Technical Support Engineer", |
| 83 | + goal="Resolve technical issues with products and services", |
| 84 | + backstory="I'm trained in troubleshooting all our products and technical services.", |
| 85 | + instructions="""I help with: |
| 86 | + - Product setup and configuration |
| 87 | + - Troubleshooting technical issues |
| 88 | + - Software problems |
| 89 | + - Hardware diagnostics""" |
| 90 | +) |
| 91 | + |
| 92 | +escalation_agent = Agent( |
| 93 | + name="Senior Manager", |
| 94 | + role="Customer Experience Manager", |
| 95 | + goal="Handle escalated issues and ensure customer satisfaction", |
| 96 | + backstory="I'm a senior manager with authority to make exceptions and handle complex cases.", |
| 97 | + instructions="""I handle: |
| 98 | + - Escalated complaints |
| 99 | + - Special requests requiring manager approval |
| 100 | + - Complex issues that need executive decisions |
| 101 | + - Customer retention situations""" |
| 102 | +) |
| 103 | + |
| 104 | +# Create main customer service agent with handoffs |
| 105 | +customer_service_agent = Agent( |
| 106 | + name="Customer Service", |
| 107 | + role="Customer Service Representative", |
| 108 | + goal="Provide excellent customer service by understanding needs and routing to the right specialist", |
| 109 | + backstory="I'm your first point of contact and I'll make sure you get the help you need.", |
| 110 | + instructions=f"""{RECOMMENDED_PROMPT_PREFIX} |
| 111 | +
|
| 112 | +Welcome! I'm here to help you today. I can assist with various requests or connect you with the right specialist: |
| 113 | +
|
| 114 | +- For order tracking and status → Order Specialist |
| 115 | +- For refunds and returns → Refund Specialist |
| 116 | +- For common questions → FAQ Assistant |
| 117 | +- For technical problems → Technical Support |
| 118 | +- For complaints or special requests → Senior Manager |
| 119 | +
|
| 120 | +How can I help you today?""", |
| 121 | + handoffs=[ |
| 122 | + order_agent, |
| 123 | + refund_agent, |
| 124 | + faq_agent, |
| 125 | + technical_agent, |
| 126 | + handoff( |
| 127 | + escalation_agent, |
| 128 | + tool_description_override="Escalate to senior management for complex issues or complaints" |
| 129 | + ) |
| 130 | + ] |
| 131 | +) |
| 132 | + |
| 133 | +# Example interaction |
| 134 | +if __name__ == "__main__": |
| 135 | + print("=== Customer Service System ===") |
| 136 | + print("Welcome to our customer service! Type 'quit' to exit.\n") |
| 137 | + |
| 138 | + # Simulated customer interactions |
| 139 | + test_scenarios = [ |
| 140 | + "I need to check the status of my order #12345", |
| 141 | + "I want a refund for order #67890, the product was damaged", |
| 142 | + "What's your return policy?", |
| 143 | + "My device won't turn on after the update", |
| 144 | + "I'm very unhappy with the service and want to speak to a manager!", |
| 145 | + "How long does shipping usually take?" |
| 146 | + ] |
| 147 | + |
| 148 | + print("Running automated test scenarios:\n") |
| 149 | + for scenario in test_scenarios: |
| 150 | + print(f"Customer: {scenario}") |
| 151 | + response = customer_service_agent.chat(scenario) |
| 152 | + print(f"Agent: {response}") |
| 153 | + print("-" * 80) |
| 154 | + |
| 155 | + # Interactive mode |
| 156 | + print("\nNow entering interactive mode. You can ask questions directly:") |
| 157 | + while True: |
| 158 | + user_input = input("\nYou: ") |
| 159 | + if user_input.lower() == 'quit': |
| 160 | + break |
| 161 | + response = customer_service_agent.chat(user_input) |
| 162 | + print(f"Agent: {response}") |
0 commit comments