“Hey Vinh, I just asked our newly minted AI bot: ‘What is our company’s registered charter capital and shareholder breakdown for this year?’. The bot cheerfully cited a balance sheet from… three years ago on page 14, with 100% unwavering confidence! If we take this hallucinating bot into our executive board demo tomorrow, HR will have our resignation letters ready by lunch!”

That was the frantic Monday morning call I received during the first internal trial of our RAG bot on a massive, 200-page technical engineering & ESG report.

If you have ever browsed YouTube or read standard “instant noodle” RAG tutorials, building an AI document assistant looks as effortless as boiling water:

$$\text{PDF Document} \longrightarrow \text{LangChain Splitter} \longrightarrow \text{ChromaDB / FAISS} \longrightarrow \text{Prompt ChatGPT}$$

Those 5-minute tutorials always test on a clean, 3-sentence fairy tale about Snow White and the Seven Dwarfs. Everything looks silky smooth. But the instant you throw that naive recipe at a real-world enterprise document — packed with multi-column financial statements, physicochemical water parameter tables, and bureaucratic sentences spanning half a page — you collide with an uncomfortable truth:

PDF was invented by Adobe in 1993 to print ink on dead trees, NOT for AI to parse! And mechanical fixed-size chunking is the single greatest catalyst of AI hallucinations!

Today, I want to share the candid story of how we tamed those hallucinations, designed a Dual-Collection architecture on Qdrant, benchmarked Vietnamese embedding models, and how you can clone and run this production-grade PDF-RAG pipeline directly from our open-source repository vinh-gogo/pdf-rag.


1. The Three Traps That Turn Vanilla RAG Into Hallucination Machines

Why do cookie-cutter RAG setups faceplant when challenged by 200-page technical documents? After hundreds of debug sessions, we unmasked three fundamental culprits:

┌────────────────────────────────────────────────────────┐
│             THE 3 FATAL FLAWS OF VANILLA RAG           │
├──────────────────────────┬─────────────────────────────┤
│ 1. Guillotine Chunker    │ Slices sentences & numbers   │
│ 2. Macro Blindness       │ Has details, misses context │
│ 3. The Table Abyss       │ Scrambled rows & noisy garbage│
└──────────────────────────┴─────────────────────────────┘

🔪 Trap 1: The “Guillotine Chunker” (The Broken Sentence Syndrome)

Standard text splitters (like RecursiveCharacterTextSplitter) behave like blind guillotines: the moment text hits chunk_size = 500 characters, the blade drops!

Consider how this guillotine slices an audited balance sheet:

