File size: 3,455 Bytes
8fb9f5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# FrozenLake RL Trajectories → LLM SFT Converter

This utility converts numeric RL testing trajectories collected from the FrozenLake wrapper into language trajectories suitable for supervised fine-tuning (SFT) of LLM agents.

It reconstructs the text grid from the one-hot observations saved during RL testing and formats per-turn prompts and responses to mirror RAGEN’s `ContextManager` chat format.

## Path Layout
- Input: `runs/<experiment>/trajectories/step_XXXXXX/trajectories.jsonl`
- Output: `runs/<experiment>/sft/step_XXXXXX_sft.jsonl`
python scripts/convert_rl_to_sft_frozenlake.py runs/FrozenLake__ppo_frozenlake__1__1762841111 --step step_993280 --include_failed

Each output line is a JSON object:
- `messages`: chat array with roles `system`, `user`, `assistant`, `user (reward)`
- `meta`: `{ episode_return, episode_success, global_step }`

## Requirements
- Python 3.8+
- Optional: `pyyaml` if you want the script to read `config/envs.yaml` and `config/base.yaml` for `env_instruction`, `action_sep`, and `enable_think`. If not installed, built-in defaults are used.

## Usage

Convert the latest step under the run directory:

```
python scripts/convert_rl_to_sft_frozenlake.py runs/FrozenLake__ppo_frozenlake__1__1762841111
```

Convert a specific step directory:

```
python scripts/convert_rl_to_sft_frozenlake.py runs/FrozenLake__ppo_frozenlake__1__1762841111 --step step_993280
```

Include failed episodes (by default only successful episodes are kept):

```
python scripts/convert_rl_to_sft_frozenlake.py runs/FrozenLake__ppo_frozenlake_nochangeenv__1__1763561679 --step step_1986560
```

## What the Converter Does
- Reconstructs the 4×4 grid text from the FrozenLake numeric state:
  - One-hot over 4 cell types plus 2 normalized coordinates for player position
  - Displays `P` for player on frozen, `X` for player in hole, and `√` for player on goal
- Builds a chat-style prompt per episode:
  - `system`: "You're a helpful assistant."
  - `user`: `env_instruction` followed by per-turn blocks with `State`, remaining actions, and format constraints
  - `assistant`: Tagged action outputs: `<think></think><answer>Action</answer>` (or `<answer>Action</answer>` if think disabled)
  - `user`: `Reward` after each assistant response
- Maps RL actions (0..3) to RAGEN’s `{Left, Down, Right, Up}`
- Reads `global_step` from the step’s `metrics.json` if present

## Configuration Hooks
- `config/envs.yaml` → `FrozenLake.env_instruction`, `FrozenLake.max_tokens`
- `config/base.yaml` → `agent_proxy.action_sep` (default `||`), `agent_proxy.enable_think` (default `True`)

If these files are not present or `pyyaml` is not installed, the converter uses safe defaults.

## Output Example (truncated)

```
{
  "messages": [
    {"role": "system", "content": "You're a helpful assistant. "},
    {"role": "user", "content": "You are solving the FrozenLake puzzle...\nTurn 1:\nState:\n__PO\n____\nG___\n____\nYou have 4 actions left..."},
    {"role": "assistant", "content": "<think></think><answer>Left</answer>"},
    {"role": "user", "content": "Reward:\n0.0\n"},
    ...
  ],
  "meta": {"episode_return": 1.0, "episode_success": true, "global_step": 993280}
}
```

## Notes
- The converter currently targets FrozenLake; extending to Bandit and Sokoban is straightforward by adapting the state decoder.
- The per-turn structure follows the training-time prompt generator so SFT will be consistent with RL rollouts.