File size: 11,192 Bytes
24f7855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
030e7a9
24f7855
030e7a9
 
 
24f7855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
030e7a9
24f7855
 
 
 
 
 
 
 
 
 
 
 
 
 
030e7a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24f7855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7820a
24f7855
 
7e7820a
24f7855
 
 
 
 
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
import os
import sys
import time
from pathlib import Path
from contextlib import asynccontextmanager

from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException, status, Request
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from starlette.concurrency import run_in_threadpool
import numpy as np
import tiktoken

# Ensure workspace root is in sys.path
PROJECT_ROOT = Path(__file__).resolve().parent.parent
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

load_dotenv(PROJECT_ROOT / ".env")

# Project Core Modules
from chunker.recursive_character_chunker import recursive_splitters
from Embedding.bge_embeddings import BGEEmbedding
from storage.Qdrant import QdrantStore
from storage.BM25 import BM25Store
from query_processing.query_classification import QueryClassifier
from query_processing.query_decomposer import Query_decomposer
from query_processing.query_expander import QueryExpander
from reranker.base import get_reranker
from generation.prompt_builder import PromptBuilder
from generation.llm_provider import GeminiProvider
from generation.citation_Extractor import CitationExtractor

# CRAG Core Engine Modules
from CRAG.orchestrate import CRAGOrchestrator
from CRAG.source_attributor import (
    SourceAttributor,
    CRAG_SYSTEM_PROMPT,
    CRAG_USER_PROMPT_TEMPLATE,
)

# API Schemas & Service
from api.schemas import (
    QueryRequest,
    GlassBoxQueryResponse,
    HealthResponse,
    SystemInfoResponse,
)
from api.service import run_glassbox_crag_pipeline

# File Paths
DATA_DIR = PROJECT_ROOT / "data"
CACHE_FILE = DATA_DIR / "aapl_clean_text.txt"
VECTORS_CACHE_FILE = DATA_DIR / "aapl_bge_vectors.npy"


def init_pipeline_context():
    """
    Initializes and warms up all heavy ML assets:
    BGE embeddings (CPU), Qdrant in-memory, BM25, CrossEncoder (CPU), CRAG Orchestrator, LLM provider.
    Returns (pipeline_context, total_chunks).
    """
    if not CACHE_FILE.exists():
        raise FileNotFoundError(f"Required corpus file missing: {CACHE_FILE}")

    with open(CACHE_FILE, "r", encoding="utf-8") as f:
        clean_text = f.read()

    docs = recursive_splitters(clean_text, chunk_size=1000, chunk_overlap=100)
    for idx, d in enumerate(docs):
        d.metadata["chunk_id"] = f"chunk_{idx:03d}"
        d.metadata["source"] = "aapl-20250927.htm"
        d.metadata["ticker"] = "AAPL"
        d.metadata["fiscal_year"] = "2025"
    print(f"[+] Loaded and indexed {len(docs)} chunks from SEC 10-K filing.", flush=True)

    embedder = BGEEmbedding()
    if VECTORS_CACHE_FILE.exists():
        print(f"[*] Loading precomputed embeddings from cache: {VECTORS_CACHE_FILE}", flush=True)
        cached_vectors = np.load(VECTORS_CACHE_FILE)
        vectors = cached_vectors.tolist()
    else:
        print("[*] Generating BGE embeddings...", flush=True)
        chunk_texts = [d.page_content for d in docs]
        vectors = embedder.embed_documents(chunk_texts)
        np.save(VECTORS_CACHE_FILE, np.array(vectors, dtype=np.float32))

    qdrant = QdrantStore(collection_name="aapl_production", vector_size=embedder.dimension, location=":memory:")
    qdrant.upsert_documents(documents=docs, vectors=vectors)

    bm25 = BM25Store()
    bm25.index_documents(documents=docs)

    classifier = QueryClassifier(model_name="gemini-3.5-flash-lite")
    decomposer = Query_decomposer(model_name="gemini-3.5-flash-lite")
    expander = QueryExpander(model_name="gemini-3.5-flash-lite")

    reranker = get_reranker("local")
    crag_orchestrator = CRAGOrchestrator(reranker=reranker)

    prompt_builder = PromptBuilder(
        system_prompt=CRAG_SYSTEM_PROMPT,
        user_template=CRAG_USER_PROMPT_TEMPLATE,
    )
    gemini_llm = GeminiProvider(model_name="gemini-3.5-flash-lite")
    citation_extractor = CitationExtractor()
    source_attributor = SourceAttributor()
    tokenizer = tiktoken.get_encoding("cl100k_base")

    pipeline_context = {
        "embedder": embedder,
        "qdrant": qdrant,
        "bm25": bm25,
        "classifier": classifier,
        "decomposer": decomposer,
        "expander": expander,
        "reranker": reranker,
        "crag_orchestrator": crag_orchestrator,
        "prompt_builder": prompt_builder,
        "gemini_llm": gemini_llm,
        "citation_extractor": citation_extractor,
        "source_attributor": source_attributor,
        "tokenizer": tokenizer,
    }
    return pipeline_context, len(docs)


