-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
462 lines (396 loc) · 16.5 KB
/
Copy pathmain.py
File metadata and controls
462 lines (396 loc) · 16.5 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# pyright: strict
import argparse
import asyncio
import logging
import sys
from pathlib import Path
from src.config.logging import setup_logging, update_logging_from_config
setup_logging()
logger = logging.getLogger(__name__)
from pydantic import ValidationError # noqa: E402
from src.adapters.docling_adapter import DoclingAdapter # noqa: E402
from src.adapters.embeddings import embedding_mode # noqa: E402
from src.adapters.http_client import HTTPClientPool # noqa: E402
from src.adapters.minio_adapter import MinIOAdapter # noqa: E402
from src.api.factory import ServerFactory # noqa: E402
from src.api.rest.auth.jwt_handler import JWTHandler # noqa: E402
from src.config.logging.interceptor import FrameworkInterceptor # noqa: E402
from src.core.accessor import ConfigAccessor # noqa: E402
from src.core.admin import WorkspaceManager # noqa: E402
from src.core.health import run_preflight_checks # noqa: E402
from src.core.rag_builder import RagFactory # noqa: E402
# Восстанавливаем propagate=True для всех логгеров сразу после их физического импорта
FrameworkInterceptor.enforce()
async def index_command(path: str) -> None:
"""Index files from the given path into the RAG pipeline."""
embedding_mode.set("document")
secrets = ConfigAccessor.get_secrets()
if not secrets.llm_api_key:
logger.error("LLM_API_KEY is not set in secrets")
return
if not secrets.embed_api_key:
logger.error("EMBED_API_KEY is not set in secrets")
return
if not secrets.qdrant_url:
logger.error("QDRANT_URL is not set in secrets")
return
path_obj = Path(path)
if not path_obj.exists():
logger.error(f"Path does not exist: {path}")
return
files_to_process: list[Path] = []
if path_obj.is_file():
files_to_process.append(path_obj)
else:
files_to_process = (
list(path_obj.rglob("*.md"))
+ list(path_obj.rglob("*.txt"))
+ list(path_obj.rglob("*.pdf"))
+ list(path_obj.rglob("*.docx"))
)
if not files_to_process:
logger.warning(f"No supported files found in {path}")
return
logger.info(f"Found {len(files_to_process)} files to process")
rag_pipeline = None
try:
await HTTPClientPool.init_client()
try:
await run_preflight_checks()
except RuntimeError as e:
logger.critical(str(e))
sys.exit(1)
rag_pipeline = await RagFactory.create_rag()
FrameworkInterceptor.enforce()
docling_adapter = DoclingAdapter()
FrameworkInterceptor.enforce()
minio_adapter = None
if secrets.minio_endpoint:
minio_adapter = MinIOAdapter()
core_config = ConfigAccessor.get_core_config()
workspace = core_config.workspace_name or "default"
for file_path in files_to_process:
logger.info(f"Processing: {file_path}")
try:
ext = file_path.suffix.lower()
doc_id = None
if ext in (".md", ".txt"):
content_list = docling_adapter.process_file(str(file_path))
else:
content_list, doc_id = await rag_pipeline.parse_with_fallback(
str(file_path), workspace
)
for content in content_list:
if minio_adapter:
for key in ["img_path", "table_img_path", "equation_img_path"]:
if content.get("type") in [
"image",
"table",
"equation",
] and content.get(key):
image_path = content[key]
if image_path:
uri = await minio_adapter.upload_file(
workspace, image_path
)
content[key] = uri
await rag_pipeline.insert_content_list(
content_list=content_list,
file_path=str(file_path),
doc_id=doc_id,
)
logger.info(f"Successfully indexed: {file_path}")
except Exception as e:
logger.error(f"Error processing {file_path}: {e}")
finally:
if rag_pipeline:
await asyncio.shield(rag_pipeline.shutdown_workers())
await asyncio.shield(rag_pipeline.finalize_storages())
await asyncio.shield(HTTPClientPool.close_client())
import shutil
core_config = ConfigAccessor.get_core_config()
workspace = core_config.workspace_name or "default"
shutil.rmtree(Path("output") / workspace, ignore_errors=True)
async def query_command(question: str) -> None:
"""Query the RAG system with the given question."""
embedding_mode.set("query")
secrets = ConfigAccessor.get_secrets()
if not secrets.llm_api_key:
logger.error("LLM_API_KEY is not set in secrets")
return
if not secrets.embed_api_key:
logger.error("EMBED_API_KEY is not set in secrets")
return
if not secrets.qdrant_url:
logger.error("QDRANT_URL is not set in secrets")
return
rag_pipeline = None
try:
await HTTPClientPool.init_client()
try:
await run_preflight_checks()
except RuntimeError as e:
logger.critical(str(e))
sys.exit(1)
rag_pipeline = await RagFactory.create_rag()
FrameworkInterceptor.enforce()
result = await rag_pipeline.aquery(question, mode="hybrid", stream=False)
print(f"\nAnswer: {result}\n")
finally:
if rag_pipeline:
await asyncio.shield(rag_pipeline.shutdown_workers())
await asyncio.shield(rag_pipeline.finalize_storages())
await asyncio.shield(HTTPClientPool.close_client())
async def check_command() -> None:
"""Run preflight health checks."""
secrets = ConfigAccessor.get_secrets()
if not secrets.llm_api_key:
logger.error("LLM_API_KEY is not set in secrets")
return
if not secrets.embed_api_key:
logger.error("EMBED_API_KEY is not set in secrets")
return
if not secrets.qdrant_url:
logger.error("QDRANT_URL is not set in secrets")
return
try:
await HTTPClientPool.init_client()
try:
await run_preflight_checks()
except RuntimeError as e:
logger.critical(str(e))
sys.exit(1)
FrameworkInterceptor.enforce()
logger.info("All checks passed.")
finally:
await asyncio.shield(HTTPClientPool.close_client())
async def serve_command(server_type: str, port: int, webui: bool = False) -> None:
"""Start the API server (REST or MCP)."""
secrets = ConfigAccessor.get_secrets()
if not secrets.llm_api_key:
logger.error("LLM_API_KEY is not set in secrets")
return
if not secrets.embed_api_key:
logger.error("EMBED_API_KEY is not set in secrets")
return
if not secrets.qdrant_url:
logger.error("QDRANT_URL is not set in secrets")
return
server_config = ConfigAccessor.get_server_config()
token_secret = secrets.token_secret
if not token_secret or not token_secret.get_secret_value():
logger.error(
"TOKEN_SECRET is required but not set in .env. "
"Server will not start without a secret. "
"Please configure it and use 'python main.py trivialtoken' to generate an access token."
)
sys.exit(1)
jwt_handler = JWTHandler(
token_secret=token_secret.get_secret_value(),
algorithm=server_config.jwt_algorithm,
expire_hours=server_config.token_expire_hours,
)
logger.info("JWT authentication configured")
# Validate server_type
if server_type != "rest":
raise ValueError(f"Invalid server type: {server_type}")
# REST server uses lifespan for graceful shutdown
server = ServerFactory.create(
server_type=server_type, # type: ignore[arg-type]
server_config=server_config,
port=port,
jwt_handler=jwt_handler,
webui=webui,
)
logger.info(f"Starting {server_type.upper()} server on {server_config.host}:{port}")
FrameworkInterceptor.enforce()
await server.start()
def trivialtoken_command() -> None:
"""Generate a long-lived trivial JWT token for CLI usage."""
secrets = ConfigAccessor.get_secrets()
server_config = ConfigAccessor.get_server_config()
if not secrets.token_secret or not secrets.token_secret.get_secret_value():
logger.error(
"TOKEN_SECRET is not set or empty in secrets. Cannot generate trivial token."
)
sys.exit(1)
return
try:
jwt_handler = JWTHandler(
token_secret=secrets.token_secret.get_secret_value(),
algorithm=server_config.jwt_algorithm,
expire_hours=server_config.token_expire_hours,
)
token = jwt_handler.create_token(username="admin", custom_expire_hours=8760)
print(token)
except Exception as e:
logger.error(f"Failed to generate trivial token: {e}")
sys.exit(1)
def main() -> None:
parser = argparse.ArgumentParser(
description="My RAG Pipeline Orchestrator",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Required arguments for all commands:
--workspace NAME Logical name for the collection/namespace (prevents data corruption)
--max-async N Maximum concurrent API requests (I/O-bound limit)
--max-workers N Batch processing threads (CPU-bound limit)
Example usage:
python main.py index --path ./docs --workspace openwrt --max-async 8 --max-workers 4
python main.py query --question "How to configure VLAN?" --workspace openwrt --max-async 8 --max-workers 4
python main.py serve --type rest --port 8000 --workspace openwrt --max-async 8 --max-workers 4
python main.py check --workspace openwrt --max-async 8 --max-workers 4
""",
)
subparsers = parser.add_subparsers(dest="command", required=True)
# Common arguments for all commands
def add_common_args(subparser: argparse.ArgumentParser) -> None:
subparser.add_argument(
"--workspace",
type=str,
required=True,
help="Workspace name (logical namespace for data isolation in Qdrant/Neo4j)",
)
subparser.add_argument(
"--max-async",
type=int,
required=True,
help="Maximum concurrent API requests (prevents rate limiting)",
)
subparser.add_argument(
"--max-workers",
type=int,
required=True,
help="Batch processing threads (controls CPU-bound parallelism)",
)
index_parser = subparsers.add_parser("index", help="Index files or directories")
index_parser.add_argument(
"--path", required=True, help="Path to file or directory to index"
)
add_common_args(index_parser)
query_parser = subparsers.add_parser("query", help="Query the RAG system")
query_parser.add_argument("--question", required=True, help="Question to ask")
add_common_args(query_parser)
check_parser = subparsers.add_parser("check", help="Run preflight health checks")
check_parser.add_argument(
"--workspace", type=str, required=False, help="Workspace name"
)
check_parser.add_argument(
"--max-async", type=int, required=False, help="Maximum concurrent API requests"
)
check_parser.add_argument(
"--max-workers", type=int, required=False, help="Batch processing threads"
)
serve_parser = subparsers.add_parser("serve", help="Start the API server")
serve_parser.add_argument(
"--type",
choices=["rest"],
default="rest",
help="Server type: rest (FastAPI)",
)
serve_parser.add_argument(
"--port",
type=int,
required=True,
help="Server port (required)",
)
serve_parser.add_argument(
"--webui", action="store_true", help="Mount WebUI on the same port"
)
add_common_args(serve_parser)
# Trivial token subcommand
subparsers.add_parser(
"trivialtoken",
help="Generate a long-lived trivial JWT token",
description="Generate a long-lived trivial JWT token",
)
# Workspaces subcommand
workspaces_parser = subparsers.add_parser("workspaces", help="Manage workspaces")
workspaces_subparsers = workspaces_parser.add_subparsers(
dest="workspace_command", required=True
)
workspaces_subparsers.add_parser("list", help="List all workspaces")
delete_parser = workspaces_subparsers.add_parser(
"delete", help="Delete a workspace"
)
delete_parser.add_argument(
"--name", required=True, help="Name of the workspace to delete"
)
delete_parser.add_argument("--force", action="store_true", help="Confirm deletion")
args = parser.parse_args()
# Build overrides dictionary from CLI arguments
overrides: dict[str, str | int] = {}
if hasattr(args, "workspace") and args.workspace is not None:
overrides["workspace_name"] = args.workspace
if hasattr(args, "max_async") and args.max_async is not None:
overrides["max_async"] = args.max_async
if hasattr(args, "max_workers") and args.max_workers is not None:
overrides["max_workers"] = args.max_workers
if args.command == "check" or args.command == "workspaces":
overrides.setdefault("workspace_name", "default")
overrides.setdefault("max_async", 1)
overrides.setdefault("max_workers", 1)
# Load configuration with CLI overrides before any command execution
try:
ConfigAccessor.load(overrides=overrides)
update_logging_from_config(ConfigAccessor.get_logging_config())
except ValidationError as e:
logger.error(f"Configuration validation failed:\n{e}")
sys.exit(1)
try:
if args.command == "index":
asyncio.run(index_command(args.path))
elif args.command == "query":
asyncio.run(query_command(args.question))
elif args.command == "check":
asyncio.run(check_command())
elif args.command == "serve":
asyncio.run(
serve_command(args.type, args.port, getattr(args, "webui", False))
)
elif args.command == "trivialtoken":
trivialtoken_command()
elif args.command == "workspaces":
if args.workspace_command == "list":
try:
workspaces = asyncio.run(WorkspaceManager.list_workspaces())
if not workspaces:
print("No workspaces found.")
else:
print("Workspaces:")
for w in workspaces:
print(f"- {w}")
except Exception as e:
logger.error(f"Failed to list workspaces: {e}")
elif args.workspace_command == "delete":
try:
workspaces = asyncio.run(WorkspaceManager.list_workspaces())
if args.name not in workspaces:
logger.warning(
f"Workspace '{args.name}' not found in active workspaces."
)
sys.exit(0)
if not args.force:
user_input = input(
f"Are you sure you want to delete workspace '{args.name}'? (y/n): "
)
if user_input.strip().lower() != "y":
print("Deletion cancelled.")
sys.exit(0)
asyncio.run(WorkspaceManager.delete_workspace(args.name))
print(f"Workspace '{args.name}' deleted successfully.")
except Exception as e:
logger.error(f"Failed to delete workspace '{args.name}': {e}")
except KeyboardInterrupt:
logger.info("Application shutdown requested via KeyboardInterrupt")
try:
core_config = ConfigAccessor.get_core_config()
workspace = core_config.workspace_name
if workspace:
import shutil
shutil.rmtree(Path("output") / workspace, ignore_errors=True)
except Exception:
pass
sys.exit(0)
if __name__ == "__main__":
main()