TanujInsane commited on
Commit ·
6558d6e
1
Parent(s): 2378f58
fix: restore TicketAgent class for hybrid ML+rule ticket routing
Browse files
agent.py
CHANGED
|
@@ -1,148 +1,291 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"
|
| 16 |
-
|
| 17 |
-
"
|
| 18 |
-
|
| 19 |
-
"
|
| 20 |
-
|
| 21 |
-
"
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
"
|
| 27 |
-
|
| 28 |
-
"
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 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
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import os
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
CATEGORY_NAMES = {
|
| 6 |
+
"easy": {0:"General", 1:"Billing", 2:"Support", 3:"Technical", 4:"HR"},
|
| 7 |
+
"medium": {0:"General", 1:"Billing", 2:"Support", 3:"Technical", 4:"HR",
|
| 8 |
+
5:"Legal", 6:"Sales", 7:"Marketing", 8:"Operations", 9:"Complaints"},
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
CATEGORY_OVERRIDE = {
|
| 12 |
+
"Technical": ["crash", "crashes", "bug", "error", "not working", "broken", "upload fail",
|
| 13 |
+
"application", "software", "login fail", "404", "500", "exception",
|
| 14 |
+
"loading", "slow", "freeze", "hang", "install", "update failed", "glitch"],
|
| 15 |
+
"Billing": ["invoice", "charge", "charged", "payment", "refund", "subscription", "billing",
|
| 16 |
+
"amount", "price", "cost", "fee", "receipt", "transaction", "overcharged"],
|
| 17 |
+
"HR": ["leave", "salary", "policy", "maternity", "paternity", "holiday", "resignation",
|
| 18 |
+
"offer letter", "payslip", "appraisal", "human resources", "benefits"],
|
| 19 |
+
"Complaints": ["complaint", "unacceptable", "disgusted", "terrible", "worst", "rude", "awful",
|
| 20 |
+
"disappointed", "frustrated", "escalate"],
|
| 21 |
+
"Legal": ["legal", "lawsuit", "compliance", "gdpr", "contract", "terms", "court",
|
| 22 |
+
"attorney", "lawyer", "liability", "copyright", "trademark"],
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
PRIORITY_RULES = {
|
| 26 |
+
"urgent": ["urgent", "asap", "immediately", "critical", "emergency", "not working",
|
| 27 |
+
"broken", "down", "error", "failed", "crashes", "crash"],
|
| 28 |
+
"high": ["incorrect", "wrong", "missing", "cannot", "can't", "unable",
|
| 29 |
+
"problem", "issue", "complaint"],
|
| 30 |
+
"medium": ["review", "check", "update", "change", "request", "need", "want", "please"],
|
| 31 |
+
"low": ["inquiry", "question", "info", "information", "general", "curious", "wondering"]
|
| 32 |
}
|
| 33 |
|
| 34 |
+
DEPARTMENT_MAP = {
|
| 35 |
+
"Billing": {"team": "Billing & Finance", "email": "billing@company.com", "sla": "24h"},
|
| 36 |
+
"Technical": {"team": "Tech Support", "email": "techsupport@company.com","sla": "4h"},
|
| 37 |
+
"HR": {"team": "Human Resources", "email": "hr@company.com", "sla": "48h"},
|
| 38 |
+
"Support": {"team": "Customer Support", "email": "support@company.com", "sla": "12h"},
|
| 39 |
+
"General": {"team": "General Enquiries", "email": "hello@company.com", "sla": "48h"},
|
| 40 |
+
"Legal": {"team": "Legal & Compliance", "email": "legal@company.com", "sla": "72h"},
|
| 41 |
+
"Sales": {"team": "Sales", "email": "sales@company.com", "sla": "6h"},
|
| 42 |
+
"Marketing": {"team": "Marketing", "email": "marketing@company.com", "sla": "48h"},
|
| 43 |
+
"Operations": {"team": "Operations", "email": "ops@company.com", "sla": "24h"},
|
| 44 |
+
"Complaints": {"team": "Customer Relations", "email": "complaints@company.com", "sla": "8h"},
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
REPLY_TEMPLATES = {
|
| 48 |
+
"Billing": """Dear Customer,
|
| 49 |
+
|
| 50 |
+
Thank you for contacting us regarding your billing inquiry.
|
| 51 |
+
|
| 52 |
+
We have received your request and our Billing & Finance team will review your account details within 24 hours.
|
| 53 |
+
|
| 54 |
+
What we'll do:
|
| 55 |
+
- Review your invoice/payment details
|
| 56 |
+
- Verify the transaction in question
|
| 57 |
+
- Send you a detailed resolution
|
| 58 |
+
|
| 59 |
+
Reference ID: {ref_id}
|
| 60 |
+
Priority: {priority}
|
| 61 |
+
Expected resolution: {sla}
|
| 62 |
+
|
| 63 |
+
If urgent, call: +1-800-BILLING
|
| 64 |
+
|
| 65 |
+
Best regards,
|
| 66 |
+
Billing & Finance Team""",
|
| 67 |
+
|
| 68 |
+
"Technical": """Dear Customer,
|
| 69 |
+
|
| 70 |
+
Thank you for reporting this technical issue.
|
| 71 |
+
|
| 72 |
+
Our Tech Support team has been notified and will respond within 4 hours.
|
| 73 |
+
|
| 74 |
+
Immediate steps you can try:
|
| 75 |
+
- Clear browser cache and retry
|
| 76 |
+
- Check our status page: status.company.com
|
| 77 |
+
- Restart the application
|
| 78 |
+
|
| 79 |
+
Reference ID: {ref_id}
|
| 80 |
+
Priority: {priority}
|
| 81 |
+
Expected resolution: {sla}
|
| 82 |
+
|
| 83 |
+
For critical issues: techsupport@company.com
|
| 84 |
+
|
| 85 |
+
Best regards,
|
| 86 |
+
Tech Support Team""",
|
| 87 |
+
|
| 88 |
+
"HR": """Dear Team Member,
|
| 89 |
+
|
| 90 |
+
Thank you for reaching out to Human Resources.
|
| 91 |
+
|
| 92 |
+
Your query has been received and will be handled with full confidentiality.
|
| 93 |
+
|
| 94 |
+
Our HR team will respond within 48 hours regarding:
|
| 95 |
+
- Policy clarifications
|
| 96 |
+
- Benefits & compensation
|
| 97 |
+
- Workplace concerns
|
| 98 |
+
|
| 99 |
+
Reference ID: {ref_id}
|
| 100 |
+
Priority: {priority}
|
| 101 |
+
Expected resolution: {sla}
|
| 102 |
+
|
| 103 |
+
Best regards,
|
| 104 |
+
Human Resources Team""",
|
| 105 |
+
|
| 106 |
+
"Support": """Dear Customer,
|
| 107 |
+
|
| 108 |
+
Thank you for contacting Customer Support.
|
| 109 |
+
|
| 110 |
+
We have logged your request and a support agent will be in touch within 12 hours.
|
| 111 |
+
|
| 112 |
+
Reference ID: {ref_id}
|
| 113 |
+
Priority: {priority}
|
| 114 |
+
Expected resolution: {sla}
|
| 115 |
+
|
| 116 |
+
Track your ticket: support.company.com/track/{ref_id}
|
| 117 |
+
|
| 118 |
+
Best regards,
|
| 119 |
+
Customer Support Team""",
|
| 120 |
+
|
| 121 |
+
"General": """Dear Customer,
|
| 122 |
+
|
| 123 |
+
Thank you for your inquiry.
|
| 124 |
+
|
| 125 |
+
We have received your message and will respond within 48 hours.
|
| 126 |
+
|
| 127 |
+
Reference ID: {ref_id}
|
| 128 |
+
Priority: {priority}
|
| 129 |
+
|
| 130 |
+
Best regards,
|
| 131 |
+
General Enquiries Team""",
|
| 132 |
+
|
| 133 |
+
"Complaints": """Dear Customer,
|
| 134 |
|
| 135 |
+
We sincerely apologize for your experience.
|
| 136 |
+
|
| 137 |
+
Your complaint has been escalated to our Customer Relations team who will personally handle this within 8 hours.
|
| 138 |
+
|
| 139 |
+
Reference ID: {ref_id}
|
| 140 |
+
Priority: {priority}
|
| 141 |
+
Expected resolution: {sla}
|
| 142 |
+
|
| 143 |
+
We take all complaints seriously and are committed to resolving this promptly.
|
| 144 |
+
|
| 145 |
+
Best regards,
|
| 146 |
+
Customer Relations Team""",
|
| 147 |
+
|
| 148 |
+
"Sales": """Dear Customer,
|
| 149 |
+
|
| 150 |
+
Thank you for your interest!
|
| 151 |
+
|
| 152 |
+
Our Sales team will reach out within 6 hours with a personalized proposal.
|
| 153 |
+
|
| 154 |
+
Reference ID: {ref_id}
|
| 155 |
+
Priority: {priority}
|
| 156 |
+
|
| 157 |
+
Best regards,
|
| 158 |
+
Sales Team""",
|
| 159 |
+
|
| 160 |
+
"Legal": """Dear Customer,
|
| 161 |
+
|
| 162 |
+
Thank you for contacting our Legal & Compliance team.
|
| 163 |
+
|
| 164 |
+
Your matter will be reviewed by our legal team within 72 hours.
|
| 165 |
+
|
| 166 |
+
Reference ID: {ref_id}
|
| 167 |
+
Priority: {priority}
|
| 168 |
+
Expected resolution: {sla}
|
| 169 |
+
|
| 170 |
+
Best regards,
|
| 171 |
+
Legal & Compliance Team""",
|
| 172 |
+
|
| 173 |
+
"Marketing": """Dear Customer,
|
| 174 |
+
|
| 175 |
+
Thank you for reaching out to Marketing.
|
| 176 |
+
|
| 177 |
+
We will respond within 48 hours.
|
| 178 |
+
|
| 179 |
+
Reference ID: {ref_id}
|
| 180 |
+
Priority: {priority}
|
| 181 |
+
|
| 182 |
+
Best regards,
|
| 183 |
+
Marketing Team""",
|
| 184 |
+
|
| 185 |
+
"Operations": """Dear Customer,
|
| 186 |
+
|
| 187 |
+
Thank you for contacting Operations.
|
| 188 |
+
|
| 189 |
+
Your request has been logged and will be addressed within 24 hours.
|
| 190 |
+
|
| 191 |
+
Reference ID: {ref_id}
|
| 192 |
+
Priority: {priority}
|
| 193 |
+
Expected resolution: {sla}
|
| 194 |
+
|
| 195 |
+
Best regards,
|
| 196 |
+
Operations Team""",
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
class TicketAgent:
|
| 201 |
+
def __init__(self, model_dir="."):
|
| 202 |
+
self.models = {}
|
| 203 |
+
self.model_dir = model_dir
|
| 204 |
+
self._load_models()
|
| 205 |
+
|
| 206 |
+
def _load_models(self):
|
| 207 |
+
for d in ["easy", "medium", "hard"]:
|
| 208 |
+
path = os.path.join(self.model_dir, f"model_{d}.pkl")
|
| 209 |
+
if os.path.exists(path):
|
| 210 |
+
with open(path, "rb") as f:
|
| 211 |
+
self.models[d] = pickle.load(f)
|
| 212 |
+
|
| 213 |
+
def _to_name(self, c, difficulty):
|
| 214 |
+
name_map = CATEGORY_NAMES.get(difficulty, {})
|
| 215 |
try:
|
| 216 |
+
return name_map.get(int(c), str(c))
|
| 217 |
+
except:
|
| 218 |
+
return str(c)
|
| 219 |
+
|
| 220 |
+
def classify(self, text, difficulty="easy"):
|
| 221 |
+
text_lower = text.lower()
|
| 222 |
+
|
| 223 |
+
# Step 1 - Keyword override (hybrid rule check)
|
| 224 |
+
for category, keywords in CATEGORY_OVERRIDE.items():
|
| 225 |
+
if any(kw in text_lower for kw in keywords):
|
| 226 |
+
if difficulty in self.models:
|
| 227 |
+
model = self.models[difficulty]
|
| 228 |
+
raw_classes = model.classes_
|
| 229 |
+
proba = model.predict_proba([text])[0]
|
| 230 |
+
scores = {
|
| 231 |
+
self._to_name(c, difficulty): round(float(p), 4)
|
| 232 |
+
for c, p in zip(raw_classes, proba)
|
| 233 |
+
}
|
| 234 |
+
else:
|
| 235 |
+
scores = {category: 1.0}
|
| 236 |
+
scores[category] = max(scores.get(category, 0), 0.85)
|
| 237 |
+
return category, scores
|
| 238 |
+
|
| 239 |
+
# Step 2 - Pure ML classification
|
| 240 |
+
if difficulty not in self.models:
|
| 241 |
+
return "General", {"General": 1.0}
|
| 242 |
+
model = self.models[difficulty]
|
| 243 |
+
raw_classes = model.classes_
|
| 244 |
+
proba = model.predict_proba([text])[0]
|
| 245 |
+
pred_raw = raw_classes[proba.argmax()]
|
| 246 |
+
top_cat = self._to_name(pred_raw, difficulty)
|
| 247 |
+
scores = {
|
| 248 |
+
self._to_name(c, difficulty): round(float(p), 4)
|
| 249 |
+
for c, p in zip(raw_classes, proba)
|
| 250 |
+
}
|
| 251 |
+
return top_cat, scores
|
| 252 |
+
|
| 253 |
+
def get_priority(self, text):
|
| 254 |
+
text_lower = text.lower()
|
| 255 |
+
for priority, keywords in PRIORITY_RULES.items():
|
| 256 |
+
if any(kw in text_lower for kw in keywords):
|
| 257 |
+
return priority
|
| 258 |
+
return "low"
|
| 259 |
+
|
| 260 |
+
def route(self, category):
|
| 261 |
+
return DEPARTMENT_MAP.get(category, DEPARTMENT_MAP["General"])
|
| 262 |
+
|
| 263 |
+
def generate_reply(self, category, priority, ref_id):
|
| 264 |
+
dept = self.route(category)
|
| 265 |
+
template = REPLY_TEMPLATES.get(category, REPLY_TEMPLATES["General"])
|
| 266 |
+
return template.format(
|
| 267 |
+
ref_id=ref_id,
|
| 268 |
+
priority=priority.upper(),
|
| 269 |
+
sla=dept["sla"]
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
def process_ticket(self, text, difficulty="easy", ticket_id=None):
|
| 273 |
+
ref_id = ticket_id or f"TKT-{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
| 274 |
+
category, scores = self.classify(text, difficulty)
|
| 275 |
+
priority = self.get_priority(text)
|
| 276 |
+
dept = self.route(category)
|
| 277 |
+
reply = self.generate_reply(category, priority, ref_id)
|
| 278 |
+
top3 = sorted(scores.items(), key=lambda x: -x[1])[:3]
|
| 279 |
+
|
| 280 |
+
return {
|
| 281 |
+
"ref_id": ref_id,
|
| 282 |
+
"category": category,
|
| 283 |
+
"priority": priority,
|
| 284 |
+
"department": dept["team"],
|
| 285 |
+
"email": dept["email"],
|
| 286 |
+
"sla": dept["sla"],
|
| 287 |
+
"confidence": scores.get(category, 0.0),
|
| 288 |
+
"top3": top3,
|
| 289 |
+
"reply": reply,
|
| 290 |
+
"timestamp": datetime.now().isoformat()
|
| 291 |
+
}
|