Update env.py
Browse files
env.py
CHANGED
|
@@ -1,48 +1,71 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
class EmailTriageEnv
|
| 10 |
-
def __init__(self, task="
|
| 11 |
-
|
| 12 |
-
self.
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
{"
|
|
|
|
|
|
|
| 19 |
]
|
| 20 |
-
|
| 21 |
-
if task
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
self.
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
if
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List, Dict, Any, Tuple
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
URGENCY_LABELS = ["low", "medium", "high"]
|
| 5 |
+
ROUTING_LABELS = ["general", "support", "security"]
|
| 6 |
+
RESOLUTION_LABELS = ["ignore", "respond", "escalate"]
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class EmailTriageEnv:
|
| 10 |
+
def __init__(self, task: str = "easy"):
|
| 11 |
+
self.task = task
|
| 12 |
+
self._queue: List[Dict] = []
|
| 13 |
+
self._index = 0
|
| 14 |
+
self._done = False
|
| 15 |
+
|
| 16 |
+
def _generate_emails(self) -> List[Dict]:
|
| 17 |
+
emails = [
|
| 18 |
+
{"description": "Password reset not working", "label": [2, 1, 2]},
|
| 19 |
+
{"description": "Billing refund request", "label": [1, 2, 2]},
|
| 20 |
+
{"description": "App is slow and buggy", "label": [0, 1, 1]},
|
| 21 |
]
|
| 22 |
+
|
| 23 |
+
if self.task in ["medium", "hard"]:
|
| 24 |
+
emails += [
|
| 25 |
+
{"description": "Possible phishing attempt detected", "label": [2, 2, 2]},
|
| 26 |
+
{"description": "Invoice mismatch and payment issue", "label": [1, 2, 2]},
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
if self.task == "hard":
|
| 30 |
+
emails += [
|
| 31 |
+
{"description": "Ransomware attack suspected on system", "label": [2, 2, 2]},
|
| 32 |
+
{"description": "User reports data breach and performance issues", "label": [2, 2, 2]},
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
random.shuffle(emails)
|
| 36 |
+
return emails
|
| 37 |
+
|
| 38 |
+
def reset(self) -> Dict[str, Any]:
|
| 39 |
+
self._queue = self._generate_emails()
|
| 40 |
+
self._index = 0
|
| 41 |
+
self._done = False
|
| 42 |
+
return self.state()
|
| 43 |
+
|
| 44 |
+
def state(self) -> Dict[str, Any]:
|
| 45 |
+
if self._done:
|
| 46 |
+
return {"done": True}
|
| 47 |
+
|
| 48 |
+
current = self._queue[self._index]
|
| 49 |
+
return {
|
| 50 |
+
"description": current["description"],
|
| 51 |
+
"step": self._index,
|
| 52 |
+
"remaining": len(self._queue) - self._index,
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
def step(self, action: List[int]) -> Tuple[Dict, float, bool, Dict, Dict]:
|
| 56 |
+
if self._done:
|
| 57 |
+
return self.state(), 0.0, True, {}, {}
|
| 58 |
+
|
| 59 |
+
correct = self._queue[self._index]["label"]
|
| 60 |
+
|
| 61 |
+
reward = sum(
|
| 62 |
+
1.0 if a == b else 0.0
|
| 63 |
+
for a, b in zip(action, correct)
|
| 64 |
+
) / 3.0
|
| 65 |
+
|
| 66 |
+
self._index += 1
|
| 67 |
+
|
| 68 |
+
if self._index >= len(self._queue):
|
| 69 |
+
self._done = True
|
| 70 |
+
|
| 71 |
+
return self.state(), reward, self._done, {}, {}
|