-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
executable file
·121 lines (102 loc) · 3.51 KB
/
api.py
File metadata and controls
executable file
·121 lines (102 loc) · 3.51 KB
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
#!/usr/bin/env python3
"""
KidSearch API Backend Launcher
Starts the FastAPI server for unified search (Typesense + Google CSE + Reranking)
"""
import os
import sys
import logging
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Setup logging
log_level = os.getenv("LOG_LEVEL", "INFO").upper()
logging.basicConfig(
level=log_level,
format="%(asctime)s - [API] - %(levelname)s:%(filename)s:%(lineno)d:%(funcName)s: %(message)s",
)
# Force log level for application loggers (uvicorn may override root logger)
app_logger = logging.getLogger("kidsearch")
app_logger.setLevel(log_level)
logger = logging.getLogger(__name__)
def main():
"""Launch the API server."""
# Check if API is enabled
api_enabled = os.getenv("API_ENABLED", "false").lower() == "true"
if not api_enabled:
logger.warning(
"API backend is disabled. Set API_ENABLED=true in .env to enable."
)
sys.exit(1)
# Get configuration
host = os.getenv("API_HOST", "0.0.0.0")
port = int(os.getenv("API_PORT", "8080"))
workers = int(os.getenv("API_WORKERS", "4"))
# Check required dependencies
try:
import fastapi # noqa: F401
import uvicorn # noqa: F401
except ImportError:
logger.error(
"FastAPI dependencies not installed. Run: pip install -r requirements.txt"
)
sys.exit(1)
# Check Typesense connection
typesense_url = os.getenv("TYPESENSE_URL", "http://localhost:8108")
logger.info(f"Typesense URL: {typesense_url}")
# Check Google CSE configuration
cse_api_key = os.getenv("GOOGLE_CSE_API_KEY")
cse_id = os.getenv("GOOGLE_CSE_ID")
if (
not cse_api_key
or not cse_id
or cse_api_key == "your_google_api_key_here"
or cse_id == "your_search_engine_id_here"
):
logger.warning(
"Google CSE not configured. "
"API will work with Typesense only. "
"Set GOOGLE_CSE_API_KEY and GOOGLE_CSE_ID in .env for unified search."
)
# Log configuration
logger.info("=" * 60)
logger.info("KidSearch API Backend")
logger.info("=" * 60)
logger.info(f"Log Level: {log_level}")
logger.info(f"Host: {host}")
logger.info(f"Port: {port}")
logger.info(f"Workers: {workers}")
logger.info(f"Reranking: {os.getenv('RERANKING_ENABLED', 'true')}")
logger.info(
f"Reranker Model: {os.getenv('RERANKER_MODEL', 'intfloat/multilingual-e5-base')}"
)
logger.info("=" * 60)
logger.info("")
logger.info("API Documentation:")
logger.info(f" Swagger UI: http://{host}:{port}/api/docs")
logger.info(f" ReDoc: http://{host}:{port}/api/redoc")
logger.info("")
logger.info("Endpoints:")
logger.info(f" GET http://{host}:{port}/api/health")
logger.info(f" GET http://{host}:{port}/api/search?q=dinosaures")
logger.info(f" GET http://{host}:{port}/api/stats")
logger.info(f" POST http://{host}:{port}/api/feedback")
logger.info("=" * 60)
# Start server
try:
import uvicorn
uvicorn.run(
"kidsearch.api.server:app",
host=host,
port=port,
workers=workers,
log_level=log_level.lower(),
reload=False, # Set to True for development
)
except KeyboardInterrupt:
logger.info("API server stopped by user")
except Exception as e:
logger.error(f"Failed to start API server: {e}", exc_info=True)
sys.exit(1)
if __name__ == "__main__":
main()