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.