diff --git a/pkg/openai/keymgr.py b/pkg/openai/keymgr.py index be6d728f..af560b29 100644 --- a/pkg/openai/keymgr.py +++ b/pkg/openai/keymgr.py @@ -32,16 +32,9 @@ def get_using_key_md5(self): return hashlib.md5(self.using_key.encode('utf-8')).hexdigest() def __init__(self, api_key): - - if type(api_key) is dict: - self.api_key = api_key - elif type(api_key) is str: - self.api_key = { - "default": api_key - } - elif type(api_key) is list: - for i in range(len(api_key)): - self.api_key[str(i)] = api_key[i] + + assert type(api_key) == dict + self.api_key = api_key # 从usage中删除未加载的api-key的记录 # 不删了,也许会运行时添加曾经有记录的api-key diff --git a/pkg/openai/session.py b/pkg/openai/session.py index 1f9c76de..19a69ea2 100644 --- a/pkg/openai/session.py +++ b/pkg/openai/session.py @@ -25,34 +25,6 @@ class SessionOfflineStatus: EXPLICITLY_CLOSED = 'explicitly_closed' -# 重置session.prompt -def reset_session_prompt(session_name, prompt): - # 备份原始数据 - bak_path = 'logs/{}-{}.bak'.format( - session_name, - time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) - ) - f = open(bak_path, 'w+') - f.write(prompt) - f.close() - # 生成新数据 - config = context.get_config_manager().data - prompt = [ - { - 'role': 'system', - 'content': config['default_prompt']['default'] if type(config['default_prompt']) == dict else config['default_prompt'] - } - ] - # 警告 - logging.warning( - """ -用户[{}]的数据已被重置,有可能是因为数据版本过旧或存储错误 -原始数据将备份在: -{}""".format(session_name, bak_path) - ) # 为保证多行文本格式正确故无缩进 - return prompt - - # 从数据加载session def load_sessions(): """从数据库加载sessions""" @@ -70,12 +42,10 @@ def load_sessions(): temp_session.name = session_name temp_session.create_timestamp = session_data[session_name]['create_timestamp'] temp_session.last_interact_timestamp = session_data[session_name]['last_interact_timestamp'] - try: - temp_session.prompt = json.loads(session_data[session_name]['prompt']) - temp_session.token_counts = json.loads(session_data[session_name]['token_counts']) - except Exception: - temp_session.prompt = reset_session_prompt(session_name, session_data[session_name]['prompt']) - temp_session.persistence() + + temp_session.prompt = json.loads(session_data[session_name]['prompt']) + temp_session.token_counts = json.loads(session_data[session_name]['token_counts']) + temp_session.default_prompt = json.loads(session_data[session_name]['default_prompt']) if \ session_data[session_name]['default_prompt'] else [] @@ -493,12 +463,10 @@ def last_session(self): self.create_timestamp = last_one['create_timestamp'] self.last_interact_timestamp = last_one['last_interact_timestamp'] - try: - self.prompt = json.loads(last_one['prompt']) - self.token_counts = json.loads(last_one['token_counts']) - except json.decoder.JSONDecodeError: - self.prompt = reset_session_prompt(self.name, last_one['prompt']) - self.persistence() + + self.prompt = json.loads(last_one['prompt']) + self.token_counts = json.loads(last_one['token_counts']) + self.default_prompt = json.loads(last_one['default_prompt']) if last_one['default_prompt'] else [] self.just_switched_to_exist_session = True @@ -514,12 +482,10 @@ def next_session(self): self.create_timestamp = next_one['create_timestamp'] self.last_interact_timestamp = next_one['last_interact_timestamp'] - try: - self.prompt = json.loads(next_one['prompt']) - self.token_counts = json.loads(next_one['token_counts']) - except json.decoder.JSONDecodeError: - self.prompt = reset_session_prompt(self.name, next_one['prompt']) - self.persistence() + + self.prompt = json.loads(next_one['prompt']) + self.token_counts = json.loads(next_one['token_counts']) + self.default_prompt = json.loads(next_one['default_prompt']) if next_one['default_prompt'] else [] self.just_switched_to_exist_session = True diff --git a/pkg/plugin/host.py b/pkg/plugin/host.py index 50af1ec0..bf41003a 100644 --- a/pkg/plugin/host.py +++ b/pkg/plugin/host.py @@ -576,7 +576,3 @@ def emit(self, event_name: str, **kwargs) -> EventContext: ) return event_context - - -if __name__ == "__main__": - pass diff --git a/pkg/qqbot/cmds/plugin/plugin.py b/pkg/qqbot/cmds/plugin/plugin.py index 1481985f..5e699bba 100644 --- a/pkg/qqbot/cmds/plugin/plugin.py +++ b/pkg/qqbot/cmds/plugin/plugin.py @@ -38,9 +38,6 @@ def process(cls, ctx: aamgr.Context) -> tuple[bool, list]: reply = [reply_str] return True, reply - elif ctx.params[0].startswith("http"): - reply = ["[bot]err: 此命令已弃用,请使用 !plugin get <插件仓库地址> 进行安装"] - return True, reply else: return False, [] diff --git a/pkg/qqbot/cmds/session/default.py b/pkg/qqbot/cmds/session/default.py index 6103d210..1a1ff756 100644 --- a/pkg/qqbot/cmds/session/default.py +++ b/pkg/qqbot/cmds/session/default.py @@ -39,8 +39,6 @@ def process(cls, ctx: aamgr.Context) -> tuple[bool, list]: reply_str += "\n当前默认情景预设:{}\n".format(dprompt.mode_inst().get_using_name()) reply_str += "请使用 !default set <情景预设名称> 来设置默认情景预设" reply = [reply_str] - elif params[0] != "set": - reply = ["[bot]err: 已弃用,请使用!default set <情景预设名称> 来设置默认情景预设"] else: return False, [] @@ -69,7 +67,5 @@ def process(cls, ctx: aamgr.Context) -> tuple[bool, list]: reply = ["[bot]已设置默认情景预设为:{}".format(full_name)] except Exception as e: reply = ["[bot]err: {}".format(e)] - else: - reply = ["[bot]err: 仅管理员可设置默认情景预设"] return True, reply diff --git a/pkg/qqbot/cmds/session/list.py b/pkg/qqbot/cmds/session/list.py index 56fa4569..fb00976d 100644 --- a/pkg/qqbot/cmds/session/list.py +++ b/pkg/qqbot/cmds/session/list.py @@ -39,12 +39,9 @@ def process(cls, ctx: aamgr.Context) -> tuple[bool, list]: # 时间(使用create_timestamp转换) 序号 部分内容 datetime_obj = datetime.datetime.fromtimestamp(results[i]['create_timestamp']) msg = "" - try: - msg = json.loads(results[i]['prompt']) - except json.decoder.JSONDecodeError: - msg = pkg.openai.session.reset_session_prompt(session_name, results[i]['prompt']) - # 持久化 - pkg.openai.session.get_session(session_name).persistence() + + msg = json.loads(results[i]['prompt']) + if len(msg) >= 2: reply_str += "#{} 创建:{} {}\n".format(i + page * 10, datetime_obj.strftime("%Y-%m-%d %H:%M:%S"), diff --git a/pkg/qqbot/ratelimit.py b/pkg/qqbot/ratelimit.py index e8dd2913..96d289ff 100644 --- a/pkg/qqbot/ratelimit.py +++ b/pkg/qqbot/ratelimit.py @@ -17,14 +17,10 @@ def get_limitation(session_name: str) -> int: """获取会话的限制次数""" config = context.get_config_manager().data - if type(config['rate_limitation']) == dict: - # 如果被指定了 - if session_name in config['rate_limitation']: - return config['rate_limitation'][session_name] - else: - return config['rate_limitation']["default"] - elif type(config['rate_limitation']) == int: - return config['rate_limitation'] + if session_name in config['rate_limitation']: + return config['rate_limitation'][session_name] + else: + return config['rate_limitation']["default"] def add_usage(session_name: str): diff --git a/pkg/utils/log.py b/pkg/utils/log.py index e45f97e6..6be28b5b 100644 --- a/pkg/utils/log.py +++ b/pkg/utils/log.py @@ -28,10 +28,6 @@ def init_runtime_log_file(): if not os.path.exists("logs"): os.mkdir("logs") - # 检查本目录是否有qchatgpt.log,若有,移动到logs目录 - if os.path.exists("qchatgpt.log"): - shutil.move("qchatgpt.log", "logs/qchatgpt.legacy.log") - log_file_name = "logs/qchatgpt-%s.log" % time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())