-
Notifications
You must be signed in to change notification settings - Fork 2
/
emp_gpt3.py
335 lines (256 loc) · 11.4 KB
/
emp_gpt3.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
from typing import List
import os
import json
import openai
from regex import P
from tqdm import tqdm
import pickle as pc
import random
from copy import deepcopy
import argparse
from collections import defaultdict
import torch
import numpy as np
from sentence_transformers import SentenceTransformer, util
os.environ["TOKENIZERS_PARALLELISM"] = "true"
openai.organization = "<YOUR ORG>"
# Load your API key from an environment variable or secret management service
openai.api_key = "<YOUR KEY>"
class Prompt(object):
def __init__(self, prompt_path: str, spk1: str, spk2: str,
do_fewshot: bool, k: int, fewshot_data: List, fewshot_type: str
):
self.prompt_path = prompt_path
with open(prompt_path, 'r') as f:
self.init_prompt = f.read()
self.spk1 = spk1
self.spk2 = spk2
self.do_fewshot = do_fewshot
self.k = k
self.fewshot_data = fewshot_data
self.reverse_order = True
self.fewshot_type = fewshot_type
if self.do_fewshot:
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model = SentenceTransformer('stsb-roberta-large').to(self.device)
if self.fewshot_type == 'situation':
# data {conv_idx: situation} format
fewshot_data = []
for conv in self.fewshot_data:
for example in conv:
situation = example['situation']
fewshot_data.append(situation)
self.fewshot_embedding = self.model.encode(fewshot_data, convert_to_tensor=True, device=self.device)
elif self.fewshot_type == 'emotion':
fewshot_data = defaultdict(list)
for conv in self.fewshot_data:
for example in conv:
conv_idx = example['conv_idx']
emotion = example['emotion']
situation = example['situation']
fewshot_data[emotion].append((situation, conv_idx))
fewshot_emb = {}
for emo, _conv in fewshot_data.items():
conv = [ele[0] for ele in _conv]
fewshot_emb[emo] = self.model.encode(conv, convert_to_tensor=True, device=self.device)
self.fewshot_embedding = fewshot_emb
self.fewshot_data_for_emo = fewshot_data
def _prepare_fewshot_embedding(self, fewshot_data):
results = {}
for conv_idx, situation in tqdm(fewshot_data.items(), total=len(fewshot_data)):
sit_embedding = self.model.encode(situation, convert_to_tensor=True, device=self.device)
results[conv_idx] = sit_embedding
return results
def get_test_embedding(self, test_situation):
return self.model.encode(test_situation, convert_to_tensor=True, device=self.device)
def get_cosine_sim(self, test_embedding, emotion=None):
if emotion is not None:
fewshot_embedding = self.fewshot_embedding[emotion]
else:
fewshot_embedding = self.fewshot_embedding
cosine_scores = util.cos_sim(test_embedding, fewshot_embedding)[0].detach().cpu().numpy()
sort = np.argsort(cosine_scores)[::-1]
return sort[:self.k], [cosine_scores[idx] for idx in sort[:self.k]]
def make_fewshot_prompt_input(self, test_situation, emotion=None):
test_embedding = self.get_test_embedding(test_situation)
fewshot_indices, fewshot_sim_scores = self.get_cosine_sim(test_embedding, emotion)
fewshot_prompt = []
fewshot_sim_results = []
fewshot_check_results = []
for fewshot_idx, fewshot_sim_score in zip(fewshot_indices, fewshot_sim_scores):
input_prompt = deepcopy(self.init_prompt)
if self.fewshot_type == 'situation':
conv = self.fewshot_data[fewshot_idx]
elif self.fewshot_type == 'emotion':
_, conv_idx = self.fewshot_data_for_emo[emotion][fewshot_idx]
conv = self.fewshot_data[conv_idx]
for i, example in enumerate(conv):
utter = example['utter']
emotion = example['emotion']
if i % 2 == 0:
spk = self.spk1
else:
spk = self.spk2
input_prompt += f'{spk}: {utter}\n'
fewshot_prompt.append(input_prompt)
fewshot_sim_results.append(fewshot_sim_score)
fewshot_check_results.append([example['situation'], example['emotion']])
if self.reverse_order:
fewshot_prompt.reverse()
fewshot_sim_results.reverse()
fewshot_check_results.reverse()
fewshot_prompt_text = '\n'.join(fewshot_prompt)
return fewshot_prompt_text, fewshot_sim_results, fewshot_check_results
def make_fewshot_prompt_input_random(self):
sampled_fewshot_data = random.sample(self.fewshot_data, self.k)
fewshot_prompt = []
for conv in sampled_fewshot_data:
input_prompt = deepcopy(self.init_prompt)
for i, example in enumerate(conv):
utter = example['utter']
emotion = example['emotion']
if i % 2 == 0:
spk = self.spk1
else:
spk = self.spk2
input_prompt += f'{spk}: {utter}\n'
fewshot_prompt.append(input_prompt)
fewshot_prompt_text = '\n'.join(fewshot_prompt)
return fewshot_prompt_text
def make_prompt_input(self, conv_data):
results = []
cnt = 0
check_results = []
for conv in tqdm(conv_data):
cnt += 1
if self.do_fewshot:
if self.fewshot_type == 'situation':
situation = conv[0]['situation']
fewshot_prompt_input, fewshot_sim_results, fewshot_check_results = self.make_fewshot_prompt_input(situation)
check_results.append({
'prompt_input': fewshot_prompt_input,
'target_sit': situation,
'target_emo': conv[0]['emotion'],
'ex': fewshot_check_results,
'sim': fewshot_sim_results,
})
#assert 1 == 0
elif self.fewshot_type == 'emotion':
emotion = conv[0]['emotion']
situation = conv[0]['situation']
fewshot_prompt_input, fewshot_sim_results, fewshot_check_results = self.make_fewshot_prompt_input(situation, emotion)
check_results.append({
'prompt_input': fewshot_prompt_input,
'target_sit': situation,
'target_emo': emotion,
'ex': fewshot_check_results,
'sim': fewshot_sim_results,
})
elif self.fewshot_type == 'random':
fewshot_prompt_input = self.make_fewshot_prompt_input_random()
input_prompt = fewshot_prompt_input + '\n' + deepcopy(self.init_prompt)
else:
input_prompt = deepcopy(self.init_prompt)
for i, example in enumerate(conv):
utter = example['utter']
emotion = example['emotion']
if i % 2 == 0:
spk = self.spk1
else:
spk = self.spk2
if i == len(conv) - 1:
input_prompt += f'{spk}:'
gold_resp = utter
gold_emo = emotion
else:
input_prompt += f'{spk}: {utter}\n'
_ret = {
'prompt_input': input_prompt,
'dialog': conv,
'gold_resp': gold_resp,
'gold_emo': gold_emo
}
if self.do_fewshot and self.fewshot_type != 'random':
_ret['fewshot_sim_scores'] = fewshot_sim_results
results.append(_ret)
assert len(results) == len(conv_data)
return results
def make_openai_call(self, prompt_input, stop_seq: List[str]):
temp = 0.8
max_tokens = 128
top_p = 1
freq_penalty = 0.4
pres_penalty = 0.4
#stop_seq = ['###']
response = openai.Completion.create(
engine="davinci",
prompt=prompt_input,
temperature=temp,
max_tokens=max_tokens,
top_p=top_p,
frequency_penalty=freq_penalty,
presence_penalty=pres_penalty,
stop=stop_seq,
)
resp = response['choices'][0]['text']
return resp[1:].strip()
def make_conversation(data):
conv_data, tmp = [], []
prev_conv_idx = 0
for i, example in enumerate(data):
conv_idx = example['conv_idx']
utter_idx = example['utter_idx']
utter = example['utterance']
emotion = example['emotion']
situation = example['situation']
if prev_conv_idx != conv_idx:
conv_data.append(tmp)
tmp = []
prev_conv_idx = conv_idx
tmp.append({'conv_idx': conv_idx, 'utter': utter, 'emotion': emotion, 'situation': situation})
conv_data.append(tmp)
return conv_data
def prepare_train_for_fewshot():
data_dir = f'./data/annotated_data/empathetic_dialogues/train.jsonl'
with open(data_dir, 'r') as f:
data = [json.loads(line.strip()) for line in f.readlines()]
return make_conversation(data)
def _parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--do-fewshot', action="store_true")
parser.add_argument('--k', default=0, type=int)
parser.add_argument('--do-empgpt3', action="store_true")
parser.add_argument('--fewshot-type', default='situation')
args = parser.parse_args()
return args
if __name__ == '__main__':
datatype = 'test'
data_dir = f'./data/annotated_data/empathetic_dialogues/{datatype}.jsonl'
with open(data_dir, 'r') as f:
data = [json.loads(line.strip()) for line in f.readlines()]
# make conversation dataset
conv_data = make_conversation(data)
fewshot_data = prepare_train_for_fewshot()
print(len(fewshot_data))
args = _parse_args()
if args.do_empgpt3:
prompt_dir = './prompt/prompt.txt'
spk1 = 'Human'
spk2 = 'Empathy AI'
prompt_typ = 'empathy'
else:
prompt_dir = './prompt/vanilla_prompt.txt'
spk1 = 'Human'
spk2 = 'AI'
prompt_typ = 'vanilla'
prompt = Prompt(prompt_dir, spk1, spk2, args.do_fewshot, args.k, fewshot_data, args.fewshot_type)
results = prompt.make_prompt_input(conv_data)
result_save_dir = f'./result/davinci/empgpt3_{prompt_typ}_prompt_fewshot_{args.k}_{args.fewshot_type}_all/{datatype}'
os.makedirs(result_save_dir, exist_ok=True)
ret_val = []
for result in tqdm(results):
resp = prompt.make_openai_call(result['prompt_input'], stop_seq=[f'{spk1}:', f'{spk2}:'])
result.update({'pred_resp': resp})
ret_val.append(result)
with open(os.path.join(result_save_dir, 'results.pkl'), 'wb') as f:
pc.dump(ret_val, f)