SupportFlowAI / README.md
Mmanikandan's picture
readme updated
f6bbec9
---
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.