File size: 1,965 Bytes
481dfe6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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"}