#!/usr/bin/env python3 """ Convert RL test trajectories (numeric states/actions) from FrozenLake into LLM SFT-ready language trajectories in chat-style messages. Input: runs//trajectories/step_XXXXXX/trajectories.jsonl Output: runs//sft/step_XXXXXX_sft.jsonl Each output JSON line contains: - messages: [{role: system|user|assistant, content: str}, ...] - meta: {episode_return: float, episode_success: bool, global_step: int} We mirror RAGEN ContextManager’s prompt format as much as possible: - system: "You're a helpful assistant. " - user: env_instruction + per-turn state blocks with action constraints - assistant: "Action" (or without think if disabled) - user (reward): "Reward:\n{reward}\n" """ import argparse import json import math import os from pathlib import Path from typing import List, Tuple try: import yaml # type: ignore except Exception: yaml = None ACTION_LOOKUP = {1: "Left", 2: "Down", 3: "Right", 4: "Up"} def infer_grid_dims(state_vec: List[float]) -> Tuple[int, int]: """Infer (rows, cols) from flattened one-hot grid length. Our PPO wrapper encodes each cell as one-hot over 6 tokens: ['P','_','O','G','X','√']. """ n = len(state_vec) assert n % 6 == 0, f"State length {n} not divisible by 6 (channels)" n_cells = n // 6 r = int(math.isqrt(n_cells)) assert r * r == n_cells, f"Grid is not square: {n_cells} cells" return r, r def decode_state_to_grid_text(state_vec: List[float]) -> str: """Decode numeric state vector back to textual grid. Encoding per PPO wrapper: One-hot per cell over tokens = ['P', '_', 'O', 'G', 'X', '√'] in this order. The wrapper already encodes P/X/√ directly in the grid; no separate coords needed. """ tokens = ['P', '_', 'O', 'G', 'X', '√'] rows, cols = infer_grid_dims(state_vec) lines = [] for i in range(rows): row_chars = [] for j in range(cols): base = (i * cols + j) * 6 cell = state_vec[base: base + 6] idx = max(range(6), key=lambda k: cell[k]) ch = tokens[idx] if 0 <= idx < len(tokens) else '_' row_chars.append(ch) lines.append("".join(row_chars)) return "\n".join(lines) def load_env_instruction_and_cfg(repo_root: Path) -> Tuple[str, int, str, bool]: """Load FrozenLake env_instruction, max_tokens, action_sep, enable_think from config. Fallbacks are provided if YAML is unavailable. """ default_instruction = ( "You are solving the FrozenLake puzzle. Forbid the hole and go to the target. " "You may move to unintended directions due to slippery ice. " "Example answer format: To forbid the hole and go to the target, I should go left then go up.Left || Up" "The meaning of each symbol in the state is:\nP: player, _: empty, O: hole, G: goal, X: player in hole, √: player on goal \nYour available actions are: \nLeft, Down, Right, Up \nYou can make up to 10 actions, separated by the action separator ' || '" ) instruction = default_instruction max_tokens = 100 action_sep = "||" enable_think = True if yaml is None: return instruction, max_tokens, action_sep, enable_think # envs.yaml envs_yaml = repo_root / "config" / "envs.yaml" if envs_yaml.exists(): try: with open(envs_yaml, "r", encoding="utf-8") as f: envs = yaml.safe_load(f) if isinstance(envs, dict) and "FrozenLake" in envs: fl = envs["FrozenLake"] instruction = fl.get("env_instruction", instruction) max_tokens = int(fl.get("max_tokens", max_tokens)) except Exception: pass # base.yaml base_yaml = repo_root / "config" / "base.yaml" if base_yaml.exists(): try: with open(base_yaml, "r", encoding="utf-8") as f: base_cfg = yaml.safe_load(f) ap = base_cfg.get("agent_proxy", {}) if isinstance(base_cfg, dict) else {} action_sep = ap.get("action_sep", action_sep) enable_think = bool(ap.get("enable_think", enable_think)) except Exception: pass return instruction, max_tokens, action_sep, enable_think def build_messages_for_episode( states: List[List[float]], actions: List[int], rewards: List[float], instruction: str, max_tokens: int, action_sep: str, enable_think: bool, ) -> List[dict]: """Construct chat messages mirroring ContextManager format. - First system message. - One user message containing the instruction and per-turn state blocks. - Assistant messages per executed action with tag-only outputs. - User messages for rewards. """ messages = [ {"role": "system", "content": "You're a helpful assistant. "}, {"role": "user", "content": instruction}, ] total_actions = len(actions) # Append state blocks into the initial user content for t, state in enumerate(states): grid_text = decode_state_to_grid_text(state) actions_left = max(0, total_actions - t) # before taking action at turn t format_prompt = ( " [Your thoughts] [your answer] " if enable_think else " [your answer] " ) length_prompt = f"Max response length: {max_tokens} words (tokens)." messages[-1]["content"] += ( f"\nTurn {t + 1}:\n" f"State:\n{grid_text}\n" f"You have {actions_left} actions left. Always output: {format_prompt} " f"with no extra text. Strictly follow this format. {length_prompt}\n" ) # If action exists for this turn, add assistant + reward if t < total_actions: # Map RL action (0..3) -> RAGEN action (1..4) -> text action_id = actions[t] + 1 action_name = ACTION_LOOKUP.get(action_id, "unknown") if enable_think: assistant_text = f"{action_name}" else: assistant_text = f"{action_name}" messages.append({"role": "assistant", "content": assistant_text}) # Reward message reward_val = rewards[t] if t < len(rewards) else 0.0 messages.append({"role": "user", "content": f"Reward:\n{reward_val}\n"}) # import pdb;pdb.set_trace() messages.append({"role": "assistant", "content": ""}) return messages def convert_file(step_dir: Path, output_dir: Path, repo_root: Path, include_failed: bool = False) -> Path: traj_path = step_dir / "trajectories.jsonl" metrics_path = step_dir / "metrics.json" if not traj_path.exists(): raise FileNotFoundError(f"Missing trajectories.jsonl at {traj_path}") instruction, max_tokens, action_sep, enable_think = load_env_instruction_and_cfg(repo_root) output_dir.mkdir(parents=True, exist_ok=True) out_path = output_dir / f"{step_dir.name}_sft.jsonl" # Read global step from metrics if available global_step = None if metrics_path.exists(): try: with open(metrics_path, "r", encoding="utf-8") as f: m = json.load(f) global_step = m.get("global_step") except Exception: pass written = 0 with open(traj_path, "r", encoding="utf-8") as fin, open(out_path, "w", encoding="utf-8") as fout: for line in fin: line = line.strip() if not line: continue traj = json.loads(line) # Filter if requested ep_success = bool(traj.get("episode_success", False)) if (not include_failed) and (not ep_success): continue states = traj.get("states", []) actions = traj.get("actions", []) rewards = traj.get("rewards", []) messages = build_messages_for_episode( states=states, actions=actions, rewards=rewards, instruction=instruction, max_tokens=max_tokens, action_sep=action_sep, enable_think=enable_think, ) record = { "messages": messages, "meta": { "episode_return": traj.get("episode_return", None), "episode_success": ep_success, "global_step": global_step, }, } fout.write(json.dumps(record, ensure_ascii=False) + "\n") written += 1 if written == 0: # Still write an empty file to signal conversion executed with open(out_path, "w", encoding="utf-8") as f: pass return out_path def find_latest_step_dir(traj_root: Path) -> Path: step_dirs = [p for p in traj_root.iterdir() if p.is_dir() and p.name.startswith("step_")] if not step_dirs: raise FileNotFoundError(f"No step_* directories under {traj_root}") # Sort by numeric suffix step_dirs.sort(key=lambda p: int(p.name.split("_")[-1])) return step_dirs[-1] def main(): parser = argparse.ArgumentParser(description="Convert FrozenLake RL trajectories to LLM SFT chat JSONL") parser.add_argument("run_dir", help="Path to the run directory (contains trajectories/)" ) parser.add_argument("--step", default=None, help="Specific step directory name (e.g., step_993280)") parser.add_argument("--include_failed", action="store_true", help="Include failed episodes in SFT data") args = parser.parse_args() repo_root = Path(__file__).resolve().parents[1] run_dir = Path(args.run_dir) traj_root = run_dir / "trajectories" if not traj_root.exists(): raise FileNotFoundError(f"Not found trajectories directory: {traj_root}") step_dir = traj_root / args.step if args.step else find_latest_step_dir(traj_root) output_dir = run_dir / "sft" out_path = convert_file(step_dir=step_dir, output_dir=output_dir, repo_root=repo_root, include_failed=args.include_failed) print(f"SFT data written to: {out_path}") if __name__ == "__main__": main()