File size: 3,938 Bytes
2054ea3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Convert LongAlign-10k and HotpotQA into the {"data": [user, assistant, ...]}
JSONL that sft_dataset.py reads.

    python3 convert_datasets.py --longalign LongAlign-10k/long.jsonl \
        --out-longalign longalign_sft.jsonl

    python3 convert_datasets.py --hotpotqa hotpotqa/distractor/train-00000-of-00002.parquet \
        --out-hotpotqa hotpotqa_sft.jsonl --max-examples 20000

LongAlign rows are already chat-formatted, so the conversion is a flattening of
`messages` into an alternating user/assistant list (SFTDataset assigns roles by
position parity, so the order is what matters).

HotpotQA is turned into a retrieval-shaped example: the ten paragraphs of the
distractor setting become the user turn, the question is appended, and the
answer is the assistant turn. Supporting-fact passages stay in the context, but
the paragraph order is shuffled so the answer is not always first.
"""
import argparse
import json
import random

import pyarrow.parquet as pq


def parse_args():
    p = argparse.ArgumentParser()
    p.add_argument('--longalign', default=None)
    p.add_argument('--out-longalign', default='longalign_sft.jsonl')
    p.add_argument('--hotpotqa', default=None)
    p.add_argument('--out-hotpotqa', default='hotpotqa_sft.jsonl')
    p.add_argument('--max-examples', type=int, default=20000)
    p.add_argument('--docs', type=int, default=10,
                   help='how many paragraphs to keep per HotpotQA example')
    p.add_argument('--seed', type=int, default=0)
    return p.parse_args()


def convert_longalign(src, dst):
    n = kept = 0
    with open(src) as f, open(dst, 'w') as out:
        for line in f:
            line = line.strip()
            if not line:
                continue
            n += 1
            row = json.loads(line)
            msgs = row.get('messages') or []
            data = [m.get('content', '') for m in msgs if m.get('role') in ('user', 'assistant')]
            if len(data) < 2 or len(data) % 2 != 0:
                continue        # need a complete user/assistant pairing
            out.write(json.dumps({'data': data}, ensure_ascii=False) + '\n')
            kept += 1
    print(f'longalign: {kept}/{n} examples -> {dst}')


def convert_hotpotqa(src, dst, limit, n_docs, seed):
    rng = random.Random(seed)
    table = pq.read_table(src)
    kept = 0
    with open(dst, 'w') as out:
        for row in table.to_pylist():
            q = (row.get('question') or '').strip()
            a = (row.get('answer') or '').strip()
            if not q or not a:
                continue
            titles = row['context']['title']
            sents = row['context']['sentences']
            order = list(range(len(titles)))
            rng.shuffle(order)
            order = order[:n_docs]

            blocks = []
            for idx in order:
                text = ' '.join(sents[idx]).strip()
                if not text:
                    continue
                blocks.append(f'[{len(blocks) + 1}] {titles[idx]}: {text}')
            if not blocks:
                continue

            user = ('Read the passages below and answer the question.\n\n'
                    + '\n\n'.join(blocks)
                    + f'\n\nQuestion: {q}\nAnswer with the shortest possible phrase.')
            out.write(json.dumps({'data': [user, a]}, ensure_ascii=False) + '\n')
            kept += 1
            if kept >= limit:
                break
    print(f'hotpotqa: {kept} examples from {src} -> {dst}')


def main():
    args = parse_args()
    if args.longalign:
        convert_longalign(args.longalign, args.out_longalign)
    if args.hotpotqa:
        convert_hotpotqa(args.hotpotqa, args.out_hotpotqa, args.max_examples,
                         args.docs, args.seed)
    if not args.longalign and not args.hotpotqa:
        raise SystemExit('nothing to do: pass --longalign and/or --hotpotqa')


if __name__ == '__main__':
    main()