完成动态加载的重构
parent
1355dbbf35
commit
a6e80850cc
@ -1,41 +0,0 @@
|
||||
from aiohttp import web
|
||||
from api.controller.ChatComplete import ChatComplete
|
||||
|
||||
from api.controller.Hanzi import Hanzi
|
||||
from api.controller.Index import Index
|
||||
from api.controller.Kanji import Kanji
|
||||
from api.controller.Hanja import Hanja
|
||||
from api.controller.EmbeddingSearch import EmbeddingSearch
|
||||
|
||||
def init(app: web.Application):
|
||||
app.router.add_routes([
|
||||
web.route('*', '/hanzi/pinyin/', Hanzi.hanziToPinyin),
|
||||
web.route('*', '/hanzi/split/', Hanzi.splitHanzi),
|
||||
|
||||
web.route('*', '/kanji/romaji/', Kanji.kanji2romaji),
|
||||
|
||||
web.route('*', '/hanja/romaja/', Hanja.hanja2roma),
|
||||
|
||||
web.route('*', '/title/info', Index.update_title_info),
|
||||
web.route('*', '/title/search', Index.search_title),
|
||||
web.route('*', '/user/info', Index.get_user_info),
|
||||
web.route('*', '/conversation/list', Index.get_conversation_list),
|
||||
web.route('*', '/conversation/info', Index.get_conversation_info),
|
||||
web.route('POST', '/conversation/remove', Index.remove_conversation),
|
||||
web.route('DELETE', '/conversation/remove', Index.remove_conversation),
|
||||
web.route('POST', '/conversation/set_pinned', Index.set_conversation_pinned),
|
||||
web.route('POST', '/conversation/set_title', Index.set_conversation_title),
|
||||
|
||||
web.route('*', '/embedding_search/index_page', EmbeddingSearch.index_page),
|
||||
web.route('*', '/embedding_search/search', EmbeddingSearch.search),
|
||||
web.route('POST', '/sys/embedding_search/title/update', EmbeddingSearch.sys_update_title_info),
|
||||
|
||||
web.route('*', '/chatcomplete/conversation_chunk/list', ChatComplete.get_conversation_chunk_list),
|
||||
web.route('*', '/chatcomplete/conversation_chunk/info', ChatComplete.get_conversation_chunk),
|
||||
web.route('POST', '/chatcomplete/conversation/fork', ChatComplete.fork_conversation),
|
||||
web.route('POST', '/chatcomplete/message', ChatComplete.start_chat_complete),
|
||||
web.route('GET', '/chatcomplete/message/stream', ChatComplete.chat_complete_stream),
|
||||
web.route('POST', '/chatcomplete/get_point_cost', ChatComplete.get_point_cost),
|
||||
web.route('*', '/chatcomplete/persona/list', ChatComplete.get_persona_list),
|
||||
web.route('*', '/chatcomplete/persona/info', ChatComplete.get_persona_info),
|
||||
])
|
@ -0,0 +1,9 @@
|
||||
from aiohttp import web
|
||||
from utils.server import register_server_module
|
||||
|
||||
from server.route.base_api import register_route
|
||||
|
||||
def init(app: web.Application):
|
||||
register_route(app)
|
||||
|
||||
register_server_module("base_api", init)
|
@ -0,0 +1,22 @@
|
||||
from aiohttp import web
|
||||
from utils.server import register_server_module
|
||||
|
||||
import init.service.mw_api as _ # Init mediawiki api
|
||||
import init.service.database as _ # Init database
|
||||
import init.service.tiktoken as _ # Init tiktoken
|
||||
|
||||
import toolbox_ui as _ # Init toolbox ui
|
||||
import embedding_search as _ # Init embedding search
|
||||
|
||||
# Auto create database tables
|
||||
from server.model.chat_complete.conversation import ConversationChunkModel as _
|
||||
from server.model.chat_complete.bot_persona_category import BotPersonaCategoryModel as _
|
||||
from server.model.chat_complete.bot_persona import BotPersonaModel as _
|
||||
|
||||
# Route
|
||||
from server.route.chat_complete import register_route
|
||||
|
||||
def init(app: web.Application):
|
||||
register_route(app)
|
||||
|
||||
register_server_module("chat_complete", init)
|
@ -0,0 +1,17 @@
|
||||
from aiohttp import web
|
||||
from utils.server import register_server_module
|
||||
|
||||
import init.service.database as _ # Init database
|
||||
import init.service.tiktoken as _ # Init tiktoken
|
||||
|
||||
# Auto create database tables
|
||||
from server.model.embedding_search.title_collection import TitleCollectionModel as _
|
||||
from server.model.embedding_search.title_index import TitleIndexModel as _
|
||||
|
||||
# Route
|
||||
from server.route.embedding_search import register_route
|
||||
|
||||
def init(app: web.Application):
|
||||
register_route(app)
|
||||
|
||||
register_server_module("embedding_search", init)
|
@ -0,0 +1,16 @@
|
||||
from aiohttp import web
|
||||
from utils.server import register_server_module
|
||||
|
||||
import init.service.database as _ # Init database
|
||||
|
||||
# Auto create database tables
|
||||
from server.model.toolbox_ui.page_title import PageTitleModel as _
|
||||
from server.model.toolbox_ui.conversation import ConversationModel as _
|
||||
|
||||
# Route
|
||||
from server.route.toolbox_ui import register_route
|
||||
|
||||
def init(app: web.Application):
|
||||
register_route(app)
|
||||
|
||||
register_server_module("toolbox_ui", init)
|
@ -0,0 +1,23 @@
|
||||
from __future__ import annotations
|
||||
from aiohttp import web
|
||||
from utils.server import register_server_module
|
||||
|
||||
from server.model.base import BaseModel
|
||||
from service.database import DatabaseService
|
||||
|
||||
async def init_database(app: web.Application):
|
||||
dbs = await DatabaseService.create(app)
|
||||
print("Database connected.")
|
||||
|
||||
async with dbs.engine.begin() as conn:
|
||||
await conn.run_sync(BaseModel.metadata.create_all)
|
||||
|
||||
async def close_database(app: web.Application):
|
||||
dbs = await DatabaseService.create(app)
|
||||
await dbs.close()
|
||||
|
||||
def init(app: web.Application):
|
||||
app.on_startup.append(init_database)
|
||||
app.on_cleanup.append(close_database)
|
||||
|
||||
register_server_module("database", init)
|
@ -0,0 +1,33 @@
|
||||
from __future__ import annotations
|
||||
from aiohttp import web
|
||||
from utils.server import register_server_module
|
||||
|
||||
from lib.config import Config
|
||||
from service.mediawiki_api import MediaWikiApi
|
||||
|
||||
|
||||
async def init_mw_api(app: web.Application):
|
||||
mw_api = MediaWikiApi.create()
|
||||
|
||||
bot_username = Config.get("mediawiki.bot_username", "", str)
|
||||
bot_password = Config.get("mediawiki.bot_password", "", str)
|
||||
|
||||
if bot_username and bot_password:
|
||||
try:
|
||||
await mw_api.robot_login(bot_username, bot_password)
|
||||
except Exception as e:
|
||||
print("Cannot login to Robot account, please check config.")
|
||||
|
||||
site_meta = await mw_api.get_site_meta()
|
||||
|
||||
print(
|
||||
"Connected to Wiki %s, Robot username: %s"
|
||||
% (site_meta["sitename"], site_meta["user"])
|
||||
)
|
||||
|
||||
|
||||
def init(app: web.Application):
|
||||
app.on_startup.append(init_mw_api)
|
||||
|
||||
|
||||
register_server_module("mediawiki_api", init)
|
@ -0,0 +1,16 @@
|
||||
from aiohttp import web
|
||||
from utils.server import register_server_module
|
||||
|
||||
from service.tiktoken import TikTokenService
|
||||
|
||||
|
||||
async def init_tiktoken(app: web.Application):
|
||||
await TikTokenService.create()
|
||||
print("Tiktoken model loaded.")
|
||||
|
||||
|
||||
async def init(app: web.Application):
|
||||
app.on_startup.append(init_tiktoken)
|
||||
|
||||
|
||||
register_server_module("tiktoken", init)
|
@ -1,8 +1,8 @@
|
||||
import sys
|
||||
import traceback
|
||||
from aiohttp import web
|
||||
from api.model.embedding_search.title_collection import TitleCollectionHelper
|
||||
from api.model.embedding_search.title_index import TitleIndexHelper
|
||||
from server.model.embedding_search.title_collection import TitleCollectionHelper
|
||||
from server.model.embedding_search.title_index import TitleIndexHelper
|
||||
from service.database import DatabaseService
|
||||
from service.embedding_search import EmbeddingRunningException, EmbeddingSearchService
|
||||
from service.mediawiki_api import MediaWikiApi, MediaWikiApiException, MediaWikiPageNotFoundException
|
@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
import sqlalchemy
|
||||
from api.model.base import BaseHelper, BaseModel
|
||||
from server.model.base import BaseHelper, BaseModel
|
||||
|
||||
import sqlalchemy
|
||||
from sqlalchemy import select, update
|
@ -0,0 +1,15 @@
|
||||
from aiohttp import web
|
||||
|
||||
from server.controller.Hanja import Hanja
|
||||
from server.controller.Hanzi import Hanzi
|
||||
from server.controller.Kanji import Kanji
|
||||
|
||||
def register_route(app: web.Application):
|
||||
app.router.add_routes([
|
||||
web.route('*', '/hanzi/pinyin/', Hanzi.hanziToPinyin),
|
||||
web.route('*', '/hanzi/split/', Hanzi.splitHanzi),
|
||||
|
||||
web.route('*', '/kanji/romaji/', Kanji.kanji2romaji),
|
||||
|
||||
web.route('*', '/hanja/romaja/', Hanja.hanja2roma),
|
||||
])
|
@ -0,0 +1,15 @@
|
||||
from aiohttp import web
|
||||
|
||||
from server.controller.ChatComplete import ChatComplete
|
||||
|
||||
def register_route(app: web.Application):
|
||||
app.router.add_routes([
|
||||
web.route('*', '/chatcomplete/conversation_chunk/list', ChatComplete.get_conversation_chunk_list),
|
||||
web.route('*', '/chatcomplete/conversation_chunk/info', ChatComplete.get_conversation_chunk),
|
||||
web.route('POST', '/chatcomplete/conversation/fork', ChatComplete.fork_conversation),
|
||||
web.route('POST', '/chatcomplete/message', ChatComplete.start_chat_complete),
|
||||
web.route('GET', '/chatcomplete/message/stream', ChatComplete.chat_complete_stream),
|
||||
web.route('POST', '/chatcomplete/get_point_cost', ChatComplete.get_point_cost),
|
||||
web.route('*', '/chatcomplete/persona/list', ChatComplete.get_persona_list),
|
||||
web.route('*', '/chatcomplete/persona/info', ChatComplete.get_persona_info),
|
||||
])
|
@ -0,0 +1,10 @@
|
||||
from aiohttp import web
|
||||
|
||||
from server.controller.EmbeddingSearch import EmbeddingSearch
|
||||
|
||||
def register_route(app: web.Application):
|
||||
app.router.add_routes([
|
||||
web.route('*', '/embedding_search/index_page', EmbeddingSearch.index_page),
|
||||
web.route('*', '/embedding_search/search', EmbeddingSearch.search),
|
||||
web.route('POST', '/sys/embedding_search/title/update', EmbeddingSearch.sys_update_title_info),
|
||||
])
|
@ -0,0 +1,16 @@
|
||||
from aiohttp import web
|
||||
|
||||
from server.controller.ToolboxIndex import ToolboxIndex
|
||||
|
||||
def register_route(app: web.Application):
|
||||
app.router.add_routes([
|
||||
web.route('*', '/title/info', ToolboxIndex.update_title_info),
|
||||
web.route('*', '/title/search', ToolboxIndex.search_title),
|
||||
web.route('*', '/user/info', ToolboxIndex.get_user_info),
|
||||
web.route('*', '/conversation/list', ToolboxIndex.get_conversation_list),
|
||||
web.route('*', '/conversation/info', ToolboxIndex.get_conversation_info),
|
||||
web.route('POST', '/conversation/remove', ToolboxIndex.remove_conversation),
|
||||
web.route('DELETE', '/conversation/remove', ToolboxIndex.remove_conversation),
|
||||
web.route('POST', '/conversation/set_pinned', ToolboxIndex.set_conversation_pinned),
|
||||
web.route('POST', '/conversation/set_title', ToolboxIndex.set_conversation_title),
|
||||
])
|
@ -0,0 +1,23 @@
|
||||
from __future__ import annotations
|
||||
from typing import TypedDict
|
||||
|
||||
class ServerModuleItem(TypedDict):
|
||||
name: str
|
||||
init: callable
|
||||
|
||||
_SERVER_MODULES: list[ServerModuleItem] = []
|
||||
|
||||
def register_server_module(name: str, init_method: callable):
|
||||
global _SERVER_MODULES
|
||||
|
||||
for module in _SERVER_MODULES:
|
||||
if module["name"] == name:
|
||||
return
|
||||
|
||||
_SERVER_MODULES.append({
|
||||
"name": name,
|
||||
"init": init_method
|
||||
})
|
||||
|
||||
def get_server_modules() -> list[ServerModuleItem]:
|
||||
return _SERVER_MODULES
|
Loading…
Reference in New Issue