-
Notifications
You must be signed in to change notification settings - Fork 76
/
rrm_augmentation.py
205 lines (194 loc) · 4.69 KB
/
rrm_augmentation.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import random
import re
import tqdm
from datasets import load_dataset
from datasets import Dataset, DatasetDict
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--input_dataset", type=str)
parser.add_argument("--output_dataset", type=str)
args = parser.parse_args()
def get_fields(messages) -> dict[str, str]:
delimiters = r'\[CONTEXT\]|\[RESPONSE A\]|\[RESPONSE B\]'
# Split the string
result = re.split(delimiters, messages[0]['content'])
# Remove empty strings that may result from consecutive delimiters or delimiters at the start/end
result = [x for x in result if x]
assert len(result) == 3
assert messages[1]['content'] in ['A', 'B', 'Same']
if messages[1]['content'] == 'A':
return {
'context': result[0],
'response_w': result[1],
'response_l': result[2],
'neutral': False
}
elif messages[1]['content'] == 'B':
return {
'context': result[0],
'response_l': result[1],
'response_w': result[2],
'neutral': False
}
else:
return {
'context': result[0],
'response_w': result[1],
'response_l': result[2],
'neutral': True
}
def to_messages(fields: dict[str, str]) -> str:
context = fields['context']
neutral = fields["neutral"]
if random.randint(0,1):
response_a = fields['response_w']
response_b = fields['response_l']
label = "A"
else:
response_a = fields['response_l']
response_b = fields['response_w']
label = "B"
if neutral:
label = "Same"
message_0 = {
"role": "user",
"content": f"[CONTEXT]{context}" +
f"[RESPONSE A]{response_a}" +
f"[RESPONSE B]{response_b}"
}
message_1 = {
"role": "assistant",
"content": label
}
return [message_0, message_1]
def get_augmented(data):
data_i = data
data_j = data_i.copy()
random.shuffle(data_j)
data_k = data_j.copy()
random.shuffle(data_k)
for ex_i, ex_j, ex_k in zip(data_i, data_j, data_k):
xi = ex_i['context']
xj = ex_j['context']
xk = ex_k['context']
ywi = ex_i['response_w']
ywj = ex_j['response_w']
ywk = ex_k['response_w']
yli = ex_i['response_l']
ylj = ex_j['response_l']
ylk = ex_k['response_l']
# xi_ywi_ywj
yield {
"context": xi,
"response_w": ywi,
"response_l": ywj,
"neutral": False
}
# xi_ywi_ywk
yield {
"context": xi,
"response_w": ywi,
"response_l": ywk,
"neutral": False
}
# xi_ywi_ylj
yield {
"context": xi,
"response_w": ywi,
"response_l": ylj,
"neutral": False
}
# xi_ywi_ylk
yield {
"context": xi,
"response_w": ywi,
"response_l": ylk,
"neutral": False
}
# xi_yli_ywj
yield {
"context": xi,
"response_w": yli,
"response_l": ywj,
"neutral": False
}
# xi_yli_ywk
yield {
"context": xi,
"response_w": yli,
"response_l": ywk,
"neutral": False
}
# xi_yli_ylj
yield {
"context": xi,
"response_w": yli,
"response_l": ylj,
"neutral": False
}
# xi_yli_ylk
yield {
"context": xi,
"response_w": yli,
"response_l": ylk,
"neutral": False
}
# xi_ywj_ylj
yield {
"context": xi,
"response_w": ywj,
"response_l": ylj,
"neutral": True
}
# xi_ywk_ylk
yield {
"context": xi,
"response_w": ywk,
"response_l": ylk,
"neutral": True
}
# xi_ywj_ywk
yield {
"context": xi,
"response_w": ywj,
"response_l": ywk,
"neutral": True
}
# xi_ywj_ylk
yield {
"context": xi,
"response_w": ywj,
"response_l": ylk,
"neutral": True
}
# xi_ywk_ylj
yield {
"context": xi,
"response_w": ywk,
"response_l": ylj,
"neutral": True
}
# xi_ylj_ylk
yield {
"context": xi,
"response_w": ylj,
"response_l": ylk,
"neutral": True
}
def process_data(data):
all_fields = []
for d in tqdm.tqdm(data):
try:
all_fields.append(get_fields(d['messages']))
except:
print(d['messages'])
for fields in tqdm.tqdm(get_augmented(all_fields)):
yield to_messages(fields)
ds = load_dataset(args.input_dataset, split='train')
processed_messages = list(process_data(list(ds)))
new_ds = {'train': []}
for m in processed_messages:
new_ds['train'].append({'messages': m})
dict_data = {'messages': [x['messages'] for x in new_ds['train']]}
dataset = Dataset.from_dict(dict_data)
DatasetDict({"train": dataset}).push_to_hub(args.output_dataset)