-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdialogue_test.py
322 lines (254 loc) · 11.4 KB
/
dialogue_test.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
from env import *
# import os
import re
import sys
import argparse
from os.path import join
from tools import *
import logging
from core.api import set_api_logger
from core.chat import ChatBot, Turn, set_chat_logger
import gradio as gr
from prompts.dialogue import *
args: argparse.Namespace = None
bot: ChatBot = None
# Global Hyper Parameters
no_long_term_memory = False
naive_memory = False
embed_summary = False
translation_map = {}
def summarize_embed_one_turn(bot: ChatBot, dialogue_text, dialogue_text_with_index):
global embed_summary
lang2template = {
LANG_EN: en_turn_summarization_prompt,
LANG_ZH: zh_turn_summarization_prompt
}
tmp = choose_language_template(lang2template, dialogue_text)
input_text = tmp.format(input=dialogue_text)
logger.info(f'turn summarization input_text: \n\n{input_text}')
# 如果原文很短,保留原文即可
summarization = dialogue_text_with_index
if get_token_count_davinci(input_text) > 300:
logger.info(f'current turn text token count > 300, summarize !\n\n')
summarization = bot.ask(input_text)
logger.info(f'Summarization is:\n\n{summarization}\n\n')
else:
logger.info(f'Raw content is short, keep raw content as summarization:\n\n{summarization}\n\n')
if embed_summary:
embedding = bot.vectorize(summarization)
else:
embedding = bot.vectorize(dialogue_text_with_index)
return summarization, embedding
def get_concat_input(user_str, pre_sre, hist_str=None):
lang2template = {
LANG_EN: en_no_history_agent_prompt,
LANG_ZH: zh_no_history_agent_prompt
}
templates_no_hist = choose_language_template(lang2template, user_str)
lang2template = {
LANG_EN: en_history_agent_prompt,
LANG_ZH: zh_history_agent_prompt
}
templates_hist = choose_language_template(lang2template, user_str)
if hist_str:
input_text = templates_hist.format(history_turn_text=hist_str, pre_turn_text=pre_sre, input=user_str)
else:
input_text = templates_no_hist.format(pre_turn_text=pre_sre, input=user_str)
return input_text
def check_key_file(key_file):
if not os.path.exists(key_file):
print(f'[{key_file}] not found! Please put your apikey in the txt file.')
sys.exit(-1)
def get_first_prompt(user_text, model_name):
if model_name in [ENGINE_TURBO]:
return user_text
else:
lang2template = {
LANG_EN: en_start_prompt,
LANG_ZH: zh_start_prompt
}
tmp = choose_language_template(lang2template, user_text)
concat_input = tmp.format(input=user_text)
return concat_input
def check_string_format(input_str):
input_str = input_str.strip()
if 'filename' not in input_str or 'dial_id' not in input_str:
return False
filename = dial_id = False
for item in input_str.split('; '):
if 'filename' in item:
if item.split(': ')[1]:
filename = True
elif 'dial_id' in item:
if item.split(': ')[1]:
dial_id = True
return filename and dial_id
def extract_values(input_str):
if 'filename' not in input_str or 'dial_id' not in input_str:
return False
filename, dial_id = None, None
for item in input_str.split('; '):
if 'filename' in item:
filename = item.split(': ')[1]
elif 'dial_id' in item:
dial_id = item.split(': ')[1]
return filename, dial_id
def replace_code(s: str) -> str:
start_index = s.find("```")
end_index = s.rfind("```")
if start_index != -1 and end_index != -1:
end_index = min(end_index+3, len(s)-1)
s = s[:start_index] + "Ommit Code Here ..." + s[end_index:]
return s
def load_history_dialogue(filename, dial_id):
data = load_json_file(filename)
for item in data:
if dial_id == item['id']:
return item['dialogue']
raise ValueError('Invalid dial_id: {dial_id}')
def initialize_bot_and_dial(dialogues, dial_id):
history = []
turn_idx = 0
history.append(('请输入待标注的对话ID', dial_id))
total = len(dialogues) // 2
for i in range(0, len(dialogues), 2):
turn_idx += 1
if i+1 < len(dialogues):
user_text = dialogues[i]
user_text_display = user_text
if translation_map and translation_map.get(user_text, None):
zh_text = translation_map.get(user_text)
zh_text = replace_code(zh_text)
user_text_display += f"\n\n{zh_text}"
user_text_display = user_text_display.replace('__', 'prefix_')
# user_text = dialogues[i].replace('\\n', '\n')
assistant_text = dialogues[i+1]
assistant_text_display = assistant_text
if translation_map and translation_map.get(assistant_text, None):
zh_text = translation_map.get(assistant_text)
zh_text = replace_code(zh_text)
assistant_text_display += f"\n\n{zh_text}"
assistant_text_display = assistant_text_display.replace('__', 'prefix_')
# assistant_text = dialogues[i+1].replace('\\n', '\n')
cur = (replace_newline(user_text_display), replace_newline(assistant_text_display))
# cur = (user_text_display, assistant_text_display)
history.append(cur)
cur_text_without_index = '用户:{}\n\n助手:{}'.format(user_text, assistant_text)
cur_text_with_index = '[第{}轮]\n\n用户:{}\n\n助手:{}'.format(turn_idx, user_text, assistant_text)
if detect_language(user_text+assistant_text) == LANG_EN:
cur_text_without_index = 'User: {}\n\nAssistant: {}'.format(user_text, assistant_text)
cur_text_with_index = '[Turn {}]\n\nUser: {}\n\nAssistant: {}'.format(turn_idx, user_text, assistant_text)
print(f"loading progress : {turn_idx} / {total}, {cur_text_with_index[:200]} ...\n")
summary, embedding = summarize_embed_one_turn(bot, cur_text_without_index, cur_text_with_index)
cur_turn = Turn(user_input=user_text, system_response=assistant_text, user_sys_text=cur_text_with_index, summ=summary, embedding=embedding)
bot.add_turn_history(turn = cur_turn)
return history
def my_chatbot(user_input, history):
global no_long_term_memory
global naive_memory
history = history or []
user_input = user_input.strip()
my_history = list(sum(history, ()))
COMMAND_RETURN = '命令已成功执行!'
if user_input in ['清空', 'reset']:
# history.append((user_input, COMMAND_RETURN))
history = []
bot.clear_history()
logger.info(f'[User Command]: {user_input} {COMMAND_RETURN}')
return history, history
elif user_input in ['导出', 'export']:
# history.append((user_input, COMMAND_RETURN))
bot.export_history()
logger.info(f'[User Command]: {user_input} {COMMAND_RETURN}')
return history, history
elif user_input in ['回退', '回滚', 'roll back']:
history.pop()
bot.roll_back()
logger.info(f'[User Command]: {user_input} {COMMAND_RETURN}')
return history, history
elif check_string_format(user_input):
filename, dial_id = extract_values(user_input)
dialogues = load_history_dialogue(filename, dial_id)
history = initialize_bot_and_dial(dialogues, dial_id)
return history, history
len_hist = len(bot.history)
cur_turn_index = len_hist + 1
if len_hist == 0:
concat_input = get_first_prompt(user_input, args.model_name)
else:
retrieve = None
if no_long_term_memory:
pass
elif cur_turn_index > 2:
if naive_memory:
retrieve = bot.get_related_turn(user_input, k=args.similar_top_k, naive=True)
else:
retrieve = bot.get_related_turn(user_input, k=args.similar_top_k)
else:
pass
logger.info(f"no_long_term_memory: {no_long_term_memory}")
logger.info(f"retrieve: \n{retrieve}\n")
concat_input = get_concat_input(user_input, bot.get_turn_for_previous(), hist_str=retrieve)
logger.info(f'\n--------------\n[第{cur_turn_index}轮] concat_input:\n\n{concat_input}\n--------------\n')
try:
rsp: str = bot.ask(concat_input)
except Exception as e:
logger.error(f'ERROR: \n\n{e}')
rsp = 'System error, please check log file for details.'
history.append((user_input, rsp))
return history, history
system_text = rsp.strip()
logger.info(f'\n--------------\n[第{cur_turn_index}轮] system_text:\n\n{system_text}\n--------------\n')
my_history.append(user_input)
output = system_text
output_display = replace_newline(output)
history.append((user_input, output_display))
return history, history
if __name__ == '__main__':
parser = argparse.ArgumentParser()
model_choices = [ENGINE_DAVINCI_003, ENGINE_TURBO]
parser.add_argument("--apikey_file", type=str, default="./config/apikey.txt")
parser.add_argument("--model_name", type=str, default=ENGINE_DAVINCI_003, choices=model_choices)
parser.add_argument("--logfile", type=str, default="./logs/load_dialogue_log.txt")
parser.add_argument("--translation_file", type=str, default=None)
parser.add_argument("--no_long_term_memory", action='store_true', help='do not use long-term memory, default False')
parser.add_argument("--naive_memory", action='store_true', help='naive concat topk memory and concate history')
parser.add_argument("--embed_summary", action='store_true', help='use summary embedding for each turn')
# embed_summary
parser.add_argument("--similar_top_k", type=int, default=6)
args = parser.parse_args()
check_key_file(args.apikey_file)
log_path = args.logfile
makedirs(log_path)
# 配置日志记录
logger = logging.getLogger('dialogue_logger')
logger.setLevel(logging.INFO)
formatter = logging.Formatter('【%(asctime)s - %(levelname)s】 - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
file_handler = logging.FileHandler(log_path, encoding='utf-8')
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
set_chat_logger(logger)
set_api_logger(logger)
logger.info('\n\n\n')
logger.info('#################################')
logger.info('#################################')
logger.info('#################################')
logger.info('\n\n\n')
logger.info(f"args: \n\n{args}\n")
stamp = datetime2str()
# print(stamp)
if args.translation_file:
translation_map = load_json_file(args.translation_file)
bot = ChatBot(model_name=args.model_name)
# whether use scm for history memory
no_long_term_memory = True if args.no_long_term_memory else False
naive_memory = True if args.naive_memory else False
embed_summary = True if args.embed_summary else False
with gr.Blocks() as demo:
gr.Markdown(f"<h1><center>Long Dialogue Chatbot ({args.model_name}) for test</center></h1>")
chatbot = gr.Chatbot()
state = gr.State()
txt = gr.Textbox(show_label=False, placeholder="Ask me a question and press enter.").style(container=False)
txt.submit(my_chatbot, inputs=[txt, state], outputs=[chatbot, state])
demo.launch(share = False)