-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharegpt_extract.py
516 lines (500 loc) · 24.8 KB
/
sharegpt_extract.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# -*- coding: utf-8 -*-
# 脚本设置使用utf-8编码
from datetime import datetime
import json
from enum import Enum
import schema
import hashlib
import logging
import re
# 配置日志记录
logging.basicConfig(
filename='shareGTP_log_file.log', # 指定日志文件的名称
level=logging.INFO, # 指定日志级别(INFO、WARNING、ERROR、CRITICAL等)
format='%(asctime)s [%(levelname)s]: %(message)s', # 日志格式
datefmt='%Y-%m-%d %H:%M:%S' # 日期和时间格式
)
# 定义一个枚举类
class Json_str(Enum):
JSON_START = "{"
ID = '"id":'
LANG = '"lang":'
TEXT = '"text":'
CONVERSATION_START = '"conversations":'
CONVERSATION_ALL = '"conversations": []'
CONVERSATION_END = ']'
JSON_END = '},'
JSON_END_END = '}'
NONE = ''
# 读取文件
# file_path_all = 'final_data_sample_230706test.json' # 替换为实际文件路径
# 获取每一段对话,输入到json中处理
def process_json_file_gpt4(file_path, write_file, start_line=1, model='gpt4'):
with open(file_path, 'r', encoding='utf-8') as f:
# 定位到指定行数
for _ in range(start_line - 1):
f.readline()
buffer = ""
json_len = 0
json_str_flag = Json_str.NONE.value # 检测到json串时修改为对应状态,检测完成修改回NONE
for line_number, line in enumerate(f, start=start_line):
# 打印迭代信息
logging.debug(f"Line {line_number}: {line}")
# 如果json_len大于1000,就退出
if json_len > 1000:
logging.error(f"Line {line_number}, json len > 1000, exit!") # json检测失败
break
this_line = line.strip()
# 最后的逻辑放到前面,避免重复判断
if json_str_flag == Json_str.CONVERSATION_END.value or json_str_flag == Json_str.CONVERSATION_ALL.value:
if this_line == Json_str.JSON_END.value or this_line == Json_str.JSON_END_END.value:
logging.debug(f"Line {line_number}, json end!") # json解析开始
buffer += '}'
# 重置状态
json_str_flag = Json_str.NONE.value
json_len = 0
if process_json_gpt4(buffer, write_file, model):
buffer = ''
json_len += 1
continue
else:
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"parse stage: {json_str_flag}, json str: {buffer}")
break
else:
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"parse stage: {json_str_flag}, json str: {buffer}")
break
if json_str_flag == Json_str.NONE.value:
if this_line == "[":
logging.debug(f"Line {line_number}, start of text!") # json检测文件开始
continue
elif this_line == "]":
logging.debug(f"Line {line_number}, end of text!") # json检测还没开始就到末尾了
break
elif this_line != "{":
logging.error(f"Line {line_number}, error!") # json检测失败
break
elif this_line == Json_str.JSON_START.value:
logging.debug(f"Line {line_number}, start parsing json!") # json解析开始
json_str_flag = Json_str.JSON_START.value
buffer += this_line
continue
else:
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"parse stage: {json_str_flag}, json str: {buffer}")
break
if json_str_flag == Json_str.JSON_START.value:
if this_line.startswith(Json_str.ID.value):
logging.debug(f"Line {line_number}, id detected!") # json解析开始
json_str_flag = Json_str.ID.value
buffer += this_line
continue
else:
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"parse stage: {json_str_flag}, json str: {buffer}")
break
if json_str_flag == Json_str.ID.value:
if this_line.startswith(Json_str.CONVERSATION_ALL.value):
logging.debug(f"Line {line_number}, conversations all detected!")
json_str_flag = Json_str.CONVERSATION_ALL.value
buffer += this_line
elif this_line.startswith(Json_str.CONVERSATION_START.value):
logging.debug(f"Line {line_number}, conversations detected!") # json解析开始
json_str_flag = Json_str.CONVERSATION_START.value
buffer += this_line
continue
else:
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"parse stage: {json_str_flag}, json str: {buffer}")
break
if json_str_flag == Json_str.CONVERSATION_START.value:
if this_line == Json_str.CONVERSATION_END.value:
logging.debug(f"Line {line_number}, conversations end!")
json_str_flag = Json_str.CONVERSATION_END.value
buffer += this_line
continue
else:
logging.debug(f"Line {line_number}, conversations parsing!")
# TODO:检测下buffer长度,以免数据异常
buffer += this_line
continue
# 撒分支都没走进去
logging.error(f"json parse error!") # json检测失败
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"parse stage: {json_str_flag}, json str: {buffer}")
def process_json_gpt4(json_str, write_file=None, model='gpt4'):
# Check if str is a valid JSON
try:
json_data = json.loads(json_str)
id = json_data['id']
#用json_data的md5值作为id
unique_id = hashlib.md5(json_str.encode('utf-8')).hexdigest()
question_detail = "\"from\": \"human\""
answer_detail = "\"from\": \"gpt\""
other_field = ''
conversation = json_data['conversations']
# 打印conversation的长度,并加上说明
logging.debug(f"conversation length: {len(conversation)}")
# 标识conversation中的对话数以及开始标记
i = 1
conversation_start = False
question = ''
# 循环处理conversation中的每一个json,如果json中的from不是human或gpt,就退出
while len(conversation) > 0:
if conversation[0]['from'] != 'human' and conversation[0]['from'] != 'gpt':
unknown_q_or_a = conversation.pop(0)
# 记录并继续
logging.error(f"unknown conversation: {unknown_q_or_a}")
continue
q_or_a = conversation.pop(0)
if q_or_a['from'] == 'gpt':
# 如果是gpt,且前面没有human,就丢弃
if conversation_start == False:
continue
# 如果是gpt,且前面有human,就生成json
else:
answer = q_or_a['value']
# 生成json
json_str = schema.ShareGPTQASchema(unique_id, question, answer, question_detail, answer_detail, id, i, model, other_field).to_json()
write_file.write(json_str)
write_file.write('\n')
# 对话重置且问答序号加1
conversation_start = False
i += 1
continue
# 如果是human,分两种情况。如果前面也是human,先将前面的human作为单独的问生成json,本次作为新一轮的问答;否则,就将human作为问暂存
if q_or_a['from'] == 'human':
if conversation_start == False:
question = q_or_a['value']
conversation_start = True
continue
else:
# 生成json
json_str = schema.ShareGPTQASchema(unique_id, question, '', question_detail, answer_detail, id, i, model, other_field).to_json()
write_file.write(json_str)
write_file.write('\n')
question = q_or_a['value']
# 对话重置且问答序号加1
conversation_start = False
i += 1
continue
# 如果最后一个是human,就生成json
if conversation_start == True:
# 生成json
json_str = schema.ShareGPTQASchema(unique_id, question, '', question_detail, answer_detail, id, i, model, other_field).to_json()
write_file.write(json_str)
write_file.write('\n')
return True
except json.JSONDecodeError:
logging.error("JSONDecodeError")
return False
# 获取每一段对话,输入到json中处理
def process_json_file_multilang(file_path, write_file, start_line=1, model='multilang'):
with open(file_path, 'r', encoding='utf-8') as f:
# 定位到指定行数
for _ in range(start_line - 1):
f.readline()
buffer = ""
json_len = 0
json_str_flag = Json_str.NONE.value # 检测到json串时修改为对应状态,检测完成修改回NONE
for line_number, line in enumerate(f, start=start_line):
# 打印迭代信息
logging.debug(f"Line {line_number}: {line}")
# 如果json_len大于1000,就退出
if json_len > 1000:
logging.error(f"Line {line_number}, json len > 1000, exit!") # json检测失败
break
this_line = line.strip()
# 最后的逻辑放到前面,避免重复判断
if json_str_flag == Json_str.TEXT.value:
if this_line == Json_str.JSON_END.value or this_line == Json_str.JSON_END_END.value:
logging.debug(f"Line {line_number}, json end!") # json解析开始
buffer += '}'
# 重置状态
json_str_flag = Json_str.NONE.value
json_len = 0
if process_json_multilang(buffer, write_file, model):
buffer = ''
json_len += 1
continue
else:
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"parse stage: {json_str_flag}, json str: {buffer}")
break
else:
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"parse stage: {json_str_flag}, json str: {buffer}")
break
if json_str_flag == Json_str.NONE.value:
if this_line == "[":
logging.debug(f"Line {line_number}, start of text!") # json检测文件开始
continue
elif this_line == "]":
logging.debug(f"Line {line_number}, end of text!") # json检测还没开始就到末尾了
break
elif this_line != "{":
logging.error(f"Line {line_number}, error!") # json检测失败
break
elif this_line == Json_str.JSON_START.value:
logging.debug(f"Line {line_number}, start parsing json!") # json解析开始
json_str_flag = Json_str.JSON_START.value
buffer += this_line
continue
else:
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"parse stage: {json_str_flag}, json str: {buffer}")
break
if json_str_flag == Json_str.JSON_START.value:
if this_line.startswith(Json_str.ID.value):
logging.debug(f"Line {line_number}, id detected!") # json解析开始
json_str_flag = Json_str.ID.value
buffer += this_line
continue
else:
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"parse stage: {json_str_flag}, json str: {buffer}")
break
if json_str_flag == Json_str.ID.value:
if this_line.startswith(Json_str.LANG.value):
logging.debug(f"Line {line_number}, lang detected!")
json_str_flag = Json_str.LANG.value
buffer += this_line
continue
else:
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"parse stage: {json_str_flag}, json str: {buffer}")
break
if json_str_flag == Json_str.LANG.value:
if this_line.startswith(Json_str.TEXT.value):
logging.debug(f"Line {line_number}, text detected!")
json_str_flag = Json_str.TEXT.value
buffer += this_line
continue
else:
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"parse stage: {json_str_flag}, json str: {buffer}")
break
# 撒分支都没走进去
logging.error(f"json parse error!") # json检测失败
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"parse stage: {json_str_flag}, json str: {buffer}")
def process_json_multilang(json_str, write_file=None, model='multilang'):
# Check if str is a valid JSON
try:
json_data = json.loads(json_str)
id = json_data['id']
#用json_data的md5值作为id
unique_id = hashlib.md5(json_str.encode('utf-8')).hexdigest()
question_detail = "<|user|>"
answer_detail = "<|bot|>"
other_field = ", \"lang\": \""+ json_data['lang'] +"\""
conversation = json_data['text']
logging.debug(f"conversation: {json_data['text']}")
# 如果conversation以"\n<|bot|>:"开头,则去掉
if conversation.startswith("\n<|bot|>:"):
logging.error(f"conversation start with \"\n<|bot|>:\", remove it!")
logging.error(f"conversation: {json_data['text']}")
elif conversation.startswith("\n<|user|>:"):
# 标识conversation中的对话数
i = 1
# 定义正则表达式匹配首轮对话
pattern = r'\n<\|user\|>: (.*?)\n\n<\|bot\|>: (.*?)\n\n'
# 使用正则表达式查找匹配项
match_once = re.search(pattern, conversation, re.DOTALL)
if match_once == None:
# 定义正则表达式匹配首轮对话
pattern = r'\n<\|user\|>: (.*?)\n\n$'
# 使用正则表达式查找匹配项
match_once = re.search(pattern, conversation, re.DOTALL)
if match_once == None:
logging.error(f"conversation match error!")
logging.error(f"conversation: {json_data['text']}")
return False
else:
question = match_once.group(1)
answer = ''
# 生成json
json_str = schema.ShareGPTQASchema(unique_id, question, answer, question_detail, answer_detail, id, i, model, other_field).to_json()
write_file.write(json_str)
write_file.write('\n')
# 问答序号加1
i += 1
else:
question = match_once.group(1)
answer = match_once.group(2)
# 生成json
json_str = schema.ShareGPTQASchema(unique_id, question, answer, question_detail, answer_detail, id, i, model, other_field).to_json()
write_file.write(json_str)
write_file.write('\n')
# 问答序号加1
i += 1
# 定义正则表达式匹配后续对话
pattern = r'\n\n<\|user\|>: (.*?)\n\n<\|bot\|>: (.*?)\n\n'
# 使用正则表达式查找匹配项
matches = re.findall(pattern, conversation, re.DOTALL)
# 打印匹配项
for match in matches:
# 如果刚开始匹配,报错
if i == 1:
logging.error(f"conversation match error!")
logging.error(f"conversation: {json_data['text']}")
return False
question = match[0]
answer = match[1]
# 生成json
json_str = schema.ShareGPTQASchema(unique_id, question, answer, question_detail, answer_detail, id, i, model, other_field).to_json()
write_file.write(json_str)
write_file.write('\n')
# 问答序号加1
i += 1
else:
logging.error(f"conversation string error!")
logging.error(f"conversation: {json_data['text']}")
return False
return True
except json.JSONDecodeError:
logging.error("JSONDecodeError")
return False
# 获取每一段对话,输入到json中处理
def process_json_file_common(file_path, write_file, start_line=1, model='common_en'):
with open(file_path, 'r', encoding='utf-8') as f:
# 定位到指定行数
for _ in range(start_line - 1):
f.readline()
for line_number, line in enumerate(f, start=start_line):
# 打印迭代信息
logging.debug(f"Line {line_number}: {line}")
this_line = line.strip()
if process_json_common(this_line, write_file, model):
this_line = ''
continue
else:
logging.error("common json parse error!")
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"json str: {this_line}")
break
def process_json_common(json_str, write_file=None, model='common_en'):
# Check if str is a valid JSON
try:
json_data = json.loads(json_str)
id = json_data['conversation_id']
#用json_data的md5值作为id
unique_id = hashlib.md5(json_str.encode('utf-8')).hexdigest()
question_detail = "\"human\""
answer_detail = "\"assistant\""
other_field = ", \"category\": \""+ json_data['category'] +"\""
conversation = json_data['conversation']
# 打印conversation的长度,并加上说明
logging.debug(f"conversation length: {len(conversation)}")
# 标识conversation中的对话数以及开始标记
i = 1
question = ''
# 循环处理conversation中的每一个json,json在conversation中序号+1就是该轮对话的序号
while len(conversation) > 0:
# 判断conversation中的human是否为null或者是空字符串
if conversation[0]['human'] == None or conversation[0]['human'] == '':
unknown_q_or_a = conversation.pop(0)
# 记录并继续
logging.error(f"unknown conversation: {unknown_q_or_a}")
continue
q_or_a = conversation.pop(0)
question = q_or_a['human']
# 如果assistant不是null或者是空字符串,就生成json
if q_or_a['assistant'] != None and q_or_a['assistant'] != '':
answer = q_or_a['assistant']
else:
answer = ''
# 生成json
json_str = schema.ShareGPTQASchema(unique_id, question, answer, question_detail, answer_detail, id, i, model, other_field).to_json()
write_file.write(json_str)
write_file.write('\n')
# 问答序号加1
i += 1
return True
except json.JSONDecodeError:
logging.error("JSONDecodeError")
return False
# 获取每一段对话,输入到json中处理
def process_json_file_baiduzhidao(file_path, write_file, start_line=1, model='common_en', cur_time='', max_size=500 * 1024 * 1024, output='shareGPT'):
# 文件序号
file_number = 1
write_file = open(f'{args.output}_{args.model}_{cur_time}_{file_number:02}.jsonl', 'w', encoding='utf-8')
with open(file_path, 'r', encoding='utf-8') as f:
# 定位到指定行数
for _ in range(start_line - 1):
f.readline()
for line_number, line in enumerate(f, start=start_line):
# 打印迭代信息
logging.debug(f"Line {line_number}: {line}")
this_line = line.strip()
if process_json_baiduzhidao(this_line, write_file, model):
this_line = ''
else:
logging.error("common json parse error!")
logging.error(f"Line {line_number}, error!") # json检测失败
logging.error(f"json str: {this_line}")
break
# 如果文件超过500M,就关闭文件,新建文件
if write_file.tell() > max_size:
write_file.close()
file_number += 1
write_file = open(f'{args.output}_{args.model}_{cur_time}_{file_number:02}.jsonl', 'w', encoding='utf-8')
def process_json_baiduzhidao(json_str, write_file=None, model='common_en'):
# Check if str is a valid JSON
try:
json_data = json.loads(json_str)
#用json_data的md5值作为id
unique_id = hashlib.md5(json_str.encode('utf-8')).hexdigest()
question_detail = "\"input\""
answer_detail = "\"output\""
question = json_data['input']
answer = json_data['output']
json_schema = schema.ShareGPTQASchema(unique_id, question, answer, question_detail, answer_detail, '', 1, model, '')
# 生成json
json_str = json_schema.to_json()
write_file.write(json_str)
write_file.write('\n')
return True
except json.JSONDecodeError:
logging.error("JSONDecodeError")
return False
except KeyError:
logging.error("KeyError")
return False
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Parse shareGPT into QA data.")
parser.add_argument("source_files",type=str, default="final_data_sample_230706test.json", help="文件名")
parser.add_argument("-o","--output",type=str, default="shareGPT",help="output file name (without extension)")
parser.add_argument("-l","--start_line",type=int,default=1,help="read start line")
parser.add_argument("-s","--max_size",type=int,default=500 * 1024 * 1024,help="max chunk size")
# model:
# gpt4:https://huggingface.co/datasets/Ejafa/GPT_4_with_ShareGPT/tree/main
# common:https://huggingface.co/datasets/shareAI/ShareGPT-Chinese-English-90k/tree/main/sharegpt_jsonl
# multilang:https://huggingface.co/datasets/cryscan/multilingual-share/tree/main
# vicuna:https://huggingface.co/datasets/anon8231489123/ShareGPT_Vicuna_unfiltered/tree/main
# baiduzhidao:baiduzhidao-train.jsonl
parser.add_argument("-m","--model",type=str,default='gpt4',help="multi model parse")
args = parser.parse_args()
# 当前时间
cur_time = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
of = open(f'{args.output}_{args.model}_{cur_time}.jsonl', 'w', encoding='utf-8')
if args.model == 'vicuna' or args.model == 'gpt4':
# 调用函数来处理 JSON 文件,默认从第1行开始读取
process_json_file_gpt4(args.source_files, of, args.start_line, args.model)
elif args.model == 'multilang':
logging.info(f"model: {args.model}")
# 调用函数来处理 JSON 文件,默认从第1行开始读取
process_json_file_multilang(args.source_files, of, args.start_line)
elif args.model == 'common_en' or args.model == 'common_zh':
logging.info(f"model: {args.model}")
# 调用函数来处理 JSON 文件,默认从第1行开始读取
process_json_file_common(args.source_files, of, args.start_line, args.model)
elif args.model == 'baiduzhidao':
logging.info(f"model: {args.model}")
# 调用函数来处理 JSON 文件,默认从第1行开始读取
process_json_file_baiduzhidao(args.source_files, of, args.start_line, args.model, cur_time, args.max_size, args.output)
else:
logging.error(f"model: {args.model} not support!")
of.close()