-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_server.py
More file actions
213 lines (172 loc) · 7.34 KB
/
Copy pathstart_server.py
File metadata and controls
213 lines (172 loc) · 7.34 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
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
#!/usr/bin/env python3
"""
Production startup script for Multilingual RAG System.
No Docker required - just Python!
"""
import sys
import os
import logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
logger = logging.getLogger(__name__)
def check_python_version():
"""Check Python version is 3.11+"""
if sys.version_info < (3, 11):
logger.error("Python 3.11+ required. Current: %s", sys.version)
return False
logger.info("✓ Python version: %s", sys.version.split()[0])
return True
def check_env_file():
"""Check .env file exists"""
if not os.path.exists('.env'):
logger.error("✗ .env file not found!")
logger.info(" Copy .env.example to .env and add your Gemini API key")
return False
logger.info("✓ .env file found")
return True
def check_api_key():
"""Check API key is configured"""
from dotenv import load_dotenv
load_dotenv()
raw_keys = os.getenv('LLM_API_KEYS', '')
api_key = os.getenv('LLM_API_KEY', '')
placeholder = 'your-gemini-api-key-here'
keys = [k.strip() for k in raw_keys.split(',') if k.strip() and k.strip() != placeholder] if raw_keys else []
if not keys and api_key and api_key != placeholder:
keys = [api_key]
if not keys:
logger.error("✗ Gemini API key not configured!")
logger.info(" Edit .env and set LLM_API_KEY or LLM_API_KEYS")
return False
if len(keys) > 1:
logger.info(f"✓ Gemini API keys configured ({len(keys)} keys, load balanced)")
else:
logger.info("✓ Gemini API key configured")
return True
def check_auth_config(production=True):
"""Fail fast when a production server would bind 0.0.0.0 with no endpoint auth.
The server binds 0.0.0.0; without API_KEYS every endpoint (including the
destructive /purge/* routes) accepts anonymous requests. In production mode
this blocks startup unless ALLOW_UNAUTHENTICATED=1 explicitly opts in;
dev mode (--dev) only warns.
"""
if not os.getenv('API_KEYS'):
if production and os.getenv('ALLOW_UNAUTHENTICATED') != '1':
logger.error("✗ API_KEYS not set — server would bind 0.0.0.0 with NO endpoint auth.")
logger.error(" All endpoints (including DELETE /purge/*) would accept anonymous requests.")
logger.info(" Set API_KEYS (and ADMIN_API_KEY) in .env, or opt in to anonymous")
logger.info(" access with ALLOW_UNAUTHENTICATED=1 (private hosts only).")
return False
logger.warning("⚠ API_KEYS not set — server binds 0.0.0.0 with NO endpoint auth.")
logger.warning(" All endpoints (including DELETE /purge/*) accept anonymous requests.")
logger.warning(" Set API_KEYS (and ADMIN_API_KEY) in .env before exposing publicly.")
elif not os.getenv('ADMIN_API_KEY'):
logger.warning("⚠ ADMIN_API_KEY not set — destructive /purge/* routes are disabled (403).")
else:
logger.info("✓ Endpoint auth configured (API_KEYS + ADMIN_API_KEY)")
return True
def check_dependencies():
"""Check required packages are installed"""
import importlib.util
required = [
('fastapi', 'fastapi'),
('uvicorn', 'uvicorn'),
('chromadb', 'chromadb'),
('sentence-transformers', 'sentence_transformers'),
('google-genai', 'google.genai')
]
missing = []
for pip_name, import_name in required:
if importlib.util.find_spec(import_name) is None:
missing.append(pip_name)
if missing:
logger.error("✗ Missing dependencies: %s", ', '.join(missing))
logger.info(" Run: pip install -r requirements.txt")
return False
logger.info("✓ All dependencies installed")
return True
def check_documents():
"""Check if documents are ingested"""
try:
import vector_store
collection = vector_store.get_or_create_collection()
stats = vector_store.get_collection_stats(collection)
if stats['count'] == 0:
logger.warning("⚠ No documents ingested yet")
logger.info(" Add PDFs to papers/ and run: python example_ingest.py")
else:
logger.info("✓ %d document chunks indexed", stats['count'])
return True
except Exception as e:
logger.warning("⚠ Could not check documents: %s", e)
return True
def start_server(mode='production', port=8080):
"""Start the API server.
Runs uvicorn in-process (not via subprocess.run) so SIGTERM/Ctrl+C reach
uvicorn's own signal handlers directly instead of being lost across a
subprocess boundary — uvicorn then drains in-flight requests and runs
the FastAPI `lifespan` shutdown before exiting.
"""
import uvicorn
logger.info("\n" + "="*60)
logger.info("Starting Multilingual RAG API Server")
logger.info("="*60)
reload = mode == 'development'
logger.info("Mode: %s%s", "Development" if reload else "Production",
" (auto-reload enabled)" if reload else "")
logger.info("\nAPI will be available at:")
logger.info(f" → http://localhost:{port}")
logger.info(f" → http://localhost:{port}/api/docs (interactive docs)")
logger.info("\nPress Ctrl+C to stop\n")
logger.info("="*60 + "\n")
uvicorn.run(
"api_server:app",
host="0.0.0.0",
port=port,
reload=reload,
workers=1,
log_level="info",
)
def main():
"""Main entry point"""
import argparse
parser = argparse.ArgumentParser(description='Start the Multilingual RAG API Server')
parser.add_argument('--dev', action='store_true', help='Run in development mode with auto-reload')
parser.add_argument('--skip-checks', action='store_true', help='Skip pre-flight checks')
parser.add_argument('--port', type=int, default=8080, help='Port to run server on (default: 8080)')
args = parser.parse_args()
# Load .env early (before any huggingface_hub import) and quiet HF Hub HTTP
# cache-check noise. Set HF_HUB_OFFLINE=1 in .env to skip the checks entirely.
from dotenv import load_dotenv
load_dotenv()
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("huggingface_hub").setLevel(logging.WARNING)
logger.info("Multilingual Scientific RAG System")
logger.info("="*60)
if not args.skip_checks:
logger.info("\nRunning pre-flight checks...")
logger.info("-"*60)
checks = [
check_python_version(),
check_env_file(),
check_api_key(),
check_dependencies(),
check_auth_config(production=not args.dev),
check_documents()
]
if not all(checks[:5]): # First 5 are critical (auth blocks in production mode)
logger.error("\n✗ Pre-flight checks failed!")
logger.info("Fix the issues above and try again\n")
sys.exit(1)
logger.info("-"*60)
logger.info("✓ All checks passed!\n")
# Ensure directories exist before starting
try:
import config
config.ensure_directories()
except Exception as e:
logger.error(f"Failed to create directories: {e}")
sys.exit(1)
mode = 'development' if args.dev else 'production'
start_server(mode, port=args.port)
if __name__ == '__main__':
main()