from __future__ import annotations import asyncio import logging import sys from contextlib import asynccontextmanager from pathlib import Path from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware PROJECT_ROOT = Path(__file__).resolve().parents[1] if str(PROJECT_ROOT) not in sys.path: sys.path.insert( 0, str(PROJECT_ROOT), ) from app.routes import router from app.security import ( validate_security_configuration, ) from scripts.embedding_utils import embed_query logger = logging.getLogger(__name__) OPENWEBUI_ORIGIN = ( "https://ui.tukekemt.xyz" ) @asynccontextmanager async def lifespan( _: FastAPI, ): # Bez správne nakonfigurovanej security # aplikáciu nespustíme. validate_security_configuration() # Predhriatie embedding modelu je # optimalizácia, nie podmienka samotného # spustenia HTTP API. # # Ak warm-up zlyhá, API zostane dostupné. # Retrieval endpoint následne vráti # kontrolovanú chybu 503, ak embeddingový # model skutočne nebude dostupný. try: await asyncio.to_thread( embed_query, "warmup", ) except Exception: logger.exception( "Predhriatie embedding modelu " "zlyhalo. API pokračuje bez " "úspešného warm-upu." ) yield app = FastAPI( title="ZP Agent API", description=( "RAG API pre vyhľadávanie " "a získavanie informácií " "z repozitára záverečných prác " "ZP Wiki." ), version="0.9.0", lifespan=lifespan, ) app.add_middleware( CORSMiddleware, allow_origins=[ OPENWEBUI_ORIGIN, ], allow_credentials=True, allow_methods=[ "GET", "POST", "OPTIONS", ], allow_headers=["*"], allow_private_network=True, ) app.include_router( router )