github-actions[bot]
Automated deployment from GitHub Actions: a9d5a91de63ad7224bafe6039fb2b7479388fbf4
0a8c81b Download src/agents/customer_bot_agent.py from VLTA-AI/dropshop: direct link, hf CLI and curl.
- Browser
- Download file 5.48 kB
-
https://huggingface.co/spaces/VLTA-AI/dropshop/resolve/main/src/agents/customer_bot_agent.py
- Command line
-
hf download hf://spaces/VLTA-AI/dropshop/src/agents/customer_bot_agent.py
-
curl -L -o customer_bot_agent.py https://huggingface.co/spaces/VLTA-AI/dropshop/resolve/main/src/agents/customer_bot_agent.py
5.48 kB
| import logging | |
| logger = logging.getLogger(__name__) | |
| import re | |
| import os | |
| from pydantic import BaseModel | |
| class CustomerBotResponse(BaseModel): | |
| reply_text: str | |
| escalated_to_human: bool | |
| detected_language: str | |
| class CustomerBotAgent: | |
| """ | |
| Customer Service Bot Agent (Tri-Router Architecture). | |
| Routes between Order Queries, FAQ RAG, and Human Escalation. | |
| """ | |
| def __init__(self): | |
| # Simulated database of orders with emails | |
| self.mock_db = { | |
| "1001": {"email": "buyer@example.com", "status": "AWAITING_PAYMENT", "eta": "2-3 business days"}, | |
| "1002": {"email": "john@test.com", "status": "SHIPPED", "eta": "5-7 business days"}, | |
| "1003": {"email": "alice@gmail.com", "status": "DELIVERED", "eta": "Delivered"} | |
| } | |
| # Load local Knowledge Base | |
| self.kb_path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "docs", "knowledge_base", "faq.md") | |
| self.faq_content = "" | |
| if os.path.exists(self.kb_path): | |
| with open(self.kb_path, "r", encoding="utf-8") as f: | |
| self.faq_content = f.read() | |
| def _classify_intent(self, message: str) -> str: | |
| """Simulates LLM Intent Routing""" | |
| msg = message.lower() | |
| if any(keyword in msg for keyword in ["refund", "broken", "lost", "angry", "fake", "scam", "sue", "cancel"]): | |
| return "ESCALATION" | |
| elif any(keyword in msg for keyword in ["shipping", "time", "return", "policy", "payment", "card", "pay", "how long", "days"]): | |
| return "FAQ_RAG" | |
| elif any(keyword in msg for keyword in ["order", "where", "status", "track", "100"]): | |
| return "ORDER_QUERY" | |
| else: | |
| # Fallback to FAQ for general questions | |
| return "FAQ_RAG" | |
| def _handle_escalation(self, email: str) -> str: | |
| return f"I apologize for the inconvenience. Please email support@vltadropshopping.com from '{email}' so our human team can resolve this for you immediately." | |
| def _handle_faq_rag(self, message: str) -> str: | |
| """Simulates RAG (Retrieval-Augmented Generation) based on faq.md""" | |
| msg = message.lower() | |
| if "shipping" in msg or "how long" in msg or "time" in msg or "days" in msg: | |
| return "[AI RAG Reply]: We offer worldwide shipping. Standard to US/EU takes 7-15 business days, and Express takes 3-5 business days. Please allow 24-48 hours for warehouse processing." | |
| elif "return" in msg or "policy" in msg: | |
| return "[AI RAG Reply]: We have a strict 30-day return policy. Items must be unused in original packaging with a receipt." | |
| elif "payment" in msg or "pay" in msg or "card" in msg: | |
| return "[AI RAG Reply]: We accept all major credit cards via Stripe, PayPal, and Apple Pay. We do not accept Cash on Delivery." | |
| else: | |
| return "[AI RAG Reply]: If you have further questions I cannot answer, please email our human support team at support@vltadropshopping.com. They reply within 24 hours." | |
| def _handle_order_query(self, message: str, email: str) -> str: | |
| if not email: | |
| return "Hello! To check your order status, please provide the email address you used during purchase." | |
| order_match = re.search(r'\b(100[1-9])\b', message) | |
| if order_match: | |
| order_id = order_match.group(1) | |
| order_data = self.mock_db.get(order_id) | |
| if not order_data: | |
| return f"I'm sorry, I couldn't find Order #{order_id} in our system. Please double check the number." | |
| elif order_data["email"].lower() != email.lower(): | |
| return f"For security reasons, I can only provide updates for orders associated with '{email}'. Order #{order_id} belongs to a different email." | |
| else: | |
| status = order_data["status"] | |
| eta = order_data["eta"] | |
| if status in ["AWAITING_PAYMENT", "DRAFTED"]: | |
| return f"Thank you! Your Order #{order_id} is currently being processed. Estimated time: {eta}." | |
| elif status == "SHIPPED": | |
| return f"Great news! Your Order #{order_id} has been shipped. Estimated delivery: {eta}." | |
| elif status == "DELIVERED": | |
| return f"According to our records, Order #{order_id} has already been delivered." | |
| return f"Hello {email}! I see you're asking about an order. Please provide your 4-digit Shopify Order ID." | |
| def process_message(self, message: str, email: str = "") -> CustomerBotResponse: | |
| logger.info(f"🤖 [BOT AGENT] Received message: '{message}' from '{email}'") | |
| # 1. Intent Routing | |
| intent = self._classify_intent(message) | |
| logger.warning(f"🛣️ [BOT AGENT] Intent Router classified as: {intent}") | |
| reply_text = "" | |
| escalated = False | |
| # 2. Execution Branches | |
| if intent == "ESCALATION": | |
| reply_text = self._handle_escalation(email) | |
| escalated = True | |
| elif intent == "FAQ_RAG": | |
| reply_text = self._handle_faq_rag(message) | |
| elif intent == "ORDER_QUERY": | |
| reply_text = self._handle_order_query(message, email) | |
| return CustomerBotResponse( | |
| reply_text=reply_text, | |
| escalated_to_human=escalated, | |
| detected_language="en" | |
| ) | |