Spaces:
Running on Zero
Running on Zero
File size: 6,792 Bytes
712c393 dda0d18 712c393 dda0d18 712c393 dda0d18 712c393 dda0d18 712c393 dda0d18 712c393 dda0d18 712c393 | 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 | from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from dotenv import load_dotenv
load_dotenv()
if sys.stdout and hasattr(sys.stdout, "reconfigure"):
try:
sys.stdout.reconfigure(encoding="utf-8")
except Exception:
pass
from .components.chunking import ChunkingPipeline
from .components.indexing import (
BM25Indexer,
DocumentStore,
IndexingPipeline,
QdrantIndexer,
SentenceTransformerEmbedder,
)
from .components.ingestion.config import IngestionConfig
from .components.ingestion.pipeline import IngestionPipeline as DocIngestionPipeline
from .components.orchestration import RAGOrchestrator
from .components.registry import SystemRegistry
def get_system():
embedder = SystemRegistry.get_embedder()
qdrant = SystemRegistry.get_qdrant()
bm25 = SystemRegistry.get_bm25()
doc_store = SystemRegistry.get_doc_store()
chunker = ChunkingPipeline()
indexing_pipe = IndexingPipeline(
embedder=embedder,
qdrant_indexer=qdrant,
bm25_indexer=bm25,
document_store=doc_store,
chunking_pipeline=chunker,
)
ingestion_pipe = DocIngestionPipeline(IngestionConfig())
orchestrator = RAGOrchestrator(
embedder=embedder,
qdrant_indexer=qdrant,
bm25_indexer=bm25,
document_store=doc_store,
)
return ingestion_pipe, indexing_pipe, orchestrator, qdrant, bm25, doc_store
def cmd_health(args):
_, _, _, qdrant, bm25, doc_store = get_system()
q_ok = qdrant.health_check()
docs = doc_store.list_documents()
status = {
"status": "healthy" if q_ok else "degraded",
"qdrant_connected": q_ok,
"indexed_documents": len(docs),
"qdrant_points": qdrant.count(),
"bm25_chunks": bm25.count(),
}
print(json.dumps(status, indent=2))
def cmd_ingest(args):
path = Path(args.path)
if not path.exists():
print(f"Error: Path does not exist: {path}", file=sys.stderr)
sys.exit(1)
ingestion_pipe, indexing_pipe, _, qdrant, bm25, doc_store = get_system()
if getattr(args, "rebuild_index", False):
print("Rebuilding indexes (clearing Qdrant collection, BM25, and DocumentStore)...")
qdrant.recreate_collection()
bm25.clear()
doc_store.clear()
if path.is_file():
files = [path]
else:
files = [p for p in path.rglob("*") if p.is_file()]
print(f"Ingesting {len(files)} files from {path}...")
ingest_result = ingestion_pipe.ingest_many(files, strict=False)
print(f"Parsed {len(ingest_result.documents)} documents. Indexing...")
stats = indexing_pipe.index_documents(
ingest_result.documents,
force_reindex=args.force or getattr(args, "rebuild_index", False),
)
print(
f"Indexed {stats.documents_indexed} documents ({stats.chunks_indexed} chunks) "
f"in {stats.duration_ms:.1f}ms. Skipped: {stats.documents_skipped}."
)
if stats.errors:
print(f"Errors encountered: {len(stats.errors)}")
for err in stats.errors:
print(f" - {err}")
def cmd_search(args):
_, _, orchestrator, _, bm25, _ = get_system()
print(f"Searching for: '{args.query}' (strategy: {args.strategy})...")
if args.strategy == "keyword":
hits = bm25.search(args.query, top_k=args.top_k)
for h in hits:
print(f"[{h.rank}] score={h.score:.3f} | {h.source_locator} | {h.text[:120]}...")
elif args.strategy == "dense":
resp = orchestrator.dense_retriever.retrieve(args.query, top_k=args.top_k)
for h in resp.results:
print(f"[{h.rank}] score={h.score:.3f} | {h.source_locator} | {h.text[:120]}...")
else:
# Hybrid
hits = bm25.search(args.query, top_k=args.top_k)
for h in hits:
print(f"[{h.rank}] score={h.score:.3f} | {h.source_locator} | {h.text[:120]}...")
def cmd_query(args):
_, _, orchestrator, _, _, _ = get_system()
mode_str = f" [mode: {args.mode}]" if getattr(args, "mode", None) else ""
print(f"Executing RAG query: '{args.query}'{mode_str}...")
res = orchestrator.query(args.query, mode=getattr(args, "mode", None))
print("-" * 60)
print(f"QUERY TYPE : {res.query_type}")
print(f"EXEC MODE : {res.execution_mode}")
print(f"CONFIDENCE : {res.confidence:.2f}")
print(f"ABSTAINED : {res.abstained}")
if res.abstained:
print(f"REASON : {res.abstention_reason}")
print(f"LATENCY : {res.latency_ms:.1f}ms")
print("-" * 60)
print("ANSWER:")
print(res.answer)
print("-" * 60)
if res.sources:
print(f"SOURCES ({len(res.sources)}):")
for s in res.sources:
print(f" [{s.get('citation_id')}] {s.get('filename')} (locator: {s.get('source_locator')})")
print(f" \"{s.get('snippet', '')}...\"")
def main():
parser = argparse.ArgumentParser(
description="Nexus - Production-Grade RAG CLI"
)
subparsers = parser.add_subparsers(dest="command", required=True)
# health
subparsers.add_parser("health", help="Check system and index health")
# ingest
p_ingest = subparsers.add_parser("ingest", help="Ingest and index documents")
p_ingest.add_argument("path", help="Path to document file or directory")
p_ingest.add_argument(
"--force", action="store_true", help="Force reindexing existing documents"
)
p_ingest.add_argument(
"--rebuild-index",
action="store_true",
help="Rebuild Qdrant collection, BM25, and metadata matching active embedder dimension",
)
# search
p_search = subparsers.add_parser("search", help="Execute vector/keyword search")
p_search.add_argument("query", help="Search query")
p_search.add_argument(
"--strategy", choices=["hybrid", "dense", "keyword"], default="hybrid"
)
p_search.add_argument("--top-k", type=int, default=5)
# query
p_query = subparsers.add_parser("query", help="Execute grounded RAG query")
p_query.add_argument("query", help="Natural language question")
p_query.add_argument(
"--mode", choices=["fast", "balanced", "deep"], default=None, help="Execution mode (fast, balanced, deep)"
)
args = parser.parse_args()
if args.command == "health":
cmd_health(args)
elif args.command == "ingest":
cmd_ingest(args)
elif args.command == "search":
cmd_search(args)
elif args.command == "query":
cmd_query(args)
if __name__ == "__main__":
main()
|