@asynccontextmanager
async def lifespan(app: FastAPI):
    """
    ASGI Lifespan Manager:
    Executes once on server startup to warm up and load all heavy ML assets
    into global application memory (app.state), avoiding per-request initialization.
    """
    print("=" * 80, flush=True)
    print("[*] FASTAPI STARTUP: Initializing Enterprise CRAG Intelligence Engine...", flush=True)
    print("=" * 80, flush=True)

    app.state.start_time = time.time()
    t_boot_0 = time.perf_counter()

    ctx, total_chunks = init_pipeline_context()
    app.state.pipeline_context = ctx
    app.state.total_chunks = total_chunks

    t_boot_sec = time.perf_counter() - t_boot_0
    print(f"[+] All models, stores, and CRAG chains initialized in {t_boot_sec:.2f}s.", flush=True)
    print("[+] Server ready to accept incoming production queries!", flush=True)

    yield

    # Shutdown logic
    print("[*] FASTAPI SHUTDOWN: Releasing resources and clearing caches...", flush=True)
    app.state.pipeline_context.clear()
    print("[+] Shutdown complete.", flush=True)


app = FastAPI(
    title="Enterprise Corrective RAG (CRAG) Intelligence API",
    description=(
        "Production financial intelligence backend serving Apple Inc. Form 10-K (FY 2025) disclosures. "
        "Implements hybrid dense/sparse retrieval, Reciprocal Rank Fusion, Cross-Encoder reranking, "
        "and a 3-state Corrective RAG (CRAG) self-evaluating engine with Tavily web search fallback."
    ),
    version="1.0.0",
    lifespan=lifespan,
)

# Enable CORS for external frontends (Streamlit, React, Next.js, Vite)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


# ==============================================================================
# ROUTE HANDLERS
# ==============================================================================

@app.get(
    "/health",
    response_model=HealthResponse,
    tags=["System Health"],
    summary="Health check probe for container orchestrators"
)
async def health_check(request: Request):
    """
    Returns server health, uptime, and index readiness.
    Used by Kubernetes, AWS ECS, or Render health probes.
    """
    uptime = time.time() - getattr(request.app.state, "start_time", time.time())
    models_ready = bool(getattr(request.app.state, "pipeline_context", None))
    total_chunks = getattr(request.app.state, "total_chunks", 0)

    return HealthResponse(
        status="healthy" if models_ready else "initializing",
        target_document="Apple Inc. Form 10-K (FY 2025)",
        total_indexed_chunks=total_chunks,
        dense_vector_dim=768,
        models_ready=models_ready,
        uptime_seconds=round(uptime, 2),
    )


