File size: 12,276 Bytes
072bcad
026df2c
 
 
 
072bcad
 
026df2c
 
 
 
 
 
 
072bcad
 
026df2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a013d0
026df2c
 
 
 
9a013d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
026df2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a013d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
026df2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e28cfb
026df2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
---
title: UPI Banking Support Environment
emoji: 🏦
colorFrom: blue
colorTo: indigo
sdk: docker
pinned: false
app_port: 8000
tags:
  - openenv
  - banking
  - upi
  - customer-support
base_path: /web
---

# UPI Banking Support Environment

OpenEnv-style environment for evaluating agents on UPI customer support workflows. The benchmark focuses on realistic banking support decisions rather than generic FAQ matching.

## Motivation

This environment is designed to test whether an agent can behave like a safe and useful support assistant for a UPI payments product such as Paytm, PhonePe, or Google Pay style support flows.

The goal is not only to answer customers correctly, but also to:
- identify the right issue type
- retrieve the right knowledge entry
- escalate fraud or overdue review cases when needed
- avoid unsafe behavior such as asking for PINs or OTPs
- handle multi-turn conversations before closing a case

## Environment Description

The environment uses three tasks with increasing difficulty:
- `easy`: classify a customer issue into the correct support track
- `medium`: choose the right FAQ or escalate when human/manual review is required
- `hard`: run a short multi-turn support conversation with clarification, guidance, and closure

The current support tracks are:
- `payment_failure`
- `refund_delay`
- `fraud_complaint`
- `kyc_account_restriction`
- `upi_pin_or_bank_linking`

The dataset includes:
- 40 banking FAQ entries in [data/knowledge_base.json](data/knowledge_base.json)
- 10 `easy` tickets in [data/tickets/easy.json](data/tickets/easy.json)
- 10 `medium` tickets in [data/tickets/medium.json](data/tickets/medium.json)
- 10 `hard` tickets in [data/tickets/hard.json](data/tickets/hard.json)

## Architecture

The environment is organized around a small set of components that work together during each episode:

```text
Ticket datasets + knowledge base
data/tickets/*.json + data/knowledge_base.json
                |
                v
HelpdeskEnv
server/helpdesk_environment.py
reset() / step() / state()
                |
                +------------------------+
                |                        |
                v                        v
Observation for agent         UserSimulator for hard tasks
models.py                     user_simulator.py
                |                        |
                +-----------+------------+
                            |
                            v
Inference agent loop
inference.py
model call -> action -> normalized action
                            |
                            v
Graders + reward shaping
graders/*.py + reward logic in HelpdeskEnv
correctness / safety / resolution / efficiency / penalties
                            |
                            v
Per-step reward + final episode score
```

How it works in practice:

1. A task split such as `easy`, `medium`, or `hard` selects a ticket from the dataset.
2. [server/helpdesk_environment.py](server/helpdesk_environment.py) builds the observation and tracks hidden gold state.
3. [inference.py](inference.py) sends a compact task-specific prompt to the model and converts the reply into a valid action.
4. The environment applies the action, updates the conversation state, and calls the relevant grading logic.
5. For `hard` tasks, [user_simulator.py](user_simulator.py) produces realistic follow-up user responses.
6. The environment returns a shaped reward, and `inference.py` aggregates the episode score by task type.

## Why JSON Knowledge Base

We use a JSON knowledge base instead of an LLM-backed knowledge source for a few practical reasons:

- It gives a fixed source of truth for FAQ retrieval and evaluation.
- It makes matching the predicted FAQ against the gold FAQ simple and deterministic.
- It is easier to audit, expand, and maintain across categories and tags.
- It avoids adding extra model variance, latency, and cost inside the environment itself.
- It keeps retrieval quality and safe guidance grounded in controlled support content.

LLMs are still useful on top of the JSON knowledge base for response generation or judging nuanced outputs, but the knowledge source itself is intentionally structured and deterministic.

## Action Space

The public inference script and server accept the legacy action names below, which are internally mapped to the compact action model in [models.py](models.py).

