RAG: Chat with Your Own Documents

Make a language model answer from your own files: split documents into chunks, turn them into embeddings, search by meaning, build a grounded prompt with citations, improve retrieval, and measure whether it works.

ជំពូកនេះមិនទាន់ត្រូវបានបកប្រែជាភាសាខ្មែរនៅឡើយទេ — សូមអានជាភាសាអង់គ្លេសខាងក្រោម។

A language model knows a lot about the world, but it has never read your school handbook, your company wiki, or last week's meeting notes. Ask it about them and it will either say it does not know or, worse, invent a confident answer. Retrieval-Augmented Generation (RAG) is the most common fix, and it is simple at heart: before you ask the model, find the few passages of your documents that are relevant, paste them into the prompt, and tell the model to answer only from them.

This series builds that pipeline from the ground up. You will load documents and cut them into chunks, turn text into embeddings (lists of numbers that capture meaning), store them in a small vector index, search it by meaning, assemble a grounded prompt with citations, make the model say "I don't know" when the documents have no answer, improve retrieval with keyword search and other tricks, and finally measure quality with an evaluation set instead of guessing. The last chapter turns it into a command-line tool and a web endpoint, with a checklist for privacy and cost.

You build raglab, a plain Node.js project with no dependencies. The sample data is the handbook of a fictional student coding club (eight short documents), and a mock LLM server (the same idea as in the previous series, now with an embeddings endpoint) lets every example run on your own computer for free, with no account and no API key. The mock is not smart, and its toy embeddings only match shared words, but the pipeline, the code and the mistakes you will meet are the same ones you will meet with a real provider, which you can switch to with three environment variables.

You should have finished Build a Chatbot with an LLM API (RAG sends the same kind of request, with documents added) and it helps to have read Embeddings and Attention and Why Models Hallucinate in How LLMs Work (for Developers). You need Node.js (the LTS version) and a code editor.

Every chapter ends with a Quick Review: Common Questions section: short, honest answers to the questions students and developers most often ask about RAG.

  1. 01Why RAG?See why a model cannot answer questions about your own documents, why pasting everything and fine-tuning are poor fixes, how the RAG pipeline works end to end, and build a 30-line keyword version that runs against a free local mock.
  2. 02Loading and Chunking DocumentsRead your files into text, then cut them into chunks that retrieve well: fixed size with overlap, splitting at headings, keeping source and heading metadata for citations, and handling Khmer text, tokens versus characters, and messy formats like PDF.
  3. 03Embeddings in PracticeTurn the chunks into vectors: call the embeddings endpoint, build embed() from a bare fetch to batching to a cache, compare vectors with cosine similarity, see exactly where the mock's toy embeddings fail and how real models differ, and handle cost, rate limits, model changes and Khmer.
  4. 04Vector SearchBuild a small vector store that holds chunks and their embeddings, searches by cosine similarity, and saves to a JSON file, plus an indexer that re-embeds only changed chunks. Learn top-k, score thresholds, metadata filters, approximate search, and when you really need a vector database.
  5. 05Retrieve and GenerateConnect search to the model: build the grounded prompt, write the rules that make it cite and say "I don't know", refuse before the model when nothing relevant is found, order and size the context, handle follow-up questions, and defend against instructions hidden inside documents.
  6. 06Improving RetrievalFix the misses: write BM25 keyword search from scratch, combine it with vector search using reciprocal rank fusion, then try query rewriting, reranking, de-duplication, metadata filters, chunk-size experiments and parent-document retrieval.
  7. 07Evaluating a RAG SystemStop guessing whether your document chatbot works: write a small eval set, measure hit rate and MRR for retrieval, check answers and refusals, compare chunking and search settings with real numbers, and turn it all into a test that fails when quality drops.
  8. 08Ship a Document ChatbotTurn the pipeline into a product: an ingest command, a command-line chatbot with citations, and a safe web endpoint, then walk the production checklist for access control, privacy, freshness, cost and monitoring, and learn when RAG is the wrong tool.