From 07807e4653f7fa115fdb84b8d672a80d66a56d2f Mon Sep 17 00:00:00 2001 From: binary-husky <505030475@qq.com> Date: Sun, 23 Apr 2023 11:17:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=92=E4=BB=B6=E6=94=AF=E6=8C=81=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E5=AF=B9=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functional.py | 5 ++++ crazy_functions/对话历史存档.py | 42 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 crazy_functions/对话历史存档.py diff --git a/crazy_functional.py b/crazy_functional.py index 45fd561..df695e2 100644 --- a/crazy_functional.py +++ b/crazy_functional.py @@ -20,12 +20,17 @@ def get_crazy_functions(): from crazy_functions.解析项目源代码 import 解析一个CSharp项目 from crazy_functions.总结word文档 import 总结word文档 from crazy_functions.解析JupyterNotebook import 解析ipynb文件 + from crazy_functions.对话历史存档 import 对话历史存档 function_plugins = { "解析整个Python项目": { "Color": "stop", # 按钮颜色 "Function": HotReload(解析一个Python项目) }, + "保存当前的对话": { + "AsButton":False, + "Function": HotReload(对话历史存档) + }, "[测试功能] 解析Jupyter Notebook文件": { "Color": "stop", "AsButton":False, diff --git a/crazy_functions/对话历史存档.py b/crazy_functions/对话历史存档.py new file mode 100644 index 0000000..1b232de --- /dev/null +++ b/crazy_functions/对话历史存档.py @@ -0,0 +1,42 @@ +from toolbox import CatchException, update_ui +from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive + +def write_chat_to_file(chatbot, file_name=None): + """ + 将对话记录history以Markdown格式写入文件中。如果没有指定文件名,则使用当前时间生成文件名。 + """ + import os + import time + if file_name is 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: + for i, contents in enumerate(chatbot): + for content in contents: + try: # 这个bug没找到触发条件,暂时先这样顶一下 + if type(content) != str: content = str(content) + except: + continue + f.write(content) + f.write('\n\n') + f.write('
\n\n') + + res = '对话历史写入:' + os.path.abspath(f'./gpt_log/{file_name}') + print(res) + return res + +@CatchException +def 对话历史存档(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port): + """ + txt 输入栏用户输入的文本,例如需要翻译的一段话,再例如一个包含了待处理文件的路径 + llm_kwargs gpt模型参数,如温度和top_p等,一般原样传递下去就行 + plugin_kwargs 插件模型的参数,暂时没有用武之地 + chatbot 聊天显示框的句柄,用于显示给用户 + history 聊天历史,前情提要 + system_prompt 给gpt的静默提醒 + web_port 当前软件运行的端口号 + """ + + chatbot.append(("保存当前对话", f"[Local Message] {write_chat_to_file(chatbot)}")) + yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 由于请求gpt需要一段时间,我们先及时地做一次界面更新 +