Spaces:
Runtime error
Runtime error
Update api.py
Browse files
api.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
# Install necessary libraries if not already present
|
3 |
# These lines will be executed as shell commands by %%writefile
|
4 |
# !pip install duckduckgo_search dateparser
|
@@ -33,6 +32,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
33 |
from fastapi import FastAPI, Request, HTTPException, Depends, Security
|
34 |
from fastapi.security.api_key import APIKeyHeader
|
35 |
from dotenv import load_dotenv # For loading environment variables from a .env file
|
|
|
36 |
|
37 |
# Load environment variables from .env file (if it exists)
|
38 |
load_dotenv()
|
@@ -498,7 +498,7 @@ Query: {query}
|
|
498 |
for question in cleaned_questions:
|
499 |
print(f"\nAnalyzing question for tool determination: '{question}'")
|
500 |
determined_tools[question] = determine_tool_usage(question)
|
501 |
-
print(f"Determined tool for '{question}': '{
|
502 |
|
503 |
print("\nSummary of determined tools per question:")
|
504 |
for question, tool in determined_tools.items():
|
@@ -583,10 +583,44 @@ async def chat_endpoint(request: Request, api_key: str = Depends(get_api_key)):
|
|
583 |
print(traceback.format_exc())
|
584 |
raise HTTPException(status_code=500, detail=f"Internal server error: {e}")
|
585 |
|
586 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
587 |
@app.get("/")
|
588 |
async def read_root():
|
589 |
-
|
|
|
|
|
590 |
status = {
|
591 |
"message": "LLM with Tools API is running",
|
592 |
"llm_client_initialized": client is not None,
|
|
|
|
|
1 |
# Install necessary libraries if not already present
|
2 |
# These lines will be executed as shell commands by %%writefile
|
3 |
# !pip install duckduckgo_search dateparser
|
|
|
32 |
from fastapi import FastAPI, Request, HTTPException, Depends, Security
|
33 |
from fastapi.security.api_key import APIKeyHeader
|
34 |
from dotenv import load_dotenv # For loading environment variables from a .env file
|
35 |
+
from fastapi.responses import JSONResponse # Import JSONResponse
|
36 |
|
37 |
# Load environment variables from .env file (if it exists)
|
38 |
load_dotenv()
|
|
|
498 |
for question in cleaned_questions:
|
499 |
print(f"\nAnalyzing question for tool determination: '{question}'")
|
500 |
determined_tools[question] = determine_tool_usage(question)
|
501 |
+
print(f"Determined tool for '{question}': '{determined_tools[question]}'") # Corrected print statement
|
502 |
|
503 |
print("\nSummary of determined tools per question:")
|
504 |
for question, tool in determined_tools.items():
|
|
|
583 |
print(traceback.format_exc())
|
584 |
raise HTTPException(status_code=500, detail=f"Internal server error: {e}")
|
585 |
|
586 |
+
# Health Check Endpoint
|
587 |
+
@app.get("/health")
|
588 |
+
async def health_check():
|
589 |
+
"""
|
590 |
+
Health check endpoint to verify the application is running and essential components are loaded.
|
591 |
+
"""
|
592 |
+
status = {
|
593 |
+
"status": "ok",
|
594 |
+
"llm_client_initialized": client is not None,
|
595 |
+
"business_info_loaded": business_info_available,
|
596 |
+
"spacy_loaded": nlp is not None,
|
597 |
+
"embedder_loaded": embedder is not None,
|
598 |
+
"reranker_loaded": reranker is not None,
|
599 |
+
"secrets_loaded": {
|
600 |
+
"HF_TOKEN": HF_TOKEN is not None,
|
601 |
+
"SHEET_ID": SHEET_ID is not None,
|
602 |
+
"GOOGLE_BASE64_CREDENTIALS": GOOGLE_BASE64_CREDENTIALS is not None,
|
603 |
+
"API_KEY": API_KEY is not None,
|
604 |
+
}
|
605 |
+
}
|
606 |
+
unhealthy_components = [key for key, value in status.items() if isinstance(value, bool) and not value]
|
607 |
+
if status["secrets_loaded"] and not all(status["secrets_loaded"].values()):
|
608 |
+
unhealthy_components.append("secrets_loaded (partial)")
|
609 |
+
|
610 |
+
if unhealthy_components:
|
611 |
+
status["status"] = "unhealthy"
|
612 |
+
status["unhealthy_components"] = unhealthy_components
|
613 |
+
return JSONResponse(status=503, content=status) # Return 503 Service Unavailable if unhealthy
|
614 |
+
|
615 |
+
return status # Return 200 OK if healthy
|
616 |
+
|
617 |
+
|
618 |
+
# Optional: Root endpoint for basic info
|
619 |
@app.get("/")
|
620 |
async def read_root():
|
621 |
+
"""
|
622 |
+
Root endpoint providing basic application information and status.
|
623 |
+
"""
|
624 |
status = {
|
625 |
"message": "LLM with Tools API is running",
|
626 |
"llm_client_initialized": client is not None,
|