Advanced RAG for Company Document Analysis

Annual-report Q&A with Docling table parsing, typed answer schemas and a rate-limited async scheduler.

LLM / RAG2025All work

Fig. 01PDF upload surface, reports parsed with Docling and TableFormer.
PDF upload surface, reports parsed with Docling and TableFormer.

What it is

What it does

Enterprise annual reports break naive RAG: multi-page tables, financial jargon and questions whose answers must be exact. This system parses PDFs with Docling, chunks pages, embeds into per-document FAISS indexes, and answers through strictly typed schemas, with a provider switch between OpenAI structured outputs and Gemini.

Why it exists

An investor once asked me 'what was EBITDA margin in FY21?' and a vanilla RAG gave a confident, wrong paragraph. After that I stopped trusting prose answers for numbers. I wanted tables parsed properly, answers that validate against a schema, and no guessing.

5
Typed schemas
3.5k
Requests/min cap
2
LLM providers

Repo Amit-kr26/Advanced_RAG_for_Company_Document_AnalysisDemo advanced-rag-for-company-document.onrender.com/Stack Python · Docling · FAISS · OpenAI · Gemini · Pydantic · FastAPI

Hosted on a free tier, so the demo may take about 60s to wake on first load.

Build8decisions

How it is built

Docling + TableFormer

PDFs parse in ProcessPoolExecutor workers; Docling's ACCURATE TableFormer mode recovers real table structure instead of scrambled cell text.

Table serialisation module

A dedicated path converts extracted tables into context-independent textual blocks (driven by gpt-4o-mini prompts) so tabular facts survive chunking.

Per-document FAISS

Pages chunk at 300 tokens / 50 overlap and embed with text-embedding-3-large into IndexFlatIP indexes. One index per report keeps retrieval scoped.

Five typed answer schemas

Questions route to name, number, boolean, names or comparative Pydantic schemas; number questions carry rules forbidding invented figures.

Two providers, one interface

OpenAI path uses beta.chat.completions.parse structured outputs; Gemini embeds the schema into the prompt and parses manually under the same send_message contract.

JSON self-repair loop

Malformed Gemini output goes through json_repair, then a repair round-trip with an AnswerSchemaFixPrompt prompt, retried via tenacity (3 attempts, 20s wait) before falling back to raw text.

Token-bucket scheduler

An asyncio parallel processor adapted from OpenAI's cookbook caps 3,500 requests/min and 3.5M tokens/min, estimates tokens with tiktoken (o200k_base), retries failures ×5 and persists results to JSONL with original_index for lossless reordering.

HTMX front-end

Server-rendered Tailwind + HTMX UI keeps the demo dependency-light.

How the pieces connect

Ingest, per document Index and answer Annual report PDF uploaded, 10 max Docling parse OCR + TableFormer Merge pages blocks + table markdown Chunk 300/50 tiktoken tokens Embed 3-large batches of 100 Question + schema 5 typed answers Vector retrieve top 10, all reports FAISS IndexFlatIP one index per report Context assembly untrusted, 24k budget GPT-4o structured Pydantic validated one FAISS index per report