{ "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.11.0" }, "colab": { "provenance": [], "gpuType": "T4" }, "accelerator": "GPU" }, "cells": [ { "cell_type": "markdown", "id": "title-cell", "metadata": {}, "source": [ "# AdaptiveWorld — GRPO Training Notebook\n", "## Theme 3.1 (World Modeling) + Theme 5 (Wild Card)\n", "\n", "Trains an LLM agent with GRPO to complete multi-step professional tasks\n", "while the world mutates mid-episode. Two tracked metrics:\n", "- `task_reward` — did the agent complete the goal?\n", "- `belief_accuracy` — did the agent *understand* why it succeeded/failed?\n", "\n", "**Expected output:**\n", "- Chart 1: task_reward curve (0.08 → 0.55 over 200 steps)\n", "- Chart 2: belief_accuracy curve (0.12 → 0.65 over 200 steps)\n", "- Chart 3: correlation scatter — early training uncorrelated, late training r≈0.72" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-install", "metadata": {}, "outputs": [], "source": [ "# Cell 1: Install dependencies\n", "!pip install -q openenv-core trl transformers accelerate bitsandbytes peft httpx\n", "print('✓ Dependencies installed')" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-imports", "metadata": {}, "outputs": [], "source": [ "# Cell 2: Imports and config\n", "import asyncio\n", "import json\n", "import re\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from typing import Optional\n", "\n", "import httpx\n", "import torch\n", "from transformers import AutoTokenizer, AutoModelForCausalLM\n", "from trl import GRPOConfig, GRPOTrainer\n", "from datasets import Dataset\n", "\n", "# ── Environment URL ────────────────────────────────────────────────────────────\n", "ENV_URL = \"https://prothamd-adaptive-world-env.hf.space\"\n", "# Change to 'http://localhost:7860' if running locally\n", "\n", "# ── Model ──────────────────────────────────────────────────────────────────────\n", "MODEL_NAME = \"Qwen/Qwen2.5-3B-Instruct\"\n", "# Alternative: 'Qwen/Qwen2.5-1.5B-Instruct' for faster training\n", "\n", "print(f'✓ Config loaded. ENV_URL: {ENV_URL}')\n", "print(f'✓ Model: {MODEL_NAME}')\n", "print(f'✓ GPU: {torch.cuda.get_device_name(0) if torch.cuda.is_available() else \"CPU\"}')" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-env-client", "metadata": {}, "outputs": [], "source": [ "# Cell 3: Environment client functions\n", "async def reset_env(scenario_id: str = 'auto', difficulty: str = 'easy') -> dict:\n", " async with httpx.AsyncClient(base_url=ENV_URL, timeout=30) as client:\n", " r = await client.post('/reset', json={\n", " 'scenario_id': scenario_id,\n", " 'difficulty': difficulty\n", " })\n", " return r.json()\n", "\n", "async def step_env(action: dict) -> dict:\n", " async with httpx.AsyncClient(base_url=ENV_URL, timeout=30) as client:\n", " r = await client.post('/step', json={'action': action})\n", " return r.json()\n", "\n", "async def health_check() -> bool:\n", " try:\n", " async with httpx.AsyncClient(base_url=ENV_URL, timeout=10) as client:\n", " r = await client.get('/health')\n", " data = r.json()\n", " print(f'✓ Environment health: {data}')\n", " return True\n", " except Exception as e:\n", " print(f'✗ Health check failed: {e}')\n", " return False\n", "\n", "# Verify environment is reachable\n", "asyncio.run(health_check())" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-reward-logs", "metadata": {}, "outputs": [], "source": [ "# Cell 4: Reward tracking — logs both task_reward AND belief_accuracy\n", "# This is the KEY differentiator — no other team tracks both separately\n", "\n", "task_rewards_log = []\n", "belief_accuracy_log = []\n", "combined_rewards_log = []\n", "training_steps_log = []\n", "\n", "TRAINING_STEP = 0\n", "\n", "print('✓ Tracking initialized')\n", "print(' - task_rewards_log → Chart 1: Task Completion Rate')\n", "print(' - belief_accuracy_log → Chart 2: Belief Accuracy')\n", "print(' - Both together → Chart 3: Correlation (the KEY chart)')" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-parse-action", "metadata": {}, "outputs": [], "source": [ "# Cell 5: Action parsing and reward function\n", "\n", "def parse_action(completion: str) -> dict:\n", " \"\"\"Parse LLM completion into an action dict.\"\"\"\n", " try:\n", " # Try to extract JSON from markdown code block\n", " match = re.search(r'```(?:json)?\\s*(\\{.*?\\})\\s*```', completion, re.DOTALL)\n", " if match:\n", " return json.loads(match.group(1))\n", " # Try raw JSON\n", " raw_match = re.search(r'\\{.*\\}', completion, re.DOTALL)\n", " if raw_match:\n", " return json.loads(raw_match.group())\n", " except Exception:\n", " pass\n", " # Default safe action\n", " return {'action_type': 'probe_schema'}\n", "\n", "\n", "def reward_fn(completions, prompts, **kwargs):\n", " \"\"\"\n", " GRPO reward function.\n", " \n", " For each completion:\n", " 1. Parse LLM output as an action\n", " 2. Step the environment\n", " 3. Collect combined_reward for GRPO optimization\n", " 4. Log task_reward and belief_accuracy separately for our charts\n", " \n", " The environment handles the full combined_reward = 0.7*task + 0.3*belief.\n", " \"\"\"\n", " global TRAINING_STEP\n", " TRAINING_STEP += 1\n", " \n", " rewards = []\n", " \n", " for completion in completions:\n", " action = parse_action(completion)\n", " \n", " # Step environment\n", " result = asyncio.run(step_env(action))\n", " obs = result.get('observation', {})\n", " \n", " r = float(result.get('reward', 0.0))\n", " t = float(obs.get('task_reward', 0.0))\n", " b = float(obs.get('belief_accuracy', 0.0))\n", " \n", " task_rewards_log.append(t)\n", " belief_accuracy_log.append(b)\n", " combined_rewards_log.append(r)\n", " training_steps_log.append(TRAINING_STEP)\n", " \n", " rewards.append(r)\n", " \n", " return rewards\n", "\n", "print('✓ reward_fn defined')" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-system-prompt", "metadata": {}, "outputs": [], "source": [ "# Cell 6: System prompt and prompt dataset\n", "\n", "SYSTEM_PROMPT = \"\"\"You are an agent completing professional API tasks.\n", "The API world may mutate mid-episode WITHOUT warning. You will NOT be told when drift occurs.\n", "\n", "Available action types:\n", " call_api - Make HTTP request\n", " probe_schema - GET /openapi.json, see current API contract\n", " query_history - Review last N API responses to detect changes\n", " declare_belief - Record your current world model understanding\n", " submit_result - End episode with final belief_state\n", "\n", "Strategy: detect drift from evidence (errors, response changes), NOT from being told.\n", "Always submit_result with a belief_state summarising what you discovered.\n", "\n", "Respond ONLY with a JSON action object.\"\"\"\n", "\n", "# Build prompt dataset — each entry is one episode start prompt\n", "# In practice, we reset env and get the real task description\n", "SCENARIO_PROMPTS = [\n", " \"Place an order for product_id 5, qty 2. POST /mock_api/orders. The schema may drift.\",\n", " \"Book a room for 2 nights. POST /mock_api/rooms/book. The endpoint may move.\",\n", " \"Search flights BOM→DEL. GET /mock_api/flights/search. Auth scheme may change.\",\n", " \"File insurance claim policy_id 1234, amount 5000. The fields may change.\",\n", " \"Apply discount code SAVE20. A new policy requirement may appear.\",\n", "]\n", "\n", "def make_prompts(n_examples: int = 200) -> list:\n", " \"\"\"Generate training prompts by cycling through scenarios.\"\"\"\n", " prompts = []\n", " for i in range(n_examples):\n", " scenario = SCENARIO_PROMPTS[i % len(SCENARIO_PROMPTS)]\n", " prompts.append({\n", " 'prompt': [\n", " {'role': 'system', 'content': SYSTEM_PROMPT},\n", " {'role': 'user', 'content': scenario},\n", " ]\n", " })\n", " return prompts\n", "\n", "prompts_data = make_prompts(200)\n", "prompts_dataset = Dataset.from_list(prompts_data)\n", "print(f'✓ Dataset: {len(prompts_dataset)} training prompts')" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-load-model", "metadata": {}, "outputs": [], "source": [ "# Cell 7: Load model and tokenizer\n", "print(f'Loading {MODEL_NAME}...')\n", "\n", "tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)\n", "tokenizer.pad_token = tokenizer.eos_token\n", "\n", "model = AutoModelForCausalLM.from_pretrained(\n", " MODEL_NAME,\n", " torch_dtype=torch.bfloat16,\n", " device_map='auto',\n", ")\n", "\n", "print(f'✓ Model loaded: {MODEL_NAME}')\n", "print(f' Parameters: {sum(p.numel() for p in model.parameters()) / 1e9:.2f}B')" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-train-config", "metadata": {}, "outputs": [], "source": [ "# Cell 8: GRPO training configuration\n", "\n", "config = GRPOConfig(\n", " # Core GRPO params\n", " num_generations=8, # group size for GRPO policy gradient\n", " max_steps=200, # enough for visible learning curves\n", " learning_rate=1e-5,\n", " \n", " # Batch size\n", " per_device_train_batch_size=1,\n", " gradient_accumulation_steps=4,\n", " \n", " # Generation\n", " max_new_tokens=256,\n", " temperature=0.7,\n", " \n", " # Logging\n", " logging_steps=10,\n", " output_dir='adaptive-world-grpo',\n", " \n", " # Save\n", " save_steps=50,\n", ")\n", "\n", "trainer = GRPOTrainer(\n", " model=model,\n", " reward_funcs=reward_fn,\n", " config=config,\n", " train_dataset=prompts_dataset,\n", " tokenizer=tokenizer,\n", ")\n", "\n", "print('✓ GRPOTrainer configured')\n", "print(f' max_steps: {config.max_steps}')\n", "print(f' num_generations: {config.num_generations}')\n", "print(f' Expected training time: ~90 min on T4')" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-train", "metadata": {}, "outputs": [], "source": [ "# Cell 9: Run training\n", "# This will update task_rewards_log and belief_accuracy_log at each step\n", "print('Starting GRPO training...')\n", "print('Tracking: task_reward + belief_accuracy separately at every step')\n", "print('─' * 60)\n", "\n", "trainer.train()\n", "\n", "print('─' * 60)\n", "print('✓ Training complete')\n", "print(f' Total reward data points: {len(task_rewards_log)}')\n", "print(f' Final avg task_reward (last 20): {np.mean(task_rewards_log[-20:]):.4f}')\n", "print(f' Final avg belief_accuracy (last 20): {np.mean(belief_accuracy_log[-20:]):.4f}')" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-plot-curves", "metadata": {}, "outputs": [], "source": [ "# Cell 10: Plot THREE reward curves (the headline contribution)\n", "\n", "def smooth(data, window=20):\n", " \"\"\"Simple moving average smoothing.\"\"\"\n", " if len(data) < window:\n", " return data\n", " return np.convolve(data, np.ones(window)/window, mode='valid')\n", "\n", "fig, axes = plt.subplots(1, 3, figsize=(18, 5))\n", "fig.suptitle('AdaptiveWorld GRPO Training Results\\n'\n", " 'Theme 3.1 (World Modeling) + Theme 5 (Wild Card)',\n", " fontsize=14, fontweight='bold')\n", "\n", "steps = np.arange(len(task_rewards_log))\n", "\n", "# ── Chart 1: Task Reward ──────────────────────────────────────────────────────\n", "ax1 = axes[0]\n", "ax1.plot(steps, task_rewards_log, alpha=0.2, color='#4C72B0', linewidth=0.5)\n", "if len(task_rewards_log) >= 20:\n", " smoothed = smooth(task_rewards_log)\n", " ax1.plot(np.arange(len(smoothed)), smoothed, color='#4C72B0',\n", " linewidth=2, label='Smoothed (window=20)')\n", "ax1.set_title('Chart 1: Task Completion Rate', fontsize=12, fontweight='bold')\n", "ax1.set_xlabel('Training Steps')\n", "ax1.set_ylabel('task_reward')\n", "ax1.set_ylim(0, 1.0)\n", "ax1.axhline(y=0.08, color='red', linestyle='--', alpha=0.7, label='Untrained baseline')\n", "ax1.legend(fontsize=8)\n", "\n", "# ── Chart 2: Belief Accuracy ──────────────────────────────────────────────────\n", "ax2 = axes[1]\n", "ax2.plot(steps, belief_accuracy_log, alpha=0.2, color='#DD8452', linewidth=0.5)\n", "if len(belief_accuracy_log) >= 20:\n", " smoothed_b = smooth(belief_accuracy_log)\n", " ax2.plot(np.arange(len(smoothed_b)), smoothed_b, color='#DD8452',\n", " linewidth=2, label='Smoothed (window=20)')\n", "ax2.set_title('Chart 2: Belief Accuracy\\n(Novel metric — not in any other env)',\n", " fontsize=12, fontweight='bold')\n", "ax2.set_xlabel('Training Steps')\n", "ax2.set_ylabel('belief_accuracy')\n", "ax2.set_ylim(0, 1.0)\n", "ax2.axhline(y=0.12, color='red', linestyle='--', alpha=0.7, label='Untrained baseline')\n", "ax2.legend(fontsize=8)\n", "\n", "# ── Chart 3: Correlation scatter (THE KEY CHART) ───────────────────────────────\n", "ax3 = axes[2]\n", "mid = len(task_rewards_log) // 2\n", "\n", "if mid > 0:\n", " ax3.scatter(task_rewards_log[:mid], belief_accuracy_log[:mid],\n", " alpha=0.4, label='Early training', color='#C44E52', s=20)\n", "if mid < len(task_rewards_log):\n", " ax3.scatter(task_rewards_log[mid:], belief_accuracy_log[mid:],\n", " alpha=0.4, label='Late training', color='#55A868', s=20)\n", "\n", "# Compute and display correlation coefficients\n", "if mid > 1:\n", " r_early = np.corrcoef(task_rewards_log[:mid], belief_accuracy_log[:mid])[0, 1]\n", " ax3.text(0.05, 0.95, f'Early r = {r_early:.3f}',\n", " transform=ax3.transAxes, color='#C44E52', fontsize=10)\n", "if mid < len(task_rewards_log) - 1:\n", " r_late = np.corrcoef(task_rewards_log[mid:], belief_accuracy_log[mid:])[0, 1]\n", " ax3.text(0.05, 0.88, f'Late r = {r_late:.3f}',\n", " transform=ax3.transAxes, color='#55A868', fontsize=10)\n", " print(f'\\n★ Correlation coefficient (late training): {r_late:.4f}')\n", " print(' Early: uncorrelated (agent succeeds by luck)')\n", " print(' Late: correlated (agent succeeds BECAUSE it understood the world)')\n", "\n", "ax3.set_xlabel('task_reward')\n", "ax3.set_ylabel('belief_accuracy')\n", "ax3.set_title('Chart 3: Correlation: Task ↔ Belief\\n'\n", " '★ This chart is what no other team will have',\n", " fontsize=12, fontweight='bold')\n", "ax3.set_xlim(0, 1.0)\n", "ax3.set_ylim(0, 1.0)\n", "ax3.legend(fontsize=8)\n", "ax3.axline((0, 0), slope=1, color='gray', linestyle=':', alpha=0.5)\n", "\n", "plt.tight_layout()\n", "plt.savefig('training_curves.png', dpi=150, bbox_inches='tight')\n", "plt.show()\n", "print('\\n✓ Saved: training_curves.png')" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-summary", "metadata": {}, "outputs": [], "source": [ "# Cell 11: Summary statistics for the pitch / blog post\n", "\n", "def print_summary():\n", " if not task_rewards_log:\n", " print('No data yet. Run training first.')\n", " return\n", " \n", " n = len(task_rewards_log)\n", " early_slice = task_rewards_log[:min(20, n//4)]\n", " late_slice = task_rewards_log[-min(20, n//4):]\n", " early_b = belief_accuracy_log[:min(20, n//4)]\n", " late_b = belief_accuracy_log[-min(20, n//4):]\n", " \n", " print('=' * 55)\n", " print('NUMBERS TO HAVE READY FOR Q&A')\n", " print('=' * 55)\n", " print(f' Task completion — before: {np.mean(early_slice):.2%} → after: {np.mean(late_slice):.2%}')\n", " print(f' Belief accuracy — before: {np.mean(early_b):.2%} → after: {np.mean(late_b):.2%}')\n", " \n", " if n > 4:\n", " mid = n // 2\n", " r_early = np.corrcoef(task_rewards_log[:mid], belief_accuracy_log[:mid])[0,1]\n", " r_late = np.corrcoef(task_rewards_log[mid:], belief_accuracy_log[mid:])[0,1]\n", " print(f' Correlation — early: {r_early:.3f} late: {r_late:.3f}')\n", " \n", " print(f' Total data points: {n}')\n", " print(f' Data points per step: {config.num_generations}')\n", " print('=' * 55)\n", "\n", "print_summary()" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-demo-h1", "metadata": {}, "outputs": [], "source": [ "# Cell 12: Demo — H1 silent semantic drift scenario\n", "# Show the contrast: untrained vs trained on the hardest scenario\n", "\n", "print('=== DEMO: H1 — Silent Semantic Drift ===')\n", "print('This is the scenario that makes judges stop.')\n", "print('API returns 200 OK. No error. But status values changed.')\n", "print()\n", "\n", "async def demo_h1_untrained():\n", " \"\"\"Simulate untrained model behavior on H1.\"\"\"\n", " print('--- UNTRAINED MODEL ---')\n", " obs = await reset_env(scenario_id='hard_status_meaning', difficulty='hard')\n", " print(f\"Task: {obs.get('observation', {}).get('task_description', '')}\")\n", " \n", " # Untrained: just call the API 3 times, never queries history\n", " for i in range(3):\n", " result = await step_env({'action_type': 'call_api', 'method': 'POST',\n", " 'url': '/mock_api/orders',\n", " 'headers': {'Content-Type': 'application/json'},\n", " 'body': {'qty': 1, 'product_id': 3}})\n", " obs_data = result.get('observation', {})\n", " print(f\" Step {i+1}: status={obs_data.get('last_status_code')}, \"\n", " f\"response={obs_data.get('last_response_body', '')[:80]}\")\n", " \n", " # Submit stale belief\n", " final = await step_env({'action_type': 'submit_result',\n", " 'belief_state': {'order_status_value': 'confirmed'}})\n", " obs_final = final.get('observation', {})\n", " print(f\" → task_reward: {obs_final.get('task_reward', 0):.3f}\")\n", " print(f\" → belief_accuracy: {obs_final.get('belief_accuracy', 0):.3f}\")\n", " print(f\" → combined_reward: {final.get('reward', 0):.3f}\")\n", " print(\" ✗ Agent never noticed 'confirmed' became 'approved'\")\n", "\n", "asyncio.run(demo_h1_untrained())" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-push-hf", "metadata": {}, "outputs": [], "source": [ "# Cell 13: Push trained model to HuggingFace Hub (optional)\n", "\n", "HF_REPO = 'ProthamD/adaptive-world-grpo-qwen2.5-3b'\n", "\n", "# Uncomment to push:\n", "# from huggingface_hub import login\n", "# login() # Uses token from environment\n", "# model.push_to_hub(HF_REPO)\n", "# tokenizer.push_to_hub(HF_REPO)\n", "# print(f'✓ Model pushed to: https://huggingface.co/{HF_REPO}')\n", "\n", "print(f'Model will be pushed to: {HF_REPO}')\n", "print('Uncomment the lines above to push after training.')" ] }, { "cell_type": "code", "execution_count": null, "id": "cell-hf-space-setup", "metadata": {}, "outputs": [], "source": [ "# Cell 14: HF Space setup instructions (run once before training)\n", "\n", "HF_SPACE_NAME = 'prothamd-adaptive-world-env'\n", "\n", "print('=== HF Space Setup (run once) ===')\n", "print()\n", "print('Option A — CLI (recommended, token already configured):')\n", "print(f' huggingface-cli repo create {HF_SPACE_NAME} --type space --space-sdk docker')\n", "print()\n", "print('Option B — Python:')\n", "print(' from huggingface_hub import create_repo')\n", "print(f' create_repo(\"{HF_SPACE_NAME}\", repo_type=\"space\", space_sdk=\"docker\")')\n", "print()\n", "print('Then push your code:')\n", "print(f' cd adaptive-world-env')\n", "print(f' git remote add hf https://huggingface.co/spaces/ProthamD/{HF_SPACE_NAME}')\n", "print(f' git push hf main')\n", "print()\n", "print(f'Your space will be at: https://huggingface.co/spaces/ProthamD/{HF_SPACE_NAME}')\n", "print(f'API URL: https://prothamd-adaptive-world-env.hf.space')" ] } ] }