Spaces:
Configuration error
Configuration error
Commit ·
f45aa51
1
Parent(s): 4d7f6c3
Add complete Trust & Safety RL Environment with README
Browse files
README.md
CHANGED
|
@@ -1,12 +1,104 @@
|
|
| 1 |
-
---
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
---
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Set-Content -Path "README.md" -Encoding UTF8 -Value @'
|
| 2 |
+
# 🛡️ Trust & Safety RL Environment — OpenEnv Hackathon
|
| 3 |
+
|
| 4 |
+
> A production-grade, 3-layer risk-aware content moderation environment built on [OpenEnv](https://huggingface.co/openenv), designed to train LLMs via GRPO reinforcement learning to make nuanced Trust & Safety decisions.
|
| 5 |
+
|
| 6 |
+
[](https://huggingface.co/spaces/Jeromerich/openenv)
|
| 7 |
+
[](https://huggingface.co/openenv)
|
| 8 |
+
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
## What This Project Does
|
| 12 |
+
|
| 13 |
+
This project builds a reinforcement learning environment where an LLM agent learns to moderate social media content. The agent reviews content tickets, investigates using tools, extracts risk signals, and makes a final moderation decision. The environment rewards the agent based on 8 components: decision correctness, tool usage, signal accuracy, confidence calibration, and more.
|
| 14 |
+
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
## The 6 Moderation Tasks
|
| 18 |
+
|
| 19 |
+
| Task | Difficulty | Risk | Scenario | Ground Truth |
|
| 20 |
+
|------|-----------|------|----------|-------------|
|
| 21 |
+
| T-001 | Easy | Low | Phishing spam impersonating Meta Security | REMOVE |
|
| 22 |
+
| T-002 | Medium | Low | Competitive gaming banter between players | ALLOW |
|
| 23 |
+
| T-003 | Hard | High | Political satire mocking a public figure | ALLOW_WITH_WARNING |
|
| 24 |
+
| T-004 | Hard | High | Coordinated hate speech targeting a protected group | REMOVE |
|
| 25 |
+
| T-005 | Medium | Medium | Protest content with inflammatory language | ALLOW_WITH_WARNING |
|
| 26 |
+
| T-006 | Hard | High | Ambiguous content — possible coordinated manipulation | ESCALATE |
|
| 27 |
+
|
| 28 |
+
---
|
| 29 |
+
|
| 30 |
+
## 3-Layer Environment Logic
|
| 31 |
+
|
| 32 |
+
Layer 1 — Evidence Gathering: agent calls tools (read_comments, check_user_history, check_entity_status, view_policy). Missing a required tool costs -0.25.
|
| 33 |
+
|
| 34 |
+
Layer 2 — Signal Extraction: agent outputs ContentSignals (toxicity_level, intent, context_type, content_flags, confidence). Signals are validated for internal consistency.
|
| 35 |
+
|
| 36 |
+
Layer 3 — Policy Engine: rule-based engine recommends a decision. Agent decision is scored against both ground truth and policy recommendation.
|
| 37 |
+
|
| 38 |
+
---
|
| 39 |
+
|
| 40 |
+
## 8-Component Reward System
|
| 41 |
+
|
| 42 |
+
FINAL REWARD = base_score + policy_alignment + signal_accuracy_bonus + escalation_adjustment + signal_process_bonus - tool_cost - tool_miss_penalty - validation_penalty - risk_penalty - confidence_discipline_penalty
|
| 43 |
+
|
| 44 |
+
| Component | Range | Description |
|
| 45 |
+
|-----------|-------|-------------|
|
| 46 |
+
| Base Decision Score | 0.00-1.00 | Correctness vs ground truth (asymmetric FP/FN) |
|
| 47 |
+
| Policy Alignment | +-0.12 | Matches policy engine recommendation |
|
| 48 |
+
| Signal Accuracy Bonus | 0.00-0.15 | Signals match ground truth signals |
|
| 49 |
+
| Escalation Adjustment | +-0.20 | Correct escalation behaviour |
|
| 50 |
+
| Signal Process Bonus | +-0.10 | Agent extracted signals before deciding |
|
| 51 |
+
| Tool Miss Penalty | -0.25/tool | Skipped required investigation tools |
|
| 52 |
+
| Risk Penalty | 0.00-0.20 | Wrong decisions on high-risk content |
|
| 53 |
+
| Confidence Discipline | 0.00-0.22 | High confidence + wrong answer = large penalty |
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
## Baseline Results (GPT-4o-mini, zero-shot)
|
| 58 |
+
|
| 59 |
+
| Task | Score |
|
| 60 |
+
|------|-------|
|
| 61 |
+
| T-001 Phishing Spam | 1.0000 |
|
| 62 |
+
| T-002 Gaming Banter | 0.9140 |
|
| 63 |
+
| T-003 Political Satire | 0.7540 |
|
| 64 |
+
| Average | 0.8893 |
|
| 65 |
+
|
| 66 |
+
---
|
| 67 |
+
|
| 68 |
+
## API Endpoints
|
| 69 |
+
|
| 70 |
+
Base URL: https://jeromerich-openenv.hf.space
|
| 71 |
+
|
| 72 |
+
- GET /health — health check
|
| 73 |
+
- GET /docs — interactive API docs
|
| 74 |
+
- POST /reset — start episode {"episode_id": "T-001"}
|
| 75 |
+
- POST /step — take action {"action_type": "final_decision", "final_decision": "REMOVE"}
|
| 76 |
+
- GET /state — current episode state
|
| 77 |
+
|
| 78 |
+
---
|
| 79 |
+
|
| 80 |
+
## GRPO Training
|
| 81 |
+
|
| 82 |
+
Fine-tuned Qwen/Qwen2.5-0.5B-Instruct using GRPOTrainer (TRL 1.0.0) with environment reward as the training signal. 180 training rows across 6 tasks.
|
| 83 |
+
|
| 84 |
+
---
|
| 85 |
+
|
| 86 |
+
## File Structure
|
| 87 |
+
|
| 88 |
+
- app.py FastAPI server
|
| 89 |
+
- your_environment.py 3-layer environment + reward engine
|
| 90 |
+
- models.py Pydantic types: TrustAction, TrustObservation, ContentSignals
|
| 91 |
+
- tasks.py 6 task definitions with ground truth
|
| 92 |
+
- client.py OpenEnv EnvClient wrapper
|
| 93 |
+
- inference.py Baseline evaluation
|
| 94 |
+
- train.py GRPO training script
|
| 95 |
+
- openenv.yaml OpenEnv manifest
|
| 96 |
+
- Dockerfile HF Spaces container (port 7860)
|
| 97 |
+
|
| 98 |
+
---
|
| 99 |
+
|
| 100 |
+
## Author
|
| 101 |
+
|
| 102 |
+
Jerome Richard — AI Engineering Student, Amrita Vishwa Vidyapeetham
|
| 103 |
+
Built for the OpenEnv Hackathon 2026
|
| 104 |
+
'@
|
simpley
DELETED
|
@@ -1,1716 +0,0 @@
|
|
| 1 |
-
app.py:
|
| 2 |
-
from __future__ import annotations
|
| 3 |
-
|
| 4 |
-
import json
|
| 5 |
-
from typing import Any, Dict, Optional
|
| 6 |
-
|
| 7 |
-
from fastapi import FastAPI, HTTPException
|
| 8 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
-
from fastapi.responses import JSONResponse
|
| 10 |
-
from pydantic import BaseModel
|
| 11 |
-
|
| 12 |
-
from models import TrustAction, TrustObservation, TrustState, ContentSignals
|
| 13 |
-
from your_environment import TrustSafetyEnvironment
|
| 14 |
-
|
| 15 |
-
# ── Force manual FastAPI (openenv_core create_app causes 422 on /step) ────────
|
| 16 |
-
print("[app] Using manual FastAPI ✅")
|
| 17 |
-
|
| 18 |
-
_env = TrustSafetyEnvironment(seed=42)
|
| 19 |
-
|
| 20 |
-
app = FastAPI(
|
| 21 |
-
title="Trust & Safety RL Environment",
|
| 22 |
-
description="Risk-aware content moderation environment for agent training.",
|
| 23 |
-
version="1.0.0",
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
app.add_middleware(
|
| 27 |
-
CORSMiddleware,
|
| 28 |
-
allow_origins=["*"],
|
| 29 |
-
allow_methods=["*"],
|
| 30 |
-
allow_headers=["*"],
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# ── Serializers ───────────────────────────────────────────────────────────────
|
| 35 |
-
|
| 36 |
-
def _obs_to_dict(obs: TrustObservation) -> Dict[str, Any]:
|
| 37 |
-
return {
|
| 38 |
-
"ticket_id": obs.ticket_id,
|
| 39 |
-
"post_text": obs.post_text,
|
| 40 |
-
"image_description": obs.image_description,
|
| 41 |
-
"comments_found": obs.comments_found,
|
| 42 |
-
"user_history_found": obs.user_history_found,
|
| 43 |
-
"entity_status_found": obs.entity_status_found,
|
| 44 |
-
"policy_found": obs.policy_found,
|
| 45 |
-
"extracted_signals": obs.extracted_signals,
|
| 46 |
-
"validation_result": obs.validation_result,
|
| 47 |
-
"step_number": obs.step_number,
|
| 48 |
-
"info": obs.info,
|
| 49 |
-
"done": obs.done,
|
| 50 |
-
"reward": obs.reward,
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
def _state_to_dict(s: TrustState) -> Dict[str, Any]:
|
| 55 |
-
return {
|
| 56 |
-
"episode_id": s.episode_id,
|
| 57 |
-
"step_count": s.step_count,
|
| 58 |
-
"current_task_id": s.current_task_id,
|
| 59 |
-
"difficulty": s.difficulty,
|
| 60 |
-
"ambiguity_level": s.ambiguity_level,
|
| 61 |
-
"risk_level": s.risk_level,
|
| 62 |
-
"tools_used": s.tools_used,
|
| 63 |
-
"signals_extracted": s.signals_extracted,
|
| 64 |
-
"is_done": s.is_done,
|
| 65 |
-
}
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# ── Request bodies ─────────────────────────────────────────────────────────────
|
| 69 |
-
|
| 70 |
-
class ResetRequest(BaseModel):
|
| 71 |
-
seed: Any = None
|
| 72 |
-
episode_id: Any = None
|
| 73 |
-
|
| 74 |
-
model_config = {"extra": "ignore"}
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
class ActionRequest(BaseModel):
|
| 78 |
-
action_type: str = ""
|
| 79 |
-
tool_name: Optional[str] = None
|
| 80 |
-
signals: Optional[Dict[str, Any]] = None # raw dict — validated below
|
| 81 |
-
final_decision: Optional[str] = None
|
| 82 |
-
|
| 83 |
-
model_config = {"extra": "ignore"} # ← ignore unknown keys from LLM
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
# ── Helpers ────────────────────────────────────────────────────────────────────
|
| 87 |
-
|
| 88 |
-
def _parse_signals(raw: Dict[str, Any]) -> ContentSignals:
|
| 89 |
-
"""Defensively normalise LLM signal output before Pydantic validation."""
|
| 90 |
-
# Clamp floats
|
| 91 |
-
raw["toxicity_level"] = float(raw.get("toxicity_level", 0.5))
|
| 92 |
-
raw["confidence"] = float(raw.get("confidence", 0.5))
|
| 93 |
-
|
| 94 |
-
# content_flags must be a list of strings
|
| 95 |
-
flags = raw.get("content_flags", [])
|
| 96 |
-
if not isinstance(flags, list):
|
| 97 |
-
flags = [flags] if isinstance(flags, str) else []
|
| 98 |
-
raw["content_flags"] = [str(f) for f in flags]
|
| 99 |
-
|
| 100 |
-
# boolean coercion
|
| 101 |
-
raw["is_protected_class"] = bool(raw.get("is_protected_class", False))
|
| 102 |
-
raw["is_direct_attack"] = bool(raw.get("is_direct_attack", False))
|
| 103 |
-
raw["abusive_language_present"] = bool(raw.get("abusive_language_present", False))
|
| 104 |
-
|
| 105 |
-
# string fields — fallback to sensible defaults
|
| 106 |
-
raw.setdefault("target", "none")
|
| 107 |
-
raw.setdefault("intent", "ambiguous")
|
| 108 |
-
raw.setdefault("context_type", "statement")
|
| 109 |
-
|
| 110 |
-
return ContentSignals(**raw)
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
# ── Routes ─────────────────────────────────────────────────────────────────────
|
| 114 |
-
|
| 115 |
-
@app.get("/health")
|
| 116 |
-
async def health():
|
| 117 |
-
return {"status": "ok", "environment": "trust-safety-env", "version": "1.0.0"}
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
@app.get("/")
|
| 121 |
-
async def root():
|
| 122 |
-
return {"status": "ok", "docs": "/docs"}
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
@app.post("/reset")
|
| 126 |
-
async def reset(body: ResetRequest = ResetRequest()):
|
| 127 |
-
obs = _env.reset(seed=body.seed, episode_id=body.episode_id)
|
| 128 |
-
return JSONResponse(_obs_to_dict(obs))
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
@app.post("/step")
|
| 132 |
-
async def step(body: ActionRequest):
|
| 133 |
-
# Parse + validate signals defensively
|
| 134 |
-
signals: Optional[ContentSignals] = None
|
| 135 |
-
if body.signals:
|
| 136 |
-
try:
|
| 137 |
-
signals = _parse_signals(dict(body.signals)) # copy so we don't mutate
|
| 138 |
-
except Exception as e:
|
| 139 |
-
raise HTTPException(status_code=400, detail=f"Invalid signals payload: {e}")
|
| 140 |
-
|
| 141 |
-
action = TrustAction(
|
| 142 |
-
action_type = body.action_type,
|
| 143 |
-
tool_name = body.tool_name,
|
| 144 |
-
signals = signals,
|
| 145 |
-
final_decision = body.final_decision,
|
| 146 |
-
)
|
| 147 |
-
|
| 148 |
-
try:
|
| 149 |
-
obs = _env.step(action)
|
| 150 |
-
except (RuntimeError, ValueError) as e:
|
| 151 |
-
raise HTTPException(status_code=400, detail=str(e))
|
| 152 |
-
|
| 153 |
-
return JSONResponse(_obs_to_dict(obs))
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
@app.get("/state")
|
| 157 |
-
async def state():
|
| 158 |
-
return JSONResponse(_state_to_dict(_env.state))
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
client.py:
|
| 163 |
-
from __future__ import annotations
|
| 164 |
-
from typing import Any
|
| 165 |
-
from openenv.core.http_env_client import HTTPEnvClient
|
| 166 |
-
from openenv.core.types import StepResult
|
| 167 |
-
from models import TrustAction, TrustObservation, TrustState, ContentSignals
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
class TrustSafetyEnv(HTTPEnvClient[TrustAction, TrustObservation]):
|
| 171 |
-
"""
|
| 172 |
-
Typed HTTP client for the Trust & Safety RL Environment.
|
| 173 |
-
|
| 174 |
-
Usage:
|
| 175 |
-
client = TrustSafetyEnv(base_url="http://localhost:8000")
|
| 176 |
-
result = client.reset()
|
| 177 |
-
result = client.step(TrustAction(action_type="final_decision",
|
| 178 |
-
final_decision="ALLOW"))
|
| 179 |
-
state = client.state()
|
| 180 |
-
client.close()
|
| 181 |
-
"""
|
| 182 |
-
|
| 183 |
-
def _step_payload(self, action: TrustAction) -> dict:
|
| 184 |
-
payload: dict = {"action_type": action.action_type}
|
| 185 |
-
if action.tool_name is not None:
|
| 186 |
-
payload["tool_name"] = action.tool_name
|
| 187 |
-
if action.signals is not None:
|
| 188 |
-
s = action.signals
|
| 189 |
-
payload["signals"] = {
|
| 190 |
-
"target": s.target,
|
| 191 |
-
"is_protected_class": s.is_protected_class,
|
| 192 |
-
"toxicity_level": s.toxicity_level,
|
| 193 |
-
"is_direct_attack": s.is_direct_attack,
|
| 194 |
-
"context_type": s.context_type,
|
| 195 |
-
"intent": s.intent,
|
| 196 |
-
"confidence": s.confidence,
|
| 197 |
-
"abusive_language_present": s.abusive_language_present,
|
| 198 |
-
"content_flags": s.content_flags,
|
| 199 |
-
}
|
| 200 |
-
if action.final_decision is not None:
|
| 201 |
-
payload["final_decision"] = action.final_decision
|
| 202 |
-
return payload
|
| 203 |
-
|
| 204 |
-
def _parse_result(self, payload: dict) -> StepResult[TrustObservation]:
|
| 205 |
-
obs_data = payload.get("observation", payload) # handle flat or nested
|
| 206 |
-
signals_raw = obs_data.get("extracted_signals")
|
| 207 |
-
signals = None
|
| 208 |
-
if isinstance(signals_raw, dict):
|
| 209 |
-
try:
|
| 210 |
-
signals = ContentSignals(**signals_raw)
|
| 211 |
-
except Exception:
|
| 212 |
-
signals = None
|
| 213 |
-
|
| 214 |
-
obs = TrustObservation(
|
| 215 |
-
ticket_id=obs_data.get("ticket_id", ""),
|
| 216 |
-
post_text=obs_data.get("post_text", ""),
|
| 217 |
-
image_description=obs_data.get("image_description", ""),
|
| 218 |
-
comments_found=obs_data.get("comments_found"),
|
| 219 |
-
user_history_found=obs_data.get("user_history_found"),
|
| 220 |
-
entity_status_found=obs_data.get("entity_status_found"),
|
| 221 |
-
policy_found=obs_data.get("policy_found"),
|
| 222 |
-
extracted_signals=obs_data.get("extracted_signals"),
|
| 223 |
-
validation_result=obs_data.get("validation_result"),
|
| 224 |
-
step_number=obs_data.get("step_number", 0),
|
| 225 |
-
info=obs_data.get("info"),
|
| 226 |
-
done=payload.get("done", obs_data.get("done", False)),
|
| 227 |
-
reward=payload.get("reward", obs_data.get("reward")),
|
| 228 |
-
)
|
| 229 |
-
return StepResult(
|
| 230 |
-
observation=obs,
|
| 231 |
-
reward=payload.get("reward", obs_data.get("reward")),
|
| 232 |
-
done=payload.get("done", obs_data.get("done", False)),
|
| 233 |
-
)
|
| 234 |
-
|
| 235 |
-
def _parse_state(self, payload: dict) -> TrustState:
|
| 236 |
-
return TrustState(
|
| 237 |
-
episode_id=payload.get("episode_id"),
|
| 238 |
-
step_count=payload.get("step_count", 0),
|
| 239 |
-
current_task_id=payload.get("current_task_id"),
|
| 240 |
-
difficulty=payload.get("difficulty"),
|
| 241 |
-
ambiguity_level=payload.get("ambiguity_level"),
|
| 242 |
-
risk_level=payload.get("risk_level"),
|
| 243 |
-
tools_used=payload.get("tools_used", []),
|
| 244 |
-
signals_extracted=payload.get("signals_extracted", False),
|
| 245 |
-
is_done=payload.get("is_done", False),
|
| 246 |
-
)
|
| 247 |
-
|
| 248 |
-
DockerFile:
|
| 249 |
-
FROM python:3.11-slim
|
| 250 |
-
|
| 251 |
-
WORKDIR /app
|
| 252 |
-
|
| 253 |
-
RUN apt-get update \
|
| 254 |
-
&& apt-get install -y --no-install-recommends curl \
|
| 255 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 256 |
-
|
| 257 |
-
COPY pyproject.toml .
|
| 258 |
-
RUN pip install --no-cache-dir -e "."
|
| 259 |
-
|
| 260 |
-
COPY . .
|
| 261 |
-
|
| 262 |
-
ENV PYTHONPATH="/app:$PYTHONPATH"
|
| 263 |
-
|
| 264 |
-
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
|
| 265 |
-
CMD curl -f http://localhost:8000/health || exit 1
|
| 266 |
-
|
| 267 |
-
EXPOSE 8000
|
| 268 |
-
|
| 269 |
-
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
inference.py:
|
| 273 |
-
"""
|
| 274 |
-
inference.py — Trust & Safety RL Environment Evaluation
|
| 275 |
-
========================================================
|
| 276 |
-
MANDATORY env vars:
|
| 277 |
-
API_BASE_URL LLM endpoint (e.g. https://router.huggingface.co/v1)
|
| 278 |
-
MODEL_NAME Model ID (e.g. meta-llama/Llama-3.1-8B-Instruct)
|
| 279 |
-
HF_TOKEN API key
|
| 280 |
-
ENV_BASE_URL Environment server URL (default: http://localhost:8000)
|
| 281 |
-
"""
|
| 282 |
-
|
| 283 |
-
import os, json, time, requests
|
| 284 |
-
from openai import OpenAI
|
| 285 |
-
|
| 286 |
-
API_BASE_URL = os.environ.get("API_BASE_URL", "https://router.huggingface.co/v1")
|
| 287 |
-
API_KEY = os.environ.get("HF_TOKEN") or os.environ.get("API_KEY", "")
|
| 288 |
-
MODEL_NAME = os.environ.get("MODEL_NAME", "meta-llama/Llama-3.1-8B-Instruct")
|
| 289 |
-
ENV_BASE_URL = os.environ.get("ENV_BASE_URL", "http://localhost:8000")
|
| 290 |
-
|
| 291 |
-
if not API_KEY:
|
| 292 |
-
raise EnvironmentError("Set HF_TOKEN (your Hugging Face / API key).")
|
| 293 |
-
if not MODEL_NAME:
|
| 294 |
-
raise EnvironmentError("Set MODEL_NAME.")
|
| 295 |
-
|
| 296 |
-
SYSTEM_PROMPT = """You are a Trust & Safety analyst. Follow this EXACT 3-stage pipeline.
|
| 297 |
-
Skipping any stage results in a score penalty.
|
| 298 |
-
|
| 299 |
-
════════════════════════════════════════════
|
| 300 |
-
STAGE 1 — Investigate (optional)
|
| 301 |
-
════════════════════════════════════════════
|
| 302 |
-
Use tools only when context is needed. For OBVIOUS violations (spam, phishing), skip.
|
| 303 |
-
|
| 304 |
-
{"action_type": "use_tool", "tool_name": "read_comments"}
|
| 305 |
-
{"action_type": "use_tool", "tool_name": "check_user_history"}
|
| 306 |
-
{"action_type": "use_tool", "tool_name": "check_entity_status"}
|
| 307 |
-
{"action_type": "use_tool", "tool_name": "view_policy"}
|
| 308 |
-
|
| 309 |
-
════════════════════════════════════════════
|
| 310 |
-
STAGE 2 — Extract signals (MANDATORY)
|
| 311 |
-
════════════════════════════════════════════
|
| 312 |
-
You MUST call extract_signals before final_decision. No exceptions.
|
| 313 |
-
You are a FEATURE EXTRACTOR — output what the content shows, do NOT decide.
|
| 314 |
-
|
| 315 |
-
{"action_type": "extract_signals", "signals": {
|
| 316 |
-
"target": "individual|group|religion|race|gender|political|none",
|
| 317 |
-
"is_protected_class": true|false,
|
| 318 |
-
"toxicity_level": 0.0-1.0,
|
| 319 |
-
"is_direct_attack": true|false,
|
| 320 |
-
"context_type": "statement|satire|news|meme|personal_attack|protest|education|spam",
|
| 321 |
-
"intent": "harmful|descriptive|ambiguous|satirical|educational",
|
| 322 |
-
"confidence": 0.0-1.0,
|
| 323 |
-
"abusive_language_present": true|false,
|
| 324 |
-
"content_flags": ["spam","phishing","slur","threat","misinformation",
|
| 325 |
-
"political_content","public_figure","competitive_language"]
|
| 326 |
-
}}
|
| 327 |
-
|
| 328 |
-
After submitting signals you receive VALIDATION. If needs_escalation_hint is true → prefer ESCALATE.
|
| 329 |
-
|
| 330 |
-
════════════════════════════════════════════
|
| 331 |
-
STAGE 3 — Final decision (MANDATORY)
|
| 332 |
-
════════════════════════════════════════════
|
| 333 |
-
{"action_type": "final_decision", "final_decision": "ALLOW|REMOVE|ALLOW_WITH_WARNING|ESCALATE"}
|
| 334 |
-
|
| 335 |
-
DECISION RULES (apply in order):
|
| 336 |
-
1. Phishing / spam / impersonation → REMOVE
|
| 337 |
-
2. Explicit hate speech, direct threats → REMOVE
|
| 338 |
-
3. Political satire of a PUBLIC FIGURE → ALLOW_WITH_WARNING
|
| 339 |
-
4. News / journalism / education → ALLOW_WITH_WARNING
|
| 340 |
-
5. Gaming / sports banter (confirmed) → ALLOW
|
| 341 |
-
6. Adjusted confidence after validation < 0.6 → ESCALATE
|
| 342 |
-
7. Genuinely ambiguous, no clear rule → ESCALATE
|
| 343 |
-
8. Safe, no signals triggered → ALLOW
|
| 344 |
-
|
| 345 |
-
NEVER jump from Stage 1 directly to final_decision.
|
| 346 |
-
ALWAYS: [optional tools] → extract_signals → final_decision
|
| 347 |
-
Respond in strict JSON only. No markdown."""
|
| 348 |
-
|
| 349 |
-
SIGNAL_DEFAULTS = {
|
| 350 |
-
"target": "none", "is_protected_class": False,
|
| 351 |
-
"toxicity_level": 0.5, "is_direct_attack": False,
|
| 352 |
-
"context_type": "statement", "intent": "ambiguous",
|
| 353 |
-
"confidence": 0.5, "abusive_language_present": False,
|
| 354 |
-
"content_flags": [],
|
| 355 |
-
}
|
| 356 |
-
VALID_TOOLS = {"read_comments","check_user_history","check_entity_status","view_policy"}
|
| 357 |
-
VALID_DECISIONS = {"ALLOW","REMOVE","ALLOW_WITH_WARNING","ESCALATE"}
|
| 358 |
-
VALID_TYPES = {"use_tool","extract_signals","final_decision"}
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
def _clamp_signals(raw: dict) -> dict:
|
| 362 |
-
result = SIGNAL_DEFAULTS.copy()
|
| 363 |
-
for k in SIGNAL_DEFAULTS:
|
| 364 |
-
if k in raw:
|
| 365 |
-
result[k] = raw[k]
|
| 366 |
-
try:
|
| 367 |
-
result["toxicity_level"] = max(0.0, min(1.0, float(result["toxicity_level"])))
|
| 368 |
-
result["confidence"] = max(0.0, min(1.0, float(result["confidence"])))
|
| 369 |
-
except (TypeError, ValueError):
|
| 370 |
-
result["toxicity_level"] = 0.5
|
| 371 |
-
result["confidence"] = 0.5
|
| 372 |
-
if not isinstance(result["content_flags"], list):
|
| 373 |
-
result["content_flags"] = []
|
| 374 |
-
return result
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
def _parse(text: str) -> dict:
|
| 378 |
-
text = text.strip()
|
| 379 |
-
s, e = text.find("{"), text.rfind("}") + 1
|
| 380 |
-
if s == -1 or e == 0:
|
| 381 |
-
raise ValueError(f"No JSON in: {text}")
|
| 382 |
-
return json.loads(text[s:e])
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
def _normalize(raw: dict) -> dict:
|
| 386 |
-
t = raw.get("action_type", "")
|
| 387 |
-
if t not in VALID_TYPES:
|
| 388 |
-
return {"action_type": "final_decision", "final_decision": "ESCALATE"}
|
| 389 |
-
if t == "use_tool":
|
| 390 |
-
tool = raw.get("tool_name", "")
|
| 391 |
-
return {"action_type": "use_tool", "tool_name": tool} if tool in VALID_TOOLS \
|
| 392 |
-
else {"action_type": "final_decision", "final_decision": "ESCALATE"}
|
| 393 |
-
if t == "extract_signals":
|
| 394 |
-
sigs = raw.get("signals")
|
| 395 |
-
return {"action_type": "extract_signals", "signals": _clamp_signals(sigs)} \
|
| 396 |
-
if sigs else {"action_type": "final_decision", "final_decision": "ESCALATE"}
|
| 397 |
-
dec = raw.get("final_decision", "ESCALATE")
|
| 398 |
-
return {"action_type": "final_decision",
|
| 399 |
-
"final_decision": dec if dec in VALID_DECISIONS else "ESCALATE"}
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
def _obs_to_prompt(obs: dict) -> str:
|
| 403 |
-
lines = [
|
| 404 |
-
f"=== TICKET {obs.get('ticket_id','')} (Step {obs.get('step_number',0)}) ===",
|
| 405 |
-
f"\nPOST TEXT:\n{obs.get('post_text','')}",
|
| 406 |
-
f"\nIMAGE:\n{obs.get('image_description','')}",
|
| 407 |
-
]
|
| 408 |
-
for key, label in [
|
| 409 |
-
("comments_found","COMMENTS"),("user_history_found","USER HISTORY"),
|
| 410 |
-
("entity_status_found","ENTITY STATUS"),("policy_found","POLICY"),
|
| 411 |
-
]:
|
| 412 |
-
if obs.get(key):
|
| 413 |
-
lines.append(f"\n{label}:\n{obs[key]}")
|
| 414 |
-
if obs.get("extracted_signals"):
|
| 415 |
-
lines.append(f"\nYOUR EXTRACTED SIGNALS:\n{json.dumps(obs['extracted_signals'],indent=2)}")
|
| 416 |
-
if obs.get("validation_result"):
|
| 417 |
-
v = obs["validation_result"]
|
| 418 |
-
hint = "⚠️ YES — prefer ESCALATE" if v.get("needs_escalation_hint") else "No"
|
| 419 |
-
lines.append(
|
| 420 |
-
f"\n📋 VALIDATION:\n"
|
| 421 |
-
f" Adj. Confidence : {v.get('adjusted_confidence')}\n"
|
| 422 |
-
f" Issues : {v.get('consistency_issues')}\n"
|
| 423 |
-
f" Escalation Hint : {hint}"
|
| 424 |
-
)
|
| 425 |
-
if not obs.get("extracted_signals"):
|
| 426 |
-
lines.append("\n⚠️ REMINDER: Call extract_signals before final_decision.")
|
| 427 |
-
lines.append("\nYour next action (strict JSON only):")
|
| 428 |
-
return "\n".join(lines)
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
def run_task(client: OpenAI, task_id: str) -> float:
|
| 432 |
-
for _ in range(30):
|
| 433 |
-
# CORRECT ✅ — pass task ID directly
|
| 434 |
-
r = requests.post(
|
| 435 |
-
f"{ENV_BASE_URL}/reset",
|
| 436 |
-
json={"episode_id": task_id}, # ← this is the only change
|
| 437 |
-
timeout=10
|
| 438 |
-
)
|
| 439 |
-
r.raise_for_status()
|
| 440 |
-
obs = r.json()
|
| 441 |
-
# Handle both flat (TrustObservation) and wrapped response
|
| 442 |
-
if isinstance(obs, dict) and "observation" in obs:
|
| 443 |
-
obs = obs["observation"]
|
| 444 |
-
if obs.get("ticket_id") == task_id:
|
| 445 |
-
break
|
| 446 |
-
else:
|
| 447 |
-
raise RuntimeError(f"Could not get task {task_id} after 30 resets.")
|
| 448 |
-
|
| 449 |
-
print(f"\n{'='*62}\nTask: {task_id} | Starting...\n{'='*62}")
|
| 450 |
-
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 451 |
-
final_reward = 0.0
|
| 452 |
-
|
| 453 |
-
for step_num in range(14):
|
| 454 |
-
messages.append({"role": "user", "content": _obs_to_prompt(obs)})
|
| 455 |
-
time.sleep(0.5)
|
| 456 |
-
|
| 457 |
-
resp = client.chat.completions.create(
|
| 458 |
-
model=MODEL_NAME, messages=messages, temperature=0.0,
|
| 459 |
-
response_format={"type": "json_object"},
|
| 460 |
-
)
|
| 461 |
-
llm_text = resp.choices[0].message.content or ""
|
| 462 |
-
messages.append({"role": "assistant", "content": llm_text})
|
| 463 |
-
|
| 464 |
-
try:
|
| 465 |
-
action = _normalize(_parse(llm_text))
|
| 466 |
-
except Exception as ex:
|
| 467 |
-
print(f" [Step {step_num+1}] Parse error: {ex}"); break
|
| 468 |
-
|
| 469 |
-
atype = action["action_type"]
|
| 470 |
-
if atype == "use_tool":
|
| 471 |
-
print(f" [Step {step_num+1}] 🔧 use_tool → {action.get('tool_name')}")
|
| 472 |
-
elif atype == "extract_signals":
|
| 473 |
-
s = action.get("signals", {})
|
| 474 |
-
print(f" [Step {step_num+1}] 🔍 extract_signals → "
|
| 475 |
-
f"intent={s.get('intent')} | ctx={s.get('context_type')} | "
|
| 476 |
-
f"tox={s.get('toxicity_level')} | conf={s.get('confidence')}")
|
| 477 |
-
else:
|
| 478 |
-
print(f" [Step {step_num+1}] ⚖️ final_decision → {action.get('final_decision')}")
|
| 479 |
-
|
| 480 |
-
r2 = requests.post(f"{ENV_BASE_URL}/step", json=action, timeout=30)
|
| 481 |
-
r2.raise_for_status()
|
| 482 |
-
result = r2.json()
|
| 483 |
-
|
| 484 |
-
# Handle flat (TrustObservation) and wrapped response
|
| 485 |
-
if "observation" in result:
|
| 486 |
-
obs = result["observation"]
|
| 487 |
-
done = result.get("done", obs.get("done", False))
|
| 488 |
-
final_reward = float(result.get("reward") or obs.get("reward") or 0.0)
|
| 489 |
-
else:
|
| 490 |
-
obs = result
|
| 491 |
-
done = result.get("done", False)
|
| 492 |
-
final_reward = float(result.get("reward") or 0.0)
|
| 493 |
-
|
| 494 |
-
if done:
|
| 495 |
-
info = obs.get("info") or {}
|
| 496 |
-
bd = info.get("reward_breakdown", {})
|
| 497 |
-
pol = info.get("policy_recommendation", {})
|
| 498 |
-
vr = obs.get("validation_result") or {}
|
| 499 |
-
|
| 500 |
-
print(f"\n ── EPISODE COMPLETE {'─'*42}")
|
| 501 |
-
print(f" Decision: {info.get('final_decision','N/A')}")
|
| 502 |
-
print(f" Ground Truth: {info.get('ground_truth','N/A')}")
|
| 503 |
-
print(f" Policy Engine: {pol.get('recommended','N/A')} "
|
| 504 |
-
f"[{pol.get('rule_strength','?')} rule] ({pol.get('reason','?')})")
|
| 505 |
-
print(f" Signals Extracted: {'✅' if info.get('signals_extracted') else '❌ SKIPPED'}")
|
| 506 |
-
print(f" Tools Used: {info.get('tools_used', [])}")
|
| 507 |
-
print(f" Required Tools: {info.get('required_tools', [])}")
|
| 508 |
-
print(f" Adj. Confidence: {vr.get('adjusted_confidence','N/A')}")
|
| 509 |
-
print(f" Issues: {vr.get('consistency_issues',[])}")
|
| 510 |
-
print(f" Ambiguity / Risk: {info.get('ambiguity_level','?')} / {info.get('risk_level','?')}")
|
| 511 |
-
if bd:
|
| 512 |
-
print(f"\n ── Reward Breakdown {'─'*42}")
|
| 513 |
-
print(f" 1. Base Decision Score: {bd.get('base_score',0):+.4f}")
|
| 514 |
-
print(f" 2. Policy Alignment: {bd.get('policy_alignment',0):+.4f}")
|
| 515 |
-
print(f" 3. Signal Accuracy Bonus: {bd.get('signal_accuracy_bonus',0):+.4f}")
|
| 516 |
-
print(f" 4. Escalation Adjustment: {bd.get('escalation_adj',0):+.4f}")
|
| 517 |
-
print(f" 5. Signal Process Bonus: {bd.get('signal_bonus',0):+.4f}")
|
| 518 |
-
print(f" Tool Cost: -{bd.get('tool_cost',0):.4f}")
|
| 519 |
-
print(f" Tool Miss Penalty: -{bd.get('tool_miss_penalty',0):.4f}")
|
| 520 |
-
print(f" Validation Penalty: -{bd.get('validation_penalty',0):.4f}")
|
| 521 |
-
print(f" Risk Penalty: -{bd.get('risk_penalty',0):.4f}")
|
| 522 |
-
print(f" Confidence Discipline: -{bd.get('confidence_penalty',0):.4f}")
|
| 523 |
-
print(f" {'─'*60}")
|
| 524 |
-
print(f" FINAL REWARD: {bd.get('final_reward',0):.4f}")
|
| 525 |
-
print(f"\n SCORE: {final_reward:.4f}")
|
| 526 |
-
break
|
| 527 |
-
|
| 528 |
-
return final_reward
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
def main() -> None:
|
| 532 |
-
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 533 |
-
|
| 534 |
-
print("=" * 62)
|
| 535 |
-
print("Trust & Safety RL Environment — Baseline Evaluation")
|
| 536 |
-
print("=" * 62)
|
| 537 |
-
print(f"Model : {MODEL_NAME}")
|
| 538 |
-
print(f"LLM API : {API_BASE_URL}")
|
| 539 |
-
print(f"Env Server : {ENV_BASE_URL}")
|
| 540 |
-
print(f"Reward : Accuracy · Policy · Signals · Escalation")
|
| 541 |
-
print(f" Tools · Consistency · Risk · Confidence")
|
| 542 |
-
|
| 543 |
-
tasks = [
|
| 544 |
-
("T-001", "Easy — Phishing Spam", "low"),
|
| 545 |
-
("T-002", "Medium — Gaming Banter", "low"),
|
| 546 |
-
("T-003", "Hard — Political Satire", "high"),
|
| 547 |
-
]
|
| 548 |
-
scores = []
|
| 549 |
-
for tid, desc, risk in tasks:
|
| 550 |
-
print(f"\n\n>>> {tid} | {desc} | Risk: {risk}")
|
| 551 |
-
scores.append((tid, desc, run_task(client, tid)))
|
| 552 |
-
|
| 553 |
-
print("\n" + "=" * 62)
|
| 554 |
-
print("FINAL BASELINE RESULTS")
|
| 555 |
-
print("=" * 62)
|
| 556 |
-
total = 0.0
|
| 557 |
-
for tid, desc, s in scores:
|
| 558 |
-
print(f" {tid} | {desc:<32} | {s:.4f} {'✅ PASS' if s >= 0.6 else '❌ FAIL'}")
|
| 559 |
-
total += s
|
| 560 |
-
vals = [s for _, _, s in scores]
|
| 561 |
-
print(f"\n Average : {total/len(scores):.4f}")
|
| 562 |
-
print(f" Min : {min(vals):.4f} | Max : {max(vals):.4f}")
|
| 563 |
-
print("=" * 62)
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
if __name__ == "__main__":
|
| 567 |
-
main()
|
| 568 |
-
|
| 569 |
-
models.py:
|
| 570 |
-
"""
|
| 571 |
-
inference.py — Trust & Safety RL Environment Evaluation
|
| 572 |
-
========================================================
|
| 573 |
-
MANDATORY env vars:
|
| 574 |
-
API_BASE_URL LLM endpoint (e.g. https://router.huggingface.co/v1)
|
| 575 |
-
MODEL_NAME Model ID (e.g. meta-llama/Llama-3.1-8B-Instruct)
|
| 576 |
-
HF_TOKEN API key
|
| 577 |
-
ENV_BASE_URL Environment server URL (default: http://localhost:8000)
|
| 578 |
-
"""
|
| 579 |
-
|
| 580 |
-
import os, json, time, requests
|
| 581 |
-
from openai import OpenAI
|
| 582 |
-
|
| 583 |
-
API_BASE_URL = os.environ.get("API_BASE_URL", "https://router.huggingface.co/v1")
|
| 584 |
-
API_KEY = os.environ.get("HF_TOKEN") or os.environ.get("API_KEY", "")
|
| 585 |
-
MODEL_NAME = os.environ.get("MODEL_NAME", "meta-llama/Llama-3.1-8B-Instruct")
|
| 586 |
-
ENV_BASE_URL = os.environ.get("ENV_BASE_URL", "http://localhost:8000")
|
| 587 |
-
|
| 588 |
-
if not API_KEY:
|
| 589 |
-
raise EnvironmentError("Set HF_TOKEN (your Hugging Face / API key).")
|
| 590 |
-
if not MODEL_NAME:
|
| 591 |
-
raise EnvironmentError("Set MODEL_NAME.")
|
| 592 |
-
|
| 593 |
-
SYSTEM_PROMPT = """You are a Trust & Safety analyst. Follow this EXACT 3-stage pipeline.
|
| 594 |
-
Skipping any stage results in a score penalty.
|
| 595 |
-
|
| 596 |
-
════════════════════════════════════════════
|
| 597 |
-
STAGE 1 — Investigate (optional)
|
| 598 |
-
════════════════════════════════════════════
|
| 599 |
-
Use tools only when context is needed. For OBVIOUS violations (spam, phishing), skip.
|
| 600 |
-
|
| 601 |
-
{"action_type": "use_tool", "tool_name": "read_comments"}
|
| 602 |
-
{"action_type": "use_tool", "tool_name": "check_user_history"}
|
| 603 |
-
{"action_type": "use_tool", "tool_name": "check_entity_status"}
|
| 604 |
-
{"action_type": "use_tool", "tool_name": "view_policy"}
|
| 605 |
-
|
| 606 |
-
════════════════════════════════════════════
|
| 607 |
-
STAGE 2 — Extract signals (MANDATORY)
|
| 608 |
-
════════════════════════════════════════════
|
| 609 |
-
You MUST call extract_signals before final_decision. No exceptions.
|
| 610 |
-
You are a FEATURE EXTRACTOR — output what the content shows, do NOT decide.
|
| 611 |
-
|
| 612 |
-
{"action_type": "extract_signals", "signals": {
|
| 613 |
-
"target": "individual|group|religion|race|gender|political|none",
|
| 614 |
-
"is_protected_class": true|false,
|
| 615 |
-
"toxicity_level": 0.0-1.0,
|
| 616 |
-
"is_direct_attack": true|false,
|
| 617 |
-
"context_type": "statement|satire|news|meme|personal_attack|protest|education|spam",
|
| 618 |
-
"intent": "harmful|descriptive|ambiguous|satirical|educational",
|
| 619 |
-
"confidence": 0.0-1.0,
|
| 620 |
-
"abusive_language_present": true|false,
|
| 621 |
-
"content_flags": ["spam","phishing","slur","threat","misinformation",
|
| 622 |
-
"political_content","public_figure","competitive_language"]
|
| 623 |
-
}}
|
| 624 |
-
|
| 625 |
-
After submitting signals you receive VALIDATION. If needs_escalation_hint is true → prefer ESCALATE.
|
| 626 |
-
|
| 627 |
-
════════════════════════════════════════════
|
| 628 |
-
STAGE 3 — Final decision (MANDATORY)
|
| 629 |
-
════════════════════════════════════════════
|
| 630 |
-
{"action_type": "final_decision", "final_decision": "ALLOW|REMOVE|ALLOW_WITH_WARNING|ESCALATE"}
|
| 631 |
-
|
| 632 |
-
DECISION RULES (apply in order):
|
| 633 |
-
1. Phishing / spam / impersonation → REMOVE
|
| 634 |
-
2. Explicit hate speech, direct threats → REMOVE
|
| 635 |
-
3. Political satire of a PUBLIC FIGURE → ALLOW_WITH_WARNING
|
| 636 |
-
4. News / journalism / education → ALLOW_WITH_WARNING
|
| 637 |
-
5. Gaming / sports banter (confirmed) → ALLOW
|
| 638 |
-
6. Adjusted confidence after validation < 0.6 → ESCALATE
|
| 639 |
-
7. Genuinely ambiguous, no clear rule → ESCALATE
|
| 640 |
-
8. Safe, no signals triggered → ALLOW
|
| 641 |
-
|
| 642 |
-
NEVER jump from Stage 1 directly to final_decision.
|
| 643 |
-
ALWAYS: [optional tools] → extract_signals → final_decision
|
| 644 |
-
Respond in strict JSON only. No markdown."""
|
| 645 |
-
|
| 646 |
-
SIGNAL_DEFAULTS = {
|
| 647 |
-
"target": "none", "is_protected_class": False,
|
| 648 |
-
"toxicity_level": 0.5, "is_direct_attack": False,
|
| 649 |
-
"context_type": "statement", "intent": "ambiguous",
|
| 650 |
-
"confidence": 0.5, "abusive_language_present": False,
|
| 651 |
-
"content_flags": [],
|
| 652 |
-
}
|
| 653 |
-
VALID_TOOLS = {"read_comments","check_user_history","check_entity_status","view_policy"}
|
| 654 |
-
VALID_DECISIONS = {"ALLOW","REMOVE","ALLOW_WITH_WARNING","ESCALATE"}
|
| 655 |
-
VALID_TYPES = {"use_tool","extract_signals","final_decision"}
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
def _clamp_signals(raw: dict) -> dict:
|
| 659 |
-
result = SIGNAL_DEFAULTS.copy()
|
| 660 |
-
for k in SIGNAL_DEFAULTS:
|
| 661 |
-
if k in raw:
|
| 662 |
-
result[k] = raw[k]
|
| 663 |
-
try:
|
| 664 |
-
result["toxicity_level"] = max(0.0, min(1.0, float(result["toxicity_level"])))
|
| 665 |
-
result["confidence"] = max(0.0, min(1.0, float(result["confidence"])))
|
| 666 |
-
except (TypeError, ValueError):
|
| 667 |
-
result["toxicity_level"] = 0.5
|
| 668 |
-
result["confidence"] = 0.5
|
| 669 |
-
if not isinstance(result["content_flags"], list):
|
| 670 |
-
result["content_flags"] = []
|
| 671 |
-
return result
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
def _parse(text: str) -> dict:
|
| 675 |
-
text = text.strip()
|
| 676 |
-
s, e = text.find("{"), text.rfind("}") + 1
|
| 677 |
-
if s == -1 or e == 0:
|
| 678 |
-
raise ValueError(f"No JSON in: {text}")
|
| 679 |
-
return json.loads(text[s:e])
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
def _normalize(raw: dict) -> dict:
|
| 683 |
-
t = raw.get("action_type", "")
|
| 684 |
-
if t not in VALID_TYPES:
|
| 685 |
-
return {"action_type": "final_decision", "final_decision": "ESCALATE"}
|
| 686 |
-
if t == "use_tool":
|
| 687 |
-
tool = raw.get("tool_name", "")
|
| 688 |
-
return {"action_type": "use_tool", "tool_name": tool} if tool in VALID_TOOLS \
|
| 689 |
-
else {"action_type": "final_decision", "final_decision": "ESCALATE"}
|
| 690 |
-
if t == "extract_signals":
|
| 691 |
-
sigs = raw.get("signals")
|
| 692 |
-
return {"action_type": "extract_signals", "signals": _clamp_signals(sigs)} \
|
| 693 |
-
if sigs else {"action_type": "final_decision", "final_decision": "ESCALATE"}
|
| 694 |
-
dec = raw.get("final_decision", "ESCALATE")
|
| 695 |
-
return {"action_type": "final_decision",
|
| 696 |
-
"final_decision": dec if dec in VALID_DECISIONS else "ESCALATE"}
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
def _obs_to_prompt(obs: dict) -> str:
|
| 700 |
-
lines = [
|
| 701 |
-
f"=== TICKET {obs.get('ticket_id','')} (Step {obs.get('step_number',0)}) ===",
|
| 702 |
-
f"\nPOST TEXT:\n{obs.get('post_text','')}",
|
| 703 |
-
f"\nIMAGE:\n{obs.get('image_description','')}",
|
| 704 |
-
]
|
| 705 |
-
for key, label in [
|
| 706 |
-
("comments_found","COMMENTS"),("user_history_found","USER HISTORY"),
|
| 707 |
-
("entity_status_found","ENTITY STATUS"),("policy_found","POLICY"),
|
| 708 |
-
]:
|
| 709 |
-
if obs.get(key):
|
| 710 |
-
lines.append(f"\n{label}:\n{obs[key]}")
|
| 711 |
-
if obs.get("extracted_signals"):
|
| 712 |
-
lines.append(f"\nYOUR EXTRACTED SIGNALS:\n{json.dumps(obs['extracted_signals'],indent=2)}")
|
| 713 |
-
if obs.get("validation_result"):
|
| 714 |
-
v = obs["validation_result"]
|
| 715 |
-
hint = "⚠️ YES — prefer ESCALATE" if v.get("needs_escalation_hint") else "No"
|
| 716 |
-
lines.append(
|
| 717 |
-
f"\n📋 VALIDATION:\n"
|
| 718 |
-
f" Adj. Confidence : {v.get('adjusted_confidence')}\n"
|
| 719 |
-
f" Issues : {v.get('consistency_issues')}\n"
|
| 720 |
-
f" Escalation Hint : {hint}"
|
| 721 |
-
)
|
| 722 |
-
if not obs.get("extracted_signals"):
|
| 723 |
-
lines.append("\n⚠️ REMINDER: Call extract_signals before final_decision.")
|
| 724 |
-
lines.append("\nYour next action (strict JSON only):")
|
| 725 |
-
return "\n".join(lines)
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
def run_task(client: OpenAI, task_id: str) -> float:
|
| 729 |
-
for _ in range(30):
|
| 730 |
-
# CORRECT ✅ — pass task ID directly
|
| 731 |
-
r = requests.post(
|
| 732 |
-
f"{ENV_BASE_URL}/reset",
|
| 733 |
-
json={"episode_id": task_id}, # ← this is the only change
|
| 734 |
-
timeout=10
|
| 735 |
-
)
|
| 736 |
-
r.raise_for_status()
|
| 737 |
-
obs = r.json()
|
| 738 |
-
# Handle both flat (TrustObservation) and wrapped response
|
| 739 |
-
if isinstance(obs, dict) and "observation" in obs:
|
| 740 |
-
obs = obs["observation"]
|
| 741 |
-
if obs.get("ticket_id") == task_id:
|
| 742 |
-
break
|
| 743 |
-
else:
|
| 744 |
-
raise RuntimeError(f"Could not get task {task_id} after 30 resets.")
|
| 745 |
-
|
| 746 |
-
print(f"\n{'='*62}\nTask: {task_id} | Starting...\n{'='*62}")
|
| 747 |
-
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 748 |
-
final_reward = 0.0
|
| 749 |
-
|
| 750 |
-
for step_num in range(14):
|
| 751 |
-
messages.append({"role": "user", "content": _obs_to_prompt(obs)})
|
| 752 |
-
time.sleep(0.5)
|
| 753 |
-
|
| 754 |
-
resp = client.chat.completions.create(
|
| 755 |
-
model=MODEL_NAME, messages=messages, temperature=0.0,
|
| 756 |
-
response_format={"type": "json_object"},
|
| 757 |
-
)
|
| 758 |
-
llm_text = resp.choices[0].message.content or ""
|
| 759 |
-
messages.append({"role": "assistant", "content": llm_text})
|
| 760 |
-
|
| 761 |
-
try:
|
| 762 |
-
action = _normalize(_parse(llm_text))
|
| 763 |
-
except Exception as ex:
|
| 764 |
-
print(f" [Step {step_num+1}] Parse error: {ex}"); break
|
| 765 |
-
|
| 766 |
-
atype = action["action_type"]
|
| 767 |
-
if atype == "use_tool":
|
| 768 |
-
print(f" [Step {step_num+1}] 🔧 use_tool → {action.get('tool_name')}")
|
| 769 |
-
elif atype == "extract_signals":
|
| 770 |
-
s = action.get("signals", {})
|
| 771 |
-
print(f" [Step {step_num+1}] 🔍 extract_signals → "
|
| 772 |
-
f"intent={s.get('intent')} | ctx={s.get('context_type')} | "
|
| 773 |
-
f"tox={s.get('toxicity_level')} | conf={s.get('confidence')}")
|
| 774 |
-
else:
|
| 775 |
-
print(f" [Step {step_num+1}] ⚖️ final_decision → {action.get('final_decision')}")
|
| 776 |
-
|
| 777 |
-
r2 = requests.post(f"{ENV_BASE_URL}/step", json=action, timeout=30)
|
| 778 |
-
r2.raise_for_status()
|
| 779 |
-
result = r2.json()
|
| 780 |
-
|
| 781 |
-
# Handle flat (TrustObservation) and wrapped response
|
| 782 |
-
if "observation" in result:
|
| 783 |
-
obs = result["observation"]
|
| 784 |
-
done = result.get("done", obs.get("done", False))
|
| 785 |
-
final_reward = float(result.get("reward") or obs.get("reward") or 0.0)
|
| 786 |
-
else:
|
| 787 |
-
obs = result
|
| 788 |
-
done = result.get("done", False)
|
| 789 |
-
final_reward = float(result.get("reward") or 0.0)
|
| 790 |
-
|
| 791 |
-
if done:
|
| 792 |
-
info = obs.get("info") or {}
|
| 793 |
-
bd = info.get("reward_breakdown", {})
|
| 794 |
-
pol = info.get("policy_recommendation", {})
|
| 795 |
-
vr = obs.get("validation_result") or {}
|
| 796 |
-
|
| 797 |
-
print(f"\n ── EPISODE COMPLETE {'─'*42}")
|
| 798 |
-
print(f" Decision: {info.get('final_decision','N/A')}")
|
| 799 |
-
print(f" Ground Truth: {info.get('ground_truth','N/A')}")
|
| 800 |
-
print(f" Policy Engine: {pol.get('recommended','N/A')} "
|
| 801 |
-
f"[{pol.get('rule_strength','?')} rule] ({pol.get('reason','?')})")
|
| 802 |
-
print(f" Signals Extracted: {'✅' if info.get('signals_extracted') else '❌ SKIPPED'}")
|
| 803 |
-
print(f" Tools Used: {info.get('tools_used', [])}")
|
| 804 |
-
print(f" Required Tools: {info.get('required_tools', [])}")
|
| 805 |
-
print(f" Adj. Confidence: {vr.get('adjusted_confidence','N/A')}")
|
| 806 |
-
print(f" Issues: {vr.get('consistency_issues',[])}")
|
| 807 |
-
print(f" Ambiguity / Risk: {info.get('ambiguity_level','?')} / {info.get('risk_level','?')}")
|
| 808 |
-
if bd:
|
| 809 |
-
print(f"\n ── Reward Breakdown {'─'*42}")
|
| 810 |
-
print(f" 1. Base Decision Score: {bd.get('base_score',0):+.4f}")
|
| 811 |
-
print(f" 2. Policy Alignment: {bd.get('policy_alignment',0):+.4f}")
|
| 812 |
-
print(f" 3. Signal Accuracy Bonus: {bd.get('signal_accuracy_bonus',0):+.4f}")
|
| 813 |
-
print(f" 4. Escalation Adjustment: {bd.get('escalation_adj',0):+.4f}")
|
| 814 |
-
print(f" 5. Signal Process Bonus: {bd.get('signal_bonus',0):+.4f}")
|
| 815 |
-
print(f" Tool Cost: -{bd.get('tool_cost',0):.4f}")
|
| 816 |
-
print(f" Tool Miss Penalty: -{bd.get('tool_miss_penalty',0):.4f}")
|
| 817 |
-
print(f" Validation Penalty: -{bd.get('validation_penalty',0):.4f}")
|
| 818 |
-
print(f" Risk Penalty: -{bd.get('risk_penalty',0):.4f}")
|
| 819 |
-
print(f" Confidence Discipline: -{bd.get('confidence_penalty',0):.4f}")
|
| 820 |
-
print(f" {'─'*60}")
|
| 821 |
-
print(f" FINAL REWARD: {bd.get('final_reward',0):.4f}")
|
| 822 |
-
print(f"\n SCORE: {final_reward:.4f}")
|
| 823 |
-
break
|
| 824 |
-
|
| 825 |
-
return final_reward
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
def main() -> None:
|
| 829 |
-
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 830 |
-
|
| 831 |
-
print("=" * 62)
|
| 832 |
-
print("Trust & Safety RL Environment — Baseline Evaluation")
|
| 833 |
-
print("=" * 62)
|
| 834 |
-
print(f"Model : {MODEL_NAME}")
|
| 835 |
-
print(f"LLM API : {API_BASE_URL}")
|
| 836 |
-
print(f"Env Server : {ENV_BASE_URL}")
|
| 837 |
-
print(f"Reward : Accuracy · Policy · Signals · Escalation")
|
| 838 |
-
print(f" Tools · Consistency · Risk · Confidence")
|
| 839 |
-
|
| 840 |
-
tasks = [
|
| 841 |
-
("T-001", "Easy — Phishing Spam", "low"),
|
| 842 |
-
("T-002", "Medium — Gaming Banter", "low"),
|
| 843 |
-
("T-003", "Hard — Political Satire", "high"),
|
| 844 |
-
]
|
| 845 |
-
scores = []
|
| 846 |
-
for tid, desc, risk in tasks:
|
| 847 |
-
print(f"\n\n>>> {tid} | {desc} | Risk: {risk}")
|
| 848 |
-
scores.append((tid, desc, run_task(client, tid)))
|
| 849 |
-
|
| 850 |
-
print("\n" + "=" * 62)
|
| 851 |
-
print("FINAL BASELINE RESULTS")
|
| 852 |
-
print("=" * 62)
|
| 853 |
-
total = 0.0
|
| 854 |
-
for tid, desc, s in scores:
|
| 855 |
-
print(f" {tid} | {desc:<32} | {s:.4f} {'✅ PASS' if s >= 0.6 else '❌ FAIL'}")
|
| 856 |
-
total += s
|
| 857 |
-
vals = [s for _, _, s in scores]
|
| 858 |
-
print(f"\n Average : {total/len(scores):.4f}")
|
| 859 |
-
print(f" Min : {min(vals):.4f} | Max : {max(vals):.4f}")
|
| 860 |
-
print("=" * 62)
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
if __name__ == "__main__":
|
| 864 |
-
main()
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
openenv.yaml:
|
| 868 |
-
spec_version: 1
|
| 869 |
-
name: trust-safety-env
|
| 870 |
-
type: environment
|
| 871 |
-
runtime: python
|
| 872 |
-
app: app:app
|
| 873 |
-
port: 8000
|
| 874 |
-
|
| 875 |
-
description: >
|
| 876 |
-
Risk-aware content moderation RL environment for Trust & Safety decision-making.
|
| 877 |
-
Agents investigate content, extract structured signals, and make policy-aligned
|
| 878 |
-
decisions under uncertainty across hate speech, political sensitivity, and
|
| 879 |
-
cultural nuance. Models real-world moderation at scale (Meta-style).
|
| 880 |
-
|
| 881 |
-
author: Jerome Richard D
|
| 882 |
-
version: "1.0.0"
|
| 883 |
-
license: MIT
|
| 884 |
-
|
| 885 |
-
action_space:
|
| 886 |
-
type: TrustAction
|
| 887 |
-
description: "use_tool | extract_signals | final_decision"
|
| 888 |
-
|
| 889 |
-
observation_space:
|
| 890 |
-
type: TrustObservation
|
| 891 |
-
description: "Content ticket with progressive context revelation"
|
| 892 |
-
|
| 893 |
-
tasks:
|
| 894 |
-
- id: T-001
|
| 895 |
-
name: Phishing Spam Detection
|
| 896 |
-
difficulty: easy
|
| 897 |
-
description: Identify and remove clear phishing / impersonation content
|
| 898 |
-
|
| 899 |
-
- id: T-002
|
| 900 |
-
name: Gaming Banter Classification
|
| 901 |
-
difficulty: medium
|
| 902 |
-
description: Distinguish competitive gaming banter from genuine harassment
|
| 903 |
-
|
| 904 |
-
- id: T-003
|
| 905 |
-
name: Political Satire Review
|
| 906 |
-
difficulty: hard
|
| 907 |
-
description: Handle editorial satire of public figures with high-risk sensitivity
|
| 908 |
-
|
| 909 |
-
- id: T-004
|
| 910 |
-
name: Hate Speech Disguised as Education
|
| 911 |
-
difficulty: medium
|
| 912 |
-
description: Detect hate speech hidden behind pseudoscientific or educational framing
|
| 913 |
-
|
| 914 |
-
- id: T-005
|
| 915 |
-
name: Political News with Protest Violence
|
| 916 |
-
difficulty: hard
|
| 917 |
-
description: Protect legitimate journalism on sensitive political events without over-censorship
|
| 918 |
-
|
| 919 |
-
- id: T-006
|
| 920 |
-
name: Religious Expression False Flag
|
| 921 |
-
difficulty: hard
|
| 922 |
-
description: Distinguish protected religious expression from automated false-positive flag
|
| 923 |
-
|
| 924 |
-
tags:
|
| 925 |
-
- content-moderation
|
| 926 |
-
- trust-safety
|
| 927 |
-
- hate-speech
|
| 928 |
-
- political-sensitivity
|
| 929 |
-
- cultural-nuance
|
| 930 |
-
- real-world
|
| 931 |
-
- openenv
|
| 932 |
-
|
| 933 |
-
pyproject.toml:
|
| 934 |
-
[build-system]
|
| 935 |
-
requires = ["setuptools>=68.0", "wheel"]
|
| 936 |
-
build-backend = "setuptools.backends.legacy:build"
|
| 937 |
-
|
| 938 |
-
[project]
|
| 939 |
-
name = "trust-safety-env"
|
| 940 |
-
version = "1.0.0"
|
| 941 |
-
description = "Risk-aware Trust & Safety content moderation RL environment — OpenEnv compatible"
|
| 942 |
-
readme = "README.md"
|
| 943 |
-
requires-python = ">=3.11"
|
| 944 |
-
dependencies = [
|
| 945 |
-
"openenv-core>=0.2.0",
|
| 946 |
-
"fastapi>=0.110.0",
|
| 947 |
-
"uvicorn[standard]>=0.29.0",
|
| 948 |
-
"pydantic>=2.6.0",
|
| 949 |
-
"openai>=1.30.0",
|
| 950 |
-
"requests>=2.31.0",
|
| 951 |
-
"python-dotenv>=1.0.0",
|
| 952 |
-
]
|
| 953 |
-
|
| 954 |
-
[project.optional-dependencies]
|
| 955 |
-
dev = ["pytest>=8.0"]
|
| 956 |
-
|
| 957 |
-
[tool.setuptools.packages.find]
|
| 958 |
-
where = ["."]
|
| 959 |
-
include = ["*"]
|
| 960 |
-
|
| 961 |
-
[tool.openenv]
|
| 962 |
-
name = "trust-safety-env"
|
| 963 |
-
environment_class = "your_environment.TrustSafetyEnvironment"
|
| 964 |
-
action_model = "models.TrustAction"
|
| 965 |
-
observation_model = "models.TrustObservation"
|
| 966 |
-
state_model = "models.TrustState"
|
| 967 |
-
|
| 968 |
-
requirements.txt:
|
| 969 |
-
|
| 970 |
-
fastapi>=0.110.0
|
| 971 |
-
uvicorn[standard]>=0.29.0
|
| 972 |
-
pydantic>=2.6.0
|
| 973 |
-
openai>=1.30.0
|
| 974 |
-
requests>=2.31.0
|
| 975 |
-
python-dotenv>=1.0.0
|
| 976 |
-
|
| 977 |
-
|
| 978 |
-
tasks.py :
|
| 979 |
-
TASKS = [
|
| 980 |
-
{
|
| 981 |
-
"task_id": "T-001",
|
| 982 |
-
"difficulty": "easy",
|
| 983 |
-
"ambiguity_level": "low",
|
| 984 |
-
"risk_level": "low",
|
| 985 |
-
"post_text": (
|
| 986 |
-
"🚨 URGENT: Your account has been COMPROMISED! "
|
| 987 |
-
"Click here immediately: http://meta-security-verify-now.xyz/login?token=abc123 "
|
| 988 |
-
"Act within 24 hours or your account will be PERMANENTLY BANNED. — Meta Security Team"
|
| 989 |
-
),
|
| 990 |
-
"image_description": (
|
| 991 |
-
"Poorly formatted alert page with mismatched Meta logos, "
|
| 992 |
-
"red urgent warning banners, and a suspicious external URL."
|
| 993 |
-
),
|
| 994 |
-
"tool_responses": {
|
| 995 |
-
"read_comments": "Comments disabled. Zero engagement.",
|
| 996 |
-
"check_user_history": "Account created 2 days ago. Zero posts. Flagged by spam filter 3×.",
|
| 997 |
-
"check_entity_status": "Domain 'meta-security-verify-now.xyz' registered 1 day ago — confirmed phishing domain.",
|
| 998 |
-
"view_policy": "Policy 3.2 — Phishing: Impersonating Meta to harvest credentials = immediate REMOVE.",
|
| 999 |
-
},
|
| 1000 |
-
"required_tools": [],
|
| 1001 |
-
"ground_truth": "REMOVE",
|
| 1002 |
-
"ground_truth_signals": {
|
| 1003 |
-
"target": "individual",
|
| 1004 |
-
"is_protected_class": False,
|
| 1005 |
-
"toxicity_level": 0.90,
|
| 1006 |
-
"is_direct_attack": True,
|
| 1007 |
-
"context_type": "spam",
|
| 1008 |
-
"intent": "harmful",
|
| 1009 |
-
"abusive_language_present": False,
|
| 1010 |
-
"content_flags": ["phishing", "spam", "impersonation"],
|
| 1011 |
-
},
|
| 1012 |
-
},
|
| 1013 |
-
{
|
| 1014 |
-
"task_id": "T-002",
|
| 1015 |
-
"difficulty": "medium",
|
| 1016 |
-
"ambiguity_level": "medium",
|
| 1017 |
-
"risk_level": "low",
|
| 1018 |
-
"post_text": (
|
| 1019 |
-
"LMAO I will literally SNIPE you the second the game starts, "
|
| 1020 |
-
"no mercy, you're getting absolutely destroyed 💀🎮 "
|
| 1021 |
-
"1v1 me and I'll end your whole career bro. Your team is DONE."
|
| 1022 |
-
),
|
| 1023 |
-
"image_description": (
|
| 1024 |
-
"Call of Duty: Warzone — squad wipe in progress. "
|
| 1025 |
-
"Kill notifications, sniper rifle, enemy team eliminated. Classic gaming screenshot."
|
| 1026 |
-
),
|
| 1027 |
-
"tool_responses": {
|
| 1028 |
-
"read_comments": (
|
| 1029 |
-
"47 comments: 'GG bro!', 'You always trash talk before CoD 😂', "
|
| 1030 |
-
"'rematch tonight??', 'your aim is mid bro'. "
|
| 1031 |
-
"All mutual gaming friends. Clear competitive banter context."
|
| 1032 |
-
),
|
| 1033 |
-
"check_user_history": (
|
| 1034 |
-
"Account 3 years active. 500+ posts, 90% gaming. "
|
| 1035 |
-
"Zero harassment reports. Regular CoD tournament player."
|
| 1036 |
-
),
|
| 1037 |
-
"check_entity_status": "Private individual. Standard gaming community member.",
|
| 1038 |
-
"view_policy": (
|
| 1039 |
-
"Policy 7.1 — Harassment: Hyperbolic language in established gaming communities "
|
| 1040 |
-
"is NOT harassment. 'Snipe', 'destroy', 'end your career' are non-literal expressions."
|
| 1041 |
-
),
|
| 1042 |
-
},
|
| 1043 |
-
"required_tools": ["read_comments"],
|
| 1044 |
-
"ground_truth": "ALLOW",
|
| 1045 |
-
"ground_truth_signals": {
|
| 1046 |
-
"target": "individual",
|
| 1047 |
-
"is_protected_class": False,
|
| 1048 |
-
"toxicity_level": 0.30,
|
| 1049 |
-
"is_direct_attack": False,
|
| 1050 |
-
"context_type": "meme",
|
| 1051 |
-
"intent": "ambiguous",
|
| 1052 |
-
"abusive_language_present": False,
|
| 1053 |
-
"content_flags": ["competitive_language"],
|
| 1054 |
-
},
|
| 1055 |
-
},
|
| 1056 |
-
{
|
| 1057 |
-
"task_id": "T-003",
|
| 1058 |
-
"difficulty": "hard",
|
| 1059 |
-
"ambiguity_level": "high",
|
| 1060 |
-
"risk_level": "high",
|
| 1061 |
-
"post_text": (
|
| 1062 |
-
"Sharing this political cartoon I drew. "
|
| 1063 |
-
"Mayor Davidson cutting the homeless shelter budget while sitting on a throne of gold coins. "
|
| 1064 |
-
"Caption: 'Priorities.' #LocalPolitics #HoldThemAccountable #SatiricalArt #MayorDavidson"
|
| 1065 |
-
),
|
| 1066 |
-
"image_description": (
|
| 1067 |
-
"Hand-drawn political cartoon. Exaggerated caricature of a public official on a coin throne, "
|
| 1068 |
-
"homeless families pushed away. Deliberately satirical editorial style. Artist signature visible."
|
| 1069 |
-
),
|
| 1070 |
-
"tool_responses": {
|
| 1071 |
-
"read_comments": (
|
| 1072 |
-
"23 comments: 'Powerful commentary', 'Free speech!', 'Great art', 'Defamation!'. "
|
| 1073 |
-
"No threats or coordinated harassment detected."
|
| 1074 |
-
),
|
| 1075 |
-
"check_user_history": (
|
| 1076 |
-
"Account 5 years. Verified local journalist and artist. "
|
| 1077 |
-
"Regular political commentary. No violations. Posts cited in local news."
|
| 1078 |
-
),
|
| 1079 |
-
"check_entity_status": (
|
| 1080 |
-
"Mayor Davidson: CONFIRMED PUBLIC FIGURE. "
|
| 1081 |
-
"Elected Mayor of Springfield. Active public interest in recent social services budget cuts. "
|
| 1082 |
-
"Multiple credible news articles confirm the cuts are real and newsworthy."
|
| 1083 |
-
),
|
| 1084 |
-
"view_policy": (
|
| 1085 |
-
"Policy 12.4 — Political Satire: Editorial cartoons targeting PUBLIC OFFICIALS "
|
| 1086 |
-
"on PUBLIC INTEREST matters are PROTECTED. Caricature is a recognized technique. "
|
| 1087 |
-
"Not realistic impersonation. Correct action: ALLOW_WITH_WARNING."
|
| 1088 |
-
),
|
| 1089 |
-
},
|
| 1090 |
-
"required_tools": ["check_entity_status", "view_policy"],
|
| 1091 |
-
"ground_truth": "ALLOW_WITH_WARNING",
|
| 1092 |
-
"ground_truth_signals": {
|
| 1093 |
-
"target": "political",
|
| 1094 |
-
"is_protected_class": False,
|
| 1095 |
-
"toxicity_level": 0.20,
|
| 1096 |
-
"is_direct_attack": False,
|
| 1097 |
-
"context_type": "satire",
|
| 1098 |
-
"intent": "satirical",
|
| 1099 |
-
"abusive_language_present": False,
|
| 1100 |
-
"content_flags": ["political_content", "public_figure"],
|
| 1101 |
-
},
|
| 1102 |
-
},
|
| 1103 |
-
# ── Append these to the TASKS list ──────────────────────────────────────────
|
| 1104 |
-
|
| 1105 |
-
{
|
| 1106 |
-
"task_id": "T-004",
|
| 1107 |
-
"difficulty": "medium",
|
| 1108 |
-
"ambiguity_level": "medium",
|
| 1109 |
-
"risk_level": "high",
|
| 1110 |
-
"post_text": (
|
| 1111 |
-
"Sharing this 'educational' video about why [ethnic group] people "
|
| 1112 |
-
"are genetically predisposed to criminal behaviour. "
|
| 1113 |
-
"The science doesn't lie. Share before it gets deleted. 👀 "
|
| 1114 |
-
"#HiddenTruths #CensoredFacts #JustAsking"
|
| 1115 |
-
),
|
| 1116 |
-
"image_description": (
|
| 1117 |
-
"Screenshot of a YouTube-style video thumbnail with a white lab coat figure "
|
| 1118 |
-
"and a graph. Title: 'The Uncomfortable Biology of Crime Rates'. "
|
| 1119 |
-
"Uploader has 0 verified credentials. Comments disabled."
|
| 1120 |
-
),
|
| 1121 |
-
"tool_responses": {
|
| 1122 |
-
"read_comments": (
|
| 1123 |
-
"Comments disabled by uploader. "
|
| 1124 |
-
"Zero engagement from credible academic or scientific accounts."
|
| 1125 |
-
),
|
| 1126 |
-
"check_user_history": (
|
| 1127 |
-
"Account 6 months old. 48 posts — all promoting pseudoscientific race content. "
|
| 1128 |
-
"Previously received 2 policy warnings for hate content. "
|
| 1129 |
-
"No verified credentials. Frequently uses 'just asking questions' framing."
|
| 1130 |
-
),
|
| 1131 |
-
"check_entity_status": (
|
| 1132 |
-
"Ethnic group mentioned: confirmed PROTECTED CLASS under platform policy. "
|
| 1133 |
-
"Claim about genetic criminality: DEBUNKED by mainstream biology and criminology. "
|
| 1134 |
-
"This framing is a known pseudoscience hate vector (scientific racism)."
|
| 1135 |
-
),
|
| 1136 |
-
"view_policy": (
|
| 1137 |
-
"Policy 4.3 — Hate Speech: Content attributing criminal or negative traits "
|
| 1138 |
-
"to a protected class based on race or ethnicity = REMOVE. "
|
| 1139 |
-
"Disguising hate speech as 'educational content' or 'science' does not "
|
| 1140 |
-
"change the violation. Action: REMOVE."
|
| 1141 |
-
),
|
| 1142 |
-
},
|
| 1143 |
-
"required_tools": ["check_entity_status", "view_policy"],
|
| 1144 |
-
"ground_truth": "REMOVE",
|
| 1145 |
-
"ground_truth_signals": {
|
| 1146 |
-
"target": "group",
|
| 1147 |
-
"is_protected_class": True,
|
| 1148 |
-
"toxicity_level": 0.85,
|
| 1149 |
-
"is_direct_attack": True,
|
| 1150 |
-
"context_type": "statement",
|
| 1151 |
-
"intent": "harmful",
|
| 1152 |
-
"abusive_language_present": False,
|
| 1153 |
-
"content_flags": ["slur", "misinformation", "hate_speech_disguised_as_education"],
|
| 1154 |
-
},
|
| 1155 |
-
},
|
| 1156 |
-
|
| 1157 |
-
{
|
| 1158 |
-
"task_id": "T-005",
|
| 1159 |
-
"difficulty": "hard",
|
| 1160 |
-
"ambiguity_level": "high",
|
| 1161 |
-
"risk_level": "high",
|
| 1162 |
-
"post_text": (
|
| 1163 |
-
"The Prime Minister announced new economic reforms today. "
|
| 1164 |
-
"Opposition leaders called it 'economic warfare on the poor.' "
|
| 1165 |
-
"Protests erupted in 3 cities. Security forces used tear gas. "
|
| 1166 |
-
"Government says the crackdown was 'necessary to restore order.' "
|
| 1167 |
-
"At least 12 protesters arrested. #Politics #Protest #Reform"
|
| 1168 |
-
),
|
| 1169 |
-
"image_description": (
|
| 1170 |
-
"News-style post with a photo of a large protest crowd holding signs. "
|
| 1171 |
-
"Tear gas clouds visible in background. "
|
| 1172 |
-
"Caption: 'Thousands take to streets against new austerity measures.' "
|
| 1173 |
-
"Shared by a verified news outlet handle. No graphic violence."
|
| 1174 |
-
),
|
| 1175 |
-
"tool_responses": {
|
| 1176 |
-
"read_comments": (
|
| 1177 |
-
"340 comments split sharply: "
|
| 1178 |
-
"'State violence against citizens!', 'Finally someone fixing the economy', "
|
| 1179 |
-
"'This is propaganda!', 'Biased reporting!', 'Necessary action'. "
|
| 1180 |
-
"High engagement, deep political polarization. No incitement to violence detected."
|
| 1181 |
-
),
|
| 1182 |
-
"check_user_history": (
|
| 1183 |
-
"Posted by @NationalNewsNetwork — verified news organization. "
|
| 1184 |
-
"12 years active. Consistently neutral coverage. "
|
| 1185 |
-
"No policy violations. Registered press entity."
|
| 1186 |
-
),
|
| 1187 |
-
"check_entity_status": (
|
| 1188 |
-
"Prime Minister: CONFIRMED PUBLIC FIGURE, head of government. "
|
| 1189 |
-
"Economic reforms: CONFIRMED real policy, actively debated in parliament. "
|
| 1190 |
-
"Protest: CONFIRMED real event, covered by 6+ independent news agencies. "
|
| 1191 |
-
"Political sensitivity: VERY HIGH — active governance dispute in a democracy."
|
| 1192 |
-
),
|
| 1193 |
-
"view_policy": (
|
| 1194 |
-
"Policy 12.1 — News & Political Content: Factual reporting on real government "
|
| 1195 |
-
"actions, protests, and police responses by VERIFIED NEWS entities is PROTECTED. "
|
| 1196 |
-
"Even politically contentious factual reporting is protected expression. "
|
| 1197 |
-
"Suppressing legitimate news = censorship risk. "
|
| 1198 |
-
"Action: ALLOW_WITH_WARNING (politically sensitive content label)."
|
| 1199 |
-
),
|
| 1200 |
-
},
|
| 1201 |
-
"required_tools": ["check_user_history", "check_entity_status", "view_policy"],
|
| 1202 |
-
"ground_truth": "ALLOW_WITH_WARNING",
|
| 1203 |
-
"ground_truth_signals": {
|
| 1204 |
-
"target": "political",
|
| 1205 |
-
"is_protected_class": False,
|
| 1206 |
-
"toxicity_level": 0.20,
|
| 1207 |
-
"is_direct_attack": False,
|
| 1208 |
-
"context_type": "news",
|
| 1209 |
-
"intent": "descriptive",
|
| 1210 |
-
"abusive_language_present": False,
|
| 1211 |
-
"content_flags": ["political_content", "public_figure", "protest"],
|
| 1212 |
-
},
|
| 1213 |
-
},
|
| 1214 |
-
|
| 1215 |
-
{
|
| 1216 |
-
"task_id": "T-006",
|
| 1217 |
-
"difficulty": "hard",
|
| 1218 |
-
"ambiguity_level": "high",
|
| 1219 |
-
"risk_level": "high",
|
| 1220 |
-
"post_text": (
|
| 1221 |
-
"Sending Eid Mubarak wishes to my community 🌙✨ "
|
| 1222 |
-
"During this holy month let us remember those suffering in conflict zones. "
|
| 1223 |
-
"Our brothers and sisters deserve peace and dignity. "
|
| 1224 |
-
"May Allah guide us all. #EidMubarak #Ramadan #Peace #Solidarity"
|
| 1225 |
-
),
|
| 1226 |
-
"image_description": (
|
| 1227 |
-
"Warm celebratory post with a crescent moon and stars graphic. "
|
| 1228 |
-
"Soft gold and green colors. "
|
| 1229 |
-
"Comment section shows replies in Arabic, Urdu, and English. "
|
| 1230 |
-
"Emojis: praying hands, crescent moon, dove."
|
| 1231 |
-
),
|
| 1232 |
-
"tool_responses": {
|
| 1233 |
-
"read_comments": (
|
| 1234 |
-
"89 comments: 'Eid Mubarak!', 'JazakAllah Khair', "
|
| 1235 |
-
"'prayers for peace', 'beautiful message', "
|
| 1236 |
-
"1 comment flagged by automated system: 'why is this allowed, "
|
| 1237 |
-
"these people support terrorism' — that comment is from a third party, "
|
| 1238 |
-
"not the original poster. "
|
| 1239 |
-
"No incitement, no threats, no extremist content in original post."
|
| 1240 |
-
),
|
| 1241 |
-
"check_user_history": (
|
| 1242 |
-
"Account 4 years active. 200+ posts — community announcements, "
|
| 1243 |
-
"religious holiday greetings, charity drives. "
|
| 1244 |
-
"Zero prior violations. Community leader in local Muslim cultural centre."
|
| 1245 |
-
),
|
| 1246 |
-
"check_entity_status": (
|
| 1247 |
-
"Islam / Muslim community: CONFIRMED PROTECTED CLASS (religion). "
|
| 1248 |
-
"Eid al-Fitr: globally recognized Islamic religious holiday. "
|
| 1249 |
-
"Content: religious greeting + humanitarian solidarity message. "
|
| 1250 |
-
"No extremist symbols, no prohibited organisations mentioned."
|
| 1251 |
-
),
|
| 1252 |
-
"view_policy": (
|
| 1253 |
-
"Policy 8.1 — Religious Expression: Greetings and solidarity messages "
|
| 1254 |
-
"for recognized religious holidays are PROTECTED. "
|
| 1255 |
-
"Vague 'conflict zone' solidarity without naming a prohibited group "
|
| 1256 |
-
"is NOT a violation. "
|
| 1257 |
-
"Automated flag was triggered by a COMMENT, not the original post. "
|
| 1258 |
-
"Action: ALLOW — original content is clearly safe religious expression."
|
| 1259 |
-
),
|
| 1260 |
-
},
|
| 1261 |
-
"required_tools": ["read_comments", "check_entity_status", "view_policy"],
|
| 1262 |
-
"ground_truth": "ALLOW",
|
| 1263 |
-
"ground_truth_signals": {
|
| 1264 |
-
"target": "religion",
|
| 1265 |
-
"is_protected_class": True,
|
| 1266 |
-
"toxicity_level": 0.05,
|
| 1267 |
-
"is_direct_attack": False,
|
| 1268 |
-
"context_type": "statement",
|
| 1269 |
-
"intent": "educational",
|
| 1270 |
-
"abusive_language_present": False,
|
| 1271 |
-
"content_flags": ["political_content"],
|
| 1272 |
-
},
|
| 1273 |
-
},
|
| 1274 |
-
]
|
| 1275 |
-
|
| 1276 |
-
your_environment.py:
|
| 1277 |
-
from __future__ import annotations
|
| 1278 |
-
|
| 1279 |
-
import random
|
| 1280 |
-
import uuid
|
| 1281 |
-
from typing import Optional, Dict, Any, Set
|
| 1282 |
-
|
| 1283 |
-
try:
|
| 1284 |
-
from openenv_core.env_server import Environment
|
| 1285 |
-
print("[env] Inheriting from openenv_core.env_server.Environment ✅")
|
| 1286 |
-
except ImportError:
|
| 1287 |
-
try:
|
| 1288 |
-
from openenv.core.env_server import Environment
|
| 1289 |
-
print("[env] Inheriting from openenv.core.env_server.Environment ✅")
|
| 1290 |
-
except ImportError:
|
| 1291 |
-
Environment = object
|
| 1292 |
-
print("[env] openenv_core not found — using plain object base ⚠️")
|
| 1293 |
-
|
| 1294 |
-
from models import TrustObservation, TrustAction, TrustState, ContentSignals
|
| 1295 |
-
from tasks import TASKS
|
| 1296 |
-
|
| 1297 |
-
|
| 1298 |
-
TOOL_COSTS: Dict[str, float] = {
|
| 1299 |
-
"read_comments": 0.05,
|
| 1300 |
-
"check_user_history": 0.05,
|
| 1301 |
-
"check_entity_status": 0.10,
|
| 1302 |
-
"view_policy": 0.10,
|
| 1303 |
-
}
|
| 1304 |
-
|
| 1305 |
-
MAX_STEPS = 7
|
| 1306 |
-
|
| 1307 |
-
DECISION_MATRIX: Dict[tuple, float] = {
|
| 1308 |
-
("REMOVE", "REMOVE"): 1.00,
|
| 1309 |
-
("ALLOW", "ALLOW"): 1.00,
|
| 1310 |
-
("ALLOW_WITH_WARNING", "ALLOW_WITH_WARNING"): 1.00,
|
| 1311 |
-
("ESCALATE", "ESCALATE"): 1.00,
|
| 1312 |
-
("ALLOW_WITH_WARNING", "ALLOW"): 0.75,
|
| 1313 |
-
("ALLOW", "ALLOW_WITH_WARNING"): 0.55,
|
| 1314 |
-
("ESCALATE", "ALLOW_WITH_WARNING"): 0.65,
|
| 1315 |
-
("ESCALATE", "ALLOW"): 0.45,
|
| 1316 |
-
("ESCALATE", "REMOVE"): 0.45,
|
| 1317 |
-
("REMOVE", "ALLOW"): 0.10,
|
| 1318 |
-
("REMOVE", "ALLOW_WITH_WARNING"): 0.20,
|
| 1319 |
-
("ALLOW", "REMOVE"): 0.00,
|
| 1320 |
-
("ALLOW_WITH_WARNING", "REMOVE"): 0.15,
|
| 1321 |
-
}
|
| 1322 |
-
|
| 1323 |
-
|
| 1324 |
-
class TrustSafetyEnvironment(Environment):
|
| 1325 |
-
"""
|
| 1326 |
-
3-Layer Risk-Aware Trust & Safety RL Environment.
|
| 1327 |
-
|
| 1328 |
-
Layer 1 — Evidence gathering : agent uses investigation tools (optional)
|
| 1329 |
-
Layer 2 — Signal extraction : agent outputs ContentSignals as feature extractor
|
| 1330 |
-
Layer 3 — Policy engine : validates signals, applies rules, computes reward
|
| 1331 |
-
|
| 1332 |
-
8-Component Reward: Accuracy · Policy Alignment · Signal Quality · Escalation
|
| 1333 |
-
Tool Usage · Consistency · Risk Sensitivity · Confidence
|
| 1334 |
-
"""
|
| 1335 |
-
|
| 1336 |
-
def __init__(self, seed: int = 42) -> None:
|
| 1337 |
-
super().__init__()
|
| 1338 |
-
self._rng = random.Random(seed)
|
| 1339 |
-
self._current_task: Optional[Dict[str, Any]] = None
|
| 1340 |
-
self._tools_used: Set[str] = set()
|
| 1341 |
-
self._step_count: int = 0
|
| 1342 |
-
self._extracted_signals: Optional[ContentSignals] = None
|
| 1343 |
-
self._validation_result: Optional[Dict[str, Any]] = None
|
| 1344 |
-
self._signals_extracted: bool = False
|
| 1345 |
-
self._obs: Optional[TrustObservation]= None
|
| 1346 |
-
self._state = TrustState()
|
| 1347 |
-
|
| 1348 |
-
# ✅ FIX 3 — build a dict keyed by task_id for O(1) lookup
|
| 1349 |
-
self._tasks: Dict[str, Dict[str, Any]] = {
|
| 1350 |
-
t["task_id"]: t for t in TASKS
|
| 1351 |
-
}
|
| 1352 |
-
|
| 1353 |
-
# -----------------------------------------------------------------------
|
| 1354 |
-
# OpenEnv interface
|
| 1355 |
-
# -----------------------------------------------------------------------
|
| 1356 |
-
|
| 1357 |
-
def reset(self, seed=None, episode_id=None, **kwargs) -> TrustObservation:
|
| 1358 |
-
# ✅ FIX 1 — reset() is now correctly INSIDE the class
|
| 1359 |
-
if seed is not None:
|
| 1360 |
-
self._rng.seed(seed)
|
| 1361 |
-
|
| 1362 |
-
# Pick task by episode_id if provided, else random from all 6
|
| 1363 |
-
if episode_id and episode_id in self._tasks:
|
| 1364 |
-
task = self._tasks[episode_id]
|
| 1365 |
-
else:
|
| 1366 |
-
task = self._rng.choice(list(self._tasks.values()))
|
| 1367 |
-
|
| 1368 |
-
self._current_task = task
|
| 1369 |
-
self._tools_used = set()
|
| 1370 |
-
self._step_count = 0
|
| 1371 |
-
self._extracted_signals = None
|
| 1372 |
-
self._validation_result = None
|
| 1373 |
-
self._signals_extracted = False
|
| 1374 |
-
|
| 1375 |
-
self._state = TrustState(
|
| 1376 |
-
episode_id=task["task_id"],
|
| 1377 |
-
step_count=0,
|
| 1378 |
-
current_task_id=task["task_id"],
|
| 1379 |
-
difficulty=task.get("difficulty", "medium"),
|
| 1380 |
-
risk_level=task.get("risk_level", "medium"),
|
| 1381 |
-
is_done=False,
|
| 1382 |
-
tools_used=[],
|
| 1383 |
-
signals_extracted=False,
|
| 1384 |
-
)
|
| 1385 |
-
|
| 1386 |
-
self._obs = TrustObservation(
|
| 1387 |
-
ticket_id=task["task_id"],
|
| 1388 |
-
post_text=task["post_text"],
|
| 1389 |
-
image_description=task.get("image_description", ""),
|
| 1390 |
-
step_number=0,
|
| 1391 |
-
done=False,
|
| 1392 |
-
)
|
| 1393 |
-
return self._obs # ✅ FIX 2 — single clean return, stray return removed
|
| 1394 |
-
|
| 1395 |
-
def step(self, action: TrustAction, timeouts: Optional[Any] = None,
|
| 1396 |
-
**kwargs) -> TrustObservation:
|
| 1397 |
-
if self._current_task is None or self._obs is None:
|
| 1398 |
-
raise RuntimeError("Call reset() before step().")
|
| 1399 |
-
|
| 1400 |
-
if self._step_count >= MAX_STEPS:
|
| 1401 |
-
self._obs = TrustObservation(
|
| 1402 |
-
ticket_id=self._current_task["task_id"],
|
| 1403 |
-
post_text=self._obs.post_text,
|
| 1404 |
-
image_description=self._obs.image_description,
|
| 1405 |
-
step_number=self._step_count,
|
| 1406 |
-
done=True,
|
| 1407 |
-
reward=0.0,
|
| 1408 |
-
info={"reason": "timeout", "tools_used": list(self._tools_used)},
|
| 1409 |
-
)
|
| 1410 |
-
return self._obs
|
| 1411 |
-
|
| 1412 |
-
atype = action.action_type
|
| 1413 |
-
if atype == "use_tool":
|
| 1414 |
-
return self._handle_tool(action)
|
| 1415 |
-
if atype == "extract_signals":
|
| 1416 |
-
return self._handle_signal_extraction(action)
|
| 1417 |
-
if atype == "final_decision":
|
| 1418 |
-
return self._handle_final_decision(action)
|
| 1419 |
-
raise ValueError(f"Unknown action_type: {atype!r}")
|
| 1420 |
-
|
| 1421 |
-
@property
|
| 1422 |
-
def state(self) -> TrustState:
|
| 1423 |
-
return self._state
|
| 1424 |
-
|
| 1425 |
-
# -----------------------------------------------------------------------
|
| 1426 |
-
# Layer 1 — Tool handling
|
| 1427 |
-
# -----------------------------------------------------------------------
|
| 1428 |
-
|
| 1429 |
-
def _handle_tool(self, action: TrustAction) -> TrustObservation:
|
| 1430 |
-
tool = action.tool_name
|
| 1431 |
-
if tool not in TOOL_COSTS:
|
| 1432 |
-
raise ValueError(f"Unknown tool: {tool!r}")
|
| 1433 |
-
self._tools_used.add(tool)
|
| 1434 |
-
response = self._current_task["tool_responses"].get(tool, "No data found.")
|
| 1435 |
-
field_map = {
|
| 1436 |
-
"read_comments": "comments_found",
|
| 1437 |
-
"check_user_history": "user_history_found",
|
| 1438 |
-
"check_entity_status": "entity_status_found",
|
| 1439 |
-
"view_policy": "policy_found",
|
| 1440 |
-
}
|
| 1441 |
-
self._step_count += 1
|
| 1442 |
-
self._state.step_count = self._step_count
|
| 1443 |
-
self._state.tools_used = list(self._tools_used)
|
| 1444 |
-
|
| 1445 |
-
obs_kwargs = {
|
| 1446 |
-
k: getattr(self._obs, k)
|
| 1447 |
-
for k in ("ticket_id", "post_text", "image_description",
|
| 1448 |
-
"comments_found", "user_history_found",
|
| 1449 |
-
"entity_status_found", "policy_found",
|
| 1450 |
-
"extracted_signals", "validation_result")
|
| 1451 |
-
}
|
| 1452 |
-
obs_kwargs[field_map[tool]] = response
|
| 1453 |
-
obs_kwargs["step_number"] = self._step_count
|
| 1454 |
-
obs_kwargs["done"] = False
|
| 1455 |
-
obs_kwargs["reward"] = None
|
| 1456 |
-
|
| 1457 |
-
self._obs = TrustObservation(**obs_kwargs)
|
| 1458 |
-
return self._obs
|
| 1459 |
-
|
| 1460 |
-
# -----------------------------------------------------------------------
|
| 1461 |
-
# Layer 2 — Signal extraction + validation
|
| 1462 |
-
# -----------------------------------------------------------------------
|
| 1463 |
-
|
| 1464 |
-
def _handle_signal_extraction(self, action: TrustAction) -> TrustObservation:
|
| 1465 |
-
raw = action.signals
|
| 1466 |
-
raw.toxicity_level = max(0.0, min(1.0, float(raw.toxicity_level)))
|
| 1467 |
-
raw.confidence = max(0.0, min(1.0, float(raw.confidence)))
|
| 1468 |
-
if not isinstance(raw.content_flags, list):
|
| 1469 |
-
raw.content_flags = []
|
| 1470 |
-
|
| 1471 |
-
self._extracted_signals = raw
|
| 1472 |
-
self._signals_extracted = True
|
| 1473 |
-
self._validation_result = self._validate_signals(raw)
|
| 1474 |
-
self._step_count += 1
|
| 1475 |
-
self._state.step_count = self._step_count
|
| 1476 |
-
self._state.signals_extracted = True
|
| 1477 |
-
|
| 1478 |
-
obs_kwargs = {
|
| 1479 |
-
k: getattr(self._obs, k)
|
| 1480 |
-
for k in ("ticket_id", "post_text", "image_description",
|
| 1481 |
-
"comments_found", "user_history_found",
|
| 1482 |
-
"entity_status_found", "policy_found")
|
| 1483 |
-
}
|
| 1484 |
-
obs_kwargs["extracted_signals"] = {
|
| 1485 |
-
"target": raw.target,
|
| 1486 |
-
"is_protected_class": raw.is_protected_class,
|
| 1487 |
-
"toxicity_level": raw.toxicity_level,
|
| 1488 |
-
"is_direct_attack": raw.is_direct_attack,
|
| 1489 |
-
"context_type": raw.context_type,
|
| 1490 |
-
"intent": raw.intent,
|
| 1491 |
-
"confidence": raw.confidence,
|
| 1492 |
-
"abusive_language_present": raw.abusive_language_present,
|
| 1493 |
-
"content_flags": raw.content_flags,
|
| 1494 |
-
}
|
| 1495 |
-
obs_kwargs["validation_result"] = self._validation_result
|
| 1496 |
-
obs_kwargs["step_number"] = self._step_count
|
| 1497 |
-
obs_kwargs["done"] = False
|
| 1498 |
-
obs_kwargs["reward"] = None
|
| 1499 |
-
|
| 1500 |
-
self._obs = TrustObservation(**obs_kwargs)
|
| 1501 |
-
return self._obs
|
| 1502 |
-
|
| 1503 |
-
def _validate_signals(self, s: ContentSignals) -> Dict[str, Any]:
|
| 1504 |
-
issues = []
|
| 1505 |
-
conf = s.confidence
|
| 1506 |
-
|
| 1507 |
-
if not s.abusive_language_present and s.toxicity_level > 0.75:
|
| 1508 |
-
issues.append("high_toxicity_without_abusive_language"); conf -= 0.15
|
| 1509 |
-
if s.context_type in ("satire", "education") and s.intent == "harmful":
|
| 1510 |
-
issues.append("harmful_intent_contradicts_satire_context"); conf -= 0.12
|
| 1511 |
-
if s.is_protected_class and s.target == "none":
|
| 1512 |
-
issues.append("protected_class_flagged_without_target"); conf -= 0.10
|
| 1513 |
-
if s.is_direct_attack and s.toxicity_level < 0.25:
|
| 1514 |
-
issues.append("direct_attack_with_low_toxicity"); conf -= 0.12
|
| 1515 |
-
if s.context_type == "spam" and "spam" not in s.content_flags \
|
| 1516 |
-
and "phishing" not in s.content_flags:
|
| 1517 |
-
issues.append("spam_context_missing_spam_flag"); conf -= 0.08
|
| 1518 |
-
|
| 1519 |
-
adj = round(max(0.0, min(1.0, conf)), 3)
|
| 1520 |
-
return {
|
| 1521 |
-
"original_confidence": s.confidence,
|
| 1522 |
-
"adjusted_confidence": adj,
|
| 1523 |
-
"consistency_issues": issues,
|
| 1524 |
-
"issue_count": len(issues),
|
| 1525 |
-
"is_consistent": len(issues) == 0,
|
| 1526 |
-
"needs_escalation_hint": adj < 0.55 or len(issues) >= 2,
|
| 1527 |
-
}
|
| 1528 |
-
|
| 1529 |
-
# -----------------------------------------------------------------------
|
| 1530 |
-
# Layer 3 — Policy engine + final decision
|
| 1531 |
-
# -----------------------------------------------------------------------
|
| 1532 |
-
|
| 1533 |
-
def _policy_engine(self) -> Dict[str, Any]:
|
| 1534 |
-
if not self._extracted_signals or not self._validation_result:
|
| 1535 |
-
return {"recommended": "ESCALATE", "reason": "no_signals_extracted",
|
| 1536 |
-
"rule_strength": "weak"}
|
| 1537 |
-
|
| 1538 |
-
s = self._extracted_signals
|
| 1539 |
-
adj = self._validation_result["adjusted_confidence"]
|
| 1540 |
-
|
| 1541 |
-
if adj < 0.50:
|
| 1542 |
-
return {"recommended": "ESCALATE", "reason": "low_adjusted_confidence",
|
| 1543 |
-
"rule_strength": "medium"}
|
| 1544 |
-
if "spam" in s.content_flags or "phishing" in s.content_flags:
|
| 1545 |
-
return {"recommended": "REMOVE", "reason": "spam_or_phishing_detected",
|
| 1546 |
-
"rule_strength": "strong"}
|
| 1547 |
-
if s.is_protected_class and s.is_direct_attack \
|
| 1548 |
-
and s.toxicity_level > 0.70 and adj > 0.70:
|
| 1549 |
-
return {"recommended": "REMOVE",
|
| 1550 |
-
"reason": "protected_class_direct_attack_high_toxicity",
|
| 1551 |
-
"rule_strength": "strong"}
|
| 1552 |
-
if s.toxicity_level > 0.85 and s.intent == "harmful":
|
| 1553 |
-
return {"recommended": "REMOVE", "reason": "high_toxicity_harmful_intent",
|
| 1554 |
-
"rule_strength": "strong"}
|
| 1555 |
-
if s.context_type in ("satire", "news", "education", "protest") \
|
| 1556 |
-
and s.intent in ("satirical", "descriptive", "educational"):
|
| 1557 |
-
return {"recommended": "ALLOW_WITH_WARNING",
|
| 1558 |
-
"reason": "protected_expression_context",
|
| 1559 |
-
"rule_strength": "medium"}
|
| 1560 |
-
if 0.40 < s.toxicity_level < 0.75 and s.intent == "ambiguous":
|
| 1561 |
-
return {"recommended": "ESCALATE", "reason": "ambiguous_moderate_toxicity",
|
| 1562 |
-
"rule_strength": "medium"}
|
| 1563 |
-
return {"recommended": "ALLOW", "reason": "no_policy_violation_detected",
|
| 1564 |
-
"rule_strength": "medium"}
|
| 1565 |
-
|
| 1566 |
-
def _handle_final_decision(self, action: TrustAction) -> TrustObservation:
|
| 1567 |
-
decision = action.final_decision
|
| 1568 |
-
components = self._compute_components(decision)
|
| 1569 |
-
policy_rec = components.pop("_policy_rec")
|
| 1570 |
-
reward = self._finalize_reward(components)
|
| 1571 |
-
|
| 1572 |
-
self._step_count += 1
|
| 1573 |
-
self._state.step_count = self._step_count
|
| 1574 |
-
self._state.is_done = True
|
| 1575 |
-
components["final_reward"] = reward
|
| 1576 |
-
|
| 1577 |
-
obs_kwargs = {
|
| 1578 |
-
k: getattr(self._obs, k)
|
| 1579 |
-
for k in ("ticket_id", "post_text", "image_description",
|
| 1580 |
-
"comments_found", "user_history_found",
|
| 1581 |
-
"entity_status_found", "policy_found",
|
| 1582 |
-
"extracted_signals", "validation_result")
|
| 1583 |
-
}
|
| 1584 |
-
obs_kwargs["step_number"] = self._step_count
|
| 1585 |
-
obs_kwargs["done"] = True
|
| 1586 |
-
obs_kwargs["reward"] = reward
|
| 1587 |
-
obs_kwargs["info"] = {
|
| 1588 |
-
"final_decision": decision,
|
| 1589 |
-
"ground_truth": self._current_task["ground_truth"],
|
| 1590 |
-
"policy_recommendation": policy_rec,
|
| 1591 |
-
"signals_extracted": self._signals_extracted,
|
| 1592 |
-
"tools_used": list(self._tools_used),
|
| 1593 |
-
"required_tools": self._current_task["required_tools"],
|
| 1594 |
-
"ambiguity_level": self._current_task["ambiguity_level"],
|
| 1595 |
-
"risk_level": self._current_task["risk_level"],
|
| 1596 |
-
"task_id": self._current_task["task_id"],
|
| 1597 |
-
"reward_breakdown": components,
|
| 1598 |
-
}
|
| 1599 |
-
|
| 1600 |
-
self._obs = TrustObservation(**obs_kwargs)
|
| 1601 |
-
return self._obs
|
| 1602 |
-
|
| 1603 |
-
# -----------------------------------------------------------------------
|
| 1604 |
-
# 8-Component Reward Engine
|
| 1605 |
-
# -----------------------------------------------------------------------
|
| 1606 |
-
|
| 1607 |
-
def _compute_components(self, final_decision: str) -> Dict[str, Any]:
|
| 1608 |
-
gt = self._current_task["ground_truth"]
|
| 1609 |
-
required_tools = self._current_task["required_tools"]
|
| 1610 |
-
ambiguity = self._current_task["ambiguity_level"]
|
| 1611 |
-
risk_level = self._current_task["risk_level"]
|
| 1612 |
-
policy_rec = self._policy_engine()
|
| 1613 |
-
|
| 1614 |
-
base_score = DECISION_MATRIX.get((final_decision, gt), 0.20)
|
| 1615 |
-
if final_decision == "ESCALATE" and ambiguity == "high":
|
| 1616 |
-
base_score = max(base_score, 0.70)
|
| 1617 |
-
is_correct = base_score >= 0.90
|
| 1618 |
-
|
| 1619 |
-
rule_weight = {"strong": 1.0, "medium": 0.70, "weak": 0.40}.get(
|
| 1620 |
-
policy_rec.get("rule_strength", "medium"), 0.70)
|
| 1621 |
-
policy_alignment = round(
|
| 1622 |
-
(+0.12 if final_decision == policy_rec["recommended"] else -0.18) * rule_weight, 4)
|
| 1623 |
-
|
| 1624 |
-
signal_accuracy_bonus = self._compute_signal_accuracy()
|
| 1625 |
-
|
| 1626 |
-
adj_conf = (self._validation_result["adjusted_confidence"]
|
| 1627 |
-
if self._validation_result else 0.50)
|
| 1628 |
-
should_escalate = adj_conf < 0.50
|
| 1629 |
-
if should_escalate and final_decision == "ESCALATE":
|
| 1630 |
-
escalation_adj = +0.15
|
| 1631 |
-
elif should_escalate and final_decision != "ESCALATE":
|
| 1632 |
-
escalation_adj = -0.18
|
| 1633 |
-
elif not should_escalate and final_decision == "ESCALATE" and ambiguity == "low":
|
| 1634 |
-
escalation_adj = -0.20
|
| 1635 |
-
elif not should_escalate and final_decision == "ESCALATE":
|
| 1636 |
-
escalation_adj = -0.10
|
| 1637 |
-
else:
|
| 1638 |
-
escalation_adj = 0.0
|
| 1639 |
-
|
| 1640 |
-
signal_bonus = +0.05 if self._signals_extracted else -0.10
|
| 1641 |
-
tool_cost = round(sum(TOOL_COSTS.get(t, 0.0) for t in self._tools_used), 4)
|
| 1642 |
-
missing_required = set(required_tools) - self._tools_used
|
| 1643 |
-
tool_miss_penalty = round(len(missing_required) * 0.25, 4)
|
| 1644 |
-
|
| 1645 |
-
if self._validation_result:
|
| 1646 |
-
n = self._validation_result["issue_count"]
|
| 1647 |
-
validation_penalty = {0: 0.00, 1: 0.05, 2: 0.12}.get(n, 0.20)
|
| 1648 |
-
else:
|
| 1649 |
-
validation_penalty = 0.12
|
| 1650 |
-
|
| 1651 |
-
risk_penalty = 0.0
|
| 1652 |
-
if not is_correct:
|
| 1653 |
-
risk_penalty = {"high": 0.20, "medium": 0.10, "low": 0.0}.get(risk_level, 0.0)
|
| 1654 |
-
|
| 1655 |
-
if base_score < 0.50 and adj_conf > 0.80:
|
| 1656 |
-
confidence_penalty = 0.22
|
| 1657 |
-
elif base_score < 0.50 and adj_conf > 0.65:
|
| 1658 |
-
confidence_penalty = 0.12
|
| 1659 |
-
elif self._signals_extracted and final_decision == "ESCALATE" and adj_conf < 0.55:
|
| 1660 |
-
confidence_penalty = -0.10
|
| 1661 |
-
else:
|
| 1662 |
-
confidence_penalty = 0.0
|
| 1663 |
-
|
| 1664 |
-
return {
|
| 1665 |
-
"base_score": base_score,
|
| 1666 |
-
"policy_alignment": policy_alignment,
|
| 1667 |
-
"signal_accuracy_bonus": signal_accuracy_bonus,
|
| 1668 |
-
"escalation_adj": escalation_adj,
|
| 1669 |
-
"signal_bonus": signal_bonus,
|
| 1670 |
-
"tool_cost": tool_cost,
|
| 1671 |
-
"tool_miss_penalty": tool_miss_penalty,
|
| 1672 |
-
"validation_penalty": validation_penalty,
|
| 1673 |
-
"risk_penalty": risk_penalty,
|
| 1674 |
-
"confidence_penalty": confidence_penalty,
|
| 1675 |
-
"_policy_rec": policy_rec,
|
| 1676 |
-
}
|
| 1677 |
-
|
| 1678 |
-
def _finalize_reward(self, components: Dict[str, Any]) -> float:
|
| 1679 |
-
raw = (
|
| 1680 |
-
components["base_score"]
|
| 1681 |
-
+ components["policy_alignment"]
|
| 1682 |
-
+ components["signal_accuracy_bonus"]
|
| 1683 |
-
+ components["escalation_adj"]
|
| 1684 |
-
+ components["signal_bonus"]
|
| 1685 |
-
- components["tool_cost"]
|
| 1686 |
-
- components["tool_miss_penalty"]
|
| 1687 |
-
- components["validation_penalty"]
|
| 1688 |
-
- components["risk_penalty"]
|
| 1689 |
-
- components["confidence_penalty"]
|
| 1690 |
-
)
|
| 1691 |
-
return round(max(0.0, min(1.0, raw)), 4)
|
| 1692 |
-
|
| 1693 |
-
def _compute_signal_accuracy(self) -> float:
|
| 1694 |
-
if not self._extracted_signals:
|
| 1695 |
-
return 0.0
|
| 1696 |
-
gt = self._current_task.get("ground_truth_signals", {})
|
| 1697 |
-
if not gt:
|
| 1698 |
-
return 0.05
|
| 1699 |
-
|
| 1700 |
-
s = self._extracted_signals
|
| 1701 |
-
score = 0.0
|
| 1702 |
-
if s.target == gt.get("target"): score += 0.20
|
| 1703 |
-
if s.intent == gt.get("intent"): score += 0.20
|
| 1704 |
-
if s.context_type == gt.get("context_type"): score += 0.20
|
| 1705 |
-
|
| 1706 |
-
tox_diff = abs(s.toxicity_level - gt.get("toxicity_level", 0.5))
|
| 1707 |
-
score += 0.20 if tox_diff <= 0.20 else (0.10 if tox_diff <= 0.35 else 0.0)
|
| 1708 |
-
|
| 1709 |
-
gt_flags = set(gt.get("content_flags", []))
|
| 1710 |
-
s_flags = set(s.content_flags)
|
| 1711 |
-
if gt_flags:
|
| 1712 |
-
score += 0.20 * min(1.0, len(gt_flags & s_flags) / len(gt_flags))
|
| 1713 |
-
else:
|
| 1714 |
-
score += 0.20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