File size: 2,544 Bytes
ab10f3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Turn RoboQuest-Tool-Call trajectories into per-step training rows with a chosen image horizon.

    python make_rows.py --trajectories puzzle_box/trajectories.jsonl --out rows_h2.jsonl --image-horizon 2

Each trajectory is one full episode conversation (system, then per decision: observation, assistant tool call,
tool result) with the three camera images on every observation. For decision k the row holds the conversation up
to observation k as `messages` and the k-th assistant tool call as `target`. Only the newest `--image-horizon`
observations keep their image parts (the camera label texts stay); `all` keeps every image. The RoboQuest eval
harness uses 2, and `--image-horizon 2` reproduces the published rows.jsonl exactly.

Standard library only.
"""
import argparse
import json


def rows_from_trajectory(t, horizon):
    msgs, out = t['messages'], []
    users = [i for i, m in enumerate(msgs) if m['role'] == 'user']
    calls = [i for i, m in enumerate(msgs) if m['role'] == 'assistant']
    assert len(users) == len(calls) == t['n_decisions']
    for k, (u, a) in enumerate(zip(users, calls), start=1):
        keep = set(users[:k] if horizon is None else users[max(0, k - horizon):k])
        messages = []
        for i in range(u + 1):
            m = msgs[i]
            if m['role'] == 'user' and i not in keep:
                m = {**m, 'content': [p for p in m['content'] if p.get('type') != 'image']}
            messages.append(m)
        turn = t['turns'][k - 1]
        out.append({'id': f"{t['task']}__{t['instance_id']}__{k:03d}", 'task': t['task'],
                    'instance_id': t['instance_id'], 'k': k, 'messages': messages, 'tools': t['tools'],
                    'target': msgs[a], 'tier': turn['tier'], 'subtask': turn['subtask'],
                    'subtask_kind': turn['subtask_kind']})
    return out


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('--trajectories', required=True)
    ap.add_argument('--out', required=True)
    ap.add_argument('--image-horizon', default='2', help="observations that keep images (an integer, or 'all')")
    args = ap.parse_args()
    horizon = None if args.image_horizon == 'all' else int(args.image_horizon)
    n = 0
    with open(args.trajectories) as f, open(args.out, 'w') as out:
        for line in f:
            for row in rows_from_trajectory(json.loads(line), horizon):
                out.write(json.dumps(row, separators=(',', ':')) + '\n')
                n += 1
    print('rows', n)


if __name__ == '__main__':
    main()