Watch the full 6-Layer Architecture in action here
Analyzing financial SEC filings (10-K and 10-Q) presents a major challenge for standard Retrieval-Augmented Generation (RAG) models. SEC reports are characterized by complex tables, scattered financial footnotes, legal disclosures, and highly specific year-over-year comparatives.
Warning
Naive, vector-only RAG systems suffer from high retrieval loss on tabular data and are highly prone to severe hallucinations under missing data scenarios.
Traditional, naive RAG systems suffer from three primary failures:
- Retrieval Loss: Missing context in long tables when using pure vector similarity.
- Financial Hallucinations: Making up numerical metrics or substituting a competitor's numbers when data is missing.
- High Latency/Cost: Relying on heavy local hardware or expensive closed models with proprietary APIs.
The SVS Finance RAG Copilot is a production-grade, highly optimized solution engineered to overcome these limitations. By integrating a multi-agent orchestration model, cloud-native hybrid retrieval, deterministic mathematical guardrails, and real-time audit telemetry, this copilot acts as a zero-hallucination assistant for financial analysts, trend-trackers, and risk auditors.
28 SEC PDFs (Amazon, Apple, Google, Meta)
β
[Layer 1] Ingestion (Colab T4 GPU Pipeline)
Docling β Complex Tables & Visuals Extraction (flattened to row-level)
PyMuPDF β Smart Narrative Chunking (1200 chars / 200 overlap)
β
[Layer 2] In-Memory Semantic Cache (app.py)
Embed query via sentence-transformers (Cosine Sim >= 0.92)
If Hit β Return JSON Response instantly (0ms LLM latency)
β
[Layer 3] Intent Planning & Decomposition (agents.py)
Groq LLM classifies query
If Complex/Multi-Hop β Decomposition Agent splits into parallel sub-queries
β
[Layer 4] Parallel Hybrid Retrieval (retriever.py)
ThreadPoolExecutor β N concurrent searches against Qdrant
Dense Search + Sparse Search (BM25)
Deduplication & Fusion via Reciprocal Rank Fusion (RRF)
β
[Layer 5] Re-Ranking & Fallback (retriever.py)
CrossEncoder (ms-marco-MiniLM) re-scores fused context
β
[Layer 6] Generation (agents.py)
Groq LLM writes strict dual-answer JSON (Long & Short) with citations
β
[Layer 7] Evaluation (evaluator.py)
Faithfulness Β· Groundness Β· Hallucination Rate Β· Citation Accuracy
β
[Layer 8] Guardrail UI Rendering (app.py)
5-State Conditional Degradation, Plotly Chart Injection, Dual-Pane Layout
The Copilot UI has been meticulously polished to deliver an enterprise-grade experience:
- Mermaid Diagram Sandbox & Rendering: A dedicated split-screen sandbox to generate and preview Mermaid architecture graphs. The Copilot also natively intercepts and renders LLM-generated Mermaid markdown live in the chat.
- Smart Model Routing Transparency: The sidebar dynamically reflects the exact LLM driving each agentβsaving tokens by allocating single-company Q&A to the lightning-fast Groq Llama 3.1 8B, while reserving Llama 3.3 70B for heavy multi-company or trend analysis.
- Robust JSON Extraction (Trend Agent): Replaced fragile LangChain structured output wrappers with resilient
json-repairand explicit schema prompting, guaranteeing deterministic chart rendering for complex SEC trend tables. - Nested Source Inspector: Clean, collapsible
<details>chunks inside a single Citations expander for an ultra-compact, click-to-read audit trail of exact SEC passages. - Persistent State & Long/Short Toggles: Quality score and source expanders perfectly persist across chat history. Each AI response includes a "Long/Short" toggle for instant detail collapse.
- Dedicated Audit Logs Page: Centralized SQLite audit logging moved from a tab to a dedicated full-page view accessible from the sidebar.
SVS Praveen/
β
βββ app.py β Streamlit dashboard (main UI β run this)
βββ agents.py β Multi-agent orchestration (4 agents + router)
βββ retriever.py β Hybrid retrieval engine (Dense + BM25 + RRF)
βββ ingestion.py β PDF ingestion pipeline (use to add new PDFs)
βββ evaluator.py β Deterministic evaluation (no LLM judge)
βββ database.py β PostgreSQL (Neon DB) audit logging
βββ config.py β Central config β all settings from .env
βββ deep_audit.py β Verify Qdrant Cloud data (diagnostic tool)
β
βββ .env β API keys (never commit this!)
βββ requirements.txt β All dependencies
β
βββ pdfs/ β Drop new SEC filing PDFs here to ingest
βββ logs/ β pipeline.log lives here
βββ data/ β Reserved for local data files
βββ logs/ β pipeline.log lives here
βββ bm25_index.pkl β Local BM25 index cache (auto-built on ingest)
βββ ingestion_checkpoint.pkl β Tracks which PDFs are already ingested
β
βββ study/ β Study materials & reference docs (not needed to run)
| Agent | Triggered When | Prompt Style |
|---|---|---|
| Financial Analyst | Single company Q&A | Detailed text JSON |
| Comparison Agent | 2+ companies or "compare" keyword | Markdown table JSON |
| Decomposition Agent | Complex multi-hop or multi-company query | Sub-query Arrays |
| Risk Analyzer | Risk / threat / factor queries | Bullet points JSON |
| Trend Agent | Multi-year or "trend" queries | Data Table JSON |
| Guardrail System | Non-finance query detected | Hard Block (String) |
To ensure absolute reliability in financial data, the SVS Finance Copilot implements advanced Self-Reflective RAG (Self-RAG) mechanisms. Unlike naive RAG systems that blindly retrieve and generate, this architecture continuously evaluates and reflects on its own processes across three distinct stages:
-
Pre-Retrieval Intent Reflection: The system doesn't just search the user's raw prompt. The Intent Planner Agent reflects on the query to understand its true goal (e.g., Q&A vs. multi-company comparison vs. trend analysis). It actively extracts rigid metadata filters (Company, Year, Quarter) and rewrites the query, significantly narrowing the retrieval scope.
-
Contextual Relevance Reflection (Reranking): Once chunks are retrieved from Qdrant, the system self-evaluates the relevance of the retrieved data. Using the CrossEncoder, it scores how well the chunks actually answer the rewritten query, discarding weak or unrelated data.
-
Deterministic Output Self-Evaluation: Instead of using an expensive LLM-as-a-judge which can introduce bias or its own hallucinations, the system runs a zero-cost mathematical evaluator on its final output. It grades its own response in real-time:
-
Faithfulness: Uses
RapidFuzzto ensure generated sentences are directly grounded in the retrieved text. -
Groundness: Verifies that generated financial numbers fall within a strict
$1%$ mathematical tolerance of the source documents. If numbers fail verification, they are explicitly flagged. -
Citation Accuracy: Ensures that all inline citations (e.g.,
[1]) correctly point to the retrieved SEC index.
-
Faithfulness: Uses
-
5-State Visual Guardrails (UI & Routing): The system visually and logically categorizes its own responses to guarantee user safety:
- π’ State 1 (Green): High-confidence, fully grounded answer with perfect citations.
-
π‘ State 2 (Yellow): Partial verification. Visually flags specific numerical figures that failed the
Groundnesscheck in a warning blockquote. -
π‘ State 3 (Yellow): Low confidence (
overall_quality < 50%). Displays a top-level banner warning the user to verify manually. - π΄ State 4 (Red): Out of Scope. Immediately blocks the response with an exact security message if asked about unsupported companies or unrelated topics.
- π΄ State 5 (Red): System Failure. If the Qdrant DB or Groq LLM crashes/timeouts, the app catches the exception gracefully and returns direct links to the SEC EDGAR company portals for manual searching.
Important
A running PostgreSQL instance is required for logging audit trails. Make sure to provide a valid connection URL via POSTGRES_URL. We recommend using Neon PostgreSQL (Serverless).
Create a .env file in the root directory:
GROQ_API_KEY=your_groq_key_here
QDRANT_URL=https://your-cluster.qdrant.io
QDRANT_API_KEY=your_qdrant_key_here
QDRANT_COLLECTION=praveen_rag_json
GROQ_LLM_MODEL=llama-3.3-70b-versatile
GROQ_PLANNER_MODEL=llama-3.3-70b-versatile
EMBED_MODEL=sentence-transformers/all-MiniLM-L6-v2
POSTGRES_URL=postgresql://user:password@host:port/dbname?sslmode=require
pip install -r requirements.txtstreamlit run app.pyDrop PDFs into the pdfs/ folder, then:
python ingestion.pyThe checkpoint system skips already-ingested files automatically.
python deep_audit.pyAll metrics are deterministic β no LLM judge, no randomness, zero extra cost.
| Metric | What It Measures | Weight |
|---|---|---|
| Faithfulness | % of response sentences supported by context | 35% |
| Groundness | % of financial numbers verified in context | 30% |
| Hallucination Rate | % of claims NOT found in context (lower = better) | 20% |
| Citation Accuracy | % of [N] references that match real source docs | 15% |
Grade scale: π’ Excellent (β₯85%) Β· π‘ Good (β₯70%) Β· π Acceptable (β₯50%) Β· π΄ Needs Review (<50%)
| Component | Technology | Why Chosen |
|---|---|---|
| LLM | Groq (Llama 3.3-70b-versatile) | Free API, fast inference, cloud |
| Vector DB | Qdrant Cloud | Free tier, persistent, hybrid search |
| Embeddings | all-MiniLM-L6-v2 (HuggingFace) | 90MB, CPU-only, 384-dim, accurate |
| PDF Parsing | PyMuPDF | Fast, handles damaged/scanned pages |
| Web UI | Streamlit | Rapid prototyping, no frontend code |
| Audit DB | PostgreSQL (Neon) | Cloud-hosted database for persistent scalable logging |
| Evaluation | RapidFuzz | Fast fuzzy matching, no LLM needed |
Author: SVS Praveen Β· Architecture: 6-Layer Financial RAG Β· Status: Production Ready