| Action | Parameters | Purpose |
|---|---|---|
| `classify` | `category` | Predict the correct support track for an `easy` ticket |
| `lookup_faq` | `faq_id` | Choose the best FAQ entry for `medium` or `hard` |
| `ask_clarification` | `message` | Ask a question to gather missing details in `hard` |
| `reply` | `message` | Provide safe support guidance to the user |
| `escalate` | `message` | Escalate a case that should not be fully handled automatically |
| `resolve_ticket` | none | Close the case when it appears correctly resolved |

Internally, these are normalized to:
- `ask_for_details`
- `take_action`
- `respond_to_user`
- `escalate_case`
- `close_case`

## Observation Space

The model receives an `Observation` object from [models.py](models.py).

| Field | Type | Description |
|---|---|---|
| `case_id` | `str` | Unique identifier for the active ticket |
| `track` | `str` | Task split only: `easy`, `medium`, or `hard` |
| `customer_message` | `str` | Current customer issue text shown to the agent |
| `conversation_history` | `list[dict]` | Prior user/agent turns |
| `known_facts` | `dict` | Agent-visible state such as FAQ set, available categories, and progress flags |
| `required_slots` | `list[str]` | High-level missing information requirements for the episode |
| `available_actions` | `list[str]` | Actions allowed by the environment |
| `turn_number` | `int` | Current turn count |

Important evaluation detail:
- hidden gold labels such as the correct FAQ id and escalation label are not exposed to the model in the observation

## Reward

Rewards are normalized to the range `0.0` to `1.0` in [server/helpdesk_environment.py](server/helpdesk_environment.py).

The final reward is shaped rather than purely binary. It combines:
- `correctness`
- `safety`
- `resolution`
- `efficiency`
- `penalties`

Weighted reward:

```text
0.35 * correctness
+ 0.30 * safety
+ 0.20 * resolution
+ 0.15 * efficiency
+ penalties
```

Examples:
- correct classification gives a strong `easy` reward
- correct FAQ retrieval gives partial progress on `medium`
- correct escalation gives reward on `medium`
- clarification plus guidance plus successful closure raises `hard` reward
- unsafe prompts such as asking for PIN or OTP reduce reward sharply

## Inference Scoring

`inference.py` aggregates episode rewards differently for each task:

- `easy`: single-step scoring. Final score is the reward from the one step taken.
- `medium`: terminal-step scoring. Intermediate rewards are logged, but the final score is the reward from the last step taken.
- `hard`: discounted scoring. Final score is the normalized discounted cumulative reward:

```text
score = sum(reward_t * gamma^t for t, reward_t in enumerate(rewards))
        / sum(gamma^t for t in range(len(rewards)))
```

with `gamma = 0.9`.

## Task Difficulty

| Task | Difficulty | Description | Expected Agent Behavior |
|---|---|---|---|
| `easy` | Low | Single-turn issue classification | Identify the correct banking support track |
| `medium` | Medium | FAQ retrieval or escalation decision | Select the right FAQ or escalate fraud / overdue review cases |
| `hard` | High | Multi-turn support conversation | Ask clarification, guide safely, and close only when appropriate |

## Setup

From the package root:

```bash
cd /path/to/helpdesk_env
uv sync
```

Runtime configuration is read from `.env`.
The environment currently uses:
- `API_BASE_URL` for the provider endpoint
- `MODEL` or `MODEL_NAME` for the selected model
- `API_KEY` as the primary model credential
- `OPENAI_API_KEY` and `GROQ_API_KEY` are also supported as compatibility aliases
- `HF_SPACE_URL` for the deployed Space runtime URL
- `HF_SPACE_TOKEN` for protected Space access when required

## Usage

### Using Docker

```bash
# Build the image from the repository root
docker build -t helpdesk-openenv:latest .

# Run the server
docker run -p 8000:8000 helpdesk-openenv:latest
```

Docker smoke test:

```bash
curl http://127.0.0.1:8000/health

curl http://127.0.0.1:8000/

curl -X POST http://127.0.0.1:8000/reset \
  -H "Content-Type: application/json" \
  -d '{}'

curl -X POST http://127.0.0.1:8000/step \
  -H "Content-Type: application/json" \
  -d '{"action":{"action_type":"classify","category":"payment_failure"}}'

curl http://127.0.0.1:8000/state
```

