from local import loop, noawait from aiohttp import web import config import api.route import utils.web from service.database import DatabaseService from service.mediawiki_api import MediaWikiApi # Auto create Table from api.model.base import BaseModel from api.model.toolkit_ui.conversation import ConversationModel as _ from api.model.chat_complete.conversation import ConversationChunkModel as _ from api.model.chat_complete.bot_persona import BotPersonaModel as _ from api.model.embedding_search.title_collection import TitleCollectionModel as _ from api.model.embedding_search.title_index import TitleIndexModel as _ from service.tiktoken import TikTokenService async def index(request: web.Request): return await utils.web.api_response(1, data={"message": "Isekai toolkit API"}, request=request) async def init_mw_api(app: web.Application): mw_api = MediaWikiApi.create() if config.MW_BOT_LOGIN_USERNAME and config.MW_BOT_LOGIN_PASSWORD: try: await mw_api.robot_login(config.MW_BOT_LOGIN_USERNAME, config.MW_BOT_LOGIN_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"])) 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 init_tiktoken(app: web.Application): await TikTokenService.create() print("Tiktoken model loaded.") async def stop_noawait_pool(app: web.Application): await noawait.end() if __name__ == '__main__': app = web.Application() if config.DATABASE: app.on_startup.append(init_database) if config.MW_API: app.on_startup.append(init_mw_api) if config.OPENAI_TOKEN: app.on_startup.append(init_tiktoken) app.on_shutdown.append(stop_noawait_pool) app.router.add_route('*', '/', index) api.route.init(app) web.run_app(app, host='0.0.0.0', port=config.PORT, loop=loop)