forked from FlagAI-Open/FlagAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_conversations_v3.py
69 lines (60 loc) · 2.05 KB
/
make_conversations_v3.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
import jsonlines
import re
import os
## Usage:
### 1. replace using vim or sed command
#### %s/\[Round \d\]//g
#### %s/\\n问:/ #用户#/g
#### %s/\\n答:/ #ai助手#/g
### 2. convo format converting
input_file = f'input.jsonl'
output_file = f'input_convo.jsonl'
SEP = ' '
HUMAN = SEP + '#用户#'
BOT = SEP + '#ai助手#'
fo = jsonlines.open(output_file, mode='w')
with jsonlines.open(input_file) as reader:
for idx, input_obj in enumerate(reader):
line = input_obj['prompt'] + input_obj['response']
obj = dict()
obj['id'] = f"{os.path.basename(input_file)}_%d" % idx
obj['conversations'] = []
obj['instruction'] = ''
obj['raw'] = line
role_str = HUMAN
role = 'gpt'
prev_start_idx = -1
prev_end_idx = -1
start_idx = 0
first_human = True
while True:
start_idx = line.find(role_str, start_idx)
if start_idx == -1:
conversation = dict()
conversation["from"] = role
conversation["value"] = line[prev_end_idx:]
obj['conversations'].append(conversation)
break
end_idx = start_idx + len(role_str)
if role_str == HUMAN:
if first_human:
pass
else:
conversation = dict()
conversation["from"] = role
conversation["value"] = line[prev_end_idx:start_idx]
obj['conversations'].append(conversation)
first_human = False
role_str = BOT
role = 'human'
else:
conversation = dict()
conversation["from"] = role
conversation["value"] = line[prev_end_idx:start_idx]
obj['conversations'].append(conversation)
role_str = HUMAN
role = 'gpt'
prev_start_idx = start_idx
prev_end_idx = end_idx
start_idx = end_idx + len(role_str)
fo.write(obj)