Spaces:
Sleeping
Sleeping
File size: 25,748 Bytes
cdf485e 7574c9a cdf485e 7574c9a cdf485e 5ee1380 cdf485e f3f7bc4 cdf485e f3f7bc4 cdf485e f3f7bc4 cdf485e f3f7bc4 cdf485e a4c268d cdf485e dada51b 5ee1380 cdf485e 5ee1380 cdf485e 5ee1380 cdf485e 5ee1380 cdf485e 5ee1380 cdf485e 7574c9a cdf485e 7574c9a cdf485e dada51b 5ee1380 cdf485e dada51b cdf485e dada51b cdf485e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | """
PreferenceLab Core Environment.
Implements the OpenEnv Environment base class with:
- reset() β returns initial Observation
- step() β executes action, returns Observation (reward & done embedded)
- state β @property returning a State object with episode metadata
Three tasks:
Task 1 (pairwise) - Easy: pairwise choice graded against HH-RLHF gold labels
Task 2 (likert) - Medium: multi-axis scoring graded via MSE vs UltraFeedback scores
Task 3 (consistency) - Hard: 4-way ranking graded on transitivity + quality correlation
"""
import json
import random
import uuid
from itertools import permutations
from pathlib import Path
from typing import Any
from openenv.core.env_server import Environment
from openenv.core.env_server.types import State
from models import (
ConsistencyAction,
ConsistencyObservation,
LikertAction,
LikertObservation,
PairwiseAction,
PairwiseObservation,
)
# ββ Dataset loading ββββββββββββββββββββββββββββββββββββββββββββ
DATA_DIR = Path(__file__).parent.parent / "data"
def _load_json(filename: str) -> list[dict]:
path = DATA_DIR / filename
if path.exists():
with open(path, encoding="utf-8") as f:
return json.load(f)
return []
# ββ Graders βββββββββββββββββββββββββββββββββββββββββββββββββββ
def grade_pairwise(action: PairwiseAction, example: dict) -> tuple[float, dict]:
"""
Grade Task 1: Pairwise ranking.
Gold label is 'A' (chosen) or 'B' (rejected) from dataset.
Returns:
1.0 β correct choice
0.3 β skip (abstain β partial credit)
0.0 β wrong choice
0.1 β tie (when gold is clear)
"""
gold = example.get("gold_label", "A") # 'A' = chosen is response_a
choice = action.choice
if choice == "skip":
reward = 0.3
verdict = "abstained"
elif choice == "tie":
reward = 0.1
verdict = "tie_when_clear"
elif choice == gold:
reward = 0.99
verdict = "correct"
else:
reward = 0.01
verdict = "incorrect"
return reward, {
"gold": gold,
"chosen": choice,
"verdict": verdict,
"dataset": example.get("source", "hh-rlhf"),
}
def grade_likert(action: LikertAction, example: dict) -> tuple[float, dict]:
"""
Grade Task 2: Multi-axis Likert scoring.
Compares agent's 4-axis scores to gold scores from UltraFeedback.
Reward = 1.0 - (mean_absolute_error / max_possible_error)
Max possible error per axis = 4 (1 vs 5), so max_total = 4.
"""
gold_scores = example.get("gold_scores", {
"helpfulness": 3,
"honesty": 3,
"harmlessness": 4,
"instruction_following": 3,
})
axes = ["helpfulness", "honesty", "harmlessness", "instruction_following"]
agent_scores = {
"helpfulness": action.helpfulness,
"honesty": action.honesty,
"harmlessness": action.harmlessness,
"instruction_following": action.instruction_following,
}
errors = []
per_axis = {}
for ax in axes:
err = abs(agent_scores[ax] - gold_scores.get(ax, 3))
errors.append(err)
per_axis[ax] = {"agent": agent_scores[ax], "gold": gold_scores.get(ax, 3), "error": err}
mae = sum(errors) / len(errors)
max_error = 4.0 # max abs difference on 1-5 scale
reward = round(1.0 - (mae / max_error), 4)
reward = max(0.01, min(0.99, reward))
return reward, {
"mae": round(mae, 4),
"per_axis": per_axis,
"dataset": example.get("source", "ultrafeedback"),
}
def grade_consistency(action: ConsistencyAction, example: dict) -> tuple[float, dict]:
"""
Grade Task 3: Transitive consistency chain ranking.
Scoring components:
- Transitivity score (0.0β0.5): penalise transitive violations in the ranking
- Quality correlation (0.0β0.5): Kendall's tau vs gold ranking
Total reward = transitivity_score + quality_score (max 1.0)
"""
ranking = action.ranking
gold_ranking = example.get("gold_ranking", ["A", "B", "C", "D"])
# --- Transitivity check ---
# For each triple (i, j, k) where i < j < k in the agent's ranking,
# verify that position(i) < position(j) < position(k) doesn't violate transitivity.
# A violation = agent says A > B and B > C but NOT A > C.
# Since ranking is a total order, by construction it IS transitive. But we can
# still penalise if ranking contains duplicates or invalid IDs.
valid_ids = {"A", "B", "C", "D"}
has_invalid = not (set(ranking) == valid_ids)
transitivity_score = 0.0 if has_invalid else 0.5
# --- Quality correlation (Kendall's tau, simplified) ---
if has_invalid:
quality_score = 0.0
n_concordant = 0
n_discordant = 0
else:
ids = ["A", "B", "C", "D"]
agent_pos = {r: i for i, r in enumerate(ranking)}
gold_pos = {r: i for i, r in enumerate(gold_ranking)}
n_concordant = 0
n_discordant = 0
pairs = [(ids[i], ids[j]) for i in range(4) for j in range(i + 1, 4)]
for x, y in pairs:
agent_order = agent_pos[x] < agent_pos[y]
gold_order = gold_pos[x] < gold_pos[y]
if agent_order == gold_order:
n_concordant += 1
else:
n_discordant += 1
total_pairs = n_concordant + n_discordant
tau = (n_concordant - n_discordant) / total_pairs if total_pairs > 0 else 0.0
# Normalise tau from [-1,1] to [0, 0.5]
quality_score = round((tau + 1.0) / 2.0 * 0.5, 4)
reward = round(transitivity_score + quality_score, 4)
reward = max(0.01, min(0.99, reward))
return reward, {
"transitivity_score": transitivity_score,
"quality_score": quality_score if not has_invalid else 0.0,
"agent_ranking": ranking,
"gold_ranking": gold_ranking,
"has_invalid_ids": has_invalid,
"dataset": example.get("source", "stanford-shp"),
}
# ββ Environment βββββββββββββββββββββββββββββββββββββββββββββββ
TASK_TYPES = ["pairwise", "likert", "consistency"]
MAX_STEPS_PER_EPISODE = 10
class PreferenceLabEnvironment(Environment):
"""
PreferenceLab: An RL environment simulating the RLHF preference
data collection pipeline.
Each episode consists of MAX_STEPS_PER_EPISODE annotation steps.
The task type is fixed per episode (chosen at reset).
"""
# All state is stored in instance variables (self._*), so each session
# is fully isolated β concurrent use is safe.
SUPPORTS_CONCURRENT_SESSIONS: bool = True
# Global cached dataset loading
_CACHE_PAIRWISE_DATA: list[dict] | None = None
_CACHE_LIKERT_DATA: list[dict] | None = None
_CACHE_CONSISTENCY_DATA: list[dict] | None = None
def __init__(self):
self._episode_id: str = ""
self._step_count: int = 0
self._task_type: str = "pairwise"
self._current_example: dict = {}
self._cumulative_reward: float = 0.0
self._seed: int = 0
# Load datasets globally on first init
if PreferenceLabEnvironment._CACHE_PAIRWISE_DATA is None:
PreferenceLabEnvironment._CACHE_PAIRWISE_DATA = _load_json("pairwise_data.json") or _synthetic_pairwise()
if PreferenceLabEnvironment._CACHE_LIKERT_DATA is None:
PreferenceLabEnvironment._CACHE_LIKERT_DATA = _load_json("likert_data.json") or _synthetic_likert()
if PreferenceLabEnvironment._CACHE_CONSISTENCY_DATA is None:
PreferenceLabEnvironment._CACHE_CONSISTENCY_DATA = _load_json("consistency_data.json") or _synthetic_consistency()
self._pairwise_data = PreferenceLabEnvironment._CACHE_PAIRWISE_DATA
self._likert_data = PreferenceLabEnvironment._CACHE_LIKERT_DATA
self._consistency_data = PreferenceLabEnvironment._CACHE_CONSISTENCY_DATA
# ββ OpenEnv API βββββββββββββββββββββββββββββββββββββββββββ
def reset(self, seed: int | None = None, episode_id: str | None = None, **kwargs):
"""
Reset the environment for a new episode.
Args:
seed: Optional random seed for reproducibility.
episode_id: Optional episode ID override.
**kwargs: Accepts task_type ('pairwise', 'likert', 'consistency').
Returns:
Initial observation for the episode.
"""
task_type = kwargs.get("task_type", None)
if task_type is not None and task_type not in TASK_TYPES:
raise ValueError(f"Unsupported task_type: '{task_type}'")
self._seed = seed if seed is not None else random.randint(0, 10_000)
rng = random.Random(self._seed)
self._episode_id = episode_id or str(uuid.uuid4())
self._step_count = 0
self._cumulative_reward = 0.0
self._task_type = task_type if task_type else rng.choice(TASK_TYPES)
self._current_example = self._sample_example(rng)
return self._build_observation(reward=0.0, done=False, info={"reset": True})
def step(self, action, timeout_s: float | None = None, **kwargs):
"""
Execute one annotation step.
Args:
action: A PairwiseAction, LikertAction, or ConsistencyAction.
timeout_s: Unused β required by base class signature.
Returns:
Observation with reward and done embedded as fields.
Read obs.reward and obs.done instead of unpacking a tuple.
"""
self._step_count += 1
# Grade the action
reward, info = self._grade(action)
self._cumulative_reward += reward
done = self._step_count >= MAX_STEPS_PER_EPISODE
# Sample next example if not done
if not done:
rng = random.Random(self._seed + self._step_count)
self._current_example = self._sample_example(rng)
return self._build_observation(reward=reward, done=done, info=info)
@property
def state(self) -> State:
"""Return current episode metadata as an openenv State object."""
return State(
episode_id=self._episode_id,
step_count=self._step_count,
task_type=self._task_type,
cumulative_reward=round(self._cumulative_reward, 4),
max_steps=MAX_STEPS_PER_EPISODE,
seed=self._seed,
)
# ββ Internal helpers ββββββββββββββββββββββββββββββββββββββ
def _sample_example(self, rng: random.Random) -> dict:
"""Sample one example from the appropriate dataset."""
dataset = {
"pairwise": self._pairwise_data,
"likert": self._likert_data,
"consistency": self._consistency_data,
}[self._task_type]
return rng.choice(dataset)
def _grade(self, action) -> tuple[float, dict]:
"""
Dispatch to the correct grader.
Priority: action *instance type* β self._task_type fallback.
This matters for the web UI, which always submits a PairwiseAction
(the primary schema). Routing by instance type prevents crashes when
the internal task_type is 'likert' or 'consistency'.
"""
if isinstance(action, PairwiseAction):
return grade_pairwise(action, self._current_example)
elif isinstance(action, LikertAction):
return grade_likert(action, self._current_example)
elif isinstance(action, ConsistencyAction):
return grade_consistency(action, self._current_example)
# Legacy fallback β dispatch by task_type string (e.g. dict-based actions)
if isinstance(action, dict):
if self._task_type == "pairwise":
action = PairwiseAction(**action)
return grade_pairwise(action, self._current_example)
elif self._task_type == "likert":
action = LikertAction(**action)
return grade_likert(action, self._current_example)
elif self._task_type == "consistency":
action = ConsistencyAction(**action)
return grade_consistency(action, self._current_example)
elif self._task_type == "pairwise":
return grade_pairwise(action, self._current_example)
elif self._task_type == "likert":
return grade_likert(action, self._current_example)
elif self._task_type == "consistency":
return grade_consistency(action, self._current_example)
return 0.0, {"error": "unknown_task"}
def _build_observation(self, reward: float, done: bool, info: dict):
"""Build the appropriate observation type for the current task."""
ex = self._current_example
base = {
"task_id": self._episode_id + f"_step{self._step_count}",
"reward": reward,
"done": done,
"step_count": self._step_count,
"info": info,
}
if self._task_type == "pairwise":
return PairwiseObservation(
**base,
prompt=ex.get("prompt", ""),
response_a=ex.get("response_a", ""),
response_b=ex.get("response_b", ""),
)
elif self._task_type == "likert":
return LikertObservation(
**base,
prompt=ex.get("prompt", ""),
response=ex.get("response", ""),
rubric=ex.get("rubric", DEFAULT_LIKERT_RUBRIC),
)
elif self._task_type == "consistency":
return ConsistencyObservation(
**base,
prompt=ex.get("prompt", ""),
response_a=ex.get("response_a", ""),
response_b=ex.get("response_b", ""),
response_c=ex.get("response_c", ""),
response_d=ex.get("response_d", ""),
)
raise ValueError(f"Unknown task type: {self._task_type}")
# ββ Defaults ββββββββββββββββββββββββββββββββββββββββββββββββββ
DEFAULT_LIKERT_RUBRIC = (
"Score the response on 4 axes (1=worst, 5=best): "
"Helpfulness β does it answer the question usefully? "
"Honesty β is it accurate and truthful? "
"Harmlessness β does it avoid harm? "
"Instruction Following β does it follow the instruction precisely?"
)
# ββ Synthetic fallback datasets βββββββββββββββββββββββββββββββ
def _synthetic_pairwise() -> list[dict]:
return [
{
"prompt": "Explain what machine learning is in simple terms.",
"response_a": "Machine learning is a branch of AI where computers learn patterns from data without being explicitly programmed for each task.",
"response_b": "Machine learning is when computers do stuff with numbers to make predictions.",
"gold_label": "A",
"source": "synthetic",
},
{
"prompt": "How do I improve my sleep quality?",
"response_a": "Try sleeping more.",
"response_b": "Maintain a consistent sleep schedule, avoid screens 1 hour before bed, keep your bedroom cool and dark, and limit caffeine after 2pm.",
"gold_label": "B",
"source": "synthetic",
},
{
"prompt": "What is the capital of France?",
"response_a": "Paris is the capital and largest city of France.",
"response_b": "France's capital city is called Paris, it is located in northern France.",
"gold_label": "A",
"source": "synthetic",
},
{
"prompt": "Write a haiku about autumn.",
"response_a": "Leaves fall silently / Crimson whispers touch the ground / Winter draws near now",
"response_b": "Autumn is a season. Leaves fall down. It gets cold outside.",
"gold_label": "A",
"source": "synthetic",
},
{
"prompt": "How do I reverse a list in Python?",
"response_a": "Use my_list.reverse() to reverse in-place, or my_list[::-1] to get a reversed copy.",
"response_b": "You can just use the reverse function.",
"gold_label": "A",
"source": "synthetic",
},
{
"prompt": "Is it safe to eat raw eggs?",
"response_a": "Raw eggs carry a risk of Salmonella contamination. While many people eat them without issue, cooking eggs eliminates this risk. Use pasteurised eggs if you want them raw.",
"response_b": "Yeah raw eggs are totally fine to eat, bodybuilders do it all the time.",
"gold_label": "A",
"source": "synthetic",
},
{
"prompt": "Summarise the French Revolution in 2 sentences.",
"response_a": "The French Revolution (1789β1799) was a period of radical political and social transformation in France that overthrew the monarchy, established a republic, and culminated in Napoleon's rise to power. It reshaped modern political thought by promoting ideals of liberty, equality, and popular sovereignty.",
"response_b": "The French Revolution happened in France. People revolted against the king.",
"gold_label": "A",
"source": "synthetic",
},
{
"prompt": "What is the difference between RAM and storage?",
"response_a": "RAM is temporary memory your computer uses to run active programs β it's fast but clears when you shut down. Storage (SSD/HDD) is permanent memory that holds your files and programs long-term.",
"response_b": "RAM stores temporary data while the computer is running, whereas storage permanently saves files and data even when powered off.",
"gold_label": "A",
"source": "synthetic",
},
]
def _synthetic_likert() -> list[dict]:
return [
{
"prompt": "Explain photosynthesis to a 10-year-old.",
"response": "Photosynthesis is how plants make food! They use sunlight, water from the soil, and carbon dioxide from the air. Inside their leaves, they turn all this into glucose (their food) and release oxygen β which is the air we breathe!",
"rubric": DEFAULT_LIKERT_RUBRIC,
"gold_scores": {"helpfulness": 5, "honesty": 5, "harmlessness": 5, "instruction_following": 5},
"source": "synthetic",
},
{
"prompt": "What are three tips for reducing stress?",
"response": "Here are some tips: exercise regularly, practice meditation, and get enough sleep. These have been shown to reduce cortisol levels and improve mood.",
"rubric": DEFAULT_LIKERT_RUBRIC,
"gold_scores": {"helpfulness": 4, "honesty": 5, "harmlessness": 5, "instruction_following": 4},
"source": "synthetic",
},
{
"prompt": "How do vaccines work?",
"response": "Vaccines train your immune system by introducing a harmless piece of a pathogen (or instructions to make one). Your body learns to recognise and fight it, so if you encounter the real thing later, your immune system responds quickly.",
"rubric": DEFAULT_LIKERT_RUBRIC,
"gold_scores": {"helpfulness": 5, "honesty": 5, "harmlessness": 5, "instruction_following": 5},
"source": "synthetic",
},
{
"prompt": "Write a professional email declining a job offer.",
"response": "Subject: Re: Job Offer β [Position Name]\n\nDear [Hiring Manager],\n\nThank you so much for offering me the [Position] role at [Company]. After careful consideration, I have decided to decline the offer as I have accepted another position that more closely aligns with my current career goals.\n\nI truly appreciate the time you and your team invested in the interview process and hope our paths cross again in the future.\n\nBest regards,\n[Your Name]",
"rubric": DEFAULT_LIKERT_RUBRIC,
"gold_scores": {"helpfulness": 5, "honesty": 5, "harmlessness": 5, "instruction_following": 5},
"source": "synthetic",
},
{
"prompt": "Explain blockchain in simple terms.",
"response": "A blockchain is like a shared notebook that thousands of computers all keep a copy of. Every new entry (transaction) gets added in a block, chained to the previous one. Because everyone has a copy, no single person can secretly change it.",
"rubric": DEFAULT_LIKERT_RUBRIC,
"gold_scores": {"helpfulness": 5, "honesty": 4, "harmlessness": 5, "instruction_following": 5},
"source": "synthetic",
},
{
"prompt": "List 5 healthy breakfast options.",
"response": "1. Oatmeal with berries and nuts\n2. Greek yogurt with honey and banana\n3. Avocado toast with eggs\n4. Smoothie with spinach, protein powder, and almond milk\n5. Whole grain cereal with low-fat milk",
"rubric": DEFAULT_LIKERT_RUBRIC,
"gold_scores": {"helpfulness": 5, "honesty": 5, "harmlessness": 5, "instruction_following": 5},
"source": "synthetic",
},
]
def _synthetic_consistency() -> list[dict]:
return [
{
"prompt": "Explain how to use Python decorators.",
"response_a": "Decorators are functions that wrap other functions to add behaviour. Use @decorator_name above a function definition. Example: @staticmethod, @property, or custom ones with functools.wraps.",
"response_b": "Decorators wrap functions.",
"response_c": "Python decorators use the @ symbol and are a design pattern for extending function behavior without modifying the function itself. They take a function as input and return a modified version.",
"response_d": "You put @ before a function name.",
"gold_ranking": ["C", "A", "B", "D"],
"source": "synthetic",
},
{
"prompt": "What causes climate change?",
"response_a": "Climate change is primarily caused by human activities that release greenhouse gases β mainly CO2 from burning fossil fuels, methane from agriculture and landfills, and N2O from fertilisers. These gases trap heat in the atmosphere.",
"response_b": "The sun causes climate change.",
"response_c": "Many factors contribute to climate change including greenhouse gas emissions from industry, deforestation which reduces carbon absorption, and agricultural practices. The IPCC confirms human activity is the dominant cause since the mid-20th century.",
"response_d": "Climate change happens because of pollution.",
"gold_ranking": ["C", "A", "D", "B"],
"source": "synthetic",
},
{
"prompt": "How does the internet work?",
"response_a": "The internet is a global network of computers connected via physical cables (fiber, copper) and wireless signals. Data travels in packets using TCP/IP protocols, routed through servers and ISPs to reach its destination.",
"response_b": "Computers connect together and send data.",
"response_c": "Internet works through IP addresses.",
"response_d": "The internet is a massive network where data is broken into packets, routed through interconnected servers using protocols like TCP/IP and HTTP, and reassembled at the destination. DNS translates domain names to IP addresses.",
"gold_ranking": ["D", "A", "C", "B"],
"source": "synthetic",
},
{
"prompt": "Describe the water cycle.",
"response_a": "The water cycle involves evaporation, condensation, and precipitation. Water evaporates from oceans and lakes, forms clouds, then falls as rain or snow.",
"response_b": "Water goes up and comes down.",
"response_c": "The water cycle (hydrological cycle) is the continuous movement of water through Earth's systems: evaporation from surface water, transpiration from plants, condensation into clouds, precipitation, surface runoff, and groundwater infiltration before returning to oceans.",
"response_d": "Water evaporates and rains.",
"gold_ranking": ["C", "A", "D", "B"],
"source": "synthetic",
},
{
"prompt": "Explain the difference between HTTP and HTTPS.",
"response_a": "HTTPS is like HTTP but secure.",
"response_b": "HTTP is the protocol for transferring web data. HTTPS adds SSL/TLS encryption, meaning data is encrypted in transit. This prevents eavesdropping and verifies server identity via certificates. Always use HTTPS for sensitive data.",
"response_c": "HTTP transfers web pages. HTTPS encrypts the connection using TLS, protecting data from interception. The S stands for Secure.",
"response_d": "HTTPS has a padlock icon in browsers.",
"gold_ranking": ["B", "C", "A", "D"],
"source": "synthetic",
},
]
|