@app.get(
    "/api/v1/info",
    response_model=SystemInfoResponse,
    tags=["System Info"],
    summary="Detailed architecture specifications and benchmark achievements"
)
async def system_info():
    """
    Returns comprehensive metadata about the underlying CRAG architecture,
    dual-threshold parameters, and historical benchmark milestones.
    """
    return SystemInfoResponse(
        system_name="Enterprise Corrective RAG (CRAG) Intelligence API",
        version="1.0.0",
        target_corpus="Apple Inc. Form 10-K (FY 2025)",
        architecture={
            "retrieval_dense": "BAAI/bge-base-en-v1.5 (768-dim, cosine distance, Qdrant in-memory)",
            "retrieval_sparse": "BM25Okapi (regex alphanumeric tokenizer, in-memory index)",
            "hybrid_fusion": "Reciprocal Rank Fusion (RRF, k=60, top-20 candidates)",
            "initial_reranker": "cross-encoder/ms-marco-MiniLM-L6-v2 (top-5 candidate selection)",
            "crag_evaluator": "google/gemma-4-31b-it (pointwise score via Requesty AI Router)",
            "crag_routing_thresholds": {"upper_T_H": 0.7, "lower_T_L": 0.3},
            "crag_knowledge_refinement": "Sentence tokenizer -> 2-sentence strips -> CrossEncoder strip filter",
            "crag_web_fallback": "QueryRewriter LCEL chain -> Tavily Search API",
            "generator": "Google Gemini 3.5 Flash-Lite (temperature=0.0)",
            "attribution_verification": "Dual-source citation auditor ([DOC:chunk_id] vs [WEB:url])"
        },
        benchmarks_summary={
            "latest_test_id": "TEST_008_FINAL_ENTERPRISE_CRAG_V1_PIPELINE",
            "evaluated_queries": 85,
            "mean_answer_correctness": 0.8860,
            "mean_numerical_accuracy": 0.9339,
            "web_search_fallback_rate": "16.5%",
            "out_of_corpus_attribution_accuracy": "100.0%",
            "pipeline_error_rate": "0.0%"
        }
    )


@app.post(
    "/api/v1/query",
    response_model=GlassBoxQueryResponse,
    tags=["Financial Intelligence Query"],
    summary="Run query through the complete Glass-Box CRAG pipeline"
)
async def query_crag_pipeline(
    query_request: QueryRequest,
    request: Request,
):
    """
    Main entry point for user financial queries.
    Returns the final synthesized answer along with a full 'Glass-Box' audit:
    - Stage 1: Query classification & decomposition
    - Stage 2: Qdrant vs. BM25 top candidates
    - Stage 3: RRF fusion & Cross-Encoder initial ranks
    - Stage 4: CRAG evaluation scores, routing decision, strip refinement / web fallback
    - Stage 5: Generation context & token cost
    - Stage 6: Attribution accuracy and hallucinated citation verification
    """
    pipeline_context = getattr(request.app.state, "pipeline_context", None)
    if not pipeline_context:
        raise HTTPException(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            detail="Pipeline models are still initializing. Please retry in a few seconds.",
        )

    try:
        # Offload CPU-bound inference (BGE, Cross-Encoder) to worker thread pool
        # to ensure the asyncio event loop remains non-blocking for other clients.
        response = await run_in_threadpool(
            run_glassbox_crag_pipeline,
            request=query_request,
            pipeline_context=pipeline_context,
        )
        return response
    except Exception as e:
        import traceback
        traceback.print_exc()
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Pipeline execution error: {str(e)}",
        )


# Mount static frontend assets at /studio
FRONTEND_DIR = PROJECT_ROOT / "frontend"
if FRONTEND_DIR.exists():
    app.mount("/studio", StaticFiles(directory=str(FRONTEND_DIR), html=True), name="frontend")


if __name__ == "__main__":
    import uvicorn
    uvicorn.run("api.main:app", host="0.0.0.0", port=8000, reload=True)