job_scam_env / models.py
Sumitwarrior7's picture
Upload folder using huggingface_hub
8a0b9a5 verified
Raw History Blame Contribute Delete
12.9 kB
"""
Data models for the Job Scam Detection Environment.
Design rationale
----------------
OpenEnv's ``create_app`` accepts exactly **one** Action class and **one**
Observation class. To support three task variants (easy / medium / hard)
without forking the server, we use a **unified superset** approach:
* ``JobScamAction`` β€” one class; its ``action_type`` enum is the union
of every action from all three tasks. Only the
action types that are valid for the *current
episode's task* will be accepted by the server.
* ``JobScamObservation`` β€” one class with every possible field across all
tasks declared as ``Optional``. Fields that are
irrelevant to the current task are simply ``None``.
Task-specific enumerations are grouped with comments so it is easy to extend
them when the easy / hard task designs are finalised.
Adding a new task
-----------------
1. Add new ``ActionType`` members in the "HARD TASK" / "EASY TASK" blocks.
2. Add new ``ClassificationLabel`` members if the label space differs.
3. Add new Optional fields to ``JobScamObservation`` in the appropriate block.
4. Wire the new values into ``constants.py`` and the environment.
"""
from __future__ import annotations
from dataclasses import dataclass
from enum import Enum
from typing import Any, Dict, List, Optional
from dataclasses import field
from openenv.core.env_server.types import Action, Observation
from pydantic import Field, model_validator
# ---------------------------------------------------------------------------
# Enumerations β€” unified superset across all tasks
# ---------------------------------------------------------------------------
class ActionType(str, Enum):
"""
All valid action types across every task variant.
"""
# ── MEDIUM TASK ─────────────────────────
CLASSIFY = "classify"
REQUEST_RECRUITER_PROFILE = "request_recruiter_profile"
REQUEST_COMPANY_PROFILE = "request_company_profile"
REQUEST_THREAD_HISTORY = "request_thread_history"
REQUEST_JOB_POST_COMMENTS = "request_job_post_comments"
# ── EASY TASK ────────────
# TODO: uncomment / add when easy task is designed
# REQUEST_EASY_FIELD_A = "request_easy_field_a"
# ── HARD TASK ────────────
REQUEST_SENDER_PROFILE = "request_sender_profile"
REQUEST_ORGANIZATION_PROFILE = "request_organization_profile"
REQUEST_SHARED_CHANNEL_HISTORY = "request_shared_channel_history"
REQUEST_PRIVATE_CONVERSATION_HISTORY = "request_private_conversation_history"
REQUEST_CANDIDATE_INTERACTION_HISTORY = "request_candidate_interaction_history"
REQUEST_EXTERNAL_MARKET_SIGNALS = "request_external_market_signals"
REQUEST_ATTACHED_ARTIFACTS = "request_attached_artifacts"
REQUEST_TEMPORAL_CONTEXT = "request_temporal_context"
class ClassificationLabel(str, Enum):
"""
All valid classification outcomes across every task variant.
Right now the label space is identical for easy / medium / hard.
If a task variant needs additional labels, add them here with a comment
indicating which task introduced them.
"""
# ── MEDIUM TASK labels (placeholders) ─────────────────────────────────────
LEGIT = "legit"
SUSPICIOUS = "suspicious"
SCAM = "scam"
INSUFFICIENT_INFO = "insufficient_info"
# ── EASY TASK labels (placeholders) ─────────────────────────────────────
# TODO: add easy-specific labels here if needed
SCAM_EASY = "scam" # example only β€” replace with actual easy-task labels when designed
LEGIT_EASY = "legit" # example only β€” replace with actual easy-task labels when designed
# ── HARD TASK labels (placeholders) ─────────────────────────────────────
# TODO: add hard-specific labels here if needed
# ---------------------------------------------------------------------------
# Unified Action
# ---------------------------------------------------------------------------
class JobScamAction(Action):
"""
A single step action submitted by the client β€” valid for all task variants.
For information-gathering steps, set ``action_type`` to one of the
``REQUEST_*`` variants appropriate for the active task, and leave
``label`` as ``None``.
For the terminal classification step, set ``action_type`` to
``CLASSIFY`` and provide a ``ClassificationLabel`` in ``label``.
The server will reject ``REQUEST_*`` actions that do not belong to the
current episode's task (e.g. sending a medium-task action during an
easy-task episode).
Example β€” medium info request::
JobScamAction(action_type="request_company_profile")
Example β€” classification (all tasks)::
JobScamAction(action_type="classify", label="scam")
"""
action_type: ActionType = Field(
...,
description=(
"Type of action: a context-field request (task-specific) or "
"the terminal 'classify' action (shared across all tasks)."
),
)
label: Optional[ClassificationLabel] = Field(
default=None,
description=(
"Required only when action_type is 'classify'. "
"Must be one of: legit, suspicious, scam, insufficient_info."
),
)
@model_validator(mode="after")
def _label_required_for_classify(self) -> "JobScamAction":
if self.action_type == ActionType.CLASSIFY and self.label is None:
raise ValueError(
"label must be provided when action_type is 'classify'."
)
return self
def model_dump(self, *args, **kwargs):
kwargs.setdefault("exclude_none", True)
return super().model_dump(*args, **kwargs)
def model_dump_json(self, *args, **kwargs):
kwargs.setdefault("exclude_none", True)
return super().model_dump_json(*args, **kwargs)
# ---------------------------------------------------------------------------
# Unified Observation (superset of all task-specific fields)
# ---------------------------------------------------------------------------
class JobScamObservation(Observation):
"""
Observation returned by the environment at every step.
Fields are grouped by which task / step type populates them.
Fields not relevant to the current task / step are ``None``.
── Shared fields (all tasks, all steps) ─────────────────────────────────
step_budget : remaining step budget for the episode
info : reward_breakdown + cumulative reward details
── Reset fields (all tasks, step 0) ─────────────────────────────────────
task_name : which task variant is active ("easy"|"medium"|"hard")
query_type : channel type of the job opportunity
initial_query : raw text the candidate received
available_context : names of hidden context fields the client may request
── MEDIUM TASK β€” information request fields ──────────────────────────────
requested_field : name of the context field just returned
field_content : raw text content of that field
── EASY TASK β€” information request fields (placeholders) ─────────────────
# TODO: add easy-specific observation fields here when task is designed
── HARD TASK β€” information request fields (placeholders) ─────────────────
# TODO: add hard-specific observation fields here when task is designed
── Classification / terminal fields (all tasks) ──────────────────────────
predicted_label : label the client submitted
actual_label : ground-truth label (revealed only at terminal step)
episode_done : True when the episode has ended (classification or timeout)
reason : "classification" | "timeout"
"""
# ── Shared / budget ──────────────────────────────────────────────────────
step_budget: Optional[Dict[str, int]] = Field(
default=None,
description="Keys: total, used, remaining.",
)
# ── Reset β€” shared across all tasks ──────────────────────────────────────
task_name: Optional[str] = Field(
default=None,
description=(
"Task variant active for this episode: 'easy' | 'medium' | 'hard'. "
"Set at reset and echoed in every subsequent observation."
),
)
query_type: Optional[str] = Field(
default=None,
description="Channel type: job_post | email | whatsapp_msg | telegram_msg.",
)
initial_query: Optional[str] = Field(
default=None,
description="Raw text of the job opportunity as received by the candidate.",
)
# ── MEDIUM TASK ───────────────────────────────
available_context: Optional[List[str]] = Field(
default=None,
description="[Medium] Names of hidden context fields the client may request.",
)
requested_field: Optional[str] = Field(
default=None,
description="[Medium] Name of the context field that was just returned.",
)
field_content: Optional[Any] = Field(
default=None,
description="[Medium/Hard] Raw content of the requested context field. May be text or structured data.",
)
# ── EASY TASK ─────────────────
# TODO: add easy-task-specific observation fields here.
# Example:
# easy_summary: Optional[str] = Field(default=None, description="[Easy] ...")
# ── HARD TASK ─────────────────
# TODO: add hard-task-specific observation fields here.
# Example:
# hard_social_profile: Optional[str] = Field(default=None, description="[Hard] ...")
# hard_payment_history: Optional[str] = Field(default=None, description="[Hard] ...")
# ── Terminal / classification fields (all tasks) ──────────────────────────
predicted_label: Optional[str] = Field(
default=None,
description="Label submitted by the client.",
)
actual_label: Optional[str] = Field(
default=None,
description="Ground-truth label revealed only at terminal step.",
)
episode_done: Optional[bool] = Field(
default=None,
description="True when the episode has ended (classification or timeout).",
)
reason: Optional[str] = Field(
default=None,
description="Reason for episode termination: 'classification' | 'timeout'.",
)
info: Optional[Dict[str, Any]] = Field(
default=None,
description=(
"Contains reward_breakdown (step-level) and cumulative reward "
"totals so the client can inspect grading details."
),
)
@dataclass
class RewardRule:
condition: str
reward: float
@dataclass
class HardRewardLogic:
dense_rewards: List[RewardRule] = field(default_factory=list)
sparse_rewards: List[RewardRule] = field(default_factory=list)
penalties: List[RewardRule] = field(default_factory=list)
efficiency_penalty_per_extra_tool: float = 0.05
@dataclass
class HardGradingLogic:
tool_correctness_weight: float = 0.25
trajectory_weight: float = 0.30
final_action_weight: float = 0.25
evidence_quality_weight: float = 0.10
efficiency_weight: float = 0.10
@dataclass
class HardGroundTruth:
optimal_action_sequence: List[str]
acceptable_alternative_sequences: List[List[str]]
expected_final_actions: List[str]
classification_credit: Dict[str, Dict[str, float]]
forbidden_shortcuts: List[str]
@dataclass
class HardEpisode:
episode_id: str
difficulty: str
domain: str
query_type: str
initial_signal: str
environment_state: Dict[str, Any]
allowed_tools: List[str]
ground_truth: HardGroundTruth
reward_logic: HardRewardLogic
grading_logic: HardGradingLogic