AI Workflows

Building a Reliable Local RAG Pipeline: Chunking Size, Embeddings & Retrieval Accuracy

Setting up local Retrieval-Augmented Generation (RAG) with Ollama and a vector database takes under ten minutes—yet many builders quickly notice the model still misses facts or hallucinates answers even when the right PDF or Markdown file sits in the index.

In most cases, the language model is not the bottleneck. Poor document chunking and naive similarity search feed the LLM broken fragments instead of complete context.

1. Why Default Character Splitting Breaks Technical Docs

Splitting a technical manual strictly every 500 characters often cuts a Markdown table in half, separates a warning note from its command, or orphans a paragraph from its section heading.

  • Too small (150–250 tokens): Captures keywords well, but strips surrounding context so the LLM cannot tell which product or version the sentence refers to.
  • Too large (1,500+ tokens): Dilutes embedding similarity across multiple unrelated topics in the same chunk, lowering retrieval precision.
  • Practical sweet spot: Start with 400–700 tokens per chunk and 10–15% overlap, splitting along natural boundaries (headings, paragraphs, and code blocks) first.

2. Preserve Section Headers Inside Every Chunk

Suppose a 20-page handbook has five sections each containing a bullet titled “Default Port”. If a chunk loses its parent heading, vector search cannot distinguish between the database port and the proxy port. Prepend the document title and parent H1/H2 breadcrumb to each chunk before embedding:

Document: nginx-guide.md | Section: SSL Configuration
Certbot stores renewal timers under systemd...

3. Choosing Local Embedding Models Wisely

Do not mix embeddings from different models inside the same vector collection—changing embedding models requires re-indexing all chunks. For local setups, compact dedicated embedding models (such as nomic-embed-text or bge-m3) outperform using general chat models for embeddings both in speed and retrieval recall.

Pipeline Stage Common Mistake Reliable Fix
Chunking Fixed character split mid-sentence Heading-aware recursive splitting (400–700 tokens)
Search Pure vector cosine similarity only Hybrid search (BM25 keywords + vector similarity)
Context Injection Stuffing top-20 noisy chunks Retrieve top-10, rerank to top-4 clean chunks
Evaluation Guessing by reading final LLM chat Inspect retrieved chunks directly before generation

Semantic vector embeddings excel at concepts (e.g., matching “server crash” to “out of memory kill”), but they frequently struggle with exact error codes like ERR_CONNECTION_REFUSED, model numbers, or CLI flags. Combining lexical keyword search (BM25) with vector similarity—or applying metadata filters by folder/tag—solves exact-match lookups reliably.

5. Debug Retrieval Before Tuning Prompts

Whenever a local RAG pipeline gives a wrong answer, print the exact top-k chunks returned by your database before blaming the LLM. If the correct paragraph is not in the top retrieved chunks, no amount of prompt engineering can make the model cite facts it never received.

Leave a Reply

Your email address will not be published. Required fields are marked *