-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDialogGenerator.py
More file actions
341 lines (296 loc) · 13.2 KB
/
DialogGenerator.py
File metadata and controls
341 lines (296 loc) · 13.2 KB
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
import os
import copy
import argparse
import yaml
import random
import pdb
from utils import write_json, quoter, get_index, get_leaf_nodes,\
get_JSON, RequestPool, check_trunk, ProbabilityIterator, get_token_len,\
convert_to_simple_chinese
class DialogGenerator:
def __init__(self, args, request_pool):
self.request_pool = request_pool
self.output_path = args.dialog_path
self.save_interval = args.save_interval
self.end_probability = args.end_probability
self.prompt_path = args.prompt_path
self.min_answer_len = args.min_answer_len
self.add_mode = args.add_mode
self.language = ""
self.is_generate_without_doc = args.generate_without_doc
self.prompt_config = {}
if not os.path.exists(self.output_path):
os.makedirs(self.output_path)
def __del__(self):
del self.request_pool
def set_language(self, language):
self.language = language
with open(self.prompt_path, 'r') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
for d in data:
if d['language'] == self.language:
self.prompt_config = d
def create_init_answer_prompt(self, context, question):
context = quoter(context)
question = question
#print(question)
prompt = self.prompt_config['init_answer_prompt']
prompt = prompt.replace(" ", "")
return [prompt, \
self.prompt_config["context_head"] + context + "\n" + \
self.prompt_config["question_head"] + question + "\n" + \
self.prompt_config["answer_head"]]
def create_init_answer_without_context_prompt(self, question):
question = question
prompt = self.prompt_config['init_answer_prompt']
prompt = prompt.replace(" ", "")
# prompt = ""
return [prompt, \
self.prompt_config["question_head"] + question + "\n" + \
self.prompt_config["answer_head"]]
def create_depth_question_prompt(self, context, dialog):
context = quoter(context)
dialog = dialog
prompt = self.prompt_config['depth_question_prompt']
prompt = prompt.replace(" ", "")
return [prompt, \
self.prompt_config["depth_question_advice"] + "\n" + \
self.prompt_config["context_head"] + context + "\n" + \
self.prompt_config["dialog_head"] + dialog + "\n" + \
self.prompt_config["question_head"]]
def create_width_question_prompt(self, context, dialog):
context = quoter(context)
dialog = dialog
prompt = self.prompt_config['width_question_prompt']
prompt = prompt.replace(" ", "")
return [prompt,\
self.prompt_config["width_question_advice"] + "\n" + \
self.prompt_config["context_head"] + context + "\n" + \
self.prompt_config["dialog_head"] + dialog + "\n" + \
self.prompt_config["question_head"]]
def create_following_answer_prompt(self, context, question, dialog):
context = quoter(context)
question = question
dialog_txt = self.convert_dialog(dialog)
prompt = self.prompt_config['following_answer_prompt']
prompt = prompt.replace(" ", "")
# prompt = ""
return [prompt, \
self.prompt_config["context_head"] + context + "\n" + \
self.prompt_config["dialog_head"] + dialog_txt + \
self.prompt_config["question_head"] + question + "\n" + \
self.prompt_config["answer_head"]]
def create_following_answer_without_context_prompt(self, question, dialog):
question = question
dialog_txt = self.convert_dialog(dialog)
prompt = self.prompt_config['following_answer_prompt']
prompt = prompt.replace(" ", "")
# prompt = ""
return [prompt, \
self.prompt_config["dialog_head"] + dialog_txt + \
self.prompt_config["question_head"] + question + "\n" + \
self.prompt_config["answer_head"]]
def create_question_prompt(self, context, dialog):
random_num = random.randint(0, 1)
dialog_txt = self.convert_dialog(dialog)
if random_num == 0:
return self.create_depth_question_prompt(context, dialog_txt)
else:
return self.create_width_question_prompt(context, dialog_txt)
def convert_dialog(self, dialog): # 从列表转换成一段话
txt = ''
for q, a in dialog:
txt += self.prompt_config["question_head"] + q + '\n' +\
self.prompt_config["answer_head"] + a + '\n'
return txt
def gene_dialog(self, data_path):
# 这里路径没有output,和construct_data_path不同
file_name = self.construct_data_path(data_path)
# print(f"Generate dialog for {file_name}")
if self.add_mode == False:
try:
with open(os.path.join(self.output_path, "questionHadDone.txt"), 'r') as file:
had_done = file.readlines()
if file_name + '\n' in had_done:
# print('have done, skip')
return file_name
except:
pass
index = get_index(os.path.join(self.output_path, file_name))
else:
index = 0
self.process_doc(data_path, index)
with open(os.path.join(self.output_path, "questionHadDone.txt"), 'a+') as file:
content = file_name + '\n'
if content not in file.readlines():
file.write(file_name+'\n')
# print(f'write {file_name} to questionHadDone.txt')
return file_name
def whether_to_continue(self, iterator):
prob = next(iterator)
if random.random() > prob:
return True
else:
return False
def gene_dialog_from_txt(self, txt, questions):
iterator = ProbabilityIterator(self.end_probability)
for question in questions: #拿第一个非0 question
if question == '':
continue
break
if questions == []:
return []
subdialog = []
prompt = self.create_init_answer_prompt(txt, question)
if(check_trunk("".join(prompt))):
return subdialog
answer = self.request_pool.commit(prompt).result()
print("get answer")
if(check_trunk("".join(prompt) + answer)):
return subdialog
elif len(answer) < self.min_answer_len:
return subdialog
elif len(answer) == 0: # 被过滤掉了
return subdialog
subdialog.append(copy.deepcopy([question, answer]))
isContinue = True
while (isContinue):
prompt = self.create_question_prompt(txt, subdialog)
if(check_trunk("".join(prompt))):
isContinue = False
continue
question = self.request_pool.commit(prompt).result()
if(check_trunk("".join(prompt) + question)):
isContinue = False
continue
prompt = self.create_following_answer_prompt(txt, question, subdialog)
if(check_trunk("".join(prompt))):
isContinue = False
continue
answer = self.request_pool.commit(prompt).result()
print("get answer")
if(check_trunk("".join(prompt) + answer)):
isContinue = False
continue
elif len(answer) < self.min_answer_len:
isContinue = False
continue
subdialog.append(copy.deepcopy([question, answer]))
isContinue = self.whether_to_continue(iterator)
return copy.deepcopy(subdialog)
def gene_dialog_without_txt(self, questions):
iterator = ProbabilityIterator(self.end_probability)
questionIterator = iter(questions)
subdialog = []
try:
question = next(questionIterator)
except:
return subdialog
prompt = self.create_init_answer_without_context_prompt(question)
if(check_trunk("".join(prompt))):
return subdialog
answer = self.request_pool.commit(prompt).result()
if(check_trunk("".join(prompt) + answer)):
return subdialog
elif len(answer) < self.min_answer_len:
return subdialog
elif len(answer) == 0: # 被过滤掉了
return subdialog
subdialog.append(copy.deepcopy([question, answer]))
isContinue = True
while (isContinue):
try:
question = next(questionIterator)
except:
break
prompt = self.create_following_answer_without_context_prompt(question, subdialog)
if(check_trunk("".join(prompt))):
isContinue = False
continue
answer = self.request_pool.commit(prompt).result()
if(check_trunk("".join(prompt) + answer)):
isContinue = False
continue
elif len(answer) < self.min_answer_len:
isContinue = False
continue
elif len(answer) == 0:
return subdialog
subdialog.append(copy.deepcopy([question, answer]))
isContinue = self.whether_to_continue(iterator)
return copy.deepcopy(subdialog)
def construct_data_path(self, data_path):
name = data_path.split('/')[-3:]
name = '/'.join(name).split(".")[0] + '_dialog.jsonl'
name = os.path.join(self.output_path, name)
return name
def construct_data_path_without_txt(self, data_path):
name = data_path.split('/')[-3:]
name = '/'.join(name).split(".")[0] + '_dialog_without_txt.jsonl'
name = os.path.join(self.output_path, name)
return name
def process_doc(self, data_path, index = 0) -> list:
jsonlist = get_JSON(data_path)
count = 0
dialog_list = []
if self.is_generate_without_doc:
dialog_without_doc_list = []
for item in jsonlist:
id = item['id']
txt = item['txt']
if self.language == 'zh':
txt = [convert_to_simple_chinese(t) for t in txt]
questions = item['questions']
if self.language == 'zh':
for seq_q in questions:
for q in seq_q:
q = convert_to_simple_chinese(q)
dialog = {}
dialog["id"] = id
dialog['txt'] = txt
dialog['dialogs']= []
had_done_dialog = get_JSON(self.construct_data_path(data_path))
ids = [d['id'] for d in had_done_dialog]
if self.is_generate_without_doc:
dialog_without_doc = {}
dialog_without_doc["id"] = id
dialog_without_doc['dialogs']= []
if id < index:
# print('have done, skip')
continue
if self.add_mode:
if id in ids:
continue
futures = []
for i in range(len(questions)):
futures.append(self.request_pool.submit(self.gene_dialog_from_txt, txt[i], questions[i]))
for f in futures:
result = f.result()
if result: # Ensure non-empty results are appended
dialog['dialogs'].append(result)
if self.is_generate_without_doc:
questions_without_txt = [q for q, _ in result]
dialog_without_doc_result = self.gene_dialog_without_txt(questions_without_txt)
if dialog_without_doc_result: # Check for non-empty results before appending
dialog_without_doc['dialogs'].append(dialog_without_doc_result)
if dialog['dialogs']: # Only append if 'dialogs' is not empty
dialog_list.append(dialog)
if self.is_generate_without_doc and dialog_without_doc['dialogs']: # Check for non-empty before appending
dialog_without_doc_list.append(dialog_without_doc)
count += 1
if self.save_interval > 0 and count >= self.save_interval:
name = self.construct_data_path(data_path)
write_json(dialog_list, name)
if self.is_generate_without_doc:
name2 = self.construct_data_path_without_txt(data_path)
write_json(dialog_without_doc_list, name2)
dialog_without_doc_list = []
count = 0
dialog_list = []
print(f"Save {name}")
name = self.construct_data_path(data_path)
write_json(dialog_list, name)
if self.is_generate_without_doc:
name2 = self.construct_data_path_without_txt(data_path)
write_json(dialog_without_doc_list, name2)
print(f"Save {name}")