File size: 22,974 Bytes
7043cc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
{
 "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')"
   ]
  }
 ]
}