Spaces:
Sleeping
Sleeping
Coding Ninja commited on
Commit ·
eae2b1d
1
Parent(s): 3752981
March 30 - April 1st : sever/
Browse filesFiles changed (2)
- inference.py+104-28
- server/environment.py+19-4
162 unmodified lines1631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362373 unmodified lines2412422431751761771781791801812442452462472485 unmodified lines25425525619319419525725825926026126226326426526626726826927027127227327427527627727827911 unmodified lines2912922932162942952962979 unmodified lines3073083092323103113123134 unmodified lines3183193202432443212452462472483223233241 unmodified line326327328256329330258259260331332333334335336337338339264340341266267268269270342343344345346347348349162 unmodified lines "export": "feature_request",}
CRITICAL_PRIORITY_KEYWORDS = ( "urgent", "critical", "blocking", "asap", "immediately", "locked out", "outage",)
HIGH_PRIORITY_KEYWORDS = ( "important", "high priority", "revenue", "today", "eod",)
LOW_PRIORITY_KEYWORDS = ("low", "whenever", "no rush")
ESCALATE_KEYWORDS = ( "refund", "charged twice", "still haven't", "following up", "needs immediate resolution", "locked out", "suspended", "legal",)
FULFILL_KEYWORDS = ( "please provide", "confirmation", "data processing addendum", "guidance", "fix", "reproducible", "outage", "policy", "mfa enabled",)
def heuristic_priority(text: str) -> str: if any(word in text for word in CRITICAL_PRIORITY_KEYWORDS): return "critical" if any(word in text for word in HIGH_PRIORITY_KEYWORDS): return "high" if any(word in text for word in LOW_PRIORITY_KEYWORDS): return "low" return "medium"
def heuristic_resolution_action(text: str, issue_type: str) -> str: if issue_type == "spam_phishing": return "ignore" if issue_type == "service_request": return "assign" if issue_type in {"general_inquiry", "feature_request"}: return "acknowledge" if any(keyword in text for keyword in ESCALATE_KEYWORDS): return "escalate" if any(keyword in text for keyword in FULFILL_KEYWORDS): return "fulfill" return ISSUE_TYPE_TO_RESOLUTION_ACTION.get(issue_type, "acknowledge")
def heuristic_action(ticket: dict, allowed_fields: list[str]) -> dict: text = (ticket.get("title", "") + " " + ticket.get("description", "")).lower()
3 unmodified lines issue_type = mapped_issue_type break
priority = "medium" if any(w in text for w in ["urgent", "critical", "blocking", "asap", "immediately"]): priority = "critical" elif any(w in text for w in ["important", "high priority", "revenue"]): priority = "high" elif any(w in text for w in ["low", "whenever", "no rush"]): priority = "low" priority = heuristic_priority(text) resolution_action = heuristic_resolution_action(text, issue_type)
result: dict = {} if "issue_type" in allowed_fields:5 unmodified lines issue_type, "service_desk" ) if "resolution_action" in allowed_fields: result["resolution_action"] = ISSUE_TYPE_TO_RESOLUTION_ACTION.get( issue_type, "acknowledge" ) result["resolution_action"] = resolution_action return result
def build_action( ticket: dict, allowed_fields: list[str], instructions: str) -> HelpdeskTicketAction: heuristic_dict = heuristic_action(ticket, allowed_fields)
if llm_client is None: return HelpdeskTicketAction(**heuristic_dict)
llm_dict = call_llm(ticket, allowed_fields, instructions) try: return HelpdeskTicketAction(**llm_dict) except Exception as exc: print(f" Falling back to heuristic action due to invalid LLM output: {exc}") return HelpdeskTicketAction(**heuristic_dict)
# ---------------------------------------------------------------------------# Main loop using WebSocket client for multi-step episodes# ---------------------------------------------------------------------------11 unmodified lines print(f"Available tasks: {[t['name'] for t in available_tasks.values()]}") http.close()
all_scores: dict[int, list[float]] = {} all_results: dict[int, dict[str, float | int]] = {}
for task_id in TASKS: if task_id not in available_tasks:9 unmodified lines result = sync_client.reset(seed=SEED, task_id=task_id) obs = result.observation
task_scores: list[float] = [] task_step_rewards: list[float] = [] step_num = 0
while not result.done:4 unmodified lines allowed = obs.allowed_fields instructions = obs.instructions
if llm_client is not None: action_dict = call_llm(ticket, allowed, instructions) action = build_action(ticket, allowed, instructions) else: action_dict = heuristic_action(ticket, allowed)
action = HelpdeskTicketAction(**action_dict) result = sync_client.step(action) obs = result.observation
1 unmodified line print(f" Step {step_num}: reward={result.reward} done={result.done}")
if result.reward is not None: task_scores.append(result.reward) task_step_rewards.append(float(result.reward))
all_scores[task_id] = task_scores final = task_scores[-1] if task_scores else 0.0 print(f" Task {task_id} final reward: {final:.4f}") final_reward = task_step_rewards[-1] if task_step_rewards else 0.0 all_results[task_id] = { "final_reward": final_reward, "step_count": step_num, } print(f" Task {task_id} final reward: {final_reward:.4f}")
# Summary print("\n=== RESULTS ===") overall = [] overall: list[float] = [] for tid in TASKS: if tid in all_scores: scores = all_scores[tid] avg = sum(scores) / len(scores) if scores else 0.0 overall.append(avg) print(f"Task {tid}: avg_score={avg:.4f} ({len(scores)} steps)") if tid in all_results: final_reward = float(all_results[tid]["final_reward"]) step_count = int(all_results[tid]["step_count"]) overall.append(final_reward) print(f"Task {tid}: final_reward={final_reward:.4f} ({step_count} steps)") if overall: print(f"Overall: {sum(overall) / len(overall):.4f}")
19 unmodified lines2021222324252627282930313233343536373814 unmodified lines535455435657585960464761626364652 unmodified lines686970567172737419 unmodified linesQUEUE_SIZE_RANGE = (3, 5)
def _coerce_optional_int(value: Any, field_name: str) -> Optional[int]: if value is None or value == "": return None if isinstance(value, bool): raise ValueError(f"{field_name} must be an integer") if isinstance(value, int): return value try: return int(value) except (TypeError, ValueError) as exc: raise ValueError(f"{field_name} must be an integer") from exc
class HelpdeskTicketRoutingEnvironment( Environment[HelpdeskTicketAction, HelpdeskTicketObservation, HelpdeskTicketState]):14 unmodified lines episode_id: Optional[str] = None, **kwargs: Any, ) -> HelpdeskTicketObservation: task_id: int = kwargs.get("task_id", 1) normalized_seed = _coerce_optional_int(seed, "seed") task_id_value = _coerce_optional_int(kwargs.get("task_id", 1), "task_id") task_id = 1 if task_id_value is None else task_id_value task = get_task_definition(task_id)
if seed is not None: self._rng.seed(seed) if normalized_seed is not None: self._rng.seed(normalized_seed)
queue_size = self._rng.randint(*QUEUE_SIZE_RANGE) self._queue = self._rng.sample(self._dataset, min(queue_size, len(self._dataset)))2 unmodified lines episode_id=episode_id or str(uuid.uuid4()), step_count=0, current_task_id=task_id, seed=seed, seed=normalized_seed, queue_ticket_ids=[t.ticket_id for t in self._queue], current_ticket_index=0, per_ticket_scores=[],inference.py+104-28
server/environment.py+19-4