Spaces:
Sleeping
Sleeping
fixed api endpoints
Browse files- README.md +1 -240
- __pycache__/dataset.cpython-312.pyc +0 -0
- __pycache__/environment.cpython-312.pyc +0 -0
- __pycache__/graders.cpython-312.pyc +0 -0
- __pycache__/inference.cpython-312.pyc +0 -0
- __pycache__/models.cpython-312.pyc +0 -0
- __pycache__/server.cpython-312.pyc +0 -0
- inference.py +67 -188
README.md
CHANGED
|
@@ -1,246 +1,7 @@
|
|
| 1 |
-
|
| 2 |
---
|
| 3 |
title: OpenEnv Email Triage
|
| 4 |
-
emoji:
|
| 5 |
sdk: docker
|
| 6 |
app_port: 7860
|
| 7 |
---
|
| 8 |
# OpenEnv Email Triage
|
| 9 |
-
|
| 10 |
-
AI-powered email triage system using FastAPI and Docker.
|
| 11 |
-
<<<<<<< HEAD
|
| 12 |
-
# π§ OpenEnv Email Triage Environment
|
| 13 |
-
|
| 14 |
-
[](https://openenv.dev)
|
| 15 |
-
[](LICENSE)
|
| 16 |
-
[](https://python.org)
|
| 17 |
-
[](https://fastapi.tiangolo.com)
|
| 18 |
-
|
| 19 |
-
A real-world **email triage environment** for AI agents. Agents must classify, prioritize, and respond to business emails β simulating one of the most common daily tasks for knowledge workers. Built to the full OpenEnv spec with three progressively harder tasks.
|
| 20 |
-
|
| 21 |
-
---
|
| 22 |
-
|
| 23 |
-
## Why Email Triage?
|
| 24 |
-
|
| 25 |
-
Email overload is a massive real-world problem. Studies show knowledge workers spend 28% of their day on email. The ability to triage β classify urgency, categorize, decide on actions, and draft replies β is a core productivity skill that AI agents can meaningfully assist with. This environment gives agents a realistic, varied inbox with clear success metrics.
|
| 26 |
-
|
| 27 |
-
---
|
| 28 |
-
|
| 29 |
-
## Environment Description
|
| 30 |
-
|
| 31 |
-
The agent receives emails one at a time from a simulated inbox. For each email, it must:
|
| 32 |
-
|
| 33 |
-
1. **Classify urgency** β 5 levels (critical β ignore)
|
| 34 |
-
2. **Assign category** β 10 types (complaint, spam, finance, HR, legal, etc.)
|
| 35 |
-
3. **Recommend action** β 6 options (reply, forward, archive, delete, escalate, flag)
|
| 36 |
-
4. **Draft a reply** *(Hard task only)* β contextually appropriate response
|
| 37 |
-
|
| 38 |
-
Rewards are given per-step with partial credit, not just at the end of an episode.
|
| 39 |
-
|
| 40 |
-
---
|
| 41 |
-
|
| 42 |
-
## Observation Space
|
| 43 |
-
|
| 44 |
-
```json
|
| 45 |
-
{
|
| 46 |
-
"current_email": {
|
| 47 |
-
"id": "string",
|
| 48 |
-
"subject": "string",
|
| 49 |
-
"sender": "string",
|
| 50 |
-
"sender_domain": "string",
|
| 51 |
-
"body": "string",
|
| 52 |
-
"timestamp": "ISO8601 string",
|
| 53 |
-
"has_attachment": "boolean",
|
| 54 |
-
"thread_length": "integer"
|
| 55 |
-
},
|
| 56 |
-
"emails_processed": "integer",
|
| 57 |
-
"emails_remaining": "integer",
|
| 58 |
-
"score_so_far": "float [0.0β1.0]",
|
| 59 |
-
"task_id": "string",
|
| 60 |
-
"done": "boolean",
|
| 61 |
-
"message": "string"
|
| 62 |
-
}
|
| 63 |
-
```
|
| 64 |
-
|
| 65 |
-
---
|
| 66 |
-
|
| 67 |
-
## Action Space
|
| 68 |
-
|
| 69 |
-
```json
|
| 70 |
-
{
|
| 71 |
-
"urgency": "critical | high | medium | low | ignore",
|
| 72 |
-
"category": "customer_complaint | sales_inquiry | internal_ops | hr | finance | spam | support | legal | pr | other",
|
| 73 |
-
"action": "reply | forward | archive | delete | escalate | flag_review",
|
| 74 |
-
"draft_reply": "string (required when action=reply)",
|
| 75 |
-
"forward_to": "string (required when action=forward or escalate)",
|
| 76 |
-
"reasoning": "string (optional, not scored)"
|
| 77 |
-
}
|
| 78 |
-
```
|
| 79 |
-
|
| 80 |
-
---
|
| 81 |
-
|
| 82 |
-
## Tasks
|
| 83 |
-
|
| 84 |
-
### Task 1 β Binary Spam Detection (Easy)
|
| 85 |
-
- **Emails**: 10
|
| 86 |
-
- **Objective**: Identify spam vs legitimate email; assign basic urgency
|
| 87 |
-
- **Reward**: Full (1.0) for correct spam detection; proportional for legit emails
|
| 88 |
-
- **Success Threshold**: 0.75
|
| 89 |
-
- **Baseline Score**: ~0.82
|
| 90 |
-
|
| 91 |
-
### Task 2 β Priority Inbox Triage (Medium)
|
| 92 |
-
- **Emails**: 15
|
| 93 |
-
- **Objective**: Full 3-way classification (urgency Γ category Γ action)
|
| 94 |
-
- **Reward**: Weighted sum with partial credit for close classifications
|
| 95 |
-
- **Success Threshold**: 0.65
|
| 96 |
-
- **Baseline Score**: ~0.67
|
| 97 |
-
|
| 98 |
-
### Task 3 β Full Triage + Response Drafting (Hard)
|
| 99 |
-
- **Emails**: 20
|
| 100 |
-
- **Objective**: Complete triage + draft contextually appropriate replies
|
| 101 |
-
- **Reward**: 50% triage quality + 50% reply quality (keyword coverage, tone, length)
|
| 102 |
-
- **Success Threshold**: 0.55
|
| 103 |
-
- **Baseline Score**: ~0.54
|
| 104 |
-
|
| 105 |
-
---
|
| 106 |
-
|
| 107 |
-
## Reward Function
|
| 108 |
-
|
| 109 |
-
Rewards are **per-step** (not end-of-episode) and provide partial progress signals:
|
| 110 |
-
|
| 111 |
-
```
|
| 112 |
-
reward = urgency_score Γ 0.30
|
| 113 |
-
+ category_score Γ 0.40
|
| 114 |
-
+ action_score Γ 0.30
|
| 115 |
-
β penalty
|
| 116 |
-
```
|
| 117 |
-
|
| 118 |
-
**Partial credit:**
|
| 119 |
-
- Urgency: exact=1.0, off-by-one=0.5, off-by-two=0.2
|
| 120 |
-
- Category: exact=1.0, semantically related=0.4, unrelated=0.0
|
| 121 |
-
- Action: exact=1.0, acceptable alternative=0.5
|
| 122 |
-
|
| 123 |
-
**Penalties:**
|
| 124 |
-
- Missing critical email: β0.25 to β0.30
|
| 125 |
-
- Marking legit email as spam: β0.15 to β0.30
|
| 126 |
-
|
| 127 |
-
**Reply quality (Hard task):** graded on non-empty content, length β₯100 chars, keyword coverage, and professional tone markers.
|
| 128 |
-
|
| 129 |
-
---
|
| 130 |
-
|
| 131 |
-
## API Endpoints
|
| 132 |
-
|
| 133 |
-
| Method | Path | Description |
|
| 134 |
-
|--------|------|-------------|
|
| 135 |
-
| `POST` | `/reset` | Start episode: `{"task_id": "task_easy"}` |
|
| 136 |
-
| `POST` | `/step` | Submit action, get reward |
|
| 137 |
-
| `GET` | `/state` | Full internal state |
|
| 138 |
-
| `GET` | `/health` | Health check (returns 200) |
|
| 139 |
-
| `GET` | `/tasks` | List tasks |
|
| 140 |
-
| `GET` | `/action_space` | Valid action enum values |
|
| 141 |
-
| `GET` | `/docs` | Swagger UI |
|
| 142 |
-
|
| 143 |
-
---
|
| 144 |
-
|
| 145 |
-
## Setup & Usage
|
| 146 |
-
|
| 147 |
-
### Docker (recommended)
|
| 148 |
-
|
| 149 |
-
```bash
|
| 150 |
-
# Build
|
| 151 |
-
docker build -t openenv-email-triage .
|
| 152 |
-
|
| 153 |
-
# Run
|
| 154 |
-
docker run -p 7860:7860 openenv-email-triage
|
| 155 |
-
|
| 156 |
-
# Test
|
| 157 |
-
curl http://localhost:7860/health
|
| 158 |
-
curl -X POST http://localhost:7860/reset -H "Content-Type: application/json" -d '{"task_id":"task_easy"}'
|
| 159 |
-
```
|
| 160 |
-
|
| 161 |
-
### Local Python
|
| 162 |
-
|
| 163 |
-
```bash
|
| 164 |
-
pip install -r requirements.txt
|
| 165 |
-
uvicorn server:app --host 0.0.0.0 --port 7860 --reload
|
| 166 |
-
```
|
| 167 |
-
|
| 168 |
-
### Run Baseline Inference
|
| 169 |
-
|
| 170 |
-
```bash
|
| 171 |
-
export API_BASE_URL="https://api.openai.com/v1"
|
| 172 |
-
export MODEL_NAME="gpt-4o-mini"
|
| 173 |
-
export HF_TOKEN="your-api-key"
|
| 174 |
-
|
| 175 |
-
python inference.py
|
| 176 |
-
```
|
| 177 |
-
|
| 178 |
-
### Run Validation
|
| 179 |
-
|
| 180 |
-
```bash
|
| 181 |
-
python validate.py
|
| 182 |
-
```
|
| 183 |
-
|
| 184 |
-
---
|
| 185 |
-
|
| 186 |
-
## Baseline Scores
|
| 187 |
-
|
| 188 |
-
Scores produced by `gpt-4o-mini` (temperature=0.1):
|
| 189 |
-
|
| 190 |
-
| Task | Score | Threshold | Pass |
|
| 191 |
-
|------|-------|-----------|------|
|
| 192 |
-
| task_easy | 0.82 | 0.75 | β
|
|
| 193 |
-
| task_medium | 0.67 | 0.65 | β
|
|
| 194 |
-
| task_hard | 0.54 | 0.55 | β (close) |
|
| 195 |
-
| **Overall** | **0.68** | β | β |
|
| 196 |
-
|
| 197 |
-
---
|
| 198 |
-
|
| 199 |
-
## Environment Variables
|
| 200 |
-
|
| 201 |
-
| Variable | Description |
|
| 202 |
-
|----------|-------------|
|
| 203 |
-
| `API_BASE_URL` | LLM API endpoint (OpenAI-compatible) |
|
| 204 |
-
| `MODEL_NAME` | Model identifier for inference |
|
| 205 |
-
| `HF_TOKEN` | Hugging Face / API key |
|
| 206 |
-
|
| 207 |
-
---
|
| 208 |
-
|
| 209 |
-
## Project Structure
|
| 210 |
-
|
| 211 |
-
```
|
| 212 |
-
openenv-email-triage/
|
| 213 |
-
βββ openenv.yaml # OpenEnv spec metadata
|
| 214 |
-
βββ models.py # Pydantic typed models (Observation, Action, Reward)
|
| 215 |
-
βββ dataset.py # Email dataset with ground truth labels
|
| 216 |
-
βββ graders.py # Deterministic graders for all 3 tasks
|
| 217 |
-
βββ environment.py # EmailTriageEnv with step()/reset()/state()
|
| 218 |
-
βββ server.py # FastAPI server exposing REST endpoints
|
| 219 |
-
βββ inference.py # Baseline inference script (OpenAI client)
|
| 220 |
-
βββ validate.py # Pre-submission validation script
|
| 221 |
-
βββ app.py # HF Spaces entry point
|
| 222 |
-
βββ Dockerfile # Container definition
|
| 223 |
-
βββ requirements.txt # Python dependencies
|
| 224 |
-
βββ static/
|
| 225 |
-
β βββ index.html # Interactive environment UI
|
| 226 |
-
βββ README.md # This file
|
| 227 |
-
```
|
| 228 |
-
|
| 229 |
-
---
|
| 230 |
-
|
| 231 |
-
## License
|
| 232 |
-
|
| 233 |
-
MIT Β© 2024 OpenEnv Hackathon Team
|
| 234 |
-
=======
|
| 235 |
-
---
|
| 236 |
-
title: Openenv Email Triage
|
| 237 |
-
emoji: π
|
| 238 |
-
colorFrom: purple
|
| 239 |
-
colorTo: indigo
|
| 240 |
-
sdk: docker
|
| 241 |
-
pinned: false
|
| 242 |
-
short_description: I built an AI-powered email triage system.
|
| 243 |
-
---
|
| 244 |
-
|
| 245 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 246 |
-
>>>>>>> cd1f31d4e18bc9b31dacc3102c4f119c99622835
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: OpenEnv Email Triage
|
| 3 |
+
emoji: "π€"
|
| 4 |
sdk: docker
|
| 5 |
app_port: 7860
|
| 6 |
---
|
| 7 |
# OpenEnv Email Triage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__pycache__/dataset.cpython-312.pyc
ADDED
|
Binary file (10.1 kB). View file
|
|
|
__pycache__/environment.cpython-312.pyc
ADDED
|
Binary file (7.14 kB). View file
|
|
|
__pycache__/graders.cpython-312.pyc
ADDED
|
Binary file (10.1 kB). View file
|
|
|
__pycache__/inference.cpython-312.pyc
ADDED
|
Binary file (4.11 kB). View file
|
|
|
__pycache__/models.cpython-312.pyc
ADDED
|
Binary file (6.72 kB). View file
|
|
|
__pycache__/server.cpython-312.pyc
ADDED
|
Binary file (7.28 kB). View file
|
|
|
inference.py
CHANGED
|
@@ -1,230 +1,109 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
inference.py β Baseline inference script for OpenEnv Email Triage
|
| 4 |
-
Uses OpenAI client (via API_BASE_URL + MODEL_NAME) to run an LLM agent
|
| 5 |
-
against all 3 tasks and produces reproducible scores.
|
| 6 |
-
|
| 7 |
-
Required env vars:
|
| 8 |
-
API_BASE_URL β LLM API base URL (OpenAI-compatible)
|
| 9 |
-
MODEL_NAME β Model identifier
|
| 10 |
-
HF_TOKEN β Hugging Face / API key (used as openai api_key)
|
| 11 |
|
| 12 |
-
Stdout format: strictly [START], [STEP], [END] as specified.
|
| 13 |
-
"""
|
| 14 |
import os
|
| 15 |
import sys
|
| 16 |
import json
|
| 17 |
-
import
|
| 18 |
-
import logging
|
| 19 |
-
from typing import Dict, Any, Optional
|
| 20 |
|
|
|
|
|
|
|
| 21 |
from openai import OpenAI
|
| 22 |
|
| 23 |
-
# βββ
|
|
|
|
| 24 |
|
|
|
|
| 25 |
API_BASE_URL = os.environ.get("API_BASE_URL", "https://api.openai.com/v1")
|
| 26 |
-
MODEL_NAME = os.environ.get("MODEL_NAME",
|
| 27 |
-
HF_TOKEN = os.environ.get("HF_TOKEN",
|
| 28 |
|
|
|
|
| 29 |
if not HF_TOKEN:
|
| 30 |
-
|
| 31 |
-
sys.exit(1)
|
| 32 |
|
| 33 |
client = OpenAI(api_key=HF_TOKEN, base_url=API_BASE_URL)
|
| 34 |
|
| 35 |
-
# βββ
|
| 36 |
sys.path.insert(0, os.path.dirname(__file__))
|
| 37 |
-
from environment import EmailTriageEnv
|
| 38 |
-
from models import Action, UrgencyLevel, EmailCategory, EmailAction
|
| 39 |
-
|
| 40 |
-
logging.basicConfig(level=logging.WARNING)
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
-
|
| 54 |
-
-
|
| 55 |
-
-
|
| 56 |
-
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
- Mark spam/phishing as urgency=ignore, category=spam, action=delete
|
| 60 |
-
- Mark production outages, legal issues, press inquiries as urgency=critical
|
| 61 |
-
- Always draft a reply when action=reply (aim for 100+ words, professional tone)
|
| 62 |
-
- For complaints: acknowledge, apologize, provide resolution path
|
| 63 |
-
- For enterprise sales: express enthusiasm, offer to connect with sales team
|
| 64 |
"""
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
Email to triage:
|
| 71 |
-
Subject: {email_data.get('subject', '')}
|
| 72 |
-
From: {email_data.get('sender', '')} ({email_data.get('sender_domain', '')})
|
| 73 |
-
Date: {email_data.get('timestamp', '')}
|
| 74 |
-
Has Attachment: {email_data.get('has_attachment', False)}
|
| 75 |
-
Thread Length: {email_data.get('thread_length', 1)}
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
|
|
|
| 81 |
|
|
|
|
|
|
|
| 82 |
try:
|
| 83 |
response = client.chat.completions.create(
|
| 84 |
model=MODEL_NAME,
|
| 85 |
messages=[
|
| 86 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 87 |
-
{"role": "user",
|
| 88 |
],
|
| 89 |
temperature=0.1,
|
| 90 |
-
max_tokens=600,
|
| 91 |
-
response_format={"type": "json_object"},
|
| 92 |
)
|
|
|
|
| 93 |
raw = response.choices[0].message.content or "{}"
|
| 94 |
return json.loads(raw)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
return {
|
| 98 |
-
"urgency": "medium", "category": "other", "action": "archive",
|
| 99 |
-
"draft_reply": None, "forward_to": None, "reasoning": "parse error fallback"
|
| 100 |
-
}
|
| 101 |
-
except Exception as e:
|
| 102 |
return {
|
| 103 |
-
"urgency": "medium",
|
| 104 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
}
|
| 106 |
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
return
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
def run_task(task_id: str) -> Dict[str, Any]:
|
| 115 |
-
"""Run full episode for one task. Returns result dict."""
|
| 116 |
-
env = EmailTriageEnv()
|
| 117 |
-
obs = env.reset(task_id=task_id)
|
| 118 |
|
| 119 |
-
step_results = []
|
| 120 |
-
step_num = 0
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
|
| 126 |
-
|
| 127 |
-
decision = agent_decide(email_data, task_id)
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
action = clamp_enum(str(decision.get("action", "archive")), EmailAction)
|
| 133 |
-
|
| 134 |
-
act = Action(
|
| 135 |
-
urgency=UrgencyLevel(urgency),
|
| 136 |
-
category=EmailCategory(category),
|
| 137 |
-
action=EmailAction(action),
|
| 138 |
-
draft_reply=decision.get("draft_reply"),
|
| 139 |
-
forward_to=decision.get("forward_to"),
|
| 140 |
-
reasoning=decision.get("reasoning"),
|
| 141 |
-
)
|
| 142 |
|
| 143 |
-
# Step environment
|
| 144 |
-
result = env.step(act)
|
| 145 |
-
reward_val = result.reward.value
|
| 146 |
-
step_results.append(reward_val)
|
| 147 |
-
|
| 148 |
-
# ββ [STEP] log βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 149 |
-
print(json.dumps({
|
| 150 |
-
"type": "[STEP]",
|
| 151 |
-
"task_id": task_id,
|
| 152 |
-
"step": step_num,
|
| 153 |
-
"email_id": email_data.get("id", ""),
|
| 154 |
-
"subject": email_data.get("subject", "")[:60],
|
| 155 |
-
"agent_action": {
|
| 156 |
-
"urgency": urgency,
|
| 157 |
-
"category": category,
|
| 158 |
-
"action": action,
|
| 159 |
-
},
|
| 160 |
-
"reward": round(reward_val, 4),
|
| 161 |
-
"feedback": result.reward.feedback[:120],
|
| 162 |
-
"done": result.done,
|
| 163 |
-
}))
|
| 164 |
-
|
| 165 |
-
obs = result.observation
|
| 166 |
-
|
| 167 |
-
final_score = round(sum(step_results) / len(step_results), 4) if step_results else 0.0
|
| 168 |
return {
|
| 169 |
-
"
|
| 170 |
-
"
|
| 171 |
-
"
|
| 172 |
-
"
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
# βββ Main βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 177 |
-
|
| 178 |
-
def main():
|
| 179 |
-
tasks = ["task_easy", "task_medium", "task_hard"]
|
| 180 |
-
all_results = {}
|
| 181 |
-
|
| 182 |
-
# ββ [START] log ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 183 |
-
print(json.dumps({
|
| 184 |
-
"type": "[START]",
|
| 185 |
-
"env": "email-triage-env",
|
| 186 |
-
"version": "1.0.0",
|
| 187 |
-
"model": MODEL_NAME,
|
| 188 |
-
"api_base": API_BASE_URL,
|
| 189 |
-
"tasks": tasks,
|
| 190 |
-
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
| 191 |
-
}))
|
| 192 |
-
|
| 193 |
-
for task_id in tasks:
|
| 194 |
-
print(json.dumps({"type": "[TASK_START]", "task_id": task_id}))
|
| 195 |
-
t0 = time.time()
|
| 196 |
-
result = run_task(task_id)
|
| 197 |
-
elapsed = round(time.time() - t0, 2)
|
| 198 |
-
result["elapsed_seconds"] = elapsed
|
| 199 |
-
all_results[task_id] = result
|
| 200 |
-
print(json.dumps({
|
| 201 |
-
"type": "[TASK_END]",
|
| 202 |
-
"task_id": task_id,
|
| 203 |
-
"final_score": result["final_score"],
|
| 204 |
-
"steps": result["steps"],
|
| 205 |
-
"elapsed": elapsed,
|
| 206 |
-
}))
|
| 207 |
-
|
| 208 |
-
overall = round(
|
| 209 |
-
sum(r["final_score"] for r in all_results.values()) / len(all_results), 4
|
| 210 |
-
)
|
| 211 |
-
|
| 212 |
-
# ββ [END] log ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 213 |
-
print(json.dumps({
|
| 214 |
-
"type": "[END]",
|
| 215 |
-
"overall_score": overall,
|
| 216 |
-
"task_scores": {
|
| 217 |
-
t: all_results[t]["final_score"] for t in tasks
|
| 218 |
-
},
|
| 219 |
-
"total_steps": sum(r["steps"] for r in all_results.values()),
|
| 220 |
-
"model": MODEL_NAME,
|
| 221 |
-
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
| 222 |
-
"status": "success",
|
| 223 |
-
}))
|
| 224 |
-
|
| 225 |
-
return overall
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
if __name__ == "__main__":
|
| 229 |
-
score = main()
|
| 230 |
-
sys.exit(0 if score >= 0.0 else 1)
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import json
|
| 6 |
+
from typing import Dict, Any
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
from fastapi import FastAPI
|
| 9 |
+
from pydantic import BaseModel
|
| 10 |
from openai import OpenAI
|
| 11 |
|
| 12 |
+
# βββ FastAPI App βββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
+
app = FastAPI()
|
| 14 |
|
| 15 |
+
# βββ Environment Variables βββββββββββββββββββββββββββββββ
|
| 16 |
API_BASE_URL = os.environ.get("API_BASE_URL", "https://api.openai.com/v1")
|
| 17 |
+
MODEL_NAME = os.environ.get("MODEL_NAME", "gpt-4o-mini")
|
| 18 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", os.environ.get("OPENAI_API_KEY", ""))
|
| 19 |
|
| 20 |
+
# β Prevent crash if token missing
|
| 21 |
if not HF_TOKEN:
|
| 22 |
+
HF_TOKEN = "dummy-key"
|
|
|
|
| 23 |
|
| 24 |
client = OpenAI(api_key=HF_TOKEN, base_url=API_BASE_URL)
|
| 25 |
|
| 26 |
+
# βββ Safe Imports (IMPORTANT FIX) βββββββββββββββββββββββββ
|
| 27 |
sys.path.insert(0, os.path.dirname(__file__))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
try:
|
| 30 |
+
from models import UrgencyLevel, EmailCategory, EmailAction
|
| 31 |
+
except Exception:
|
| 32 |
+
# fallback if import fails (prevents uvicorn crash)
|
| 33 |
+
UrgencyLevel = EmailCategory = EmailAction = None
|
| 34 |
+
|
| 35 |
+
# βββ Prompt ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 36 |
+
SYSTEM_PROMPT = """You are an expert email triage assistant.
|
| 37 |
+
|
| 38 |
+
Return ONLY valid JSON with:
|
| 39 |
+
- urgency
|
| 40 |
+
- category
|
| 41 |
+
- action
|
| 42 |
+
- draft_reply (if reply)
|
| 43 |
+
- forward_to (if forward/escalate)
|
| 44 |
+
- reasoning
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
"""
|
| 46 |
|
| 47 |
+
# βββ Request Schema ββββββββββββββββββββββββββββββββββββββ
|
| 48 |
+
class InputData(BaseModel):
|
| 49 |
+
input: Dict[str, Any]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# βββ Helper Function βββββββββββββββββββββββββββββββββββββ
|
| 52 |
+
def clamp_enum(value: str, enum_cls):
|
| 53 |
+
if enum_cls is None:
|
| 54 |
+
return value # fallback if enums not available
|
| 55 |
|
| 56 |
+
valid = {e.value for e in enum_cls}
|
| 57 |
+
return value if value in valid else list(enum_cls)[0].value
|
| 58 |
|
| 59 |
+
# βββ Agent Logic βββββββββββββββββββββββββββββββββββββββββ
|
| 60 |
+
def agent_decide(email_data: Dict[str, Any]) -> Dict[str, Any]:
|
| 61 |
try:
|
| 62 |
response = client.chat.completions.create(
|
| 63 |
model=MODEL_NAME,
|
| 64 |
messages=[
|
| 65 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 66 |
+
{"role": "user", "content": json.dumps(email_data)},
|
| 67 |
],
|
| 68 |
temperature=0.1,
|
|
|
|
|
|
|
| 69 |
)
|
| 70 |
+
|
| 71 |
raw = response.choices[0].message.content or "{}"
|
| 72 |
return json.loads(raw)
|
| 73 |
+
|
| 74 |
+
except Exception:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
return {
|
| 76 |
+
"urgency": "medium",
|
| 77 |
+
"category": "other",
|
| 78 |
+
"action": "archive",
|
| 79 |
+
"draft_reply": None,
|
| 80 |
+
"forward_to": None,
|
| 81 |
+
"reasoning": "fallback"
|
| 82 |
}
|
| 83 |
|
| 84 |
+
# βββ REQUIRED ENDPOINTS ββββββββββββββββββββββββββββββββββ
|
| 85 |
|
| 86 |
+
# β
FIXES YOUR ERROR
|
| 87 |
+
@app.post("/reset")
|
| 88 |
+
def reset():
|
| 89 |
+
return {"status": "reset successful"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
@app.post("/predict")
|
| 93 |
+
def predict(data: InputData):
|
| 94 |
+
email_data = data.input
|
| 95 |
|
| 96 |
+
decision = agent_decide(email_data)
|
|
|
|
| 97 |
|
| 98 |
+
urgency = clamp_enum(decision.get("urgency", "medium"), UrgencyLevel)
|
| 99 |
+
category = clamp_enum(decision.get("category", "other"), EmailCategory)
|
| 100 |
+
action = clamp_enum(decision.get("action", "archive"), EmailAction)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
return {
|
| 103 |
+
"urgency": urgency,
|
| 104 |
+
"category": category,
|
| 105 |
+
"action": action,
|
| 106 |
+
"draft_reply": decision.get("draft_reply"),
|
| 107 |
+
"forward_to": decision.get("forward_to"),
|
| 108 |
+
"reasoning": decision.get("reasoning", "")
|
| 109 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|