From 92f3c078b5928c9e72b9def13d2020af85f0d3ba Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Sat, 29 Apr 2023 02:04:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A9=E4=BF=9D=E5=AD=98=E7=9A=84html?= =?UTF-8?q?=E5=AF=B9=E8=AF=9D=E6=96=87=E4=BB=B6=E8=83=BD=E5=A4=9F=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E4=BB=A3=E7=A0=81=E9=AB=98=E4=BA=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/对话历史存档.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/crazy_functions/对话历史存档.py b/crazy_functions/对话历史存档.py index e78a0ca..bc75875 100644 --- a/crazy_functions/对话历史存档.py +++ b/crazy_functions/对话历史存档.py @@ -1,5 +1,6 @@ from toolbox import CatchException, update_ui from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive +import re def write_chat_to_file(chatbot, history=None, file_name=None): """ @@ -11,6 +12,8 @@ def write_chat_to_file(chatbot, history=None, file_name=None): file_name = 'chatGPT对话历史' + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + '.html' os.makedirs('./gpt_log/', exist_ok=True) with open(f'./gpt_log/{file_name}', 'w', encoding='utf8') as f: + from theme import advanced_css + f.write(f'对话历史') for i, contents in enumerate(chatbot): for j, content in enumerate(contents): try: # 这个bug没找到触发条件,暂时先这样顶一下 @@ -26,14 +29,31 @@ def write_chat_to_file(chatbot, history=None, file_name=None): for h in history: f.write("\n>>>" + h) f.write('') - res = '对话历史写入:' + os.path.abspath(f'./gpt_log/{file_name}') print(res) return res +def gen_file_preview(file_name): + try: + with open(file_name, 'r', encoding='utf8') as f: + file_content = f.read() + # pattern to match the text between and + pattern = re.compile(r'.*?', flags=re.DOTALL) + file_content = re.sub(pattern, '', file_content) + html, history = file_content.split('
\n\n raw chat context:\n') + history = history.strip('') + history = history.strip('') + history = history.split("\n>>>") + return list(filter(lambda x:x!="", history))[0][:100] + except: + return "" + def read_file_to_chat(chatbot, history, file_name): with open(file_name, 'r', encoding='utf8') as f: file_content = f.read() + # pattern to match the text between and + pattern = re.compile(r'.*?', flags=re.DOTALL) + file_content = re.sub(pattern, '', file_content) html, history = file_content.split('
\n\n raw chat context:\n') history = history.strip('') history = history.strip('') @@ -87,7 +107,7 @@ def 载入对话历史存档(txt, llm_kwargs, plugin_kwargs, chatbot, history, s if not success: if txt == "": txt = '空空如也的输入栏' import glob - local_history = "
".join(["`"+hide_cwd(f)+"`" for f in glob.glob(f'gpt_log/**/chatGPT对话历史*.html', recursive=True)]) + local_history = "
".join(["`"+hide_cwd(f)+f" ({gen_file_preview(f)})"+"`" for f in glob.glob(f'gpt_log/**/chatGPT对话历史*.html', recursive=True)]) chatbot.append([f"正在查找对话历史文件(html格式): {txt}", f"找不到任何html文件: {txt}。但本地存储了以下历史文件,您可以将任意一个文件路径粘贴到输入区,然后重试:
{local_history}"]) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 return