dp-zp-agent/app/main.py

106 lines
1.8 KiB
Python

from __future__ import annotations
import asyncio
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 (
RagRequest,
SearchRequest,
SyncRequest,
gitea_webhook,
health,
rag,
router,
search,
sync,
)
from app.security import (
MIN_SECRET_LENGTH,
SEARCH_API_KEY_HEADER,
SYNC_API_KEY_HEADER,
expected_gitea_repository,
repository_name_from_payload,
require_search_api_key,
require_sync_api_key,
required_environment_value,
same_repository,
search_api_key_scheme,
search_bearer_scheme,
sync_api_key_scheme,
validate_secret,
validate_security_configuration,
verify_gitea_signature,
webhook_should_pull_git,
)
from scripts.embedding_utils import embed_query
OPENWEBUI_ORIGIN = (
"https://ui.tukekemt.xyz"
)
@asynccontextmanager
async def lifespan(
_: FastAPI,
):
validate_security_configuration()
# Predhriatie embedding modelu.
await asyncio.to_thread(
embed_query,
"warmup",
)
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.8.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
)