From f4ae503abf3e0e2a9b0018a818c7aed73445f79b Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Thu, 5 Oct 2023 10:48:35 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E5=92=8C=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/qqbot/core.py | 1 + main.py | 33 ++++++++++++++-------- model/platform/qq.py | 2 +- model/provider/provider_openai_official.py | 11 +++++++- util/gplugin.py | 6 ++-- 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/cores/qqbot/core.py b/cores/qqbot/core.py index e9fe70fdc..7673008a1 100644 --- a/cores/qqbot/core.py +++ b/cores/qqbot/core.py @@ -253,6 +253,7 @@ def initBot(cfg, prov): llm_command_instance[REV_EDGEGPT] = CommandRevEdgeGPT(llm_instance[REV_EDGEGPT], _global_object) chosen_provider = REV_EDGEGPT except BaseException as e: + print(traceback.format_exc()) gu.log("加载Bing模型时发生错误, 请检查1. cookies文件是否正确放置 2. 是否设置了代理(梯子)。", gu.LEVEL_ERROR, max_len=60) if OPENAI_OFFICIAL in prov: gu.log("- OpenAI官方 -", gu.LEVEL_INFO) diff --git a/main.py b/main.py index 8252213a8..7dafa0128 100644 --- a/main.py +++ b/main.py @@ -1,19 +1,30 @@ import os, sys from pip._internal import main as pipmain +import traceback abs_path = os.path.dirname(os.path.realpath(sys.argv[0])) + '/' def main(): + + # config.yaml 配置文件加载和环境确认 try: import cores.qqbot.core as qqBot import yaml + from yaml.scanner import ScannerError + import util.general_utils as gu ymlfile = open(abs_path+"configs/config.yaml", 'r', encoding='utf-8') cfg = yaml.safe_load(ymlfile) - except BaseException as e: - print(e) + except ImportError as import_error: + print(import_error) input("第三方库未完全安装完毕,请退出程序重试。") - exit() - import util.general_utils as gu + except FileNotFoundError as file_not_found: + print(file_not_found) + input("配置文件不存在,请检查是否已经下载配置文件。") + except ScannerError as e: + print(traceback.format_exc()) + input("config.yaml 配置文件格式错误,请遵守 yaml 格式。") + + # 设置代理 if 'http_proxy' in cfg: os.environ['HTTP_PROXY'] = cfg['http_proxy'] if 'https_proxy' in cfg: @@ -21,21 +32,20 @@ def main(): os.environ['NO_PROXY'] = 'cn.bing.com,https://api.sgroup.qq.com' - # 检查temp文件夹 - if not os.path.exists(abs_path+"temp"): + # 检查并创建 temp 文件夹 + if not os.path.exists(abs_path + "temp"): os.mkdir(abs_path+"temp") + # 选择默认模型 provider = privider_chooser(cfg) if len(provider) == 0: - gu.log("未开启任何语言模型, 请在configs/config.yaml下选择开启相应语言模型。", gu.LEVEL_CRITICAL) - input("按任意键退出...") - exit() + gu.log("注意:您目前未开启任何语言模型。", gu.LEVEL_WARNING) print('[System] 开启的语言模型: ' + str(provider)) - # 执行Bot + + # 启动主程序(cores/qqbot/core.py) qqBot.initBot(cfg, provider) # 语言模型提供商选择器 -# 目前有:OpenAI官方API、逆向库 def privider_chooser(cfg): l = [] if 'rev_ChatGPT' in cfg and cfg['rev_ChatGPT']['enable']: @@ -48,6 +58,7 @@ def privider_chooser(cfg): l.append('openai_official') return l +# 检查并安装环境 def check_env(): if not (sys.version_info.major == 3 and sys.version_info.minor >= 8): print("请使用Python3.8运行本项目") diff --git a/model/platform/qq.py b/model/platform/qq.py index 0b37583d3..217e81a2b 100644 --- a/model/platform/qq.py +++ b/model/platform/qq.py @@ -100,7 +100,7 @@ class QQ: res.remove(i) node = Node(res) # node.content = res - node.uin = source.self_id + node.uin = 123456 node.name = f"To {source.sender.nickname}:" node.time = int(time.time()) # print(node) diff --git a/model/provider/provider_openai_official.py b/model/provider/provider_openai_official.py index 625e9529c..0f1f387ad 100644 --- a/model/provider/provider_openai_official.py +++ b/model/provider/provider_openai_official.py @@ -16,7 +16,12 @@ class ProviderOpenAIOfficial(Provider): self.key_list = [] if 'api_base' in cfg and cfg['api_base'] != 'none' and cfg['api_base'] != '': openai.api_base = cfg['api_base'] - print(f"设置apibase为: {openai.api_base}") + print(f"设置 api_base 为: {openai.api_base}") + # 如果 cfg['key']中有长度为1的字符串,那么是格式错误,直接报错 + for key in cfg['key']: + if len(key) == 1: + input("检查到了长度为 1 的Key。配置文件中的 openai.key 处的格式错误 (符号 - 的后面要加空格),请退出程序并检查配置文件,按回车跳过。") + raise BaseException("配置文件格式错误") if cfg['key'] != '' and cfg['key'] != None: self.key_list = cfg['key'] else: @@ -398,9 +403,13 @@ class ProviderOpenAIOfficial(Provider): #将key_list的key转储到key_record中,并记录相关数据 def init_key_record(self): + + # 不存在,创建 if not os.path.exists(key_record_path): with open(key_record_path, 'w', encoding='utf-8') as f: json.dump({}, f) + + # 打开 chatgpt_key_record with open(key_record_path, 'r', encoding='utf-8') as keyfile: try: self.key_stat = json.load(keyfile) diff --git a/util/gplugin.py b/util/gplugin.py index 22a94da36..33a34e3c6 100644 --- a/util/gplugin.py +++ b/util/gplugin.py @@ -37,7 +37,7 @@ def google_web_search(keyword) -> str: ret = "" index = 1 try: - ls = search(keyword, advanced=True, num_results=5) + ls = search(keyword, advanced=True, num_results=4) for i in ls: desc = i.description try: @@ -177,8 +177,8 @@ def fetch_website_content(url): res = res[300:1100] else: res = res[100:800] - with open(f"temp_{time.time()}.html", "w", encoding="utf-8") as f: - f.write(res) + # with open(f"temp_{time.time()}.html", "w", encoding="utf-8") as f: + # f.write(res) gu.log(f"fetch_website_content: end", tag="fetch_website_content", level=gu.LEVEL_DEBUG) return res