Spaces:
Running
Running
File size: 7,834 Bytes
2a8faae 0176a31 2a8faae 0176a31 2a8faae 0176a31 2a8faae 0176a31 2a8faae 0176a31 2a8faae 0176a31 2a8faae 20953d6 |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
"""
Health Check and System Status Router
"""
from datetime import datetime
from fastapi import APIRouter
from api.models import HealthStatus, InitializationStatus
router = APIRouter(prefix="/health", tags=["health"])
@router.get("/", response_model=HealthStatus)
async def health_check():
"""
Check the health status of the API and its components
"""
components = {}
# Check agent availability
try:
from core.agent import safe_run_agent
components["agent"] = "healthy"
except Exception:
components["agent"] = "unhealthy"
# Check vector store
try:
from core.vector_store import get_vector_store, load_vector_store
from core.background_init import is_initialization_complete
# If initialization is complete, check the vector store directly
if is_initialization_complete():
vector_store = get_vector_store()
if vector_store is not None:
components["vector_store"] = "healthy"
else:
# If vector store is None, try to load it
try:
loaded_store = load_vector_store()
if loaded_store is not None:
components["vector_store"] = "healthy"
else:
components["vector_store"] = "unhealthy"
except Exception as e:
logger.error(f"Failed to load vector store: {e}")
components["vector_store"] = "unhealthy"
else:
# If initialization is not complete, the vector store might not be ready yet
# but we'll check if it exists and can be loaded
try:
loaded_store = load_vector_store()
if loaded_store is not None:
components["vector_store"] = "healthy"
else:
components["vector_store"] = "initializing"
except Exception as e:
logger.warning(f"Vector store not ready yet: {e}")
components["vector_store"] = "initializing"
except Exception as e:
logger.error(f"Error checking vector store health: {e}")
components["vector_store"] = "unhealthy"
# Check data loaders
try:
from core.data_loaders import load_pdf_documents
components["data_loaders"] = "healthy"
except Exception:
components["data_loaders"] = "unhealthy"
# Check tools
try:
from core.tools import medical_guidelines_knowledge_tool
components["tools"] = "healthy"
except Exception:
components["tools"] = "unhealthy"
# Check initialization status
initialization_status = None
try:
from core.background_init import (
is_initialization_complete,
get_initialization_status,
is_initialization_successful,
get_initialization_error
)
initialization_status = InitializationStatus(
is_complete=is_initialization_complete(),
status_message=get_initialization_status(),
is_successful=is_initialization_successful(),
error=str(get_initialization_error()) if get_initialization_error() else None
)
except Exception as e:
initialization_status = InitializationStatus(
is_complete=False,
status_message=f"Unable to check initialization status: {str(e)}",
is_successful=False,
error=str(e)
)
# Overall status
overall_status = "healthy" if all(
status == "healthy" for status in components.values()
) else "degraded"
return HealthStatus(
status=overall_status,
version="1.0.0",
timestamp=datetime.now().isoformat(),
components=components,
initialization=initialization_status
)
@router.get("/ping")
async def ping():
"""
Simple ping endpoint for basic connectivity check
"""
return {"message": "pong", "timestamp": datetime.now().isoformat()}
@router.get("/initialization", response_model=InitializationStatus)
async def get_initialization_status():
"""
Get the current initialization status of background components
"""
try:
from background_init import (
is_initialization_complete,
get_initialization_status,
is_initialization_successful,
get_initialization_error
)
return InitializationStatus(
is_complete=is_initialization_complete(),
status_message=get_initialization_status(),
is_successful=is_initialization_successful(),
error=str(get_initialization_error()) if get_initialization_error() else None
)
except Exception as e:
return InitializationStatus(
is_complete=False,
status_message=f"Unable to check initialization status: {str(e)}",
is_successful=False,
error=str(e)
)
@router.get("/version")
async def get_version():
"""
Get API version information
"""
return {
"version": "1.0.0",
"name": "Lung Cancer AI Advisor API",
"description": "AI-powered advisor for lung cancer information and support",
"build_date": "2024-01-01"
}
@router.get("/sessions")
async def get_active_sessions():
"""
Get list of all active conversation sessions
"""
try:
from core.agent import get_active_sessions
sessions = get_active_sessions()
return {
"active_sessions": sessions,
"count": len(sessions),
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {
"error": str(e),
"active_sessions": [],
"count": 0
}
@router.delete("/sessions/{session_id}")
async def clear_session(session_id: str):
"""
Clear conversation memory for a specific session
Args:
session_id: The session identifier to clear
"""
try:
from core.agent import clear_session_memory
success = clear_session_memory(session_id)
if success:
return {
"message": f"Session '{session_id}' cleared successfully",
"session_id": session_id,
"timestamp": datetime.now().isoformat()
}
else:
return {
"message": f"Session '{session_id}' not found",
"session_id": session_id,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {
"error": str(e),
"session_id": session_id
}
@router.delete("/sessions")
async def clear_all_sessions():
"""
Clear all conversation sessions
"""
try:
from core.agent import clear_memory
clear_memory()
return {
"message": "All sessions cleared successfully",
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {
"error": str(e)
}
@router.get("/sessions/{session_id}/summary")
async def get_session_summary(session_id: str):
"""
Get conversation history summary for a specific session
Args:
session_id: The session identifier
"""
try:
from core.agent import get_memory_summary
summary = get_memory_summary(session_id)
return {
"session_id": session_id,
"summary": summary,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {
"error": str(e),
"session_id": session_id
}
|