Spaces:
Sleeping
Sleeping
File size: 18,758 Bytes
f6bbec9 d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce 5f2ce8f d34f0ce | 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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 | ---
title: Customer Support AI Environment
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false
app_port: 5001
---
# Customer Support Email Triage and Response System
A production-ready OpenEnv environment for training reinforcement learning agents to handle real-world email triage and response generation tasks.
## Table of Contents
- [Problem Description](#problem-description)
- [Environment Overview](#environment-overview)
- [Action Space](#action-space)
- [Observation Space](#observation-space)
- [State Space](#state-space)
- [Reward Design](#reward-design)
- [Tasks](#tasks)
- [Setup Instructions](#setup-instructions)
- [Running the Environment](#running-the-environment)
- [Docker Deployment](#docker-deployment)
- [Hugging Face Deployment](#hugging-face-deployment)
- [API Reference](#api-reference)
- [Performance Benchmarks](#performance-benchmarks)
## Problem Description
Modern customer support teams receive hundreds of emails daily requiring triage and response. This environment simulates that core workflow:
**Agent Objective:**
Given an incoming customer support email, the agent must:
1. **Classify** the email into a category (billing, technical, complaint, or spam)
2. **Prioritize** the response urgency (low, medium, or high)
3. **Generate** a professional, contextual response that addresses the customer's concern
**Real-World Relevance:**
- Email volume increases operational costs significantly
- Incorrect categorization leads to delayed responses and customer dissatisfaction
- Priority miscalibration can result in SLA violations
- Response quality directly impacts customer retention and satisfaction
This environment models these pressures with a **Multi-Step Reasoning** workflow, requiring agents to maintain consistency across several decision points. Agents must leverage their memory of previous steps to ensure that the final response and escalation decisions align with the initial classification and prioritization.
### Key Features
- **Realistic Support Workflow**: From initial triage to final response.
- **Multi-Step Decision Making**: Decisions are interconnected and build upon each other.
- **Tool Grounding**: Mandatory usage of knowledge base policies for complex cases.
- **Consistent Reward Signal**: Immediate feedback at each step of the workflow.
## Environment Overview
- **Type:** Multi-step episodic environment
- **Episodes:** 11 tasks of varying difficulty (easy → hard)
- **Episode Length:** Up to 6 steps (Classification, Prioritization, Tool, Strategy, Response, Escalation)
- **Action Space:** Discrete (typed actions per step)
- **Observation Space:** Structured (context-aware observations)
- **Reward Range:** [0.0, 1.0] (Normalized cumulative reward)
## Action Space
**Type:** `EmailAction` (Pydantic model)
**Components:**
```json
{
"action_type": str, // "classify", "prioritize", "decide_strategy", "respond", "escalate", "use_tool"
"content": str, // Text content or structured payload
"tool_action": object // Optional tool parameters
}
```
**Workflow Sequence:**
1. **classify**: Categorize email into billing, tech, complaint, or spam.
2. **prioritize**: Assign urgency levels (low, medium, high).
3. **use_tool**: Query the Knowledge Base for specific support policies (e.g., `POLICY_REFUND_001`).
4. **decide_strategy**: Choose the resolution approach (e.g., `offer_refund`).
5. **respond**: Generate the final customer-facing email.
6. **escalate**: Determine if high-level management intervention is required.
**Constraints:**
- Category must be one of the 4 valid options
- Priority must be one of the 3 valid options
- Response length must be between 20 and 1000 characters
- Response should be contextually appropriate to category and priority
## Observation Space
**Type:** `EmailObservation` (Pydantic model)
**Components:**
```
{
"email_id": str, # Unique identifier (e.g., "email_001")
"subject": str, # Email subject line
"body": str, # Email body content (1-500 words)
"customer_history": str, # Summary of customer relationship
"step_count": int # Current step (0 on reset, 1 after step)
}
```
**Example:**
```json
{
"email_id": "email_002",
"subject": "App performance issue",
"body": "Hi Support Team...",
"customer_history": "Casual user...",
"step_count": 2,
"workflow_step": "strategy_decision",
"previous_decisions": {"classification": "tech", "priority": "medium"}
}
```
## State Space
**Type:** `EmailState` (Pydantic model)
**Components:**
```
{
"episode_id": str, # Unique episode identifier
"step_count": int, # Number of steps taken (0-1)
"done": bool, # Whether episode is complete
"current_email": str, # ID of current email
"total_reward": float # Cumulative episode reward
}
```
## Tools & Knowledge Base Integration
This environment strongly penalizes pure parametric hallucination by mandating real-world tool usage:
- **Knowledge Base Access**: Provides real-time support policies via `search_knowledge_base`.
- **Enforced Usage Rules**: If an agent chooses complex strategies (like `offer_refund`), they **must** utilize the knowledge base and explicitly cite `POLICY_REFUND_001` in the response text. Failing to do so subtracts from the final score.
- **Exploit Prevention**: Tools grant minor micro-rewards (+0.02) for successful usage. However, tool reward frequency is actively clamped and capped to prevent "reward farming" or spamming unneeded tools.
## Reward Design
### Reward Design Principles
- **Step-Based Rewards**: Agents receive positive rewards for correct decisions at each stage (classification, prioritization, etc.).
- **Strategic Penalties**: Severe negative rewards for inconsistent logic or failing to follow mandatory policies.
- **Policy Usage (Tool Grounding)**: Bonuses for efficiently using tools to find correct information.
- **Memory Consistency**: Graders check if the final response matches the previously selected strategy and classification.
**Final Reward = 0.40 × category_score + 0.30 × priority_score + 0.30 × response_score**
### Component 1: Category Correctness (40%)
- **Type:** Binary (0.0 or 1.0)
- **Calculation:** 1.0 if predicted category matches ground truth, 0.0 otherwise
- **Rationale:** Correct classification is foundational; wrong category undermines all other efforts
- **Impact:** Incorrect category immediately caps maximum possible reward at 0.60
### Component 2: Priority Correctness (30%)
- **Type:** Binary (0.0 or 1.0)
- **Calculation:** 1.0 if predicted priority matches ground truth, 0.0 otherwise
- **Rationale:** Wrong priorities lead to SLA violations; high-priority issues delayed = business impact
- **Impact:** Incorrect priority removes 0.30 from maximum possible reward
### Component 3: Response Quality (30%)
- **Type:** Continuous (0.0 to 1.0)
- **Subcomponents:**
**Length Appropriateness (50% of response score):**
- Response too short (<20 words): scaled penalty
- Response 30-150 words: full score (1.0)
- Response >200 words: penalty (up to -0.4)
- Rationale: Professional responses need substance but shouldn't be verbose
**Politeness & Professionalism (30% of response score):**
- Contains politeness markers ("sorry", "apologize", "help", "appreciate"): 1.0
- Without markers: 0.5
- Rationale: Customer satisfaction requires empathy and professionalism
**Category Relevance (20% of response score):**
- Category-specific keywords mentioned: 1.0
- Missing category context: 0.6-0.7
- Examples:
- Billing: mention "refund", "charge", "payment"
- Tech: mention "fix", "troubleshoot", "technical"
- Complaint: mention "apologize", "improve", "feedback"
- Spam: acceptable with brief refusal
### Reward Examples
| Scenario | Category | Priority | Response Length | Politeness | Relevance | Final Reward |
|----------|----------|----------|-----------------|-----------|-----------|--------------|
| All correct, quality high | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | **0.850** |
| Correct category, medium response | 1.0 | 1.0 | 0.8 | 1.0 | 1.0 | **0.804** |
| Wrong category | 0.0 | 1.0 | 1.0 | 1.0 | 1.0 | **0.600** |
| All incorrect | 0.0 | 0.0 | 0.5 | 0.5 | 0.5 | **0.150** |
### Determinism Guarantee
The grader is **100% deterministic** with no random elements:
- No stochastic elements
- Fully reproducible across runs
- Same action on same email always yields same score
- No floating-point precision issues (rounded to 3 decimals)
## Why It's Hard
This environment is designed to challenge frontier models by requiring:
- **Multi-Step Reasoning**: The agent cannot solve the task in a single "shot". It must maintain a logical thread across 5-6 interactions.
- **Memory & Consistency**: A strategy chosen in Step 3 must be correctly implemented in Step 5.
- **Tool Grounding**: Agents must move beyond parametric knowledge and ground their actions in specific provided policies (e.g., citing the correct policy code).
- **Alignment Across Steps**: Initial classification errors propagate throughout the episode, requiring the agent to be precise from the start.
## Tasks
### Task 1: Easy Email (email_001)
**Difficulty:** Easy
**Scenario:** Clear billing issue - straightforward double-charge complaint from established customer
**Email:**
```
Subject: Refund request - duplicate charge
Body:
Hello,
I was charged twice for my subscription this month. The charge of $49.99 appeared
twice in my account on March 15. Please refund the duplicate charge immediately.
Thanks,
John
Customer History: Premium subscriber for 2 years, excellent payment history, first complaint
```
**Ground Truth:**
- Category: `billing`
- Priority: `high`
**Why Easy:**
- Unambiguous intent
- Clear problem statement
- High priority indicated by "immediately"
- Established customer history reduces ambiguity
**Expected Agent Performance:** >0.80 for competent models
---
### Task 2: Medium Email (email_002)
**Difficulty:** Medium
**Scenario:** Technical issue requiring diagnosis and prioritization judgment
**Email:**
```
Subject: App performance issue
Body:
Hi Support Team,
I've been experiencing some issues with the app lately. It seems to crash when I
try to open the settings menu. This happens on both my phone and tablet. I'm running
the latest version. Could you help me investigate this?
Sarah
Customer History: Casual user, 3 months active, 2 previous tech support tickets (both resolved)
```
**Ground Truth:**
- Category: `tech`
- Priority: `medium`
**Why Medium:**
- Technical issue is clear, but requires interpretation
- Priority requires context: established user, reproducible issue (medium), but not critical
- Customer history provides important context for priority assessment
- Response quality particularly important here
**Expected Agent Performance:** 0.65-0.75 for competent models
---
### Task 3: Hard Email (email_003)
**Difficulty:** Hard
**Scenario:** Emotional complaint from high-value enterprise customer with escalation history
**Email:**
```
Subject: Completely disappointed with your service
Body:
This is absolutely frustrating. I submitted a support ticket 5 DAYS ago about my
account being locked, and I haven't heard a single word from anyone. Your customer
service is non-existent. I've recommended your product to friends, but I regret that
now. If this isn't resolved TODAY, I'm leaving a bad review everywhere. I expect
compensation for the inconvenience and lost time.
Regards,
Michael
Customer History: Enterprise customer, $500/month contract, previously submitted 7 complaints
in past 3 months, escalated to management twice
```
**Ground Truth:**
- Category: `complaint`
- Priority: `high`
**Why Hard:**
- Emotional tone requires interpretation
- Category not immediately obvious (could be misclassified as tech)
- Customer history critical: enterprise customer, escalation history
- High priority required to prevent contract loss
- Response quality critical: must show urgency and empathy
**Expected Agent Performance:** 0.45-0.65 for competent models (significant challenge)
---
## Setup Instructions
### Prerequisites
- Python 3.10+
- pip or conda
- Docker (optional, for containerized deployment)
### Local Installation
1. **Clone or extract the project:**
```bash
cd customer_support_env
```
2. **Create virtual environment:**
```bash
python3.10 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```
3. **Install dependencies:**
```bash
pip install -r requirements.txt
```
4. **Install package in development mode:**
```bash
pip install -e .
```
### Requirements
Create `requirements.txt`:
```
fastapi==0.109.0
uvicorn==0.27.0
pydantic==2.6.1
requests==2.31.0
openai==1.13.0
pytest==7.4.4
python-dotenv==1.0.0
```
## Running the Environment
### Step 1: Start the Server
```bash
# Terminal 1: Start FastAPI server
uvicorn server.app:app --host 0.0.0.0 --port 5001 --reload
```
Server will be available at `http://localhost:5001`
API docs available at `http://localhost:5001/docs`
### Step 2: Run Inference
```bash
# Terminal 2: Run inference script
python inference.py
```
**Environment variables (optional):**
```bash
export MODEL_NAME=<your-model>
export API_BASE_URL=http://localhost:11434/v1 # For Ollama/local models
export HF_TOKEN=your_token
```
**Expected Output:**
```
[START] task=email_001 env=customer_support_env model=llama2
[STEP] step=1 action={category=billing,priority=high,response_len=45} reward=0.82 done=true error=null
[END] success=true steps=1 score=0.820 rewards=0.82
```
### Step 3: Direct API Usage
```bash
# Reset environment
curl -X POST http://localhost:5001/reset
# Execute action
curl -X POST http://localhost:5001/step \
-H "Content-Type: application/json" \
-d '{
"category": "billing",
"priority": "high",
"response": "Thank you for reporting this issue. We will process your refund immediately."
}'
# Get state
curl -X GET http://localhost:5001/state
```
## Docker Deployment
### Build Docker Image
```bash
docker build -t customer-support-env:latest ./server
```
### Run Container
```bash
docker run -d \
--name customer-support-env \
-p 5001:5001 \
customer-support-env:latest
```
### Verify Container
```bash
docker logs customer-support-env
curl http://localhost:5001/health
```
### Stop Container
```bash
docker stop customer-support-env
docker rm customer-support-env
```
## Hugging Face Deployment
### Step 1: Create Space
1. Go to https://huggingface.co/new-space
2. Select **Docker** runtime
3. Create space
### Step 2: Upload Files
Push to the space repository:
```bash
git clone https://huggingface.co/spaces/<your-username>/customer-support-env
cd customer-support-env
# Copy files
cp -r /path/to/customer_support_env/* .
# Commit and push
git add .
git commit -m "Initial commit"
git push
```
### Step 3: Create Dockerfile for HF
`Dockerfile` for HF Spaces:
```dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5001
CMD ["uvicorn", "server.app:app", "--host", "0.0.0.0", "--port", "5001"]
```
### Step 4: Configure Secrets (if needed)
In HF Spaces settings, add:
- `HF_TOKEN`: Your Hugging Face API key
- `MODEL_NAME`: Model identifier
## API Reference
### Health Check
```http
GET /health
Response:
{
"status": "healthy"
}
```
### Get Environment Info
```http
GET /info
Response:
{
"name": "customer_support_env",
"version": "1.0.0",
"action_space": "EmailAction",
"observation_space": "EmailObservation",
"reward_range": [0.0, 1.0],
"tasks": 3,
"episode_type": "single-step"
}
```
### Reset Environment
```http
POST /reset
Response:
{
"observation": {
"email_id": "email_001",
"subject": "...",
"body": "...",
"customer_history": "...",
"step_count": 0
},
"info": {
"episode_id": "episode_1_xyz123",
"difficulty": "easy",
"email_id": "email_001"
}
}
```
### Execute Step
```http
POST /step
Request Body:
{
"category": "billing",
"priority": "high",
"response": "Your response text here"
}
Response:
{
"observation": {...},
"reward": 0.82,
"done": true,
"info": {
"category_score": 1.0,
"priority_score": 1.0,
"response_score": 0.6,
"final_reward": 0.82,
"ground_truth_category": "billing",
"predicted_category": "billing"
}
}
```
### Get State
```http
GET /state
Response:
{
"episode_id": "episode_1_xyz123",
"step_count": 1,
"done": true,
"current_email": "email_001",
"total_reward": 0.82
}
```
### Get Statistics
```http
GET /stats
Response:
{
"episode_count": 5,
"remaining_tasks": 0,
"current_task_id": "email_003"
}
```
## Performance Benchmarks
### Baseline Performance (GPT-3.5-turbo)
| Task | Category Acc | Priority Acc | Response Qual | Final Reward |
|------|-------------|-------------|---------------|--------------|
| Easy | 100% | 95% | 0.85 | **0.900** |
| Medium | 98% | 85% | 0.78 | **0.803** |
| Hard | 75% | 65% | 0.72 | **0.701** |
| **Average** | **91%** | **82%** | **0.78** | **0.801** |
### Resource Requirements
- **RAM:** 1GB minimum, 4GB recommended
- **CPU:** 1 vCPU minimum, 2+ recommended
- **Storage:** 500MB
- **Network:** Minimal (local inference) to high (cloud models)
- **Inference Time:** <5 seconds per email (local), <30 seconds (cloud)
### Scalability
- **Vertical:** Supports single-GPU deployment without modification
- **Horizontal:** Can replicate server for parallel evaluation
- **Batch:** Modify server for batch processing (future enhancement)
## Troubleshooting
### Issue: Connection refused (port 5001)
**Solution:**
```bash
# Check if port is in use
netstat -an | grep 5001
# Use different port
uvicorn server.app:app --port 5002
```
### Issue: Module import errors
**Solution:**
```bash
# Ensure environment is activated
source venv/bin/activate # Unix/Mac
venv\Scripts\activate # Windows
# Reinstall requirements
pip install -r requirements.txt --force-reinstall
```
### Issue: Slow inference
**Solution:**
- Use local model (Ollama) instead of cloud API
- Reduce model size for evaluation
- Increase timeout in client
## Citation
If you use this environment, please cite:
```
@software{customer_support_env,
title={Customer Support Email Triage and Response System - OpenEnv Environment},
version={1.0.0},
year={2024}
}
```
## License
This project is provided as-is for research and educational purposes.
---
**Last Updated:** December 2024
**Status:** Production-Ready
**Support:** For issues or questions, please refer to the API reference or contact the development team.
|