-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdialog_generate.py
187 lines (158 loc) · 5.54 KB
/
dialog_generate.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
import concurrent.futures
import configparser
import json
import os
import re
from datasets import concatenate_datasets, load_dataset
from tqdm.auto import tqdm
from utils import get_api2d_response, get_azure_response, get_openai_response
def init_data(seeds_path: str, machine_path: str):
machine_data = load_dataset(
'json',
data_files = os.path.join('data', seeds_path),
split = 'train'
)
mental_health_data = load_dataset(
'json',
data_files = os.path.join('data', machine_path),
split = 'train'
)
dataset = concatenate_datasets([machine_data, mental_health_data])
templates = []
fp = open(data_path, encoding='utf-8', mode='a')
for data in tqdm(dataset):
template_data = template.format(
ai_persona = ai_persona,
user_persona = data['persona'],
user_situation = data['situation']
)
templates.append(template_data)
temp = {
'template' : template_data,
'ai_persona' : ai_persona,
'user_persona' : data['persona'],
'user_situation': data['situation']
}
fp.write(json.dumps(temp, ensure_ascii=False) + '\n')
def post_process(conversation):
fp = open('train_psyqa.json', 'a')
history = []
for conv in conversation:
prompt = re.sub(r'^[^a-zA-Z\u4e00-\u9fff]+', '', conv[0])
reply = re.sub(r'^[^a-zA-Z\u4e00-\u9fff]+', '', conv[1])
if len(reply) == 0:
return False
if len(history) == 0 and (reply== history[-1][1] or prompt == history[-1][0]):
return False
data = {
'prompt' : prompt,
'response': reply,
'history' : history
}
fp.write(
json.dumps(data, ensure_ascii=False) + '\n'
)
history.append([prompt, reply])
return True
def check_dialog_turns(text):
# if "<Round 10>" not in text:
# return False
pattern = r"<[AB]>(.+?)\n(?:<[AB]>(.+?)\n)*"
conversation = re.findall(pattern=pattern, string=text)
if len(conversation) < 10:
return False
# return post_process(conversation)
return True
def run(content):
while True:
if api_style == 'azure':
response = get_azure_response(
url if not use_16k else url16k,
apikey if not use_16k else apikey16k,
content = content,
temperature = 0.1,
_verbose = False,
frequency_penalty = 0.6,
use_16k = use_16k,
)
elif api_style == 'api2d':
response = get_api2d_response(
url,
apikey,
content = content,
_verbose = False,
)
elif api_style == 'openai':
response = get_openai_response(
url,
apikey,
content = content,
temperature = 0.1,
_verbose = False,
frequency_penalty = 0.6,
use_16k = use_16k,
)
if check_dialog_turns(response):
break
else:
print(response)
continue
return response
if __name__ == '__main__':
config = configparser.ConfigParser()
config.read('config.ini')
api_style = 'openai'
use_16k = False
if api_style == 'azure':
print("Use AZURE")
url = config.get('AZURE', 'url')
apikey = config.get('AZURE', 'apikey')
url16k = config.get('AZURE', 'url16k')
apikey16k = config.get('AZURE', 'apikey16k')
elif api_style == 'api2d':
print("Use API2D")
url = config.get('API2D', 'url')
apikey = config.get('API2D', 'apikey')
elif api_style == 'openai':
print("Use OPENAI")
url = config.get('OPENAI', 'url')
apikey = config.get('OPENAI', 'apikey')
seeds_path = 'data_seeds.json'
machine_path = 'machine_generate.json'
template_path = os.path.join('templates', 'dialog_prompt.json')
data_path = os.path.join('data', 'mechine_generate_dialog_init.json')
result_path = os.path.join('data', 'mechine_generate_dialog.json')
with open(template_path, encoding='utf-8', mode='r') as r:
template_data = json.load(fp=r)
print(template_data['prompt_en'])
template = template_data['prompt_en']
ai_persona = template_data['ai_persona_en']
if not os.path.exists(data_path):
init_data(
seeds_path = seeds_path,
machine_path = machine_path
)
datas = load_dataset(
'json',
data_files = data_path,
split = 'train'
)
print(datas)
# print(datas[0]['template'])
fp = open(result_path, mode='a', encoding='utf-8')
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [
executor.submit(
run,
data['template']
) for data in datas
]
with tqdm(total=len(futures)) as pbar:
for future, data in zip(concurrent.futures.as_completed(futures), datas):
try:
data["response"] = future.result()
fp.write(json.dumps(dict(data), ensure_ascii=False) + '\n')
pbar.update(1)
except Exception as e:
print(e)
pbar.update(1)