[Chunk 1]: ...The actual paid-in charter capital as of December 31 stands at:
----------------------- [THE CHUNKER'S CRUEL BLADE] -----------------------
[Chunk 2]: 2,199,191,000,000 VND, approved under Resolution No. 04 of...

What is the fallout?

  • Chunk 1 contains the question entity but zero numbers.
  • Chunk 2 contains the number but has zero clue whether it represents charter capital, gross operating revenue, or unpaid bad debt!
  • When a user queries charter capital, vector search retrieves Chunk 1. The LLM faces an incomplete sentence and activates its creative fiction mode: hallucinating an imaginary number with complete swagger.

🙈 Trap 2: Macro-Context Blindness (“Blind Men and an Elephant”)

Suppose vector search locates a surgically precise engineering excerpt:

“Treatment removal efficiency achieved 98.5% under operating temperatures between 28°C and 32°C.”

Can you (or your LLM) tell:

  • Does this pertain to the biological membrane filter, the anaerobic UASB digester, or the physicochemical clarifier?
  • Is it measuring Plant Unit A or Treatment Station B?
  • Was this measured in the 2024 compliance audit or was it a preliminary projection back in 2020?

When chopped into 300-word isolated islands, paragraphs lose all linkage to chapter headings, section titles, and footnote caveats. The bot reports the number 98.5% accurately, but attributes it to the completely wrong facility!

🌪️ Trap 3: The PDF Table Abyss

Tables in PDF files are essentially collections of graphical coordinate lines and loosely floating text strings. When extracted blindly, they mutate into character soup: column headers land in one chunk, row values in another, and footnotes get lost in the void. Add repeating running headers and page numbers, and your vector embeddings get flooded with semantic noise.


2. The Solution: Dual-Collection Architecture on Qdrant

To permanently solve these three flaws, we stopped forcing all text into a single flat bucket. Instead, we designed a Dual-Collection (Two-Tier) storage architecture powered by the Qdrant vector engine:

flowchart TD
    subgraph INGESTION["1. Ingestion Pipeline (PyMuPDF GPU Accelerated)"]
        PDF["Technical PDF Document (200 pages)"] --> FITZ["PyMuPDF (fitz) + CUDA Acceleration"]
        FITZ -->|"Clean Page Splitting"| PAGES["src/data/pdfs/pages (1..N)"]
        FITZ -->|"Text Sanitizer & Stopwords"| CLEAN["Cleaned Text Engine"]
    end

    subgraph DUAL_STORE["2. Dual Vector Store (Qdrant Vector DB)"]
        CLEAN -->|"Full Page Storage"| COL_PAGES[("Collection: esg_pages<br/>🎥 Macro: 360° Drone View")]
        CLEAN -->|"Natural Paragraph Chunking"| SEQUENCES["Natural Semantic Sequences<br/>(min_words = 10)"]
        SEQUENCES -->|"Store Sequence Vectors"| COL_SEQS[("Collection: esg_sequences<br/>🔍 Micro: Sherlock's Lens")]
        SEQUENCES -->|"Keyword Indexing"| BM25["BM25 Lexical Index"]
    end

    subgraph RETRIEVAL["3. Hierarchical Retrieval Engine"]
        USER["User Query"] --> EMB["Local Embedding Model"]
        EMB --> DENSE["Vector Search on esg_sequences"]
        USER --> BM25_SEARCH["BM25 Keyword Search"]
        DENSE --> FUSION["Hybrid Fusion (Dense + Sparse)"]
        BM25_SEARCH --> FUSION
        FUSION --> TOP_PAGES["Aggregate & Identify Top Page Indices"]
        TOP_PAGES --> GATHER["Fetch ENTIRE Content of Target Pages"]
    end

    subgraph SERVING["4. Generation & SSE Streaming"]
        GATHER --> PROMPT["Assemble Prompt with Full Page Context"]
        PROMPT --> LLM["LLM Engine"]
        LLM --> FASTAPI["FastAPI Backend (SSE Stream)"]
        FASTAPI --> UI["Next.js Modern Chat UI"]
    end

How Do the Two Collections Cooperate?

Think of it like a detective investigation:

  1. esg_pages (Macro Tier — The Overhead Drone Camera):

    • Payload: Entire text of each standalone page (Page 1 through Page 200).
    • Mission: Acts like a drone camera capturing the 30,000-foot perspective: chapter headings, table legends, and footer disclaimers.
    • Metadata: page_index, word_count, content.
  2. esg_sequences (Micro Tier — Sherlock Holmes’s Magnifying Glass):

    • Payload: Paragraphs partitioned by natural linguistic boundaries (punctuation, double line breaks \n\n), strictly filtering fragments with fewer than 10 words (min_words = 10).
    • Mission: Pinpoints specific sentences, exact numerical parameters, and granular definitions.
    • Metadata: page_index, seq_index, seq_id, word_count, content.

The Core Technique: “Catch the Culprit, Bring in the Whole Family” (Hierarchical Page Aggregation)

When a user asks a complex question, the system does not lazily pass 2–3 disjointed snippets to the LLM:

  • Step 1: The query searches against the micro esg_sequences collection combined with BM25 keyword matching.
  • Step 2: From the top-scoring sequence chunks, the engine extracts their parent page_index.
  • Step 3: The engine pulls the entire context of those parent pages from storage!
  • Step 4: The full page context is injected into the LLM prompt.

Now, the LLM reads the specific metric on line 5, the table header on line 1, and the unit footnote at the bottom. Hallucinations vanish instantly!


3. The Heavyweight Bout: Three Vietnamese Embedding Contenders

No RAG system survives without a resilient embedding engine. Inside vinh-gogo/pdf-rag, in the src/eval/ directory, we hosted a head-to-head showdown between three prominent embedding models:

  1. 🥊 DangVanTuan/vietnamese-embedding: A veteran RoBERTa model fine-tuned specifically for Vietnamese.
  2. 🥊 BKAI HaLong-Embedding: The academic heavyweight from Hanoi University of Science and Technology.
  3. 🥊 Qwen3-Embedding-0.6B: The multilingual next-gen transformer from Alibaba Cloud.

All three models evaluated across the full 200-page technical corpus, measuring Cosine Similarity between raw text and sanitized text to test stability against formatting noise.

Here is the empirical scorecard extracted directly from src/eval/comprehensive_results.csv:

Embedding FighterMean Cosine (Higher is better)Std Dev (Lower is more stable)Min Score (Toughest Page)Max ScoreMedian
DangVanTuan0.99280.01420.9152 (Page 154)1.00000.9998
Qwen3-Embedding0.99150.01180.9469 (Page 154)1.00000.9989
BKAI HaLong0.99020.01560.9526 (Page 154)1.00000.9982
xychart-beta
    title "Cosine Similarity Distribution Across Embedding Models"
    x-axis ["DangVanTuan", "Qwen3-Embedding", "BKAI HaLong"]
    y-axis "Cosine Similarity" 0.985 --> 0.995
    bar [0.9928, 0.9915, 0.9902]

Key Takeaways from the Ring:

  • 🥇 DangVanTuan: Took gold in overall average semantic smoothness (0.9928), capturing native Vietnamese grammatical nuance exceptionally well.
  • 🛡️ Qwen3-Embedding: Clocked the lowest standard deviation (0.0118) and the highest minimum floor (0.9469). Qwen3 proved virtually unflappable when encountering hybrid English-Vietnamese technical acronyms (COD, BOD, EBITDA, CAPEX).
  • 👻 The Infamous “Page 154”: Every model experienced a sudden dip on Page 154. Inspecting the page revealed why: it was an appendix table jam-packed with chemical monitoring values and zero prose sentences! This proved a vital lesson: Dense vector search alone is vulnerable; you must pair it with BM25 Lexical Search for numerical and acronym queries!

4. Hands-On Guide: Build Your Own PDF-RAG From Scratch

Ready to run this architecture on your local machine or server in under 10 minutes? Here is the step-by-step blueprint:

🛠️ Prerequisites

  • Python 3.10+, Docker, and Git.
  • An NVIDIA GPU with CUDA is recommended for blazing-fast extraction (< 3 seconds for 200 pages), though CPU mode works fine too.

Step 1: Clone the Repo & Launch Qdrant Vector DB

Clone the codebase:

git clone https://github.com/vinh-gogo/pdf-rag.git
cd pdf-rag

Spin up Qdrant Vector Database on port 6333 with one Docker command:

docker run -d -p 6333:6333 -p 6334:6334 \
    -v $(pwd)/qdrant_storage:/qdrant/storage:z \
    qdrant/qdrant:latest

[!TIP] Visit http://localhost:6333/dashboard in your browser to inspect Qdrant’s interactive visual console.


Step 2: Install Python Dependencies & Configure Environment

Create and activate your virtual environment:

python -m venv venv
# On Windows:
.\venv\Scripts\activate
# On Linux/macOS:
source venv/bin/activate

pip install -r requirements.txt

Create a root .env configuration file:

QDRANT_HOST=localhost
QDRANT_PORT=6333
EMBEDDING_MODEL=DangVanTuan/vietnamese-embedding
OPENAI_API_KEY=your_openai_or_groq_api_key_here

Step 3: Ingest Your PDF Document

Place your target PDF inside src/data/pdfs/ (e.g., technical_report.pdf).

Execute the ingestion pipeline:

python -m src.pipeline.pdf_to_vectorstore_pipeline --pdf src/data/pdfs/technical_report.pdf

Under the hood, the pipeline executes automatically:

  1. PyMuPDF (fitz) splits the document into standalone page files in src/data/pdfs/pages/.
  2. Sanitizes characters, trims noise, and filters stopwords.
  3. Automatically provisions two Qdrant collections: esg_pages and esg_sequences.
  4. Computes embeddings locally and indexes chunks into Qdrant with structural metadata.

Step 4: Launch the FastAPI Backend Service

The repository provides a production FastAPI server featuring real-time Server-Sent Events (SSE) streaming:

uvicorn src.api.main:app --host 0.0.0.0 --port 8000 --reload

Test the interactive API docs at http://localhost:8000/docs. You can hit /api/query_seq:

{
  "query": "What are the regulatory discharge thresholds for COD and BOD?",
  "top_k": 3
}

The server streams response events dynamically:

  • page_start: Indicates which source page is being processed.
  • chunk: Streams text fragments alongside page indices and similarity scores.
  • done: Transmits the synthesized answer and full reference citations.

Step 5: Launch the Next.js Chat Interface

To chat with your document in an interactive web UI, navigate to src/app:

cd src/app
npm install
npm run dev

Open http://localhost:3000 in your browser:

  • Interactive Streaming Chat: Real-time token streaming with zero latency lag.
  • Source Inspector: Click any citation badge to view the exact Page Index, Sequence Index, and Similarity Score.
  • Direct PDF Uploader: Drag-and-drop new PDF files to trigger ingestion and Qdrant indexing directly from the UI.

5. Empirical Performance Scorecard (tests/data/ Across 168 BIWASE Report Pages)

Inside tests/data/, we benchmarked the pipeline using src/utils/measure_retrieval_accuracy.py across 168 pages of BIWASE operational & ESG reports (indexed into 1,132 esg_sequences micro-chunks and 168 esg_pages macro-pages), comparing the 660-query suite (halong_evaluation_results.json) and 315-query suite (qwen_evaluation_results.json):

Operational & Evaluation MetricSingle-Stage Baseline (BKAI HaLong - 660 queries)Upgraded Qwen3-Embedding-0.6B + Dual-Collection (315 queries)Engineering Analysis & Production Impact
168-Page PDF Extraction Time~45 seconds (PyPDF / PDFMiner CPU)< 3 seconds (PyMuPDF fitz)15x faster while preserving table row alignment.
Post-Sanitization Cosine Similarity0.9902 (Std 0.0156, Min 0.9526)0.9915 (Lowest Std 0.0118, Full-page 0.9966)RegEx sanitizer removes OCR noise without degrading domain semantics.
Micro Tier (esg_sequences) — Hit@129.39% (MRR = 0.4177)38.41% (+9.02%, MRR = 0.5198)Monthly BIWASE bulletins share identical table headers, causing isolated 500-char chunks across months to tie in Cosine scores.
Micro Tier (esg_sequences) — Hit@5 & Hit@1057.42% (Hit@5) / 70.00% (Hit@10)68.25% (Hit@5) / 80.00% (Hit@10)80% of queries surface the true target sequence within the Top-10 candidate pool.
Micro Tier (esg_sequences) — Hit@50 & Hit@10083.33% (Hit@50) / 88.94% (Hit@100)90.16% (Hit@50) / 93.33% (Hit@100)High-recall candidate pool feeds hierarchical page_index aggregation.
Macro Tier (esg_pages Aggregation)Frequent cross-month figure misattribution100% restoration of page headers, reporting periods & footnotesAggregating esg_sequences hits into full esg_pages context resolves period ambiguity at < 1.5s – 2.0s SSE latency.

6. Closing Thoughts

Wrestling with dense, multi-hundred-page technical PDFs is a true trial by fire for any AI engineer. It teaches us a humbling truth: Never rely on naive 5-line tutorial code for production systems!

By decomposing documents into a Dual-Collection architecture (Macro Pages & Micro Sequences), employing hierarchical page aggregation, and grounding your embedding choices in rigorous empirical benchmarks, you can turn stubborn PDFs into dependable, hallucination-resistant knowledge bases.

Explore the complete source code, Docker configs, and evaluation notebooks here: 👉 GitHub Repository: https://github.com/vinh-gogo/pdf-rag

If this architecture helps save your company bot or capstone project from hallucinatory embarrassment, feel free to drop the repo a ⭐️ on GitHub! Happy building, and may your bots never misquote a balance sheet on a Monday morning again!


References

[01] Vinh-Gogo. (2025). PDF-RAG: Complete Pipeline for PDF Extraction,
     Dual Vector Collections and Hierarchical Retrieval.
     GitHub: https://github.com/Vinh-Gogo/pdf-rag
[02] Qdrant Team. (2024). High-Performance Vector Database for Production AI.
     Official Documentation: https://qdrant.tech/documentation/
[03] Dang, V. T. (2023). Vietnamese Embedding: Sentence Transformers for Vietnamese.
     Hugging Face: https://huggingface.co/bkai-foundation-models/vietnamese-bi-encoder
[04] Alibaba Cloud. (2024). Qwen3-Embedding: Representation Models for Long-Context RAG.
[05] PyMuPDF Team. (2024). High-performance PDF Rendering and Data Extraction Library.