Download source/replay.py from naklitechie/openenv-null-control: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/datasets/naklitechie/openenv-null-control/resolve/main/source/replay.py
- Command line
-
hf download hf://datasets/naklitechie/openenv-null-control/source/replay.py
-
curl -L -o replay.py https://huggingface.co/datasets/naklitechie/openenv-null-control/resolve/main/source/replay.py
2.78 kB
| """Replay example actions over /ws and assert every episode ends done=True with reward 0.5. | |
| Usage: python replay.py ws://127.0.0.1:8000/ws [log.json] | |
| """ | |
| import asyncio | |
| import json | |
| import sys | |
| from datetime import datetime, timezone | |
| import websockets | |
| TASK_IDS = ["null"] | |
| ACTIONS = [ | |
| {"answer": "done"}, # example action (submission example_actions[0]) | |
| {}, # empty object | |
| {"answer": "42", "tool": "calc", "x": 1}, # stray keys a policy may emit | |
| {"answer": 0}, # non-string answer | |
| {"answer": None}, # null answer | |
| ] | |
| # Replies that fail the schema never reach step(): the arena ends the episode and | |
| # scores it through finish_action (guide, "How the model sees your environment"). | |
| REWARD = 0.5 | |
| async def episode(url, task_id, action): | |
| async with websockets.connect(url) as ws: | |
| await ws.send(json.dumps({"type": "reset", "data": {"task_id": task_id}})) | |
| reset = json.loads(await ws.recv()) | |
| await ws.send(json.dumps({"type": "step", "data": action})) | |
| step = json.loads(await ws.recv()) | |
| await ws.send(json.dumps({"type": "close"})) | |
| return {"task_id": task_id, "action": action, "reset": reset, "step": step} | |
| async def main(url, log_path): | |
| results = [] | |
| for task_id in TASK_IDS: | |
| for action in ACTIONS: | |
| r = await episode(url, task_id, action) | |
| data = r["step"].get("data", {}) | |
| r["ok"] = (r["reset"].get("type") == "observation" and r["step"].get("type") == "observation" | |
| and data.get("done") is True and data.get("reward") == REWARD) | |
| results.append(r) | |
| print(("OK " if r["ok"] else "FAIL"), task_id, json.dumps(action), "->", json.dumps(data)) | |
| # One GRPO group: 5 rollouts of the same task in parallel sessions. | |
| group = await asyncio.gather(*(episode(url, TASK_IDS[0], ACTIONS[0]) for _ in range(5))) | |
| rewards = [g["step"].get("data", {}).get("reward") for g in group] | |
| group_ok = all(g["step"].get("data", {}).get("done") is True for g in group) and rewards == [REWARD] * 5 | |
| print(("OK " if group_ok else "FAIL"), "concurrent group of 5 rewards", rewards) | |
| results.append({"task_id": TASK_IDS[0], "concurrent_group_rewards": rewards, "ok": group_ok}) | |
| log = {"url": url, "at_utc": datetime.now(timezone.utc).isoformat(timespec="seconds"), | |
| "episodes": results, "all_ok": all(r["ok"] for r in results)} | |
| if log_path: | |
| with open(log_path, "w") as f: | |
| json.dump(log, f, indent=1) | |
| f.write("\n") | |
| return 0 if log["all_ok"] else 1 | |
| if __name__ == "__main__": | |
| sys.exit(asyncio.run(main(sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else None))) | |