forked from ContextualAI/gritlm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reformat_medi.py
77 lines (71 loc) · 2.76 KB
/
reformat_medi.py
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
import itertools
import json
DEFAULT_INSTRUCTION = "Represent this text:"
# Turn from json to list of dict (jsonl) for each task
with open('/data/niklas/gritlm/medi-data-hardnegatives.json', 'r') as f:
# Sort by task_id
data = json.load(f)
for task_id, dg in itertools.groupby(data, key=lambda x: x["task_id"]):
print("Running:", task_id)
with open(f'/data/niklas/gritlm/meditaskdata/medi-data-hardnegatives-instruct-{task_id}.jsonl', 'w') as g:
for d in dg:
for k in ["query", "pos", "neg"]:
assert len(d[k]) == 2
# Always have an instruction
if not d[k][0]:
d[k][0] = DEFAULT_INSTRUCTION
if k == "query":
d[k] = [d[k][0], d[k][1]]
else:
d[k] = [[d[k][0], d[k][1]]]
json.dump(d, g, ensure_ascii=False)
g.write('\n')
"""Slow version (-> same as above but slower)
with open('medi-data-hardnegatives.json', 'r') as f:
data = json.load(f)
for d in data:
for k in ["query", "pos", "neg"]:
assert len(d[k]) == 2
# Always have an instruction
if not d[k][0]:
d[k][0] = DEFAULT_INSTRUCTION
if k == "query":
d[k] = [d[k][0], d[k][1]]
else:
d[k] = [[d[k][0], d[k][1]]]
with open(f'medi-data-hardnegatives-instruct-{d["task_id"]}.jsonl', 'w') as g:
json.dump(d, g, ensure_ascii=False)
g.write('\n')
"""
"""Version without separate files (-> in-batch items may come from different tasks with this)
import json
DEFAULT_INSTRUCTION = "Represent this text:"
# Turn from json to list of dict (jsonl)
with open('medi-data-hardnegatives.json', 'r') as f, open('medi-data-hardnegatives-instruct.jsonl', 'w') as g:
data = json.load(f)
for d in data:
for k in ["query", "pos", "neg"]:
assert len(d[k]) == 2
# Always have an instruction
if not d[k][0]:
d[k][0] = DEFAULT_INSTRUCTION
if k == "query":
d[k] = [d[k][0], d[k][1]]
else:
d[k] = [[d[k][0], d[k][1]]]
json.dump(d, g, ensure_ascii=False)
g.write('\n')
"""
"""Version without separating out instructions (-> instruction is part of the embedding with this)
import json
separator = " "
# Turn from json to list of dict (jsonl)
with open('medi-data.json', 'r') as f, open('medi-data.jsonl', 'w') as g:
data = json.load(f)
for d in data:
for k in ["query", "pos", "neg"]:
assert len(d[k]) == 2
d[k] = separator.join(d[k])
json.dump(d, g, ensure_ascii=False)
g.write('\n')
"""