# EmailTriage OpenEnv ๐Ÿ† > **Real-world email support triage benchmark for AI agents.** > Agents learn to classify, prioritize, draft replies, and escalate critical issues โ€” mirroring actual $10B/year customer support workflows. [![HF Space](https://img.shields.io/badge/๐Ÿค—%20HuggingFace-Space-yellow)](https://huggingface.co/spaces) [![OpenEnv](https://img.shields.io/badge/OpenEnv-Compatible-blue)](https://openenv.dev) [![Python 3.12](https://img.shields.io/badge/Python-3.12-green)](https://python.org) [![Docker](https://img.shields.io/badge/Docker-Ready-blue)](https://docker.com) --- ## ๐ŸŽฏ Why Email Triage? Every support team processes hundreds of emails daily โ€” spam, urgent bug reports, feature requests, billing issues. Training AI agents on this task develops: - **Classification** (spam vs. legit, priority levels) - **Judgment** (when to escalate a P0 production outage vs. a feature request) - **Communication** (drafting concise, relevant replies) - **Hierarchy** (label before draft, escalate before reply) No existing OpenEnv benchmark covers this domain. This fills a real gap. --- ## ๐Ÿ—‚๏ธ Tasks | Task | Emails | Description | Key Skill | |------|--------|-------------|-----------| | **easy** | 5 | Binary classify: spam or legit (low/med/high) | Pattern recognition | | **medium** | 10 | Prioritize mixed inbox + delete spam | Urgency judgment | | **hard** | 15 | Full triage: classify + draft replies + escalate P0s | Compositionality | ### Grader Weights | Metric | Easy | Medium | Hard | |--------|------|--------|------| | Label Accuracy | 0.6 | 0.5 | 0.3 | | Spam Recall | 0.1 | 0.2 | 0.1 | | Reply Relevance | 0.2 | 0.2 | 0.2 | | Escalation Recall | โ€” | โ€” | 0.3 | | Inbox Cleared | 0.1 | 0.1 | 0.1 | --- ## ๐Ÿ“Š Baseline Scores | Agent | Easy | Medium | Hard | |-------|------|--------|------| | Rule-Based (keyword) | 0.75 | 0.55 | 0.40 | | GPT-4o-mini | 0.82 | 0.62 | 0.48 | | Human (simulated) | 0.95 | 0.91 | 0.87 | --- ## ๐Ÿš€ Quick Start ### Option 1: Docker (Recommended) ```bash docker build -t email-triage . docker run -p 7860:7860 email-triage ``` ### Option 2: Local Dev ```bash python -m venv .venv # Windows: .venv\Scripts\activate # Mac/Linux: source .venv/bin/activate pip install -r requirements.txt uvicorn server.app:app --host 0.0.0.0 --port 7860 --reload ``` ### Option 3: HuggingFace Space ```bash huggingface-cli login huggingface-cli repo create email-triage --type space --sdk docker git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/email-triage git push hf main ``` --- ## ๐Ÿ”Œ API Reference Base URL: `http://localhost:7860` Interactive docs: `http://localhost:7860/docs` ### `GET /tasks` List all tasks with descriptions and grader weights. ```bash curl http://localhost:7860/tasks ``` ### `POST /reset` Reset environment with a specific task. ```bash curl -X POST http://localhost:7860/reset \ -H "Content-Type: application/json" \ -d '{"task": "easy"}' ``` **Response:** ```json { "observation": { "current_emails": [ {"id": "e1", "subject": "WIN $1000 NOW!!!", "body": "...", "sender": "...", "labeled": false} ], "stats": {"total": 5, "unread": 5, "labeled": 0, "deleted": 0, "escalated": 0, "drafts": 0}, "history": [], "done": false, "step": 0 } } ``` ### `POST /step` Take one action in the environment. ```bash # Label an email curl -X POST http://localhost:7860/step \ -H "Content-Type: application/json" \ -d '{"action": "{\"type\": \"label\", \"email_id\": \"e1\", \"value\": \"spam\"}"}' # Delete spam curl -X POST http://localhost:7860/step \ -H "Content-Type: application/json" \ -d '{"action": "{\"type\": \"delete\", \"email_id\": \"e1\"}"}' # Draft a reply curl -X POST http://localhost:7860/step \ -H "Content-Type: application/json" \ -d '{"action": "{\"type\": \"draft\", \"email_id\": \"e2\", \"value\": \"We are investigating your issue immediately.\"}"}' # Escalate a P0 (hard task) curl -X POST http://localhost:7860/step \ -H "Content-Type: application/json" \ -d '{"action": "{\"type\": \"escalate\", \"email_id\": \"h1\"}"}' ``` **Response:** ```json { "observation": {...}, "reward": 0.3, "done": false, "info": {"step": 1, "task": "easy", "action_applied": "delete"} } ``` ### `POST /grader` Score the current episode (0.0 โ€“ 1.0). ```bash curl -X POST http://localhost:7860/grader -H "Content-Type: application/json" -d '{}' ``` **Response:** ```json { "score": 0.74, "breakdown": { "label_accuracy": 0.8, "spam_recall": 1.0, "reply_relevance": 0.65, "escalation_recall": 0.0, "inbox_cleared": 1.0 }, "details": {"task": "easy", "steps_used": "5", ...} } ``` ### `GET /state` Full internal state for debugging. ### `GET /health` Health check โ€” returns `{"status": "ok"}`. --- ## ๐Ÿค– Action Space ```json { "type": "label | draft | delete | escalate | archive", "email_id": "string", "value": "string (label value or reply text, optional)" } ``` ### Action Details | Action | `value` | Effect | Reward | |--------|---------|--------|--------| | `label` | `spam\|low\|med\|high\|escalate` | Tag email | +0.2 correct, -0.1 wrong | | `delete` | โ€” | Remove from inbox | +0.3 if spam, **-0.4** if legit | | `draft` | reply text | Write reply | +0.05 to +0.35 (keyword match) | | `escalate` | โ€” | Mark as P0 | **+0.4** if P0, -0.2 if not | | `archive` | โ€” | Archive email | +0.05/+0.1, -0.1 for high | --- ## ๐Ÿง  Escalation Mechanic (Hard Task) The `escalate` action is unique to production-ready support workflows. Emails requiring escalation are P0 incidents: - ๐Ÿ”ด Production database corruption - ๐Ÿ”ด Security vulnerability (SQL injection, etc.) - ๐Ÿ”ด All-user authentication failures Escalating correctly gives the highest reward (+0.4). Missing an escalation significantly hurts the hard task score (escalation recall weight: 0.3). --- ## ๐Ÿงช Running Tests ```bash pytest tests/ -v ``` Expected: **All 40+ tests pass**. --- ## ๐Ÿƒ Running the Baseline ```bash # Rule-based (no API key needed) python baseline.py --mode rule # LLM-based (requires OpenAI API key) OPENAI_API_KEY=sk-... python baseline.py --mode llm # Both modes OPENAI_API_KEY=sk-... python baseline.py --mode both ``` --- ## ๐Ÿ“ Project Structure ``` email-triage-env/ โ”œโ”€โ”€ openenv.yaml # OpenEnv manifest (tasks, schemas, metadata) โ”œโ”€โ”€ models.py # Pydantic schemas (Email, Action, Observation, State) โ”œโ”€โ”€ environment.py # Core env logic (step, reset, grader, reward shaping) โ”œโ”€โ”€ baseline.py # Rule-based + GPT-4o-mini baseline agents โ”œโ”€โ”€ requirements.txt # Pinned dependencies โ”œโ”€โ”€ pyproject.toml # Package metadata โ”œโ”€โ”€ Dockerfile # HF Spaces compatible (port 7860) โ”œโ”€โ”€ .gitignore โ”œโ”€โ”€ server/ โ”‚ โ”œโ”€โ”€ __init__.py โ”‚ โ””โ”€โ”€ app.py # FastAPI server (all endpoints) โ”œโ”€โ”€ data/ โ”‚ โ””โ”€โ”€ inboxes.json # Task emails + ground truth (easy/medium/hard) โ”œโ”€โ”€ tests/ โ”‚ โ”œโ”€โ”€ __init__.py โ”‚ โ””โ”€โ”€ test_env.py # 40+ TDD tests โ””โ”€โ”€ README.md ``` --- ## ๐ŸŒŸ Key Design Decisions 1. **No external LLM in grader** โ€” All scoring is deterministic (keyword match, exact label comparison). Reproducible, fair, fast. 2. **Shaped rewards** โ€” Dense reward signal every step (progress bonus + action quality). Agents learn incrementally. 3. **Escalation mechanic** โ€” Novel action not found in any existing benchmark. Models judgment under pressure. 4. **Seeded randomness** โ€” `random.seed(42)` in reset guarantees reproducible episodes. 5. **Hard penalties for irreversible mistakes** โ€” Deleting a legit email (โˆ’0.4) teaches caution. --- ## ๐Ÿ“„ License MIT License โ€” free to use, modify, and distribute.