2022-12-07 16:48:32 +08:00
|
|
|
|
import os
|
|
|
|
|
import shutil
|
2022-12-08 00:41:35 +08:00
|
|
|
|
import sys
|
2022-12-08 12:06:04 +08:00
|
|
|
|
import threading
|
|
|
|
|
import time
|
2022-12-08 00:41:35 +08:00
|
|
|
|
|
2022-12-09 00:01:49 +08:00
|
|
|
|
import logging
|
|
|
|
|
import colorlog
|
|
|
|
|
|
2022-12-11 19:57:50 +08:00
|
|
|
|
import sys
|
|
|
|
|
|
2022-12-15 17:52:41 +08:00
|
|
|
|
sys.path.append(".")
|
2022-12-11 19:57:50 +08:00
|
|
|
|
|
2022-12-09 00:01:49 +08:00
|
|
|
|
log_colors_config = {
|
|
|
|
|
'DEBUG': 'green', # cyan white
|
|
|
|
|
'INFO': 'white',
|
|
|
|
|
'WARNING': 'yellow',
|
|
|
|
|
'ERROR': 'red',
|
|
|
|
|
'CRITICAL': 'bold_red',
|
|
|
|
|
}
|
2022-12-08 00:41:35 +08:00
|
|
|
|
|
2022-12-09 15:44:49 +08:00
|
|
|
|
|
2022-12-08 00:41:35 +08:00
|
|
|
|
def init_db():
|
2022-12-09 15:24:47 +08:00
|
|
|
|
import pkg.database.manager
|
2022-12-11 13:58:47 +08:00
|
|
|
|
database = pkg.database.manager.DatabaseManager()
|
2022-12-08 00:41:35 +08:00
|
|
|
|
|
|
|
|
|
database.initialize_database()
|
2022-12-07 16:48:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
# 导入config.py
|
|
|
|
|
assert os.path.exists('config.py')
|
|
|
|
|
import config
|
|
|
|
|
|
2022-12-09 00:01:49 +08:00
|
|
|
|
logging.basicConfig(level=config.logging_level, # 设置日志输出格式
|
|
|
|
|
filename='qchatgpt.log', # log日志输出的文件位置和文件名
|
|
|
|
|
format="[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : %(message)s",
|
|
|
|
|
# 日志输出的格式
|
|
|
|
|
# -8表示占位符,让输出左对齐,输出长度都为8位
|
|
|
|
|
datefmt="%Y-%m-%d %H:%M:%S" # 时间输出的格式
|
|
|
|
|
)
|
|
|
|
|
sh = logging.StreamHandler()
|
|
|
|
|
sh.setLevel(config.logging_level)
|
|
|
|
|
sh.setFormatter(colorlog.ColoredFormatter(
|
|
|
|
|
fmt="%(log_color)s[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : "
|
|
|
|
|
"%(message)s",
|
|
|
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
|
|
|
log_colors=log_colors_config
|
|
|
|
|
))
|
|
|
|
|
logging.getLogger().addHandler(sh)
|
|
|
|
|
|
2022-12-09 15:44:49 +08:00
|
|
|
|
import pkg.openai.manager
|
|
|
|
|
import pkg.database.manager
|
|
|
|
|
import pkg.openai.session
|
|
|
|
|
import pkg.qqbot.manager
|
2023-01-01 23:18:32 +08:00
|
|
|
|
import pkg.utils.context
|
2022-12-09 15:44:49 +08:00
|
|
|
|
|
2022-12-08 00:41:35 +08:00
|
|
|
|
# 主启动流程
|
2022-12-11 13:58:47 +08:00
|
|
|
|
database = pkg.database.manager.DatabaseManager()
|
2022-12-08 00:41:35 +08:00
|
|
|
|
|
2022-12-11 20:02:13 +08:00
|
|
|
|
database.initialize_database()
|
|
|
|
|
|
2022-12-27 22:52:53 +08:00
|
|
|
|
openai_interact = pkg.openai.manager.OpenAIInteract(config.openai_config['api_key'])
|
2022-12-15 17:52:41 +08:00
|
|
|
|
|
2022-12-08 00:41:35 +08:00
|
|
|
|
# 加载所有未超时的session
|
|
|
|
|
pkg.openai.session.load_sessions()
|
2022-12-07 16:48:32 +08:00
|
|
|
|
|
2022-12-08 12:06:04 +08:00
|
|
|
|
# 初始化qq机器人
|
|
|
|
|
qqbot = pkg.qqbot.manager.QQBotManager(mirai_http_api_config=config.mirai_http_api_config,
|
|
|
|
|
timeout=config.process_message_timeout, retry=config.retry_times)
|
|
|
|
|
|
|
|
|
|
qq_bot_thread = threading.Thread(target=qqbot.bot.run, args=(), daemon=True)
|
|
|
|
|
qq_bot_thread.start()
|
|
|
|
|
|
2022-12-09 00:01:49 +08:00
|
|
|
|
logging.info('程序启动完成')
|
|
|
|
|
|
2022-12-08 12:06:04 +08:00
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
time.sleep(86400)
|
|
|
|
|
except KeyboardInterrupt:
|
2022-12-08 13:37:33 +08:00
|
|
|
|
try:
|
2023-01-01 23:18:32 +08:00
|
|
|
|
pkg.utils.context.get_openai_manager().key_mgr.dump_fee()
|
2022-12-08 13:37:33 +08:00
|
|
|
|
for session in pkg.openai.session.sessions:
|
2022-12-09 00:01:49 +08:00
|
|
|
|
logging.info('持久化session: %s', session)
|
2022-12-08 13:37:33 +08:00
|
|
|
|
pkg.openai.session.sessions[session].persistence()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
if not isinstance(e, KeyboardInterrupt):
|
|
|
|
|
raise e
|
2022-12-08 12:06:04 +08:00
|
|
|
|
print("程序退出")
|
2022-12-15 17:52:41 +08:00
|
|
|
|
sys.exit(0)
|
2022-12-09 15:44:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
# 检查是否有config.py,如果没有就把config-template.py复制一份,并退出程序
|
|
|
|
|
if not os.path.exists('config.py'):
|
|
|
|
|
shutil.copy('config-template.py', 'config.py')
|
|
|
|
|
print('请先在config.py中填写配置')
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) > 1 and sys.argv[1] == 'init_db':
|
|
|
|
|
init_db()
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
2023-01-01 20:46:25 +08:00
|
|
|
|
elif len(sys.argv) > 1 and sys.argv[1] == 'update':
|
|
|
|
|
try:
|
|
|
|
|
from dulwich import porcelain
|
2023-01-01 21:01:09 +08:00
|
|
|
|
repo = porcelain.open_repo('.')
|
2023-01-01 20:46:25 +08:00
|
|
|
|
porcelain.pull(repo)
|
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
|
print("dulwich模块未安装,请查看 https://github.com/RockChinQ/QChatGPT/issues/77")
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
2022-12-09 15:44:49 +08:00
|
|
|
|
main()
|