TanujInsane commited on
Commit
6558d6e
·
1 Parent(s): 2378f58

fix: restore TicketAgent class for hybrid ML+rule ticket routing

Browse files
Files changed (1) hide show
  1. agent.py +287 -144
agent.py CHANGED
@@ -1,148 +1,291 @@
1
- import numpy as np
2
- import time
3
- from sklearn.feature_extraction.text import TfidfVectorizer
4
- from sklearn.metrics.pairwise import cosine_similarity
5
- from sklearn.preprocessing import normalize
6
-
7
- CATEGORY_TEMPLATES = {
8
- "General": ["general inquiry about services", "question about company", "general information request", "policies question"],
9
- "Billing": ["billing invoice payment", "account charges subscription", "payment status bill", "billing address update"],
10
- "Billing-Dispute": ["overcharged incorrect invoice", "dispute charge billing error", "wrong amount charged", "charge should not appear"],
11
- "Billing-Refund": ["request refund money back", "process refund return", "refund my payment", "need refund overpayment"],
12
- "Support": ["technical assistance help", "customer support question", "help with issue product", "contact support"],
13
- "Support-Urgent": ["urgent critical system down", "emergency production outage", "immediate help critical", "everything broken urgent"],
14
- "Support-Normal": ["basic feature help", "how to use product", "walk me through steps", "understanding functionality"],
15
- "Technical": ["technical issue software", "API integration documentation", "system technical problem", "software technical help"],
16
- "Technical-Bug": ["bug crash error exception", "software not working defect", "application crashing broken", "bug report error found"],
17
- "Technical-Feature": ["feature request enhancement", "new feature suggestion", "add functionality improvement", "product feature idea"],
18
- "HR": ["HR department human resources", "employee workforce staff", "HR policy question", "human resources inquiry"],
19
- "HR-Payroll": ["paycheck salary payroll", "wage compensation pay stub", "payroll department salary", "incorrect pay payroll"],
20
- "HR-Benefits": ["benefits insurance enrollment", "health dental coverage", "employee benefits package", "401k vacation benefits"],
21
- "HR-Complaint": ["complaint harassment workplace", "misconduct grievance hostile", "discrimination complaint report", "hostile environment complaint"],
22
- "Legal": ["legal assistance attorney", "legal department advice", "law contract legal question", "legal liability counsel"],
23
- "Legal-Contract": ["contract review agreement", "signing contract terms clause", "contract dispute legal", "vendor agreement review"],
24
- "Legal-Compliance": ["compliance regulation GDPR", "regulatory audit requirement", "HIPAA compliance legal", "regulatory requirement compliance"],
25
- "Executive": ["executive management leadership", "senior team escalation", "executive inquiry question", "management leadership team"],
26
- "Executive-Strategic": ["strategic planning business", "executive strategy roadmap", "business vision direction", "strategic initiative planning"],
27
- "Finance": ["finance budget accounting", "revenue fiscal financial", "financial report budget", "accounting finance question"],
28
- "Marketing": ["marketing campaign promotion", "advertising brand marketing", "campaign strategy promotion", "marketing question brand"],
29
- "Operations": ["operations logistics workflow", "supply chain operational", "process management operations", "operational question logistics"],
 
 
30
  }
31
 
32
- class SmartClassificationAgent:
33
- def __init__(self, difficulty):
34
- self.difficulty = difficulty
35
- self.vectorizer = None
36
- self.template_matrix = None
37
- self.template_labels = []
38
- self.categories = []
39
- self._trained = False
40
-
41
- def train(self, categories):
42
- self.categories = categories
43
- texts, labels = [], []
44
- for cat in categories:
45
- if cat in CATEGORY_TEMPLATES:
46
- for t in CATEGORY_TEMPLATES[cat]:
47
- texts.append(t)
48
- labels.append(cat)
49
- else:
50
- texts.append(cat.lower().replace("-", " "))
51
- labels.append(cat)
52
- self.vectorizer = TfidfVectorizer(ngram_range=(1, 3), sublinear_tf=True, max_features=10000)
53
- self.template_matrix = self.vectorizer.fit_transform(texts)
54
- self.template_labels = labels
55
- self._trained = True
56
-
57
- def predict(self, obs):
58
- if not self._trained:
59
- return 0, 0.0
60
-
61
- content = obs.get("content", "").lower()
62
- features = np.array(obs.get("features", []))
63
- word_count = obs.get("word_count", 0)
64
- has_urgency = obs.get("has_urgency_markers", False)
65
-
66
- # TF-IDF content similarity
67
- content_vec = self.vectorizer.transform([content])
68
- sims = cosine_similarity(content_vec, self.template_matrix)[0]
69
- best_idx = int(np.argmax(sims))
70
- best_cat = self.template_labels[best_idx]
71
- content_conf = float(sims[best_idx])
72
-
73
- # Feature vector signal
74
- feat_cat = None
75
- feat_conf = 0.0
76
- if len(features) > 0 and np.max(features) > -0.5:
77
- valid = [(i, v) for i, v in enumerate(features) if v > -0.5]
78
- if valid:
79
- best_feat_idx = max(valid, key=lambda x: x[1])[0]
80
- if best_feat_idx < len(self.categories):
81
- feat_cat = self.categories[best_feat_idx]
82
- feat_conf = float(features[best_feat_idx])
83
-
84
- # Urgency boost
85
- if has_urgency and "Support" in best_cat and "Urgent" not in best_cat:
86
- if "Support-Urgent" in self.categories:
87
- best_cat = "Support-Urgent"
88
- content_conf = min(content_conf + 0.2, 1.0)
89
-
90
- # Combine signals
91
- final_cat = best_cat
92
- if feat_cat and feat_cat in self.categories:
93
- if feat_conf > content_conf * 0.8:
94
- final_cat = feat_cat
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  try:
97
- action = self.categories.index(final_cat)
98
- confidence = max(content_conf, feat_conf)
99
- except ValueError:
100
- action = 0
101
- confidence = 0.1
102
-
103
- return action, confidence
104
-
105
-
106
- def run_episode(difficulty, seed=42):
107
- from environment import DocumentClassificationEnv
108
- env = DocumentClassificationEnv(difficulty)
109
- cats_map = env.CATEGORY_MAPS[difficulty]
110
- cats_list = [cats_map[i] for i in sorted(cats_map.keys())]
111
-
112
- agent = SmartClassificationAgent(difficulty)
113
- agent.train(cats_list)
114
-
115
- obs, info = env.reset(seed=seed)
116
- results = []
117
- total_reward = 0
118
- step = 0
119
-
120
- while True:
121
- t0 = time.time()
122
- action, confidence = agent.predict(obs)
123
- elapsed_ms = (time.time() - t0) * 1000
124
-
125
- obs_next, reward, done, truncated, info = env.step(action)
126
- total_reward += reward
127
- step += 1
128
-
129
- results.append({
130
- "step": step,
131
- "predicted": cats_map.get(action, "Unknown"),
132
- "reward": reward,
133
- "confidence": confidence,
134
- "time_ms": elapsed_ms,
135
- })
136
-
137
- if done or truncated:
138
- break
139
- obs = obs_next
140
-
141
- accuracy = total_reward / step if step > 0 else 0
142
- return results, accuracy, info
143
-
144
-
145
- if __name__ == "__main__":
146
- for diff in ["easy", "medium", "hard"]:
147
- results, acc, info = run_episode(diff)
148
- print(f"{diff.upper()}: accuracy={acc:.4f}, steps={len(results)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ }