Vivek45L commited on
Commit
b1fd80c
·
verified ·
1 Parent(s): 18c5ba5

Create src/envs/customer_support_env/server/environment.py

Browse files
src/envs/customer_support_env/server/environment.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uuid
2
+ from typing import Optional
3
+ from envs.customer_support_env.models import (
4
+ CustomerSupportAction,
5
+ CustomerSupportObservation,
6
+ CustomerSupportState,
7
+ )
8
+
9
+ TICKETS = {
10
+ "easy": {
11
+ "ticket_id": "TKT-001",
12
+ "ticket_text": "Hi, I was charged twice for my subscription this month. Please refund the extra charge as soon as possible. My account email is john.doe@example.com.",
13
+ "correct_priority": "high",
14
+ "correct_department": "billing",
15
+ "keywords": ["refund", "charge", "billing", "subscription", "account"],
16
+ },
17
+ "medium": {
18
+ "ticket_id": "TKT-002",
19
+ "ticket_text": "Hello, I've been trying to log in for two days but keep getting 'Invalid credentials' even after resetting my password three times. Also, I noticed my last invoice shows a plan I never upgraded to — I'm on the Basic plan but the invoice says Pro. Please sort both issues.",
20
+ "correct_priority": "high",
21
+ "correct_department": "technical",
22
+ "keywords": ["login", "password", "credentials", "invoice", "plan", "reset"],
23
+ },
24
+ "hard": {
25
+ "ticket_id": "TKT-003",
26
+ "ticket_text": "This is absolutely unacceptable! Our entire team of 50 people has been locked out of the platform since 6 AM. We have a critical client demo in 3 hours and your system is DOWN. On top of that, we received an invoice for $4,800 which is 3x our agreed price. I need someone to call me immediately: +1-555-0199. If this isn't resolved in 1 hour I'm cancelling our enterprise contract and filing a chargeback.",
27
+ "correct_priority": "critical",
28
+ "correct_department": "technical",
29
+ "keywords": ["locked out", "down", "demo", "invoice", "chargeback", "enterprise", "cancel", "immediately", "urgent", "outage"],
30
+ },
31
+ }
32
+
33
+
34
+ def _grade_response(response_draft: str, keywords: list) -> float:
35
+ score = 0.0
36
+ text = response_draft.lower()
37
+ words = text.split()
38
+
39
+ if len(words) >= 20:
40
+ score += 0.10
41
+ elif len(words) >= 10:
42
+ score += 0.05
43
+
44
+ hits = sum(1 for kw in keywords if kw.lower() in text)
45
+ coverage = hits / max(len(keywords), 1)
46
+ score += round(coverage * 0.10, 4)
47
+
48
+ empathy_words = ["sorry", "apologize", "apologies", "understand", "inconvenience"]
49
+ if any(w in text for w in empathy_words):
50
+ score += 0.05
51
+
52
+ action_words = ["will", "team", "resolve", "fix", "escalate", "refund",
53
+ "contact", "call", "send", "process", "investigate"]
54
+ if any(w in text for w in action_words):
55
+ score += 0.05
56
+
57
+ return round(min(score, 0.30), 4)
58
+
59
+
60
+ def grade_action(action: CustomerSupportAction, ticket: dict) -> dict:
61
+ priority_score = 0.40 if action.priority.lower() == ticket["correct_priority"] else 0.0
62
+ dept_score = 0.30 if action.department.lower() == ticket["correct_department"] else 0.0
63
+ response_score = _grade_response(action.response_draft, ticket["keywords"])
64
+ total = round(priority_score + dept_score + response_score, 4)
65
+ return {
66
+ "total_reward": total,
67
+ "priority_score": priority_score,
68
+ "department_score": dept_score,
69
+ "response_score": response_score,
70
+ "correct_priority": ticket["correct_priority"],
71
+ "correct_department": ticket["correct_department"],
72
+ }
73
+
74
+
75
+ class CustomerSupportEnvironment:
76
+ def __init__(self):
77
+ self._episode_id: Optional[str] = None
78
+ self._task_level: Optional[str] = None
79
+ self._ticket: Optional[dict] = None
80
+ self._step_count: int = 0
81
+ self._total_reward: float = 0.0
82
+ self._done: bool = False
83
+
84
+ def reset(self, task_level: str = "easy") -> CustomerSupportObservation:
85
+ if task_level not in TICKETS:
86
+ raise ValueError(f"task_level must be one of {list(TICKETS.keys())}")
87
+ self._episode_id = str(uuid.uuid4())
88
+ self._task_level = task_level
89
+ self._ticket = TICKETS[task_level]
90
+ self._step_count = 0
91
+ self._total_reward = 0.0
92
+ self._done = False
93
+ return CustomerSupportObservation(
94
+ ticket_id=self._ticket["ticket_id"],
95
+ ticket_text=self._ticket["ticket_text"],
96
+ task_level=self._task_level,
97
+ done=False,
98
+ reward=None,
99
+ feedback={},
100
+ metadata={"hint": "Assign priority, department, and write a response draft."},
101
+ )
102
+
103
+ def step(self, action: CustomerSupportAction) -> CustomerSupportObservation:
104
+ if self._done:
105
+ raise RuntimeError("Episode is done. Call reset() to start a new episode.")
106
+ feedback = grade_action(action, self._ticket)
107
+ reward = feedback["total_reward"]
108
+ self._step_count += 1
109
+ self._total_reward += reward
110
+ self._done = True
111
+ return CustomerSupportObservation(
112
+ ticket_id=self._ticket["ticket_id"],
113
+ ticket_text=self._ticket["ticket_text"],
114
+ task_level=self._task_level,
115
+ done=True,
116
+ reward=reward,
117
+ feedback=feedback,
118
+ metadata={},
119
+ )
120
+
121
+ @property
122
+ def state(self) -> CustomerSupportState:
123
+ return CustomerSupportState(
124
+ episode_id=self._episode_id or "",
125
+ step_count=self._step_count,
126
+ task_level=self._task_level or "",
127
+ current_ticket=self._ticket["ticket_id"] if self._ticket else "",
128
+ total_reward=self._total_reward,
129
+ )