Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- Dockerfile +13 -0
- README.md +250 -5
- __init__.py +16 -0
- app.py +67 -0
- client.py +99 -0
- environment/base.py +71 -0
- graders/base.py +26 -0
- inference.py +74 -0
- models.py +27 -0
- openenv.yaml +37 -0
- pyproject.toml +45 -0
- requirements.txt +7 -0
- server/Dockerfile +80 -0
- server/__init__.py +11 -0
- server/app.py +84 -0
- server/requirements.txt +6 -0
- server/workflowops_environment.py +104 -0
- tasks/email_triage.py +118 -0
- tasks/schedule_conflict.py +81 -0
- tasks/support_ticket.py +115 -0
- uv.lock +0 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
EXPOSE 8000
|
| 11 |
+
|
| 12 |
+
ENV ENABLE_WEB_INTERFACE=true
|
| 13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
README.md
CHANGED
|
@@ -1,10 +1,255 @@
|
|
| 1 |
---
|
| 2 |
-
title: Workflowops
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Workflowops Environment Server
|
| 3 |
+
emoji: 🗜️
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
+
app_port: 8000
|
| 9 |
+
base_path: /web
|
| 10 |
+
tags:
|
| 11 |
+
- openenv
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# Workflowops Environment
|
| 15 |
+
|
| 16 |
+
A simple test environment that echoes back messages. Perfect for testing the env APIs as well as demonstrating environment usage patterns.
|
| 17 |
+
|
| 18 |
+
## Quick Start
|
| 19 |
+
|
| 20 |
+
The simplest way to use the Workflowops environment is through the `WorkflowopsEnv` class:
|
| 21 |
+
|
| 22 |
+
```python
|
| 23 |
+
from workflowops import WorkflowopsAction, WorkflowopsEnv
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# Create environment from Docker image
|
| 27 |
+
workflowopsenv = WorkflowopsEnv.from_docker_image("workflowops-env:latest")
|
| 28 |
+
|
| 29 |
+
# Reset
|
| 30 |
+
result = workflowopsenv.reset()
|
| 31 |
+
print(f"Reset: {result.observation.echoed_message}")
|
| 32 |
+
|
| 33 |
+
# Send multiple messages
|
| 34 |
+
messages = ["Hello, World!", "Testing echo", "Final message"]
|
| 35 |
+
|
| 36 |
+
for msg in messages:
|
| 37 |
+
result = workflowopsenv.step(WorkflowopsAction(message=msg))
|
| 38 |
+
print(f"Sent: '{msg}'")
|
| 39 |
+
print(f" → Echoed: '{result.observation.echoed_message}'")
|
| 40 |
+
print(f" → Length: {result.observation.message_length}")
|
| 41 |
+
print(f" → Reward: {result.reward}")
|
| 42 |
+
|
| 43 |
+
finally:
|
| 44 |
+
# Always clean up
|
| 45 |
+
workflowopsenv.close()
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
That's it! The `WorkflowopsEnv.from_docker_image()` method handles:
|
| 49 |
+
- Starting the Docker container
|
| 50 |
+
- Waiting for the server to be ready
|
| 51 |
+
- Connecting to the environment
|
| 52 |
+
- Container cleanup when you call `close()`
|
| 53 |
+
|
| 54 |
+
## Building the Docker Image
|
| 55 |
+
|
| 56 |
+
Before using the environment, you need to build the Docker image:
|
| 57 |
+
|
| 58 |
+
```bash
|
| 59 |
+
# From project root
|
| 60 |
+
docker build -t workflowops-env:latest -f server/Dockerfile .
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
## Deploying to Hugging Face Spaces
|
| 64 |
+
|
| 65 |
+
You can easily deploy your OpenEnv environment to Hugging Face Spaces using the `openenv push` command:
|
| 66 |
+
|
| 67 |
+
```bash
|
| 68 |
+
# From the environment directory (where openenv.yaml is located)
|
| 69 |
+
openenv push
|
| 70 |
+
|
| 71 |
+
# Or specify options
|
| 72 |
+
openenv push --namespace my-org --private
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
The `openenv push` command will:
|
| 76 |
+
1. Validate that the directory is an OpenEnv environment (checks for `openenv.yaml`)
|
| 77 |
+
2. Prepare a custom build for Hugging Face Docker space (enables web interface)
|
| 78 |
+
3. Upload to Hugging Face (ensuring you're logged in)
|
| 79 |
+
|
| 80 |
+
### Prerequisites
|
| 81 |
+
|
| 82 |
+
- Authenticate with Hugging Face: The command will prompt for login if not already authenticated
|
| 83 |
+
|
| 84 |
+
### Options
|
| 85 |
+
|
| 86 |
+
- `--directory`, `-d`: Directory containing the OpenEnv environment (defaults to current directory)
|
| 87 |
+
- `--repo-id`, `-r`: Repository ID in format 'username/repo-name' (defaults to 'username/env-name' from openenv.yaml)
|
| 88 |
+
- `--base-image`, `-b`: Base Docker image to use (overrides Dockerfile FROM)
|
| 89 |
+
- `--private`: Deploy the space as private (default: public)
|
| 90 |
+
|
| 91 |
+
### Examples
|
| 92 |
+
|
| 93 |
+
```bash
|
| 94 |
+
# Push to your personal namespace (defaults to username/env-name from openenv.yaml)
|
| 95 |
+
openenv push
|
| 96 |
+
|
| 97 |
+
# Push to a specific repository
|
| 98 |
+
openenv push --repo-id my-org/my-env
|
| 99 |
+
|
| 100 |
+
# Push with a custom base image
|
| 101 |
+
openenv push --base-image ghcr.io/meta-pytorch/openenv-base:latest
|
| 102 |
+
|
| 103 |
+
# Push as a private space
|
| 104 |
+
openenv push --private
|
| 105 |
+
|
| 106 |
+
# Combine options
|
| 107 |
+
openenv push --repo-id my-org/my-env --base-image custom-base:latest --private
|
| 108 |
+
```
|
| 109 |
+
|
| 110 |
+
After deployment, your space will be available at:
|
| 111 |
+
`https://huggingface.co/spaces/<repo-id>`
|
| 112 |
+
|
| 113 |
+
The deployed space includes:
|
| 114 |
+
- **Web Interface** at `/web` - Interactive UI for exploring the environment
|
| 115 |
+
- **API Documentation** at `/docs` - Full OpenAPI/Swagger interface
|
| 116 |
+
- **Health Check** at `/health` - Container health monitoring
|
| 117 |
+
- **WebSocket** at `/ws` - Persistent session endpoint for low-latency interactions
|
| 118 |
+
|
| 119 |
+
## Environment Details
|
| 120 |
+
|
| 121 |
+
### Action
|
| 122 |
+
**WorkflowopsAction**: Contains a single field
|
| 123 |
+
- `message` (str) - The message to echo back
|
| 124 |
+
|
| 125 |
+
### Observation
|
| 126 |
+
**WorkflowopsObservation**: Contains the echo response and metadata
|
| 127 |
+
- `echoed_message` (str) - The message echoed back
|
| 128 |
+
- `message_length` (int) - Length of the message
|
| 129 |
+
- `reward` (float) - Reward based on message length (length × 0.1)
|
| 130 |
+
- `done` (bool) - Always False for echo environment
|
| 131 |
+
- `metadata` (dict) - Additional info like step count
|
| 132 |
+
|
| 133 |
+
### Reward
|
| 134 |
+
The reward is calculated as: `message_length × 0.1`
|
| 135 |
+
- "Hi" → reward: 0.2
|
| 136 |
+
- "Hello, World!" → reward: 1.3
|
| 137 |
+
- Empty message → reward: 0.0
|
| 138 |
+
|
| 139 |
+
## Advanced Usage
|
| 140 |
+
|
| 141 |
+
### Connecting to an Existing Server
|
| 142 |
+
|
| 143 |
+
If you already have a Workflowops environment server running, you can connect directly:
|
| 144 |
+
|
| 145 |
+
```python
|
| 146 |
+
from workflowops import WorkflowopsEnv
|
| 147 |
+
|
| 148 |
+
# Connect to existing server
|
| 149 |
+
workflowopsenv = WorkflowopsEnv(base_url="<ENV_HTTP_URL_HERE>")
|
| 150 |
+
|
| 151 |
+
# Use as normal
|
| 152 |
+
result = workflowopsenv.reset()
|
| 153 |
+
result = workflowopsenv.step(WorkflowopsAction(message="Hello!"))
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
Note: When connecting to an existing server, `workflowopsenv.close()` will NOT stop the server.
|
| 157 |
+
|
| 158 |
+
### Using the Context Manager
|
| 159 |
+
|
| 160 |
+
The client supports context manager usage for automatic connection management:
|
| 161 |
+
|
| 162 |
+
```python
|
| 163 |
+
from workflowops import WorkflowopsAction, WorkflowopsEnv
|
| 164 |
+
|
| 165 |
+
# Connect with context manager (auto-connects and closes)
|
| 166 |
+
with WorkflowopsEnv(base_url="http://localhost:8000") as env:
|
| 167 |
+
result = env.reset()
|
| 168 |
+
print(f"Reset: {result.observation.echoed_message}")
|
| 169 |
+
# Multiple steps with low latency
|
| 170 |
+
for msg in ["Hello", "World", "!"]:
|
| 171 |
+
result = env.step(WorkflowopsAction(message=msg))
|
| 172 |
+
print(f"Echoed: {result.observation.echoed_message}")
|
| 173 |
+
```
|
| 174 |
+
|
| 175 |
+
The client uses WebSocket connections for:
|
| 176 |
+
- **Lower latency**: No HTTP connection overhead per request
|
| 177 |
+
- **Persistent session**: Server maintains your environment state
|
| 178 |
+
- **Efficient for episodes**: Better for many sequential steps
|
| 179 |
+
|
| 180 |
+
### Concurrent WebSocket Sessions
|
| 181 |
+
|
| 182 |
+
The server supports multiple concurrent WebSocket connections. To enable this,
|
| 183 |
+
modify `server/app.py` to use factory mode:
|
| 184 |
+
|
| 185 |
+
```python
|
| 186 |
+
# In server/app.py - use factory mode for concurrent sessions
|
| 187 |
+
app = create_app(
|
| 188 |
+
WorkflowopsEnvironment, # Pass class, not instance
|
| 189 |
+
WorkflowopsAction,
|
| 190 |
+
WorkflowopsObservation,
|
| 191 |
+
max_concurrent_envs=4, # Allow 4 concurrent sessions
|
| 192 |
+
)
|
| 193 |
+
```
|
| 194 |
+
|
| 195 |
+
Then multiple clients can connect simultaneously:
|
| 196 |
+
|
| 197 |
+
```python
|
| 198 |
+
from workflowops import WorkflowopsAction, WorkflowopsEnv
|
| 199 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 200 |
+
|
| 201 |
+
def run_episode(client_id: int):
|
| 202 |
+
with WorkflowopsEnv(base_url="http://localhost:8000") as env:
|
| 203 |
+
result = env.reset()
|
| 204 |
+
for i in range(10):
|
| 205 |
+
result = env.step(WorkflowopsAction(message=f"Client {client_id}, step {i}"))
|
| 206 |
+
return client_id, result.observation.message_length
|
| 207 |
+
|
| 208 |
+
# Run 4 episodes concurrently
|
| 209 |
+
with ThreadPoolExecutor(max_workers=4) as executor:
|
| 210 |
+
results = list(executor.map(run_episode, range(4)))
|
| 211 |
+
```
|
| 212 |
+
|
| 213 |
+
## Development & Testing
|
| 214 |
+
|
| 215 |
+
### Direct Environment Testing
|
| 216 |
+
|
| 217 |
+
Test the environment logic directly without starting the HTTP server:
|
| 218 |
+
|
| 219 |
+
```bash
|
| 220 |
+
# From the server directory
|
| 221 |
+
python3 server/workflowops_environment.py
|
| 222 |
+
```
|
| 223 |
+
|
| 224 |
+
This verifies that:
|
| 225 |
+
- Environment resets correctly
|
| 226 |
+
- Step executes actions properly
|
| 227 |
+
- State tracking works
|
| 228 |
+
- Rewards are calculated correctly
|
| 229 |
+
|
| 230 |
+
### Running Locally
|
| 231 |
+
|
| 232 |
+
Run the server locally for development:
|
| 233 |
+
|
| 234 |
+
```bash
|
| 235 |
+
uvicorn server.app:app --reload
|
| 236 |
+
```
|
| 237 |
+
|
| 238 |
+
## Project Structure
|
| 239 |
+
|
| 240 |
+
```
|
| 241 |
+
workflowops/
|
| 242 |
+
├── .dockerignore # Docker build exclusions
|
| 243 |
+
├── __init__.py # Module exports
|
| 244 |
+
├── README.md # This file
|
| 245 |
+
├── openenv.yaml # OpenEnv manifest
|
| 246 |
+
├── pyproject.toml # Project metadata and dependencies
|
| 247 |
+
├── uv.lock # Locked dependencies (generated)
|
| 248 |
+
├── client.py # WorkflowopsEnv client
|
| 249 |
+
├── models.py # Action and Observation models
|
| 250 |
+
└── server/
|
| 251 |
+
├── __init__.py # Server module exports
|
| 252 |
+
├── workflowops_environment.py # Core environment logic
|
| 253 |
+
├── app.py # FastAPI application (HTTP + WebSocket endpoints)
|
| 254 |
+
└── Dockerfile # Container image definition
|
| 255 |
+
```
|
__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
+
"""Workflowops Environment."""
|
| 8 |
+
|
| 9 |
+
from .client import WorkflowopsEnv
|
| 10 |
+
from .models import WorkflowopsAction, WorkflowopsObservation
|
| 11 |
+
|
| 12 |
+
__all__ = [
|
| 13 |
+
"WorkflowopsAction",
|
| 14 |
+
"WorkflowopsObservation",
|
| 15 |
+
"WorkflowopsEnv",
|
| 16 |
+
]
|
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import Dict, Any
|
| 4 |
+
import uvicorn
|
| 5 |
+
|
| 6 |
+
from tasks.email_triage import EmailTriageTask
|
| 7 |
+
from tasks.support_ticket import SupportTicketTask
|
| 8 |
+
from tasks.schedule_conflict import ScheduleConflictTask
|
| 9 |
+
|
| 10 |
+
app = FastAPI(title="WorkflowOps OpenEnv", version="1.0.0")
|
| 11 |
+
|
| 12 |
+
TASKS = {
|
| 13 |
+
"email_triage": EmailTriageTask,
|
| 14 |
+
"support_ticket": SupportTicketTask,
|
| 15 |
+
"schedule_conflict": ScheduleConflictTask
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
active_envs: Dict[str, Any] = {}
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class ActionRequest(BaseModel):
|
| 22 |
+
episode_id: str
|
| 23 |
+
action: Dict[str, Any]
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@app.get("/health")
|
| 27 |
+
async def health():
|
| 28 |
+
return {"status": "healthy", "service": "workflowops"}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@app.get("/ready")
|
| 32 |
+
async def ready():
|
| 33 |
+
return {"ready": True, "tasks": list(TASKS.keys())}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
@app.post("/env/{task_name}/reset")
|
| 37 |
+
async def reset_environment(task_name: str):
|
| 38 |
+
if task_name not in TASKS:
|
| 39 |
+
raise HTTPException(status_code=404, detail="Task not found")
|
| 40 |
+
|
| 41 |
+
env = TASKS[task_name]()
|
| 42 |
+
observation = env.reset()
|
| 43 |
+
|
| 44 |
+
active_envs[env.state.episode_id] = env
|
| 45 |
+
|
| 46 |
+
return {
|
| 47 |
+
"episode_id": env.state.episode_id,
|
| 48 |
+
"observation": observation.dict()
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
@app.post("/env/step")
|
| 53 |
+
async def step_environment(request: ActionRequest):
|
| 54 |
+
if request.episode_id not in active_envs:
|
| 55 |
+
raise HTTPException(status_code=404, detail="Episode not found")
|
| 56 |
+
|
| 57 |
+
env = active_envs[request.episode_id]
|
| 58 |
+
observation = env.step(request.action)
|
| 59 |
+
|
| 60 |
+
if observation.done:
|
| 61 |
+
del active_envs[request.episode_id]
|
| 62 |
+
|
| 63 |
+
return observation.dict()
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
client.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
+
"""Workflowops Environment Client."""
|
| 8 |
+
|
| 9 |
+
from typing import Dict
|
| 10 |
+
|
| 11 |
+
from openenv.core import EnvClient
|
| 12 |
+
from openenv.core.client_types import StepResult
|
| 13 |
+
from openenv.core.env_server.types import State
|
| 14 |
+
|
| 15 |
+
from .models import WorkflowopsAction, WorkflowopsObservation
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class WorkflowopsEnv(
|
| 19 |
+
EnvClient[WorkflowopsAction, WorkflowopsObservation, State]
|
| 20 |
+
):
|
| 21 |
+
"""
|
| 22 |
+
Client for the Workflowops Environment.
|
| 23 |
+
|
| 24 |
+
This client maintains a persistent WebSocket connection to the environment server,
|
| 25 |
+
enabling efficient multi-step interactions with lower latency.
|
| 26 |
+
Each client instance has its own dedicated environment session on the server.
|
| 27 |
+
|
| 28 |
+
Example:
|
| 29 |
+
>>> # Connect to a running server
|
| 30 |
+
>>> with WorkflowopsEnv(base_url="http://localhost:8000") as client:
|
| 31 |
+
... result = client.reset()
|
| 32 |
+
... print(result.observation.echoed_message)
|
| 33 |
+
...
|
| 34 |
+
... result = client.step(WorkflowopsAction(message="Hello!"))
|
| 35 |
+
... print(result.observation.echoed_message)
|
| 36 |
+
|
| 37 |
+
Example with Docker:
|
| 38 |
+
>>> # Automatically start container and connect
|
| 39 |
+
>>> client = WorkflowopsEnv.from_docker_image("workflowops-env:latest")
|
| 40 |
+
>>> try:
|
| 41 |
+
... result = client.reset()
|
| 42 |
+
... result = client.step(WorkflowopsAction(message="Test"))
|
| 43 |
+
... finally:
|
| 44 |
+
... client.close()
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
def _step_payload(self, action: WorkflowopsAction) -> Dict:
|
| 48 |
+
"""
|
| 49 |
+
Convert WorkflowopsAction to JSON payload for step message.
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
action: WorkflowopsAction instance
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
Dictionary representation suitable for JSON encoding
|
| 56 |
+
"""
|
| 57 |
+
return {
|
| 58 |
+
"message": action.message,
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
def _parse_result(self, payload: Dict) -> StepResult[WorkflowopsObservation]:
|
| 62 |
+
"""
|
| 63 |
+
Parse server response into StepResult[WorkflowopsObservation].
|
| 64 |
+
|
| 65 |
+
Args:
|
| 66 |
+
payload: JSON response data from server
|
| 67 |
+
|
| 68 |
+
Returns:
|
| 69 |
+
StepResult with WorkflowopsObservation
|
| 70 |
+
"""
|
| 71 |
+
obs_data = payload.get("observation", {})
|
| 72 |
+
observation = WorkflowopsObservation(
|
| 73 |
+
echoed_message=obs_data.get("echoed_message", ""),
|
| 74 |
+
message_length=obs_data.get("message_length", 0),
|
| 75 |
+
done=payload.get("done", False),
|
| 76 |
+
reward=payload.get("reward"),
|
| 77 |
+
metadata=obs_data.get("metadata", {}),
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
return StepResult(
|
| 81 |
+
observation=observation,
|
| 82 |
+
reward=payload.get("reward"),
|
| 83 |
+
done=payload.get("done", False),
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
def _parse_state(self, payload: Dict) -> State:
|
| 87 |
+
"""
|
| 88 |
+
Parse server response into State object.
|
| 89 |
+
|
| 90 |
+
Args:
|
| 91 |
+
payload: JSON response from state request
|
| 92 |
+
|
| 93 |
+
Returns:
|
| 94 |
+
State object with episode_id and step_count
|
| 95 |
+
"""
|
| 96 |
+
return State(
|
| 97 |
+
episode_id=payload.get("episode_id"),
|
| 98 |
+
step_count=payload.get("step_count", 0),
|
| 99 |
+
)
|
environment/base.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any, Optional, List
|
| 2 |
+
from uuid import uuid4
|
| 3 |
+
from pydantic import BaseModel, Field
|
| 4 |
+
from openenv.core.env_server.interfaces import Environment
|
| 5 |
+
from openenv.core.env_server.types import State
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class Observation(BaseModel):
|
| 9 |
+
done: bool = False
|
| 10 |
+
reward: float = 0.0
|
| 11 |
+
observation: Dict[str, Any] = Field(default_factory=dict)
|
| 12 |
+
metadata: Dict[str, Any] = Field(default_factory=dict)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class BaseWorkflowEnvironment(Environment):
|
| 16 |
+
SUPPORTS_CONCURRENT_SESSIONS: bool = True
|
| 17 |
+
|
| 18 |
+
def __init__(self, seed: Optional[int] = None):
|
| 19 |
+
self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 20 |
+
self.seed = seed
|
| 21 |
+
self.history: List[Dict[str, Any]] = []
|
| 22 |
+
self.max_steps: int = 20
|
| 23 |
+
self.task_state: Dict[str, Any] = {}
|
| 24 |
+
|
| 25 |
+
def reset(self) -> Observation:
|
| 26 |
+
self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 27 |
+
self.history = []
|
| 28 |
+
self.task_state = {}
|
| 29 |
+
return Observation(
|
| 30 |
+
done=False,
|
| 31 |
+
reward=0.0,
|
| 32 |
+
observation={"status": "ready", "episode_id": self._state.episode_id},
|
| 33 |
+
metadata={"reset_count": len(self.history)}
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
def step(self, action: Dict[str, Any]) -> Observation:
|
| 37 |
+
self._state.step_count += 1
|
| 38 |
+
|
| 39 |
+
# Validate action
|
| 40 |
+
if not isinstance(action, dict):
|
| 41 |
+
return Observation(
|
| 42 |
+
done=True,
|
| 43 |
+
reward=-0.5,
|
| 44 |
+
observation={"error": "Invalid action format"},
|
| 45 |
+
metadata={"step": self._state.step_count}
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Record history
|
| 49 |
+
self.history.append({
|
| 50 |
+
"step": self._state.step_count,
|
| 51 |
+
"action": action,
|
| 52 |
+
"timestamp": self._state.episode_id
|
| 53 |
+
})
|
| 54 |
+
|
| 55 |
+
# Check max steps
|
| 56 |
+
if self._state.step_count >= self.max_steps:
|
| 57 |
+
return Observation(
|
| 58 |
+
done=True,
|
| 59 |
+
reward=0.0,
|
| 60 |
+
observation={"status": "max_steps_reached"},
|
| 61 |
+
metadata={"step": self._state.step_count}
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
return self._execute_action(action)
|
| 65 |
+
|
| 66 |
+
def _execute_action(self, action: Dict[str, Any]) -> Observation:
|
| 67 |
+
raise NotImplementedError("Subclasses must implement _execute_action")
|
| 68 |
+
|
| 69 |
+
@property
|
| 70 |
+
def state(self) -> State:
|
| 71 |
+
return self._state
|
graders/base.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any, Tuple
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class BaseGrader:
|
| 5 |
+
"""Base grader class with standard scoring logic"""
|
| 6 |
+
|
| 7 |
+
def __init__(self, max_score: float = 1.0):
|
| 8 |
+
self.max_score = max_score
|
| 9 |
+
self.efficiency_penalty_per_step = 0.02
|
| 10 |
+
self.invalid_action_penalty = 0.15
|
| 11 |
+
|
| 12 |
+
def grade(self, state: Dict[str, Any], action: Dict[str, Any], step_count: int) -> Tuple[float, Dict[str, Any]]:
|
| 13 |
+
"""
|
| 14 |
+
Grade an action against current state
|
| 15 |
+
Returns: (score [0,1], metadata)
|
| 16 |
+
"""
|
| 17 |
+
raise NotImplementedError("Subclasses must implement grade method")
|
| 18 |
+
|
| 19 |
+
def apply_efficiency_penalty(self, base_score: float, step_count: int) -> float:
|
| 20 |
+
"""Apply penalty for taking too many steps"""
|
| 21 |
+
penalty = step_count * self.efficiency_penalty_per_step
|
| 22 |
+
return max(0.0, base_score - penalty)
|
| 23 |
+
|
| 24 |
+
def apply_invalid_action_penalty(self, score: float) -> float:
|
| 25 |
+
"""Apply penalty for invalid actions"""
|
| 26 |
+
return max(0.0, score - self.invalid_action_penalty)
|
inference.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import json
|
| 5 |
+
import time
|
| 6 |
+
from typing import Dict, Any
|
| 7 |
+
from openai import OpenAI
|
| 8 |
+
import requests
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def main():
|
| 12 |
+
print("[START]")
|
| 13 |
+
|
| 14 |
+
api_base = os.getenv("API_BASE_URL", "https://api.openai.com/v1")
|
| 15 |
+
model_name = os.getenv("MODEL_NAME", "gpt-3.5-turbo")
|
| 16 |
+
env_url = os.getenv("ENV_URL", "http://localhost:8000")
|
| 17 |
+
task = os.getenv("TASK_NAME", "email_triage")
|
| 18 |
+
|
| 19 |
+
client = OpenAI(base_url=api_base, api_key=os.getenv("OPENAI_API_KEY"))
|
| 20 |
+
|
| 21 |
+
# Reset environment
|
| 22 |
+
resp = requests.post(f"{env_url}/env/{task}/reset")
|
| 23 |
+
resp.raise_for_status()
|
| 24 |
+
data = resp.json()
|
| 25 |
+
|
| 26 |
+
episode_id = data["episode_id"]
|
| 27 |
+
observation = data["observation"]
|
| 28 |
+
|
| 29 |
+
step = 0
|
| 30 |
+
|
| 31 |
+
while not observation.get("done", False):
|
| 32 |
+
step += 1
|
| 33 |
+
print(f"[STEP] {step}")
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
# Call LLM
|
| 37 |
+
response = client.chat.completions.create(
|
| 38 |
+
model=model_name,
|
| 39 |
+
messages=[
|
| 40 |
+
{"role": "system", "content": "You are an operations assistant. Respond ONLY with valid JSON action."},
|
| 41 |
+
{"role": "user", "content": json.dumps(observation["observation"])}
|
| 42 |
+
],
|
| 43 |
+
temperature=0.0,
|
| 44 |
+
max_tokens=256
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
action_text = response.choices[0].message.content.strip()
|
| 48 |
+
action = json.loads(action_text)
|
| 49 |
+
|
| 50 |
+
# Execute step
|
| 51 |
+
resp = requests.post(f"{env_url}/env/step", json={
|
| 52 |
+
"episode_id": episode_id,
|
| 53 |
+
"action": action
|
| 54 |
+
})
|
| 55 |
+
resp.raise_for_status()
|
| 56 |
+
observation = resp.json()
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"[ERROR] {str(e)}", file=sys.stderr)
|
| 60 |
+
# Fallback action
|
| 61 |
+
resp = requests.post(f"{env_url}/env/step", json={
|
| 62 |
+
"episode_id": episode_id,
|
| 63 |
+
"action": {}
|
| 64 |
+
})
|
| 65 |
+
observation = resp.json()
|
| 66 |
+
|
| 67 |
+
time.sleep(0.1)
|
| 68 |
+
|
| 69 |
+
print(f"[FINAL SCORE] {observation.get('reward', 0.0)}")
|
| 70 |
+
print("[END]")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
main()
|
models.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
Data models for the Workflowops Environment.
|
| 9 |
+
|
| 10 |
+
The workflowops environment is a simple test environment that echoes back messages.
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
from openenv.core.env_server.types import Action, Observation
|
| 14 |
+
from pydantic import Field
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class WorkflowopsAction(Action):
|
| 18 |
+
"""Action for the Workflowops environment - just a message to echo."""
|
| 19 |
+
|
| 20 |
+
message: str = Field(..., description="Message to echo back")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class WorkflowopsObservation(Observation):
|
| 24 |
+
"""Observation from the Workflowops environment - the echoed message."""
|
| 25 |
+
|
| 26 |
+
echoed_message: str = Field(default="", description="The echoed message")
|
| 27 |
+
message_length: int = Field(default=0, description="Length of the echoed message")
|
openenv.yaml
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: workflowops
|
| 2 |
+
version: 1.0.0
|
| 3 |
+
description: Workflow Operations OpenEnv Environment
|
| 4 |
+
author: WorkflowOps Team
|
| 5 |
+
|
| 6 |
+
environment:
|
| 7 |
+
python_version: "3.10"
|
| 8 |
+
concurrent_sessions: true
|
| 9 |
+
max_episode_steps: 20
|
| 10 |
+
timeout: 300
|
| 11 |
+
|
| 12 |
+
tasks:
|
| 13 |
+
- name: email_triage
|
| 14 |
+
endpoint: /env/email_triage/reset
|
| 15 |
+
max_steps: 8
|
| 16 |
+
description: "Categorize incoming emails into correct priority buckets"
|
| 17 |
+
|
| 18 |
+
- name: support_ticket
|
| 19 |
+
endpoint: /env/support_ticket/reset
|
| 20 |
+
max_steps: 6
|
| 21 |
+
description: "Prioritize and assign incoming support tickets"
|
| 22 |
+
|
| 23 |
+
- name: schedule_conflict
|
| 24 |
+
endpoint: /env/schedule_conflict/reset
|
| 25 |
+
max_steps: 1
|
| 26 |
+
description: "Resolve calendar scheduling conflicts"
|
| 27 |
+
|
| 28 |
+
deployment:
|
| 29 |
+
platform: huggingface
|
| 30 |
+
port: 8000
|
| 31 |
+
health_check: /health
|
| 32 |
+
ready_check: /ready
|
| 33 |
+
|
| 34 |
+
validation:
|
| 35 |
+
deterministic: true
|
| 36 |
+
reward_range: [0.0, 1.0]
|
| 37 |
+
requires_api_key: false
|
pyproject.toml
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
+
[build-system]
|
| 8 |
+
requires = ["setuptools>=45", "wheel"]
|
| 9 |
+
build-backend = "setuptools.build_meta"
|
| 10 |
+
|
| 11 |
+
[project]
|
| 12 |
+
name = "openenv-workflowops"
|
| 13 |
+
version = "0.1.0"
|
| 14 |
+
description = "Workflowops environment for OpenEnv"
|
| 15 |
+
requires-python = ">=3.10"
|
| 16 |
+
dependencies = [
|
| 17 |
+
# Core OpenEnv runtime (provides FastAPI server + HTTP client types)
|
| 18 |
+
# install from github
|
| 19 |
+
# "openenv-core[core] @ git+https://github.com/meta-pytorch/OpenEnv.git",
|
| 20 |
+
"openenv-core[core]>=0.2.2",
|
| 21 |
+
# Environment-specific dependencies
|
| 22 |
+
# Add all dependencies needed for your environment here
|
| 23 |
+
# Examples:
|
| 24 |
+
# "numpy>=1.19.0",
|
| 25 |
+
# "torch>=2.0.0",
|
| 26 |
+
# "gymnasium>=0.29.0",
|
| 27 |
+
# "openspiel>=1.0.0",
|
| 28 |
+
# "smolagents>=1.22.0,<2",
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
[project.optional-dependencies]
|
| 32 |
+
dev = [
|
| 33 |
+
"pytest>=8.0.0",
|
| 34 |
+
"pytest-cov>=4.0.0",
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
[project.scripts]
|
| 38 |
+
# Server entry point - enables running via: uv run --project . server
|
| 39 |
+
# or: python -m workflowops.server.app
|
| 40 |
+
server = "workflowops.server.app:main"
|
| 41 |
+
|
| 42 |
+
[tool.setuptools]
|
| 43 |
+
include-package-data = true
|
| 44 |
+
packages = ["workflowops", "workflowops.server"]
|
| 45 |
+
package-dir = { "workflowops" = ".", "workflowops.server" = "server" }
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi>=0.104.0
|
| 2 |
+
uvicorn>=0.24.0
|
| 3 |
+
pydantic>=2.5.0
|
| 4 |
+
requests>=2.31.0
|
| 5 |
+
openai>=1.3.0
|
| 6 |
+
openenv>=0.1.0
|
| 7 |
+
python-multipart>=0.0.6
|
server/Dockerfile
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
+
# Multi-stage build using openenv-base
|
| 8 |
+
# This Dockerfile is flexible and works for both:
|
| 9 |
+
# - In-repo environments (with local OpenEnv sources)
|
| 10 |
+
# - Standalone environments (with openenv from PyPI/Git)
|
| 11 |
+
# The build script (openenv build) handles context detection and sets appropriate build args.
|
| 12 |
+
|
| 13 |
+
ARG BASE_IMAGE=ghcr.io/meta-pytorch/openenv-base:latest
|
| 14 |
+
FROM ${BASE_IMAGE} AS builder
|
| 15 |
+
|
| 16 |
+
WORKDIR /app
|
| 17 |
+
|
| 18 |
+
# Ensure git is available (required for installing dependencies from VCS)
|
| 19 |
+
RUN apt-get update && \
|
| 20 |
+
apt-get install -y --no-install-recommends git && \
|
| 21 |
+
rm -rf /var/lib/apt/lists/*
|
| 22 |
+
|
| 23 |
+
# Build argument to control whether we're building standalone or in-repo
|
| 24 |
+
ARG BUILD_MODE=in-repo
|
| 25 |
+
ARG ENV_NAME=workflowops
|
| 26 |
+
|
| 27 |
+
# Copy environment code (always at root of build context)
|
| 28 |
+
COPY . /app/env
|
| 29 |
+
|
| 30 |
+
# For in-repo builds, openenv is already vendored in the build context
|
| 31 |
+
# For standalone builds, openenv will be installed via pyproject.toml
|
| 32 |
+
WORKDIR /app/env
|
| 33 |
+
|
| 34 |
+
# Ensure uv is available (for local builds where base image lacks it)
|
| 35 |
+
RUN if ! command -v uv >/dev/null 2>&1; then \
|
| 36 |
+
curl -LsSf https://astral.sh/uv/install.sh | sh && \
|
| 37 |
+
mv /root/.local/bin/uv /usr/local/bin/uv && \
|
| 38 |
+
mv /root/.local/bin/uvx /usr/local/bin/uvx; \
|
| 39 |
+
fi
|
| 40 |
+
|
| 41 |
+
# Install dependencies using uv sync
|
| 42 |
+
# If uv.lock exists, use it; otherwise resolve on the fly
|
| 43 |
+
RUN --mount=type=cache,target=/root/.cache/uv \
|
| 44 |
+
if [ -f uv.lock ]; then \
|
| 45 |
+
uv sync --frozen --no-install-project --no-editable; \
|
| 46 |
+
else \
|
| 47 |
+
uv sync --no-install-project --no-editable; \
|
| 48 |
+
fi
|
| 49 |
+
|
| 50 |
+
RUN --mount=type=cache,target=/root/.cache/uv \
|
| 51 |
+
if [ -f uv.lock ]; then \
|
| 52 |
+
uv sync --frozen --no-editable; \
|
| 53 |
+
else \
|
| 54 |
+
uv sync --no-editable; \
|
| 55 |
+
fi
|
| 56 |
+
|
| 57 |
+
# Final runtime stage
|
| 58 |
+
FROM ${BASE_IMAGE}
|
| 59 |
+
|
| 60 |
+
WORKDIR /app
|
| 61 |
+
|
| 62 |
+
# Copy the virtual environment from builder
|
| 63 |
+
COPY --from=builder /app/env/.venv /app/.venv
|
| 64 |
+
|
| 65 |
+
# Copy the environment code
|
| 66 |
+
COPY --from=builder /app/env /app/env
|
| 67 |
+
|
| 68 |
+
# Set PATH to use the virtual environment
|
| 69 |
+
ENV PATH="/app/.venv/bin:$PATH"
|
| 70 |
+
|
| 71 |
+
# Set PYTHONPATH so imports work correctly
|
| 72 |
+
ENV PYTHONPATH="/app/env:$PYTHONPATH"
|
| 73 |
+
|
| 74 |
+
# Health check
|
| 75 |
+
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
| 76 |
+
CMD curl -f http://localhost:8000/health || exit 1
|
| 77 |
+
|
| 78 |
+
# Run the FastAPI server
|
| 79 |
+
# The module path is constructed to work with the /app/env structure
|
| 80 |
+
CMD ["sh", "-c", "cd /app/env && uvicorn server.app:app --host 0.0.0.0 --port 8000"]
|
server/__init__.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
+
"""Workflowops environment server components."""
|
| 8 |
+
|
| 9 |
+
from .workflowops_environment import WorkflowopsEnvironment
|
| 10 |
+
|
| 11 |
+
__all__ = ["WorkflowopsEnvironment"]
|
server/app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
FastAPI application for the Workflowops Environment.
|
| 9 |
+
|
| 10 |
+
This module creates an HTTP server that exposes the WorkflowopsEnvironment
|
| 11 |
+
over HTTP and WebSocket endpoints, compatible with EnvClient.
|
| 12 |
+
|
| 13 |
+
Endpoints:
|
| 14 |
+
- POST /reset: Reset the environment
|
| 15 |
+
- POST /step: Execute an action
|
| 16 |
+
- GET /state: Get current environment state
|
| 17 |
+
- GET /schema: Get action/observation schemas
|
| 18 |
+
- WS /ws: WebSocket endpoint for persistent sessions
|
| 19 |
+
|
| 20 |
+
Usage:
|
| 21 |
+
# Development (with auto-reload):
|
| 22 |
+
uvicorn server.app:app --reload --host 0.0.0.0 --port 8000
|
| 23 |
+
|
| 24 |
+
# Production:
|
| 25 |
+
uvicorn server.app:app --host 0.0.0.0 --port 8000 --workers 4
|
| 26 |
+
|
| 27 |
+
# Or run directly:
|
| 28 |
+
python -m server.app
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
from openenv.core.env_server.http_server import create_app
|
| 33 |
+
except Exception as e: # pragma: no cover
|
| 34 |
+
raise ImportError(
|
| 35 |
+
"openenv is required for the web interface. Install dependencies with '\n uv sync\n'"
|
| 36 |
+
) from e
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
from ..models import WorkflowopsAction, WorkflowopsObservation
|
| 40 |
+
from .workflowops_environment import WorkflowopsEnvironment
|
| 41 |
+
except ModuleNotFoundError:
|
| 42 |
+
from models import WorkflowopsAction, WorkflowopsObservation
|
| 43 |
+
from server.workflowops_environment import WorkflowopsEnvironment
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Create the app with web interface and README integration
|
| 47 |
+
app = create_app(
|
| 48 |
+
WorkflowopsEnvironment,
|
| 49 |
+
WorkflowopsAction,
|
| 50 |
+
WorkflowopsObservation,
|
| 51 |
+
env_name="workflowops",
|
| 52 |
+
max_concurrent_envs=1, # increase this number to allow more concurrent WebSocket sessions
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def main(host: str = "0.0.0.0", port: int = 8000):
|
| 57 |
+
"""
|
| 58 |
+
Entry point for direct execution via uv run or python -m.
|
| 59 |
+
|
| 60 |
+
This function enables running the server without Docker:
|
| 61 |
+
uv run --project . server
|
| 62 |
+
uv run --project . server --port 8001
|
| 63 |
+
python -m workflowops.server.app
|
| 64 |
+
|
| 65 |
+
Args:
|
| 66 |
+
host: Host address to bind to (default: "0.0.0.0")
|
| 67 |
+
port: Port number to listen on (default: 8000)
|
| 68 |
+
|
| 69 |
+
For production deployments, consider using uvicorn directly with
|
| 70 |
+
multiple workers:
|
| 71 |
+
uvicorn workflowops.server.app:app --workers 4
|
| 72 |
+
"""
|
| 73 |
+
import uvicorn
|
| 74 |
+
|
| 75 |
+
uvicorn.run(app, host=host, port=port)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
import argparse
|
| 80 |
+
|
| 81 |
+
parser = argparse.ArgumentParser()
|
| 82 |
+
parser.add_argument("--port", type=int, default=8000)
|
| 83 |
+
args = parser.parse_args()
|
| 84 |
+
main(port=args.port)
|
server/requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openenv[core]>=0.2.0
|
| 2 |
+
fastapi>=0.115.0
|
| 3 |
+
uvicorn>=0.24.0
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
server/workflowops_environment.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
Workflowops Environment Implementation.
|
| 9 |
+
|
| 10 |
+
A simple test environment that echoes back messages sent to it.
|
| 11 |
+
Perfect for testing HTTP server infrastructure.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
from uuid import uuid4
|
| 15 |
+
|
| 16 |
+
from openenv.core.env_server.interfaces import Environment
|
| 17 |
+
from openenv.core.env_server.types import State
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
from ..models import WorkflowopsAction, WorkflowopsObservation
|
| 21 |
+
except ImportError:
|
| 22 |
+
from models import WorkflowopsAction, WorkflowopsObservation
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class WorkflowopsEnvironment(Environment):
|
| 26 |
+
"""
|
| 27 |
+
A simple echo environment that echoes back messages.
|
| 28 |
+
|
| 29 |
+
This environment is designed for testing the HTTP server infrastructure.
|
| 30 |
+
It maintains minimal state and simply echoes back whatever message it receives.
|
| 31 |
+
|
| 32 |
+
Example:
|
| 33 |
+
>>> env = WorkflowopsEnvironment()
|
| 34 |
+
>>> obs = env.reset()
|
| 35 |
+
>>> print(obs.echoed_message) # "Workflowops environment ready!"
|
| 36 |
+
>>>
|
| 37 |
+
>>> obs = env.step(WorkflowopsAction(message="Hello"))
|
| 38 |
+
>>> print(obs.echoed_message) # "Hello"
|
| 39 |
+
>>> print(obs.message_length) # 5
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
# Enable concurrent WebSocket sessions.
|
| 43 |
+
# Set to True if your environment isolates state between instances.
|
| 44 |
+
# When True, multiple WebSocket clients can connect simultaneously, each
|
| 45 |
+
# getting their own environment instance (when using factory mode in app.py).
|
| 46 |
+
SUPPORTS_CONCURRENT_SESSIONS: bool = True
|
| 47 |
+
|
| 48 |
+
def __init__(self):
|
| 49 |
+
"""Initialize the workflowops environment."""
|
| 50 |
+
self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 51 |
+
self._reset_count = 0
|
| 52 |
+
|
| 53 |
+
def reset(self) -> WorkflowopsObservation:
|
| 54 |
+
"""
|
| 55 |
+
Reset the environment.
|
| 56 |
+
|
| 57 |
+
Returns:
|
| 58 |
+
WorkflowopsObservation with a ready message
|
| 59 |
+
"""
|
| 60 |
+
self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 61 |
+
self._reset_count += 1
|
| 62 |
+
|
| 63 |
+
return WorkflowopsObservation(
|
| 64 |
+
echoed_message="Workflowops environment ready!",
|
| 65 |
+
message_length=0,
|
| 66 |
+
done=False,
|
| 67 |
+
reward=0.0,
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
def step(self, action: WorkflowopsAction) -> WorkflowopsObservation: # type: ignore[override]
|
| 71 |
+
"""
|
| 72 |
+
Execute a step in the environment by echoing the message.
|
| 73 |
+
|
| 74 |
+
Args:
|
| 75 |
+
action: WorkflowopsAction containing the message to echo
|
| 76 |
+
|
| 77 |
+
Returns:
|
| 78 |
+
WorkflowopsObservation with the echoed message and its length
|
| 79 |
+
"""
|
| 80 |
+
self._state.step_count += 1
|
| 81 |
+
|
| 82 |
+
message = action.message
|
| 83 |
+
length = len(message)
|
| 84 |
+
|
| 85 |
+
# Simple reward: longer messages get higher rewards
|
| 86 |
+
reward = length * 0.1
|
| 87 |
+
|
| 88 |
+
return WorkflowopsObservation(
|
| 89 |
+
echoed_message=message,
|
| 90 |
+
message_length=length,
|
| 91 |
+
done=False,
|
| 92 |
+
reward=reward,
|
| 93 |
+
metadata={"original_message": message, "step": self._state.step_count},
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
@property
|
| 97 |
+
def state(self) -> State:
|
| 98 |
+
"""
|
| 99 |
+
Get the current environment state.
|
| 100 |
+
|
| 101 |
+
Returns:
|
| 102 |
+
Current State with episode_id and step_count
|
| 103 |
+
"""
|
| 104 |
+
return self._state
|
tasks/email_triage.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any, Tuple
|
| 2 |
+
from graders.base import BaseGrader
|
| 3 |
+
from environment.base import BaseWorkflowEnvironment, Observation
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class EmailTriageGrader(BaseGrader):
|
| 7 |
+
CATEGORIES = ["urgent", "important", "normal", "spam"]
|
| 8 |
+
|
| 9 |
+
def grade(self, state: Dict[str, Any], action: Dict[str, Any], step_count: int) -> Tuple[float, Dict[str, Any]]:
|
| 10 |
+
email = state.get("current_email", {})
|
| 11 |
+
correct_category = email.get("correct_category")
|
| 12 |
+
|
| 13 |
+
selected_category = action.get("category")
|
| 14 |
+
|
| 15 |
+
if not selected_category or selected_category not in self.CATEGORIES:
|
| 16 |
+
return self.apply_invalid_action_penalty(0.0), {"error": "Invalid category"}
|
| 17 |
+
|
| 18 |
+
if selected_category == correct_category:
|
| 19 |
+
base_score = 1.0
|
| 20 |
+
else:
|
| 21 |
+
# Partial credit for near correct
|
| 22 |
+
if correct_category == "urgent" and selected_category == "important":
|
| 23 |
+
base_score = 0.6
|
| 24 |
+
elif correct_category == "important" and selected_category == "urgent":
|
| 25 |
+
base_score = 0.6
|
| 26 |
+
else:
|
| 27 |
+
base_score = 0.2
|
| 28 |
+
|
| 29 |
+
final_score = self.apply_efficiency_penalty(base_score, step_count)
|
| 30 |
+
|
| 31 |
+
return final_score, {
|
| 32 |
+
"base_score": base_score,
|
| 33 |
+
"correct": selected_category == correct_category,
|
| 34 |
+
"step_count": step_count
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class EmailTriageTask(BaseWorkflowEnvironment):
|
| 39 |
+
TEST_EMAILS = [
|
| 40 |
+
{
|
| 41 |
+
"id": 1,
|
| 42 |
+
"subject": "SERVER OUTAGE - All systems down",
|
| 43 |
+
"body": "Production servers are not responding. Immediate action required.",
|
| 44 |
+
"correct_category": "urgent",
|
| 45 |
+
"sender": "sysadmin@company.com"
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"id": 2,
|
| 49 |
+
"subject": "Quarterly Performance Review",
|
| 50 |
+
"body": "Please schedule your performance review meeting.",
|
| 51 |
+
"correct_category": "important",
|
| 52 |
+
"sender": "hr@company.com"
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"id": 3,
|
| 56 |
+
"subject": "Team lunch tomorrow",
|
| 57 |
+
"body": "We are having team lunch at 1pm. Let us know if you are coming.",
|
| 58 |
+
"correct_category": "normal",
|
| 59 |
+
"sender": "team@company.com"
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
"id": 4,
|
| 63 |
+
"subject": "YOU WON $1,000,000!!!",
|
| 64 |
+
"body": "Click here to claim your prize now!",
|
| 65 |
+
"correct_category": "spam",
|
| 66 |
+
"sender": "scam@fake.com"
|
| 67 |
+
}
|
| 68 |
+
]
|
| 69 |
+
|
| 70 |
+
def reset(self) -> Observation:
|
| 71 |
+
super().reset()
|
| 72 |
+
self.task_state["email_index"] = 0
|
| 73 |
+
self.task_state["current_email"] = self.TEST_EMAILS[0]
|
| 74 |
+
self.task_state["graded"] = 0
|
| 75 |
+
self.task_state["score"] = 0.0
|
| 76 |
+
|
| 77 |
+
return Observation(
|
| 78 |
+
done=False,
|
| 79 |
+
reward=0.0,
|
| 80 |
+
observation={
|
| 81 |
+
"task": "email_triage",
|
| 82 |
+
"email": self.task_state["current_email"],
|
| 83 |
+
"categories": EmailTriageGrader.CATEGORIES,
|
| 84 |
+
"remaining": len(self.TEST_EMAILS)
|
| 85 |
+
}
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
def _execute_action(self, action: Dict[str, Any]) -> Observation:
|
| 89 |
+
grader = EmailTriageGrader()
|
| 90 |
+
score, meta = grader.grade(self.task_state, action, self._state.step_count)
|
| 91 |
+
|
| 92 |
+
self.task_state["score"] += score
|
| 93 |
+
self.task_state["graded"] += 1
|
| 94 |
+
|
| 95 |
+
# Move to next email
|
| 96 |
+
self.task_state["email_index"] += 1
|
| 97 |
+
|
| 98 |
+
if self.task_state["email_index"] >= len(self.TEST_EMAILS):
|
| 99 |
+
final_score = self.task_state["score"] / len(self.TEST_EMAILS)
|
| 100 |
+
return Observation(
|
| 101 |
+
done=True,
|
| 102 |
+
reward=final_score,
|
| 103 |
+
observation={"final_score": final_score, "total_graded": self.task_state["graded"]},
|
| 104 |
+
metadata=meta
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
self.task_state["current_email"] = self.TEST_EMAILS[self.task_state["email_index"]]
|
| 108 |
+
|
| 109 |
+
return Observation(
|
| 110 |
+
done=False,
|
| 111 |
+
reward=score,
|
| 112 |
+
observation={
|
| 113 |
+
"email": self.task_state["current_email"],
|
| 114 |
+
"current_score": self.task_state["score"],
|
| 115 |
+
"remaining": len(self.TEST_EMAILS) - self.task_state["email_index"]
|
| 116 |
+
},
|
| 117 |
+
metadata=meta
|
| 118 |
+
)
|
tasks/schedule_conflict.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any, Tuple
|
| 2 |
+
from graders.base import BaseGrader
|
| 3 |
+
from environment.base import BaseWorkflowEnvironment, Observation
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class ScheduleConflictGrader(BaseGrader):
|
| 7 |
+
def grade(self, state: Dict[str, Any], action: Dict[str, Any], step_count: int) -> Tuple[float, Dict[str, Any]]:
|
| 8 |
+
events = state.get("events", [])
|
| 9 |
+
solution = action.get("schedule", [])
|
| 10 |
+
|
| 11 |
+
if not isinstance(solution, list):
|
| 12 |
+
return self.apply_invalid_action_penalty(0.0), {"error": "Invalid schedule format"}
|
| 13 |
+
|
| 14 |
+
# Check for overlaps
|
| 15 |
+
overlaps = 0
|
| 16 |
+
for i in range(len(solution)):
|
| 17 |
+
for j in range(i+1, len(solution)):
|
| 18 |
+
e1 = solution[i]
|
| 19 |
+
e2 = solution[j]
|
| 20 |
+
if not (e1["end"] <= e2["start"] or e2["end"] <= e1["start"]):
|
| 21 |
+
overlaps += 1
|
| 22 |
+
|
| 23 |
+
overlap_penalty = overlaps * 0.25
|
| 24 |
+
|
| 25 |
+
# Check all events are scheduled
|
| 26 |
+
missing_events = len(events) - len(solution)
|
| 27 |
+
missing_penalty = missing_events * 0.15
|
| 28 |
+
|
| 29 |
+
base_score = 1.0 - overlap_penalty - missing_penalty
|
| 30 |
+
final_score = self.apply_efficiency_penalty(max(0.0, base_score), step_count)
|
| 31 |
+
|
| 32 |
+
return final_score, {
|
| 33 |
+
"overlaps": overlaps,
|
| 34 |
+
"missing_events": missing_events,
|
| 35 |
+
"base_score": base_score,
|
| 36 |
+
"final_score": final_score
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class ScheduleConflictTask(BaseWorkflowEnvironment):
|
| 41 |
+
TEST_SCHEDULES = [
|
| 42 |
+
{
|
| 43 |
+
"events": [
|
| 44 |
+
{"id": 1, "title": "Team Standup", "duration": 15, "priority": 2},
|
| 45 |
+
{"id": 2, "title": "Client Call", "duration": 60, "priority": 1},
|
| 46 |
+
{"id": 3, "title": "Code Review", "duration": 30, "priority": 3},
|
| 47 |
+
{"id": 4, "title": "Lunch Break", "duration": 45, "priority": 2}
|
| 48 |
+
],
|
| 49 |
+
"available_slots": [
|
| 50 |
+
{"start": 540, "end": 720}, # 9:00 - 12:00
|
| 51 |
+
{"start": 780, "end": 1020} # 13:00 - 17:00
|
| 52 |
+
]
|
| 53 |
+
}
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
def reset(self) -> Observation:
|
| 57 |
+
super().reset()
|
| 58 |
+
self.task_state["schedule_index"] = 0
|
| 59 |
+
self.task_state["events"] = self.TEST_SCHEDULES[0]["events"]
|
| 60 |
+
self.task_state["available_slots"] = self.TEST_SCHEDULES[0]["available_slots"]
|
| 61 |
+
|
| 62 |
+
return Observation(
|
| 63 |
+
done=False,
|
| 64 |
+
reward=0.0,
|
| 65 |
+
observation={
|
| 66 |
+
"task": "schedule_conflict",
|
| 67 |
+
"events": self.task_state["events"],
|
| 68 |
+
"available_slots": self.task_state["available_slots"]
|
| 69 |
+
}
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
def _execute_action(self, action: Dict[str, Any]) -> Observation:
|
| 73 |
+
grader = ScheduleConflictGrader()
|
| 74 |
+
score, meta = grader.grade(self.task_state, action, self._state.step_count)
|
| 75 |
+
|
| 76 |
+
return Observation(
|
| 77 |
+
done=True,
|
| 78 |
+
reward=score,
|
| 79 |
+
observation={"final_score": score},
|
| 80 |
+
metadata=meta
|
| 81 |
+
)
|
tasks/support_ticket.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any, Tuple
|
| 2 |
+
from graders.base import BaseGrader
|
| 3 |
+
from environment.base import BaseWorkflowEnvironment, Observation
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class SupportTicketGrader(BaseGrader):
|
| 7 |
+
PRIORITIES = ["critical", "high", "medium", "low"]
|
| 8 |
+
|
| 9 |
+
def grade(self, state: Dict[str, Any], action: Dict[str, Any], step_count: int) -> Tuple[float, Dict[str, Any]]:
|
| 10 |
+
ticket = state.get("current_ticket", {})
|
| 11 |
+
correct_priority = ticket.get("correct_priority")
|
| 12 |
+
|
| 13 |
+
selected_priority = action.get("priority")
|
| 14 |
+
estimated_time = action.get("estimated_time", 0)
|
| 15 |
+
assignee = action.get("assignee")
|
| 16 |
+
|
| 17 |
+
score = 0.0
|
| 18 |
+
|
| 19 |
+
# Priority score (60%)
|
| 20 |
+
if selected_priority == correct_priority:
|
| 21 |
+
score += 0.6
|
| 22 |
+
else:
|
| 23 |
+
priority_distance = abs(self.PRIORITIES.index(selected_priority) - self.PRIORITIES.index(correct_priority))
|
| 24 |
+
score += max(0.0, 0.6 - (priority_distance * 0.2))
|
| 25 |
+
|
| 26 |
+
# Time estimation score (30%)
|
| 27 |
+
correct_time = ticket.get("correct_time", 0)
|
| 28 |
+
time_error = abs(estimated_time - correct_time) / max(correct_time, 1)
|
| 29 |
+
score += max(0.0, 0.3 - (time_error * 0.3))
|
| 30 |
+
|
| 31 |
+
# Assignee score (10%)
|
| 32 |
+
if assignee == ticket.get("correct_assignee"):
|
| 33 |
+
score += 0.1
|
| 34 |
+
|
| 35 |
+
final_score = self.apply_efficiency_penalty(score, step_count)
|
| 36 |
+
|
| 37 |
+
return final_score, {
|
| 38 |
+
"priority_score": score >= 0.6,
|
| 39 |
+
"time_score": score >= 0.3,
|
| 40 |
+
"assignee_score": score >= 0.1,
|
| 41 |
+
"total": final_score
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class SupportTicketTask(BaseWorkflowEnvironment):
|
| 46 |
+
TEST_TICKETS = [
|
| 47 |
+
{
|
| 48 |
+
"id": 101,
|
| 49 |
+
"title": "Database connection failure",
|
| 50 |
+
"description": "Cannot connect to primary database. All transactions failing.",
|
| 51 |
+
"correct_priority": "critical",
|
| 52 |
+
"correct_time": 30,
|
| 53 |
+
"correct_assignee": "database-team"
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"id": 102,
|
| 57 |
+
"title": "User password reset request",
|
| 58 |
+
"description": "User cannot log in, needs password reset.",
|
| 59 |
+
"correct_priority": "medium",
|
| 60 |
+
"correct_time": 10,
|
| 61 |
+
"correct_assignee": "support"
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"id": 103,
|
| 65 |
+
"title": "Feature request: Dark mode",
|
| 66 |
+
"description": "Would like dark mode option for dashboard.",
|
| 67 |
+
"correct_priority": "low",
|
| 68 |
+
"correct_time": 480,
|
| 69 |
+
"correct_assignee": "frontend"
|
| 70 |
+
}
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
def reset(self) -> Observation:
|
| 74 |
+
super().reset()
|
| 75 |
+
self.task_state["ticket_index"] = 0
|
| 76 |
+
self.task_state["current_ticket"] = self.TEST_TICKETS[0]
|
| 77 |
+
self.task_state["score"] = 0.0
|
| 78 |
+
|
| 79 |
+
return Observation(
|
| 80 |
+
done=False,
|
| 81 |
+
reward=0.0,
|
| 82 |
+
observation={
|
| 83 |
+
"task": "support_ticket",
|
| 84 |
+
"ticket": self.task_state["current_ticket"],
|
| 85 |
+
"priorities": SupportTicketGrader.PRIORITIES
|
| 86 |
+
}
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
def _execute_action(self, action: Dict[str, Any]) -> Observation:
|
| 90 |
+
grader = SupportTicketGrader()
|
| 91 |
+
score, meta = grader.grade(self.task_state, action, self._state.step_count)
|
| 92 |
+
|
| 93 |
+
self.task_state["score"] += score
|
| 94 |
+
self.task_state["ticket_index"] += 1
|
| 95 |
+
|
| 96 |
+
if self.task_state["ticket_index"] >= len(self.TEST_TICKETS):
|
| 97 |
+
final_score = self.task_state["score"] / len(self.TEST_TICKETS)
|
| 98 |
+
return Observation(
|
| 99 |
+
done=True,
|
| 100 |
+
reward=final_score,
|
| 101 |
+
observation={"final_score": final_score},
|
| 102 |
+
metadata=meta
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
self.task_state["current_ticket"] = self.TEST_TICKETS[self.task_state["ticket_index"]]
|
| 106 |
+
|
| 107 |
+
return Observation(
|
| 108 |
+
done=False,
|
| 109 |
+
reward=score,
|
| 110 |
+
observation={
|
| 111 |
+
"ticket": self.task_state["current_ticket"],
|
| 112 |
+
"current_score": self.task_state["score"]
|
| 113 |
+
},
|
| 114 |
+
metadata=meta
|
| 115 |
+
)
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|