File size: 5,352 Bytes
93f12cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Convert RAGEN rollout .pkl files to OpenAI-compatible JSONL format.

Usage:
    python scripts/convert_to_jsonl.py --input results/eval/val_rollouts_*.pkl --output trajectories.jsonl
    python scripts/convert_to_jsonl.py --input results/eval/val_rollouts_*.pkl  # auto-generates output filename
"""

import argparse
import json
from pathlib import Path
from typing import Any, Dict, List
import numpy as np

from verl import DataProto


def extract_openai_messages(history: List[Dict]) -> List[Dict[str, str]]:
    """
    Extract OpenAI-compatible message format from history.

    Format: [{"role": "user"|"assistant", "content": str}, ...]
    """
    messages = []

    for i, turn in enumerate(history):
        # Add user message (environment state)
        if 'state' in turn:
            state_content = turn['state']
            if i == 0:
                # Initial state
                messages.append({
                    "role": "user",
                    "content": state_content
                })
            else:
                # Feedback from environment after action
                reward = turn.get('reward', 0)
                info_str = f" (reward: {reward})" if reward != 0 else ""
                messages.append({
                    "role": "user",
                    "content": f"{state_content}{info_str}"
                })

        # Add assistant message (LLM response with actions)
        if 'llm_response' in turn:
            llm_content = turn.get('llm_raw_response', turn.get('llm_response', ''))
            if llm_content:
                messages.append({
                    "role": "assistant",
                    "content": str(llm_content)
                })

    return messages


def rollout_to_openai_format(item: Any, index: int) -> Dict[str, Any]:
    """
    Convert a single rollout to OpenAI-compatible format.

    Returns:
        {
            "custom_id": "traj_{index}",
            "messages": [...],
            "metadata": {
                "success": bool,
                "reward": float,
                "num_turns": int,
                "env_id": int,
                "group_id": int,
                ...
            }
        }
    """
    ntb = item.non_tensor_batch or {}
    meta = item.meta_info or {}

    # Extract history
    history = ntb.get('history', [])
    messages = extract_openai_messages(history)

    # Extract metadata - safe access to batch (avoid tensordict boolean conversion)
    total_reward = 0.0
    try:
        if item.batch is not None and 'rm_scores' in item.batch:
            rm_scores = item.batch['rm_scores']
            total_reward = float(np.sum(rm_scores))
    except (AttributeError, KeyError, TypeError):
        pass

    metadata = {
        "env_id": int(ntb.get('env_ids', index)),
        "group_id": int(ntb.get('group_ids', 0)),
        "num_turns": len([h for h in history if 'actions' in h]),
        "total_reward": total_reward,
    }

    # Add metrics if available
    if 'metrics' in ntb:
        metrics = ntb['metrics']
        if isinstance(metrics, dict):
            metadata['success'] = metrics.get('success', False)
            metadata.update({k: v for k, v in metrics.items() if k != 'success'})

    # Add entropy info if available
    if 'entropys' in ntb:
        metadata['entropy'] = float(ntb['entropys'])
    if 'n_generated_tokens' in ntb:
        metadata['n_tokens'] = int(ntb['n_generated_tokens'])

    return {
        "custom_id": f"traj_{index}",
        "messages": messages,
        "metadata": metadata
    }


def convert_pkl_to_jsonl(input_path: Path, output_path: Path) -> None:
    """Convert a DataProto .pkl file to OpenAI-compatible JSONL."""
    print(f"Loading rollout data from {input_path}...")
    data = DataProto.load_from_disk(str(input_path))

    total = len(data)
    print(f"Found {total} trajectories")

    success_count = 0
    with open(output_path, 'w', encoding='utf-8') as f:
        for idx in range(total):
            try:
                item = data[idx]
                openai_obj = rollout_to_openai_format(item, idx)
                f.write(json.dumps(openai_obj, ensure_ascii=False) + '\n')
                success_count += 1
            except Exception as e:
                print(f"Warning: Failed to convert trajectory {idx}: {e}")
                continue

    print(f"Successfully converted {success_count}/{total} trajectories to {output_path}")


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Convert RAGEN rollout .pkl files to OpenAI-compatible JSONL"
    )
    parser.add_argument(
        "--input",
        required=True,
        help="Path to input .pkl file"
    )
    parser.add_argument(
        "--output",
        help="Path to output .jsonl file (default: auto-generated from input)"
    )
    return parser.parse_args()


def main():
    args = parse_args()

    input_path = Path(args.input)
    if not input_path.exists():
        raise FileNotFoundError(f"Input file not found: {input_path}")

    # Auto-generate output path if not provided
    if args.output:
        output_path = Path(args.output)
    else:
        output_path = input_path.with_suffix('.jsonl')

    convert_pkl_to_jsonl(input_path, output_path)


if __name__ == "__main__":
    main()