forked from salesforce/simpletod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_dialogue.py
392 lines (319 loc) · 15 KB
/
generate_dialogue.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import torch
from transformers import GPT2Tokenizer, GPT2LMHeadModel, OpenAIGPTLMHeadModel, OpenAIGPTTokenizer
from utils.args_parser import ArgsParser
from data.dataset.multiwoz import MultiWozDataset
from evaluate_multiwoz import MultiWozDB
from utils.multiwoz import dbPointer
from utils.simpletod import *
import tqdm
import json
import ipdb
import sys
import os
opt = ArgsParser().parse()
opt.multiwoz_version = '2.1'
opt.use_action = True
opt.use_knowledge = True
opt.context_knowledge = True
opt.lexical = True
HISTORY_LEN = None
# USE_ORACLE_BELIEF = True
USE_ORACLE_BELIEF = opt.use_oracle_belief
# USE_ORACLE_ACTION = False
USE_ORACLE_ACTION = opt.use_oracle_action
# USE_DB_SEARCH = True
USE_DB_SEARCH = opt.use_db_search
USE_DYNAMIC_DB = opt.use_dynamic_db
# EVAL_SPLIT = 'test'
EVAL_SPLIT = opt.split_set
decoding = opt.decoding
multiwoz_data = json.load(open('resources/multi-woz/lex.json','r'))
# provide model name and checkpoint directory
# exp_name = 'gpt2'
# exp_name = opt.experiment_name
# checkpoint = opt.checkpoint
# model_checkpoint = '../dialog-transformer/output/{}/{}/'.format(exp_name, checkpoint)
model_checkpoint = opt.checkpoint
exp_name = os.path.split(model_checkpoint)[0].split('/')[-2]
multiwoz_db = MultiWozDB()
opt_delex = ArgsParser().parse()
opt_delex.multiwoz_version = '2.1'
data = MultiWozDataset(opt, split=EVAL_SPLIT, shuffle=False)
data_delex = MultiWozDataset(opt_delex, split=EVAL_SPLIT, shuffle=False)
lex_dict = {}
delex_dict = {}
for d in data:
lex_dict[d['name']] = d
for d in data_delex:
delex_dict[d['name']] = d
if 'openai-gpt' in model_checkpoint:
tokenizer = OpenAIGPTTokenizer.from_pretrained(model_checkpoint)
tokenizer.add_special_tokens({'bos_token': '<|endoftext|>'})
tokenizer.add_special_tokens({'eos_token': '<|endoftext|>'})
else:
tokenizer = GPT2Tokenizer.from_pretrained(model_checkpoint)
if 'openai-gpt' in model_checkpoint:
model = OpenAIGPTLMHeadModel.from_pretrained(model_checkpoint)
else:
model = GPT2LMHeadModel.from_pretrained(model_checkpoint)
model.eval()
model.to('cuda')
break_tokens = tokenizer.encode(tokenizer.eos_token)
MAX_LEN = model.config.n_ctx
generated_dict = {}
num_data = len(data)
for i, dial_name in enumerate(lex_dict):
if EVAL_SPLIT == 'train' and i > 1000:
break
d = lex_dict[dial_name]
d_delex = delex_dict[dial_name]
print('{} [{}/{}] \r'.format(d['name'], i, num_data), end='')
sys.stdout.flush()
beliefs_raw = d['belief_raw']
user = d['input_raw']
system = d['target_raw']
system_delex = d_delex['target_raw']
if 'delex' in model_checkpoint:
target_response = system_delex
else:
target_response = system
action = d['action_raw']
target_action = []
for turn_act in action:
turn_action = []
for act in turn_act:
act_str = '{} {} {}'.format(act[0], act[1], act[2])
turn_action.append(act_str)
target_action.append(turn_action)
dialogue_aggregated_target_belief = []
dialogue_target_belief = []
for turn_belief in beliefs_raw:
turn_belief_str = []
for bs in turn_belief:
domain, slot, value = bs
if value in ['not mentioned', 'none']:
continue
bs_str = '{} {} {}'.format(domain.lower(), slot.lower(), value.lower())
if bs_str not in dialogue_aggregated_target_belief:
dialogue_aggregated_target_belief.append(bs_str)
turn_belief_str.append(bs_str)
dialogue_target_belief.append(turn_belief_str)
db_data = d['db']
goal = multiwoz_data[dial_name]['goal']
generated = []
model_context = []
for turn_id, (usr_turn, _) in enumerate(zip(user, system)):
if turn_id == 0:
tmp_text = '<|user|> {}'.format(usr_turn.strip())
else:
tmp = []
for k in range(turn_id):
tmp.append('<|user|> {}'.format(user[k].strip()))
tmp.append('<|system|> {}'.format(system[k].strip()))
tmp.append('<|user|> {}'.format(usr_turn.strip()))
# trim history
if HISTORY_LEN and len(tmp) > HISTORY_LEN:
tmp = tmp[-1*HISTORY_LEN:]
tmp_text = ' '.join(tmp)
if dial_name == 'SNG02319.json':
tmp_text = tmp_text.replace('300 will', '03:00 will')
text = '{} <|context|> {} <|endofcontext|> '.format(tokenizer._bos_token, tmp_text)
if USE_ORACLE_BELIEF:
turn_belief = dialogue_target_belief[turn_id]
belief_str = '<|belief|> {} <|endofbelief|>'.format(' , '.join(turn_belief))
text = text + ' ' + belief_str
db_text = dbPointer.convert_dbpointer_to_text(db_data[turn_id], goal, beliefs_raw[turn_id])
if USE_DB_SEARCH and USE_ORACLE_BELIEF:
if not USE_ORACLE_BELIEF:
print('warning: oracle db is true, oracle belief is false')
text += ' <|dbsearch|> {} <|endofdbsearch|>'.format(db_text)
if USE_ORACLE_ACTION:
turn_action = target_action[turn_id]
action_str = '<|action|> {} <|endofaction|>'.format(' , '.join(turn_action))
text = text + ' ' + action_str
model_context.append(text)
indexed_tokens = tokenizer.encode(text)
if len(indexed_tokens) > MAX_LEN:
indexed_tokens = indexed_tokens[-1*MAX_LEN:]
# Convert indexed tokens in a PyTorch tensor
tokens_tensor = torch.tensor([indexed_tokens])
# If you have a GPU, put everything on cuda
tokens_tensor = tokens_tensor.to('cuda')
predicted_index = indexed_tokens[-1]
if USE_DB_SEARCH and not USE_ORACLE_BELIEF: # generate belief, then get DB search results, then continue generation (greedy decoding)
with torch.no_grad():
while predicted_index not in break_tokens:
outputs = model(tokens_tensor)
predictions = outputs[0]
predicted_index = torch.argmax(predictions[0, -1, :]).item()
indexed_tokens += [predicted_index]
tokens_tensor = torch.tensor([indexed_tokens]).to('cuda')
if len(indexed_tokens) > MAX_LEN:
break
if tokenizer.decode(indexed_tokens).endswith('<|endofbelief|>'):
break
tmp_pred = tokenizer.decode(indexed_tokens)
if not USE_DYNAMIC_DB: # use oracle db
text = '{} {}'.format(tmp_pred, db_text)
else: # use dynamic db search results (using generated belief)
db_text_dynamic = get_db_dynamically(tmp_pred, goal, multiwoz_db=multiwoz_db)
text = '{} {}'.format(tmp_pred, db_text_dynamic)
# continue generation
indexed_tokens = tokenizer.encode(text)
if len(indexed_tokens) > MAX_LEN:
indexed_tokens = indexed_tokens[-1 * MAX_LEN:]
# Convert indexed tokens in a PyTorch tensor
tokens_tensor = torch.tensor([indexed_tokens])
# If you have a GPU, put everything on cuda
tokens_tensor = tokens_tensor.to('cuda')
predicted_index = indexed_tokens[-1]
# Predict all tokens
with torch.no_grad():
#while predicted_index not in break_tokens:
while predicted_index not in break_tokens:
outputs = model(tokens_tensor)
predictions = outputs[0]
predicted_index = torch.argmax(predictions[0, -1, :]).item()
indexed_tokens += [predicted_index]
# sometime model generate repeated actions, we just use truncate actions if this happens
predicted_text = tokenizer.decode(indexed_tokens)
if '<|action|>' in predicted_text:
generated_actions = predicted_text.split('<|action|>')[-1].split('<|endofaction|>')[
0].split(',')
new_actions = []
for a in generated_actions:
if a in ['', ' ']:
continue
new_actions.append(a.strip())
len_actions = len(new_actions)
if len(list(set(new_actions))) > len(new_actions) or (
len_actions > 10 and not truncate_action):
actions = '<|action|> {} <|endofaction|>'.format(' , '.join(list(set(new_actions))))
indexed_tokens = tokenizer.encode(
'{} {}'.format(predicted_text.split('<|action|>')[0], actions))
truncate_action = True
tokens_tensor = torch.tensor([indexed_tokens]).to('cuda')
if len(indexed_tokens) > MAX_LEN:
break
if tokenizer.decode(indexed_tokens).endswith('<|endofresponse|>'):
break
predicted_text = tokenizer.decode(indexed_tokens)
generated.append(predicted_text)
else: # generate belief, action, and response once
with torch.no_grad():
if decoding == 'nucleus':
sample_output = model.generate(
tokens_tensor,
# indexed_tokens,
do_sample=True,
max_length=MAX_LEN,
top_p=0.5,
top_k=0
)
predicted_text = tokenizer.decode(sample_output[0])
tmp = ' '.join([predicted_text.split('<|endofresponse|>')[0], '<|endofresponse|>'])
predicted_text = tmp
generated.append(predicted_text)
elif decoding == 'greedy':
# GREEDY DECODING
# sample_output = model.generate(
# # tokens_tensor,
# indexed_tokens,
# max_length=MAX_LEN,
# do_sample=False
# )
while predicted_index not in break_tokens:
outputs = model(tokens_tensor)
predictions = outputs[0]
predicted_index = torch.argmax(predictions[0, -1, :]).item()
indexed_tokens += [predicted_index]
# sometime model generate repeated actions, we just use truncate actions if this happens
predicted_text = tokenizer.decode(indexed_tokens)
if '<|action|>' in predicted_text:
generated_actions = predicted_text.split('<|action|>')[-1].split('<|endofaction|>')[
0].split(',')
new_actions = []
for a in generated_actions:
if a in ['', ' ']:
continue
new_actions.append(a.strip())
len_actions = len(new_actions)
if len(list(set(new_actions))) > len(new_actions) or (
len_actions > 10 and not truncate_action):
actions = '<|action|> {} <|endofaction|>'.format(' , '.join(list(set(new_actions))))
indexed_tokens = tokenizer.encode(
'{} {}'.format(predicted_text.split('<|action|>')[0], actions))
truncate_action = True
tokens_tensor = torch.tensor([indexed_tokens]).to('cuda')
if len(indexed_tokens) > MAX_LEN:
break
if tokenizer.decode(indexed_tokens).endswith('<|endofresponse|>'):
break
predicted_text = tokenizer.decode(indexed_tokens)
tmp = ' '.join([predicted_text.split('<|endofresponse|>')[0], '<|endofresponse|>'])
generated.append(predicted_text)
# predicted_text = tokenizer.decode(sample_output[0])
# tmp = ' '.join([predicted_text.split('<|endofresponse|>')[0], '<|endofresponse|>'])
# predicted_text = tmp
# generated.append(predicted_text)
dialogue_aggregated_pred_belief = []
dialogue_pred_belief = []
dialogue_pred_responses = []
dialogue_pred_action = []
# aggregate belief states
for turn, pred in enumerate(generated):
turn_pred_belief = []
if 'openai-gpt' in model_checkpoint:
belief = get_belief_openaigpt(pred)
else:
if 'dbsearch' in model_checkpoint or 'dbnmatch' in model_checkpoint or USE_DB_SEARCH or 'db' in model_checkpoint:
belief = get_belief_dbsearch(pred)
else:
belief = get_belief(pred)
if len(belief) > 0:
for bs in belief:
if bs not in ['', ' '] and bs not in dialogue_aggregated_pred_belief:
dialogue_aggregated_pred_belief.append(bs)
new_belief = list(set(belief))
dialogue_pred_belief.append(new_belief)
else:
if len(dialogue_pred_belief) == 0:
dialogue_pred_belief.append([''])
else:
dialogue_pred_belief.append(dialogue_pred_belief[-1])
if 'openai-gpt' in model_checkpoint:
gen_response = get_response_openaigpt(pred, tokenizer)
else:
gen_response = get_response(pred, tokenizer)
dialogue_pred_responses.append(gen_response)
if 'openai-gpt' in model_checkpoint:
gen_action = get_action_openaigpt(pred)
else:
gen_action = get_action(pred)
dialogue_pred_action.append(gen_action)
generated_dict[d['name']] = {
'target_belief': dialogue_aggregated_target_belief,
'target_turn_belief': dialogue_target_belief,
'generated_belief': dialogue_aggregated_pred_belief,
'generated_turn_belief': dialogue_pred_belief,
'target_response': target_response,
'generated_response': dialogue_pred_responses,
'target_action': target_action,
'generated_action': dialogue_pred_action,
'target_user': user,
'model_context': model_context
}
save_name = '{}_{}'.format(exp_name, EVAL_SPLIT)
if USE_ORACLE_BELIEF:
save_name += '_oracleBelief'
if USE_DB_SEARCH:
save_name += '_oracleDB'
if USE_ORACLE_ACTION:
save_name += '_oracleAction'
if HISTORY_LEN:
save_name += '_context[history={}]'.format(HISTORY_LEN)
else:
save_name += '_context[history=full_history]'
save_name += '_nocarry'
with open('{}.json'.format(save_name), 'wt') as f:
json.dump(generated_dict, f)