From ed691e6abacfc6a0594b8fe000b1343d1c77cb19 Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Mon, 9 Jan 2023 18:55:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E5=B7=B2=E7=9F=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/openai/core.py | 15 ++++++++++---- cores/qqbot/core.py | 47 +++++++++++++++++++++++++------------------- main.py | 16 +-------------- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/cores/openai/core.py b/cores/openai/core.py index 8026cb3..39753cd 100644 --- a/cores/openai/core.py +++ b/cores/openai/core.py @@ -3,13 +3,18 @@ from util.errors.errors import PromptExceededError import json import time +import os +import sys inst = None +# 适配pyinstaller +abs_path = os.path.dirname(os.path.realpath(sys.argv[0])) + '/' +key_record_path = abs_path+'chatgpt_key_record' class ChatGPT: def __init__(self): self.key_list = [] - with open("./configs/config.yaml", 'r', encoding='utf-8') as ymlfile: + with open(abs_path+"configs/config.yaml", 'r', encoding='utf-8') as ymlfile: cfg = yaml.safe_load(ymlfile) if cfg['openai']['key'] != '': print("读取ChatGPT Key成功") @@ -106,7 +111,7 @@ def getConfigs(self): return self.openai_configs def save_key_record(self): - with open("chatgpt_key_record", 'w', encoding='utf-8') as f: + with open(key_record_path, 'w', encoding='utf-8') as f: json.dump(self.key_stat, f) def get_key_stat(self): @@ -138,7 +143,10 @@ def check_key(self, key): # 将key_list的key转储到key_record中,并记录相关数据 def init_key_record(self): - with open("chatgpt_key_record", 'r', encoding='utf-8') as keyfile: + if not os.path.exists(key_record_path): + with open(key_record_path, 'w', encoding='utf-8') as f: + json.dump({}, f) + with open(key_record_path, 'r', encoding='utf-8') as keyfile: try: self.key_stat = json.load(keyfile) except Exception as e: @@ -149,7 +157,6 @@ def init_key_record(self): if key not in self.key_stat: self.key_stat[key] = {'exceed': False, 'used': 0} if openai.api_key is None: - print("切换") openai.api_key = key else: if self.key_stat[key]['exceed']: diff --git a/cores/qqbot/core.py b/cores/qqbot/core.py index 9e70566..1d3f3a6 100644 --- a/cores/qqbot/core.py +++ b/cores/qqbot/core.py @@ -10,12 +10,9 @@ import time from cores.database.conn import dbConn import requests -import random import util.unfit_words as uw - -import hashlib -import uuid - +import os +import sys history_dump_interval = 10 client = '' # ChatGPT的实例 @@ -36,35 +33,37 @@ 'max_tokens': '', } # 统计信息 -count = { -} +count = {} # 统计信息 stat_file = '' # 是否是独立会话(在配置改) uniqueSession = False # 日志记录 logf = open('log.log', 'a+', encoding='utf-8') -# 是否上传日志,仅上传频道数量等数量的统计信息,不包含key等敏感信息 +# 是否上传日志,仅上传频道数量等数量的统计信息 is_upload_log = True - ####################### # 公告(可自定义): announcement = "⚠公约:禁止涉政、暴力等敏感话题,关于此话题得到的回复不受控。\n目前已知的问题:部分代码(例如Java、SQL,Python代码不会)会被频道拦截。\n🤖可自己搭建一个机器人~详见QQChannelChatGPT项目。" ####################### +# 适配pyinstaller +abs_path = os.path.dirname(os.path.realpath(sys.argv[0])) + '/' +print(abs_path) + def new_sub_thread(func, args=()): thread = threading.Thread(target=func, args=args, daemon=True) - thread.start() + thread.start() class botClient(botpy.Client): # 收到At消息 async def on_at_message_create(self, message: Message): toggle_count(at=True, message=message) - # executor.submit(oper_msg, message, True) + print(message) new_sub_thread(oper_msg, (message, True)) # await oper_msg(message=message, at=True) @@ -73,6 +72,7 @@ async def on_direct_message_create(self, message: DirectMessage): toggle_count(at=False, message=message) # executor.submit(oper_msg, message, True) # await oper_msg(message=message, at=False) + print(message) new_sub_thread(oper_msg, (message, False)) # 写入统计信息 @@ -88,7 +88,7 @@ def toggle_count(at: bool, message): count[str(message.guild_id)]['count'] += 1 if not at: count[str(message.guild_id)]['direct_count'] += 1 - stat_file = open("./configs/stat", 'w', encoding='utf-8') + stat_file = open(abs_path+"configs/stat", 'w', encoding='utf-8') stat_file.write(json.dumps(count)) stat_file.flush() stat_file.close() @@ -143,7 +143,7 @@ def upload(): print("new user") res = requests.post(f'https://uqfxtww1.lc-cn-n1-shared.com/1.1/classes/bot_record', headers = headers, data = d) object_id = json.loads(res.text)['objectId'] - object_id_file = open("./configs/object_id", 'w+', encoding='utf-8') + object_id_file = open(abs_path+"configs/object_id", 'w+', encoding='utf-8') object_id_file.write(str(object_id)) object_id_file.flush() object_id_file.close() @@ -175,7 +175,10 @@ def initBot(chatgpt_inst): # 读统计信息 global stat_file - stat_file = open("./configs/stat", 'r', encoding='utf-8') + if not os.path.exists(abs_path+"configs/stat"): + with open(abs_path+"configs/stat", 'w', encoding='utf-8') as f: + json.dump({}, f) + stat_file = open(abs_path+"configs/stat", 'r', encoding='utf-8') global count res = stat_file.read() if res == '': @@ -191,14 +194,17 @@ def initBot(chatgpt_inst): if is_upload_log: # 读取object_id global object_id - object_id_file = open("./configs/object_id", 'r', encoding='utf-8') + if not os.path.exists(abs_path+"configs/object_id"): + with open(abs_path+"configs/object_id", 'w', encoding='utf-8') as f: + f.write("") + object_id_file = open(abs_path+"configs/object_id", 'r', encoding='utf-8') object_id = object_id_file.read() object_id_file.close() # 创建上传定时器线程 threading.Thread(target=upload, daemon=True).start() global uniqueSession, history_dump_interval - with open("./configs/config.yaml", 'r', encoding='utf-8') as ymlfile: + with open(abs_path+"configs/config.yaml", 'r', encoding='utf-8') as ymlfile: cfg = yaml.safe_load(ymlfile) try: @@ -224,6 +230,7 @@ def initBot(chatgpt_inst): client.run(appid=cfg['qqbot']['appid'], token=cfg['qqbot']['token']) else: raise BaseException("请在config中完善你的appid和token") + print("QQBot初始化完成\n\n如果有任何问题,请在https://github.com/Soulter/QQChannelChatGPT上提交issue说明问题!或者添加QQ:905617992\n\n") ''' 得到OpenAI的回复 @@ -440,14 +447,14 @@ def oper_msg(message, at=False, loop=None): fjson = {} try: - f = open("./configs/session", "r", encoding="utf-8") + f = open(abs_path+"configs/session", "r", encoding="utf-8") fjson = json.loads(f.read()) f.close() except: pass finally: fjson[session_id] = 'true' - f = open("./configs/session", "w", encoding="utf-8") + f = open(abs_path+"configs/session", "w", encoding="utf-8") f.write(json.dumps(fjson)) f.flush() f.close() @@ -547,7 +554,7 @@ def oper_msg(message, at=False, loop=None): def get_stat(): try: - f = open("./configs/stat", "r", encoding="utf-8") + f = open(abs_path+"configs/stat", "r", encoding="utf-8") fjson = json.loads(f.read()) f.close() guild_count = 0 @@ -561,7 +568,7 @@ def get_stat(): session_count = 0 - f = open("./configs/session", "r", encoding="utf-8") + f = open(abs_path+"configs/session", "r", encoding="utf-8") fjson = json.loads(f.read()) f.close() for k,v in fjson.items(): diff --git a/main.py b/main.py index 39d124c..640b3f8 100644 --- a/main.py +++ b/main.py @@ -1,24 +1,10 @@ import cores.qqbot.core as qqBot from cores.openai.core import ChatGPT -import asyncio -import yaml -import threading -# from cores.database.conn import dbConn def main(): - # 读取参数 - # with open('configs/config.yaml', 'r', encoding='utf-8') as f: - # cfg = yaml.safe_load(f) - # chatGPT_configs = cfg['openai']['chatGPTConfigs'] - # print(chatGPT_configs) #实例化ChatGPT chatgpt = ChatGPT() - # db = dbConn() # #执行qqBot - # qqBot.initBot(chatgpt, db) qqBot.initBot(chatgpt) if __name__ == "__main__": - # qqbot_thread = threading.Thread(target=main) - # qqbot_thread.start() - main() - + main() \ No newline at end of file