### Local Development

```bash
# Quick compile check
PYTHONPYCACHEPREFIX=/tmp/pycache python3 -m py_compile \
  inference.py server/app.py server/helpdesk_environment.py

# Run the server locally
uv run server
```

`uv run server` smoke test:

```bash
curl http://127.0.0.1:8000/health

curl http://127.0.0.1:8000/

curl -X POST http://127.0.0.1:8000/reset \
  -H "Content-Type: application/json" \
  -d '{}'

curl -X POST http://127.0.0.1:8000/step \
  -H "Content-Type: application/json" \
  -d '{"action":{"action_type":"classify","category":"payment_failure"}}'

curl http://127.0.0.1:8000/state
```

### Run Inference

```bash
API_BASE_URL=https://api.openai.com/v1 \
API_KEY=$OPENAI_API_KEY \
MODEL=gpt-5 \
TASK_NAME=easy \
python3 inference.py
```

```bash
API_BASE_URL=https://api.groq.com/openai/v1 \
API_KEY=$GROQ_API_KEY \
MODEL=llama-3.3-70b-versatile \
TASK_NAME=easy \
python3 inference.py
```

`inference.py` reads configuration from `.env`.

The script prints structured logs in the required format:

```text
[START] task=easy env=helpdesk_env model=llama-3.3-70b-versatile
[STEP] step=1 action={"action_type":"classify","category":"payment_failure"} reward=1.00 done=true error=null
[END] success=true steps=1 score=1.000 rewards=1.00
```

### Use the Python Client

```python
from helpdesk_env.client import HelpdeskEnvClient

client = HelpdeskEnvClient("http://127.0.0.1:8000")
result = client.reset("easy")
print(result.observation.customer_message)
```

For a deployed HF Space:

```python
from helpdesk_env.client import HelpdeskEnvClient

client = HelpdeskEnvClient.from_env()
print(client.health())
```

### Test the Live HF Space

```bash

curl -X POST "https://freakdivi-helpdesk.hf.space/reset" \
  -H "Content-Type: application/json" \
  -d '{"task_id":"easy"}'

curl -X POST "https://freakdivi-helpdesk.hf.space/step" \
  -H "Content-Type: application/json" \
  -d '{"action":{"action_type":"classify","category":"payment_failure"}}'
```

## Hugging Face Space Deployment

This repo is configured as a Docker-based HF Space through the YAML frontmatter at the top of this README:
- `sdk: docker`
- `app_port: 8000`
- `tags` include `openenv`

Live Space:
- https://huggingface.co/spaces/Freakdivi/helpdesk_env


## Baseline Scores

Latest observed Groq baseline run after removing answer leakage from the observation:

| Model | Easy | Medium | Hard |
|---|---:|---:|---:|
| `llama-3.3-70b-versatile` | 0.98 | 0.67 | 0.53 |

Interpretation:
- `easy` is still quite direct and can be near-perfect for strong LLMs
- `medium` and `hard` are more informative because they require retrieval, escalation judgment, and multi-turn behavior

## Project Structure

```text
helpdesk_env/
β”œβ”€β”€ README.md
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .dockerignore
β”œβ”€β”€ __init__.py
β”œβ”€β”€ client.py
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ knowledge_base.json
β”‚   └── tickets/
β”‚       β”œβ”€β”€ easy.json
β”‚       β”œβ”€β”€ medium.json
β”‚       └── hard.json
β”œβ”€β”€ inference.py
β”œβ”€β”€ models.py
β”œβ”€β”€ openenv.yaml
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ user_simulator.py
β”œβ”€β”€ graders/
β”‚   β”œβ”€β”€ category_grader.py
β”‚   β”œβ”€β”€ faq_grader.py
β”‚   └── resolution_grader.py
└── server/
    β”œβ”€β”€ app.py
    └── helpdesk_environment.py
```

## Notes

[user_simulator.py](user_simulator.py) is intentionally kept. It powers the customer-side replies for the `hard` task, which is what makes the benchmark genuinely multi-turn instead of a static single-response scoring setup.