perf: 优化报错提示和代码结构

This commit is contained in:
Soulter
2023-10-05 10:48:35 +08:00
parent f2c4ef126e
commit f4ae503abf
5 changed files with 37 additions and 16 deletions
+1
View File
@@ -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)
+22 -11
View File
@@ -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运行本项目")
+1 -1
View File
@@ -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)
+10 -1
View File
@@ -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)
+3 -3
View File
@@ -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