QChatGPT/pkg/core/app.py

102 lines
2.5 KiB
Python
Raw Normal View History

2024-01-23 20:55:20 +08:00
from __future__ import annotations
import logging
import asyncio
2024-01-30 14:58:34 +08:00
import traceback
2024-01-23 20:55:20 +08:00
2024-01-29 21:22:27 +08:00
from ..platform import manager as im_mgr
2024-01-28 19:20:10 +08:00
from ..provider.session import sessionmgr as llm_session_mgr
from ..provider.requester import modelmgr as llm_model_mgr
from ..provider.sysprompt import sysprompt as llm_prompt_mgr
from ..provider.tools import toolmgr as llm_tool_mgr
2024-01-23 20:55:20 +08:00
from ..config import manager as config_mgr
2024-01-30 16:13:33 +08:00
from ..audit.center import v2 as center_mgr
2024-01-28 00:16:42 +08:00
from ..command import cmdmgr
2024-01-29 21:22:27 +08:00
from ..plugin import manager as plugin_mgr
2024-02-23 17:46:22 +08:00
from ..pipeline import pool
from ..pipeline import controller, stagemgr
2024-01-29 21:22:27 +08:00
from ..utils import version as version_mgr, proxy as proxy_mgr
2024-01-23 20:55:20 +08:00
class Application:
2024-03-03 16:34:59 +08:00
"""运行时应用对象和上下文"""
2024-01-30 21:45:17 +08:00
im_mgr: im_mgr.PlatformManager = None
2024-01-23 20:55:20 +08:00
2024-01-28 00:16:42 +08:00
cmd_mgr: cmdmgr.CommandManager = None
sess_mgr: llm_session_mgr.SessionManager = None
model_mgr: llm_model_mgr.ModelManager = None
prompt_mgr: llm_prompt_mgr.PromptManager = None
2024-01-27 21:50:40 +08:00
tool_mgr: llm_tool_mgr.ToolManager = None
2024-02-06 21:26:03 +08:00
command_cfg: config_mgr.ConfigManager = None
2024-01-23 20:55:20 +08:00
2024-02-06 21:26:03 +08:00
pipeline_cfg: config_mgr.ConfigManager = None
platform_cfg: config_mgr.ConfigManager = None
provider_cfg: config_mgr.ConfigManager = None
system_cfg: config_mgr.ConfigManager = None
2024-01-23 20:55:20 +08:00
2024-01-30 16:13:33 +08:00
ctr_mgr: center_mgr.V2CenterAPI = None
2024-01-23 20:55:20 +08:00
2024-01-29 21:22:27 +08:00
plugin_mgr: plugin_mgr.PluginManager = None
2024-01-23 20:55:20 +08:00
query_pool: pool.QueryPool = None
ctrl: controller.Controller = None
stage_mgr: stagemgr.StageManager = None
2024-01-29 21:22:27 +08:00
ver_mgr: version_mgr.VersionManager = None
proxy_mgr: proxy_mgr.ProxyManager = None
2024-01-23 20:55:20 +08:00
logger: logging.Logger = None
def __init__(self):
pass
2024-01-27 21:50:40 +08:00
async def initialize(self):
2024-01-29 21:22:27 +08:00
pass
2024-01-27 21:50:40 +08:00
async def run(self):
2024-01-29 21:22:27 +08:00
await self.plugin_mgr.load_plugins()
2024-01-29 21:41:20 +08:00
await self.plugin_mgr.initialize_plugins()
2024-01-27 21:50:40 +08:00
2024-02-07 20:03:46 +08:00
tasks = []
2024-01-30 14:58:34 +08:00
try:
2024-02-11 23:35:05 +08:00
2024-01-30 16:13:33 +08:00
tasks = [
asyncio.create_task(self.im_mgr.run()),
asyncio.create_task(self.ctrl.run())
]
2024-02-11 23:35:05 +08:00
2024-03-03 16:34:59 +08:00
# 挂信号处理
2024-02-19 19:27:42 +08:00
import signal
def signal_handler(sig, frame):
2024-02-11 23:35:05 +08:00
for task in tasks:
task.cancel()
2024-02-19 19:27:42 +08:00
self.logger.info("程序退出.")
exit(0)
signal.signal(signal.SIGINT, signal_handler)
await asyncio.gather(*tasks, return_exceptions=True)
2024-01-30 14:58:34 +08:00
except asyncio.CancelledError:
2024-02-07 20:03:46 +08:00
pass
2024-01-30 14:58:34 +08:00
except Exception as e:
self.logger.error(f"应用运行致命异常: {e}")
self.logger.debug(f"Traceback: {traceback.format_exc()}")
2024-02-19 19:27:42 +08:00