acpcchatbotadmin's picture
Upload 40 files
481dfe6 verified
Raw History Blame Contribute Delete
2.49 kB
import time
import logging
from fastapi import APIRouter
from app.schemas.health import HealthResponse, DetailedHealthResponse, ComponentStatus
from app.memory.session_store import active_session_count
from app.config import settings
router = APIRouter(prefix="/api", tags=["health"])
logger = logging.getLogger(__name__)
_start_time = time.time()
@router.get("/health", response_model=HealthResponse)
async def health():
"""Basic health check."""
from app.agent.graph import _compiled_graph
from app.retrieval.qdrant_store import _client
return HealthResponse(
status="ok",
agent_ready=_compiled_graph is not None,
qdrant_connected=_client is not None,
active_sessions=active_session_count(),
)
@router.get("/health/detailed", response_model=DetailedHealthResponse)
async def health_detailed():
"""Detailed per-component health check."""
components: dict[str, ComponentStatus] = {}
# Check Qdrant
try:
from app.retrieval.qdrant_store import get_qdrant_client
client = get_qdrant_client()
info = client.get_collection(settings.collection_name)
components["qdrant"] = ComponentStatus(
status="ok",
detail=f"collection={settings.collection_name}, points={info.points_count}",
)
except Exception as e:
components["qdrant"] = ComponentStatus(status="error", detail=str(e))
# Check OpenAI (ping model list)
try:
from openai import OpenAI
c = OpenAI(api_key=settings.openai_api_key)
c.models.retrieve(settings.llm_model)
components["openai"] = ComponentStatus(status="ok", detail=f"model={settings.llm_model}")
except Exception as e:
components["openai"] = ComponentStatus(status="error", detail=str(e))
# Sarvam (just check key is configured)
components["sarvam"] = ComponentStatus(
status="ok" if settings.sarvam_api_key else "unconfigured",
)
# Langfuse (check key is configured)
components["langfuse"] = ComponentStatus(
status="ok" if settings.langfuse_public_key else "unconfigured",
)
all_ok = all(c.status == "ok" for c in components.values())
return DetailedHealthResponse(
status="ok" if all_ok else "degraded",
components=components,
uptime_seconds=time.time() - _start_time,
active_sessions=active_session_count(),
)