Principle:AI4Finance Foundation FinRobot RAG QA
| Knowledge Sources | |
|---|---|
| Domains | Finance, NLP, RAG |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
A retrieval-augmented question-answering method that fetches SEC 10-K filings, chunks them into passages, retrieves the most relevant chunks via BM25 keyword ranking, and grounds an LLM answer in the actual regulatory disclosures with source citations.
Description
RAG QA addresses the problem that an LLM's parametric memory does not contain a company's specific SEC filings, and even if it did, it would hallucinate passage content. The method grounds the answer in the actual text of the filing, retrieved at a specific BM25 rank, so the LLM can cite specific sections (e.g. [Item 1A - Risk Factors]).
The method decomposes into:
- 10-K fetch: The SEC provider fetches the 10-K, chunks it into passages, and stores serializable chunk dicts (text, source, chunk_index, char_start) — NOT a live BM25Index (which cannot round-trip through the JSON cache).
- BM25 index rebuild: The index is rebuilt at runtime from the cached chunk dicts. BM25 tokenizes by whitespace, lowercases, and ranks by Okapi BM25 score. Chunks with score = 0 (no term overlap) are excluded.
- Context assembly + sanitization: Retrieved chunks are sanitized to neutralize prompt-injection vectors (fake XML tags, leading markdown headings, control chars) and wrapped in explicit <untrusted_filing_excerpt> blocks. Each excerpt carries a source label and relevance score.
- LLM answer: The LLM receives the sanitized context with a system prompt instructing it to answer ONLY from the excerpts and cite specific sections.
Why BM25 over embeddings: no 80MB model download, deterministic results, easily testable. Same interface as embedding-based retrieval — the upgrade path is straightforward.
Usage
Use this principle when:
- SEC filing Q&A: Answering questions about a company's 10-K risk factors, business description, MD&A, etc.
- Grounded analysis: When the answer must cite specific SEC sections rather than rely on parametric memory
Avoid when the question requires cross-company synthesis (BM25 is per-filing) or when no 10-K is available.
Theoretical Basis
BM25 Okapi score:
Where is the term frequency in the document, is the document length, and is the average document length.
Chunking: Text is split into fixed-size chunks (default 500 chars) with sentence-boundary awareness so no sentence is stranded at a boundary.
Sanitization: Every excerpt is flattened to remove escape vectors:
- Angle-bracket tags neutralized (cannot close the <untrusted> wrapper)
- Leading markdown headings removed (cannot pose as a prompt section)
- Control chars removed
- Length bounded
Pseudo-code:
# Abstract algorithm (NOT real implementation)
chunks = data_layer.fetch(DataType.RAG_10K, ticker).data["rag_chunks"]
index = BM25Index([Chunk(**c) for c in chunks])
results = index.search(question, top_k=8)
context = sanitize_and_wrap(results)
answer = llm.run(prompt=context + question, system=QA_SYSTEM_PROMPT)
Related Pages
Implemented By
- Implementation:AI4Finance_Foundation_FinRobot_BM25Index_search
- Implementation:AI4Finance_Foundation_FinRobot_sanitize_untrusted_block