File size: 10,809 Bytes
f392960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ae2389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f392960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ae2389
 
 
f392960
0ae2389
 
 
 
 
f392960
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""Baseline inference runner for the B2B Support Triage OpenEnv benchmark."""

from __future__ import annotations

import asyncio
import json
import os
import re
import textwrap
from typing import Any, Dict, List, Optional

from openai import OpenAI

from client import B2BSupportTriageEnv
from models import ActionType, B2BSupportPayload, B2BSupportTriageAction, B2BSupportTriageObservation

MODEL_NAME = os.getenv("MODEL_NAME") or "Qwen/Qwen2.5-72B-Instruct"
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME") or os.getenv("IMAGE_NAME") or "b2b_support_triage_env-env:latest"

BENCHMARK = "b2b_support_triage_env"
TASKS = ["easy", "medium", "hard"]
TASK_SEEDS = {"easy": 101, "medium": 202, "hard": 303}
MAX_STEPS = 12
MAX_TOKENS = 220
TEMPERATURE = 0.0
SUCCESS_SCORE_THRESHOLD = 0.80

SYSTEM_PROMPT = textwrap.dedent(
    """
    You are operating a B2B SaaS support triage environment.
    Return ONLY compact JSON with this shape:
    {
      "action_type": "classify|set_priority|route|draft_reply|submit",
      "ticket_id": "<ticket id or null for submit>",
      "payload": {
        "category": "...",
        "priority": "...",
        "route_queue": "...",
        "sla_minutes": 120,
        "escalate": true,
        "reply_text": "..."
      }
    }

    Rules:
    - Do not include markdown fences.
    - Fill only payload keys needed for the chosen action_type.
    - Keep action consistent with current plan and policy hints.
    """
).strip()


def log_start(task: str, env: str, model: str) -> None:
    print(f"[START] task={task} env={env} model={model}", flush=True)


def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
    done_val = str(done).lower()
    error_val = error if error else "null"
    print(
        f"[STEP] step={step} action={action} reward={reward:.2f} done={done_val} error={error_val}",
        flush=True,
    )


def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
    rewards_str = ",".join(f"{r:.2f}" for r in rewards)
    print(
        f"[END] success={str(success).lower()} steps={steps} score={score:.3f} rewards={rewards_str}",
        flush=True,
    )


def _extract_json_object(text: str) -> Dict[str, Any]:
    candidate = text.strip()
    if not candidate:
        return {}

    try:
        return json.loads(candidate)
    except json.JSONDecodeError:
        pass

    match = re.search(r"\{.*\}", candidate, re.DOTALL)
    if not match:
        return {}

    try:
        return json.loads(match.group(0))
    except json.JSONDecodeError:
        return {}


def _deterministic_policy(obs: B2BSupportTriageObservation) -> B2BSupportTriageAction:
    task = obs.task_id
    ticket_id = obs.visible_ticket.ticket_id
    decisions = obs.applied_decisions

    targets = {
        "easy": {
            "category": "billing",
            "priority": "medium",
            "route_queue": "billing-general",
            "sla_minutes": 480,
            "escalate": False,
        },
        "medium": {
            "category": "billing",
            "priority": "high",
            "route_queue": "billing-l2",
            "sla_minutes": 120,
            "escalate": False,
        },
        "hard": {
            "category": "security",
            "priority": "urgent",
            "route_queue": "security-incident-response",
            "sla_minutes": 120,
            "escalate": True,
        },
    }

    target = targets[task]

    if "category" not in decisions:
        return B2BSupportTriageAction(
            action_type=ActionType.CLASSIFY,
            ticket_id=ticket_id,
            payload=B2BSupportPayload(category=target["category"]),
        )

    if "priority" not in decisions:
        return B2BSupportTriageAction(
            action_type=ActionType.SET_PRIORITY,
            ticket_id=ticket_id,
            payload=B2BSupportPayload(priority=target["priority"]),
        )

    if "route_queue" not in decisions or "sla_minutes" not in decisions:
        return B2BSupportTriageAction(
            action_type=ActionType.ROUTE,
            ticket_id=ticket_id,
            payload=B2BSupportPayload(
                route_queue=target["route_queue"],
                sla_minutes=target["sla_minutes"],
                escalate=target["escalate"] if task == "hard" else None,
            ),
        )

    if task == "hard" and "reply_text" not in decisions:
        reply_text = (
            "We have escalated this to our security team. "
            "The incident is escalated and under active investigation. "
            "Please reset your API key immediately; we will share an update within 2 hours."
        )
        return B2BSupportTriageAction(
            action_type=ActionType.DRAFT_REPLY,
            ticket_id=ticket_id,
            payload=B2BSupportPayload(reply_text=reply_text),
        )

    return B2BSupportTriageAction(action_type=ActionType.SUBMIT, ticket_id=None, payload=B2BSupportPayload())


def _coerce_model_action(raw: Dict[str, Any], obs: B2BSupportTriageObservation) -> Optional[B2BSupportTriageAction]:
    if not raw:
        return None

    try:
        action_type = ActionType(raw.get("action_type", ""))
    except Exception:
        return None

    payload = raw.get("payload") or {}
    ticket_id = raw.get("ticket_id")

    if action_type != ActionType.SUBMIT and not ticket_id:
        ticket_id = obs.visible_ticket.ticket_id

    try:
        return B2BSupportTriageAction(
            action_type=action_type,
            ticket_id=ticket_id,
            payload=B2BSupportPayload(
                category=payload.get("category"),
                priority=payload.get("priority"),
                route_queue=payload.get("route_queue"),
                sla_minutes=payload.get("sla_minutes"),
                escalate=payload.get("escalate"),
                reply_text=payload.get("reply_text"),
            ),
        )
    except Exception:
        return None


def _action_to_string(action: B2BSupportTriageAction) -> str:
    payload = action.payload.model_dump(exclude_none=True)
    compact = {"action_type": action.action_type.value, "ticket_id": action.ticket_id, "payload": payload}
    return json.dumps(compact, separators=(",", ":"), ensure_ascii=True)


def _build_user_prompt(step: int, obs: B2BSupportTriageObservation, history: List[str]) -> str:
    return textwrap.dedent(
        f"""
        Step: {step}
        Task: {obs.task_id}
        Ticket ID: {obs.visible_ticket.ticket_id}
        Subject: {obs.visible_ticket.subject}
        Body: {obs.visible_ticket.body}
        Current decisions: {json.dumps(obs.applied_decisions, ensure_ascii=True)}
        Current plan: {json.dumps(obs.current_plan, ensure_ascii=True)}
        Last action error: {obs.last_action_error}
        Progress score: {obs.progress_score:.3f}
        Last 4 history lines: {history[-4:] if history else []}

        Output one JSON action object only.
        """
    ).strip()


def _call_model_action(client: OpenAI, step: int, obs: B2BSupportTriageObservation, history: List[str]) -> Dict[str, Any]:
    prompt = _build_user_prompt(step, obs, history)
    completion = client.chat.completions.create(
        model=MODEL_NAME,
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": prompt},
        ],
        temperature=TEMPERATURE,
        max_tokens=MAX_TOKENS,
        stream=False,
    )
    content = (completion.choices[0].message.content or "").strip()
    return _extract_json_object(content)


def _touch_proxy(client: OpenAI) -> None:
    """Force at least one LiteLLM proxy request even if env execution fails early."""
    try:
        _ = client.models.list()
        return
    except Exception:
        pass

    # Fallback in case /models is unavailable on the proxy deployment.
    try:
        _ = client.chat.completions.create(
            model=MODEL_NAME,
            messages=[{"role": "user", "content": "Reply with JSON: {}"}],
            temperature=0.0,
            max_tokens=4,
            stream=False,
        )
    except Exception:
        pass


async def run_single_task(client: OpenAI, task_name: str, seed: int) -> float:
    rewards: List[float] = []
    history: List[str] = []
    steps_taken = 0
    final_score = 0.0
    success = False

    log_start(task=task_name, env=BENCHMARK, model=MODEL_NAME)

    env: Optional[B2BSupportTriageEnv] = None
    try:
        env = await B2BSupportTriageEnv.from_docker_image(LOCAL_IMAGE_NAME)
        result = await env.reset(task_id=task_name, seed=seed)

        for step in range(1, MAX_STEPS + 1):
            if result.done:
                break

            obs = result.observation
            deterministic = _deterministic_policy(obs)

            model_raw: Dict[str, Any] = {}
            try:
                model_raw = _call_model_action(client, step, obs, history)
            except Exception:
                model_raw = {}

            model_action = _coerce_model_action(model_raw, obs)
            action = model_action if (model_action and model_action.action_type == deterministic.action_type) else deterministic

            result = await env.step(action)
            reward = float(result.reward or 0.0)
            done = bool(result.done)
            error = result.observation.last_action_error

            rewards.append(reward)
            steps_taken = step
            history.append(f"step={step} action={action.action_type.value} reward={reward:.2f}")

            log_step(step=step, action=_action_to_string(action), reward=reward, done=done, error=error)

            if done:
                break

        final_score = float(result.observation.progress_score) if steps_taken > 0 else 0.0
        success = final_score >= SUCCESS_SCORE_THRESHOLD

    except Exception:
        success = False

    finally:
        if env is not None:
            try:
                await env.close()
            except Exception:
                pass

        log_end(success=success, steps=steps_taken, score=final_score, rewards=rewards)

    return final_score


async def main() -> None:
    api_key = os.getenv("API_KEY") or os.getenv("HF_TOKEN")
    if not api_key:
        raise RuntimeError("Missing API key: set API_KEY or HF_TOKEN")

    client = OpenAI(
        base_url=os.environ["API_BASE_URL"],
        api_key=api_key,
    )
    _touch_proxy(client)

    scores: List[float] = []
    for task_name in TASKS:
        score = await run_single_task(client, task_name, TASK_SEEDS[task_name])
        scores.append(score)

    aggregate = sum(scores) / len(scores) if scores else 0.0
    print(f"Baseline aggregate score: {aggregate:.3f}", flush=True)


if __name__ == "__main__":
    asyncio.run(main())