acpcchatbotadmin's picture
Upload 40 files
481dfe6 verified
Raw History Blame Contribute Delete
1.97 kB
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.config import settings
from app.agent.graph import get_graph
from app.retrieval.qdrant_store import get_vector_store
from app.monitoring.langfuse_setup import get_langfuse_callback_handler, flush_langfuse
from app.memory.session_store import start_cleanup_task, stop_cleanup_task
from app.routers import chat, stt, ingest, health, sessions
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application startup / shutdown lifecycle."""
logger.info("Starting ACPC Chatbot backend...")
# Pre-warm non-network singletons
get_graph()
get_langfuse_callback_handler()
start_cleanup_task()
# NOTE: Qdrant vector store is lazily initialized on first query
# (avoids startup failure if Qdrant Cloud is temporarily unreachable)
logger.info("Backend ready.")
yield
# Shutdown
stop_cleanup_task()
flush_langfuse()
logger.info("Backend shut down cleanly.")
app = FastAPI(
title="ACPC Gujarat Chatbot API",
description="Agentic RAG chatbot for ACPC Gujarat admissions queries",
version="1.0.0",
lifespan=lifespan,
)
# CORS — restrict to configured origins
app.add_middleware(
CORSMiddleware,
allow_origins=settings.allowed_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Mount routers
app.include_router(chat.router)
app.include_router(stt.router)
app.include_router(ingest.router)
app.include_router(health.router)
app.include_router(sessions.router)
@app.get("/")
async def root():
return {"status": "ok", "service": "ACPC Gujarat Chatbot API", "version": "1.0.0"}