import json import os import builtins import shutil import uuid from functools import wraps import streamlit as st from set_context import set_context set_context_all = {"不设置": ""} set_context_all.update(set_context) # 用户名 user_name = 'User' gpt_name = 'ChatGPT' # 头像(svg格式) 来自 https://www.dicebear.com/playground?style=identicon user_svg = """ """ gpt_svg = """ """ # 内容背景 user_background_color = '#ffffff' gpt_background_color = '#f0f2f6' # 模型初始设置 initial_content_history = [{"role": 'system', "content": '当你的回复中涉及代码块时,请在markdown语法中标明语言类型。如果不涉及,请忽略这句话。'}] initial_content_all = {"history": initial_content_history, "paras": { "temperature": 1.0, "top_p": 1.0, "presence_penalty": 0.0, "frequency_penalty": 0.0, }, "contexts": { 'context_select': '不设置', 'context_input': '', 'context_level': 4 } } # 聊天记录处理 def clear_folder(path): if not os.path.exists(path): return for file_name in os.listdir(path): file_path = os.path.join(path, file_name) try: shutil.rmtree(file_path) except Exception: pass def set_chats_path(): save_path = 'chat_history' if 'apikey' not in st.secrets: clear_folder('tem_files') save_path = 'tem_files/tem_chat' + str(uuid.uuid4()) return save_path # 重新open函数,路径不存在时自动创建 def create_path(func): @wraps(func) def wrapper(path, *args, **kwargs): if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) return func(path, *args, **kwargs) return wrapper open = create_path(builtins.open) def get_history_chats(path): try: os.makedirs(path) except FileExistsError: pass files = [f for f in os.listdir(f'./{path}') if f.endswith('.json')] files_with_time = [(f, os.stat(f'./{path}/' + f).st_ctime) for f in files] sorted_files = sorted(files_with_time, key=lambda x: x[1], reverse=True) chat_names = [os.path.splitext(f[0])[0] for f in sorted_files] if len(chat_names) == 0: chat_names.append('New Chat_' + str(uuid.uuid4())) return chat_names def save_data(path: str, file_name: str, history: list, paras: dict, contexts: dict, **kwargs): with open(f"./{path}/{file_name}.json", 'w', encoding='utf-8') as f: json.dump({"history": history, "paras": paras, "contexts": contexts, **kwargs}, f) def remove_data(path: str, file_name: str): try: os.remove(f"./{path}/{file_name}.json") except FileNotFoundError: pass def load_data(path: str, file_name: str) -> dict: try: with open(f"./{path}/{file_name}.json", 'r', encoding='utf-8') as f: data = json.load(f) return data except FileNotFoundError: with open(f"./{path}/{file_name}.json", 'w', encoding='utf-8') as f: f.write(json.dumps(initial_content_all)) return initial_content_all def show_each_message(message, role, area=None): if area is None: area = [st.markdown] * 2 if role == 'user': icon = user_svg name = user_name background_color = user_background_color else: icon = gpt_svg name = gpt_name background_color = gpt_background_color area[0](f"\n