{"id":313,"date":"2026-11-29T14:39:49","date_gmt":"2026-11-29T14:39:49","guid":{"rendered":"https:\/\/devdojo.co.in\/?p=313"},"modified":"2026-11-29T14:39:49","modified_gmt":"2026-11-29T14:39:49","slug":"genai-series-part-11-rag-fundamentals","status":"publish","type":"post","link":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/","title":{"rendered":"Generative AI Part 11: RAG Fundamentals: Retrieval-Augmented Generation Explained"},"content":{"rendered":"\n<div class=\"dd-post\">\n<style>\n.single article:has(.dd-hero) .entry-media{display:none}\n.dd-post{--navy:#0b1020;--blue:#3178c6;--violet:#7c5cff;--ink:#1f2433;--muted:#5b6275;--line:#e3e6ef;font-size:17px;line-height:1.75;color:var(--ink)}\n.dd-post h2{margin:2.2em 0 .6em;font-size:1.6em;line-height:1.3;scroll-margin-top:90px}\n.dd-post h3{margin:1.6em 0 .5em;font-size:1.22em;scroll-margin-top:90px}\n.dd-post a{color:var(--blue);text-decoration:underline;text-underline-offset:3px}\n.dd-post pre{background:#0f1430;color:#e6e9f5;padding:18px 20px;border-radius:12px;overflow-x:auto;line-height:1.6;border:1px solid #232a52;margin:1.2em 0;font-size:14.5px !important}\n.dd-post pre code{font-size:1em !important;background:none;color:inherit;padding:0;font-family:\"JetBrains Mono\",\"Fira Code\",Consolas,Menlo,monospace;white-space:pre}\n.dd-post :not(pre)>code{background:#eef1fb;color:#2b3a8f;padding:2px 6px;border-radius:5px;font-size:.9em}\n.dd-post .dd-file{display:inline-block;background:#151b3b;color:#c9d1ff;font:600 13px\/1 \"JetBrains Mono\",Consolas,monospace;padding:7px 12px;border-radius:8px 8px 0 0;margin:1em 0 -1.2em;position:relative}\n.dd-post .dd-hero{margin:0 0 1.6em;border-radius:16px;overflow:hidden;box-shadow:0 10px 30px rgba(11,16,32,.25)}\n.dd-post .dd-hero svg{display:block;width:100%;height:auto}\n.dd-post .dd-hero .node{transition:transform .25s,filter .25s;transform-box:fill-box;transform-origin:center;cursor:pointer}\n.dd-post .dd-hero .node:hover{transform:scale(1.08);filter:drop-shadow(0 0 10px #7c5cff)}\n.dd-post .dd-series{display:inline-flex;align-items:center;gap:8px;background:#151b3b;color:#dfe3ff;border-radius:999px;padding:6px 14px;font-size:.88em;font-weight:600;margin:0 0 .8em}\n.dd-post .dd-series span{background:var(--violet);color:#fff;border-radius:999px;padding:2px 10px}\n.dd-post .dd-prereq{font-size:.96em;color:var(--muted);border-left:3px solid var(--line);padding-left:12px;margin:.6em 0 1.2em}\n.dd-post .dd-summary{background:linear-gradient(135deg,#eef3ff,#f4efff);border:1px solid #d6dcf5;border-left:5px solid var(--violet);border-radius:12px;padding:18px 22px;margin:1.4em 0}\n.dd-post .dd-summary strong.lbl{display:block;font-size:1.05em;margin-bottom:6px;color:#3a2fa0}\n.dd-post .dd-summary ul{margin:.3em 0 0 1.1em}\n.dd-post .dd-toc{background:#f8f9fc;border:1px solid var(--line);border-radius:12px;padding:16px 22px;margin:1.4em 0}\n.dd-post .dd-toc ol{margin:.4em 0 0 1.2em;columns:2;column-gap:28px}\n.dd-post .dd-toc li{break-inside:avoid;margin:.15em 0}\n@media(max-width:640px){.dd-post .dd-toc ol{columns:1}}\n.dd-post .dd-tip,.dd-post .dd-note,.dd-post .dd-warn{border-radius:10px;padding:14px 18px;margin:1.3em 0}\n.dd-post .dd-tip{background:#ecfbf3;border-left:4px solid #1f9d61}\n.dd-post .dd-note{background:#eef4ff;border-left:4px solid var(--blue)}\n.dd-post .dd-warn{background:#fff6e8;border-left:4px solid #e08a00}\n.dd-post .dd-cards{display:grid;grid-template-columns:repeat(auto-fit,minmax(210px,1fr));gap:14px;margin:1.3em 0}\n.dd-post .dd-card{border:1px solid var(--line);border-radius:12px;padding:14px 16px;background:#fff;box-shadow:0 2px 8px rgba(20,30,70,.05)}\n.dd-post .dd-card b{display:block;color:#3a2fa0;margin-bottom:4px}\n.dd-post table{width:100%;border-collapse:collapse;margin:1.3em 0;font-size:.95em}\n.dd-post th,.dd-post td{border:1px solid var(--line);padding:10px 12px;text-align:left;vertical-align:top}\n.dd-post th{background:#151b3b;color:#fff}\n.dd-post tr:nth-child(even) td{background:#f8f9fc}\n.dd-post .dd-tablewrap{overflow-x:auto}\n.dd-post figure{margin:1.6em 0;text-align:center}\n.dd-post figure svg{width:100%;height:auto;border-radius:12px}\n.dd-post figcaption{font-size:.9em;color:var(--muted);margin-top:8px}\n.dd-post details{border:1px solid var(--line);border-radius:10px;padding:12px 16px;margin:.7em 0;background:#fff}\n.dd-post details summary{cursor:pointer;font-weight:600}\n.dd-post details[open] summary{margin-bottom:8px;color:#3a2fa0}\n.dd-post .dd-roadmap{border:2px solid #d6dcf5;background:#fbfbff}\n.dd-post .dd-roadmap h4{margin:1em 0 .3em;color:#151b3b;font-size:1em;text-transform:uppercase;letter-spacing:.04em}\n.dd-post .dd-roadmap ol{margin:.2em 0 .6em 1.4em;padding:0}\n.dd-post .dd-roadmap li{margin:.2em 0}\n.dd-post .dd-roadmap .here{background:var(--violet);color:#fff;border-radius:999px;padding:1px 9px;font-size:.8em;font-weight:700;margin-left:6px}\n.dd-post .dd-roadmap .soon{color:#8a90a6;font-size:.82em;margin-left:6px}\n.dd-post .dd-tabs{border:1px solid var(--line);border-radius:12px;padding:6px 16px 16px;margin:1.4em 0;background:#fbfbfe}\n.dd-post .dd-tabs>input{position:absolute;opacity:0;pointer-events:none}\n.dd-post .dd-tabs .labels label{display:inline-block;padding:8px 16px;margin:10px 6px 0 0;border-radius:999px;border:1px solid #cfd5ea;cursor:pointer;font-weight:600;font-size:.95em;background:#fff;transition:background .2s}\n.dd-post .dd-tabs .labels label:hover{border-color:var(--violet)}\n.dd-post .dd-tabs .panel{display:none;margin-top:12px}\n.dd-post .dd-tabs #ti1:checked~.labels label[for=ti1],.dd-post .dd-tabs #ti2:checked~.labels label[for=ti2],.dd-post .dd-tabs #ti3:checked~.labels label[for=ti3],.dd-post .dd-tabs #ti4:checked~.labels label[for=ti4]{background:var(--violet);color:#fff;border-color:var(--violet)}\n.dd-post .dd-tabs #ti1:checked~.p1,.dd-post .dd-tabs #ti2:checked~.p2,.dd-post .dd-tabs #ti3:checked~.p3,.dd-post .dd-tabs #ti4:checked~.p4{display:block}\n.dd-post .dd-tabs #ti1:focus-visible~.labels label[for=ti1],.dd-post .dd-tabs #ti2:focus-visible~.labels label[for=ti2],.dd-post .dd-tabs #ti3:focus-visible~.labels label[for=ti3],.dd-post .dd-tabs #ti4:focus-visible~.labels label[for=ti4]{outline:2px solid var(--blue);outline-offset:2px}\n.dd-post .dd-cta{background:linear-gradient(135deg,#0b1020,#151b3b);color:#e8ebff;border-radius:16px;padding:24px 26px;margin:2em 0;text-align:center}\n.dd-post .dd-cta h3{color:#fff;margin-top:0}\n.dd-post .dd-cta a{color:#a99bff}\n.dd-post .dd-vsc{position:relative;margin:1.5em 0;border-radius:12px;overflow:hidden;background:#1e1e1e;border:1px solid #2b2b2b;box-shadow:0 14px 34px rgba(0,0,0,.28);font-family:\"Segoe UI\",system-ui,sans-serif}\n.dd-post .dd-vsc>input{position:absolute;opacity:0;pointer-events:none}\n.dd-post .dd-vsc-bar{display:flex;align-items:center;gap:12px;background:#323233;color:#cccccc;font-size:12.5px;padding:8px 12px}\n.dd-post .dd-dots{display:inline-flex;gap:7px}.dd-post .dd-dots i{width:12px;height:12px;border-radius:50%;background:#ff5f57;display:block}.dd-post .dd-dots i:nth-child(2){background:#febc2e}.dd-post .dd-dots i:nth-child(3){background:#28c840}\n.dd-post .dd-vsc-title{flex:1;text-align:center;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;padding-right:50px;opacity:.85}\n.dd-post .dd-vsc-tabs{display:flex;align-items:stretch;background:#252526;border-bottom:1px solid #1e1e1e;overflow-x:auto}\n.dd-post .dd-vsc-tabs label{display:flex;align-items:center;gap:7px;padding:9px 16px;color:#969696;font-size:13px;cursor:pointer;background:#2d2d2d;border-right:1px solid #252526;border-top:1px solid transparent;white-space:nowrap;margin:0;transition:color .15s}\n.dd-post .dd-vsc-tabs label:hover{color:#e0e0e0}\n.dd-post .dd-vsc-tabs .ic{font:700 9.5px\/1 Consolas,monospace;padding:3px 4px;border-radius:3px;color:#1e1e1e}\n.dd-post .ic.py{background:#ffd43b}.dd-post .ic.js{background:#f1e05a}.dd-post .ic.sh{background:#89e051}\n.dd-post .dd-vsc-tabs label.run{margin-left:auto;background:transparent;border:0;color:#89d185;font-weight:600;display:none}\n.dd-post .dd-vsc-tabs label.run:hover{color:#b5f0b0;background:#2a2d2e}\n.dd-post .dd-vsc-pane{display:none}\n.dd-post .dd-vsc>.f1:checked~.dd-vsc-tabs .t1,.dd-post .dd-vsc>.f2:checked~.dd-vsc-tabs .t2,.dd-post .dd-vsc>.f3:checked~.dd-vsc-tabs .t3{background:#1e1e1e;color:#ffffff;border-top-color:#3178c6}\n.dd-post .dd-vsc>.f1:checked~.p1,.dd-post .dd-vsc>.f2:checked~.p2,.dd-post .dd-vsc>.f3:checked~.p3{display:block}\n.dd-post .dd-vsc>.f1:checked~.dd-vsc-tabs .run1,.dd-post .dd-vsc>.f2:checked~.dd-vsc-tabs .run2,.dd-post .dd-vsc>.f3:checked~.dd-vsc-tabs .run3{display:flex}\n.dd-post .dd-vsc>.f1:focus-visible~.dd-vsc-tabs .t1,.dd-post .dd-vsc>.f2:focus-visible~.dd-vsc-tabs .t2,.dd-post .dd-vsc>.f3:focus-visible~.dd-vsc-tabs .t3,.dd-post .dd-vsc>input[type=checkbox]:focus-visible~.dd-vsc-tabs .run{outline:2px solid #3178c6;outline-offset:-2px}\n.dd-post .dd-vsc-crumbs{background:#1e1e1e;color:#a9a9a9;font-size:12px;padding:5px 16px 2px}\n.dd-post .dd-vsc pre.dd-vsc-code{background:#1e1e1e;color:#d4d4d4;border:0;border-radius:0;margin:0;padding:8px 0 14px;counter-reset:ln;line-height:1.62;overflow-x:auto}\n.dd-post .dd-vsc pre code{font-family:Consolas,\"JetBrains Mono\",\"Fira Code\",Menlo,monospace;color:inherit;background:none;white-space:pre}\n.dd-post .dd-vsc .l{display:inline-block;min-width:100%;padding-right:18px;box-sizing:border-box}\n.dd-post .dd-vsc .l::before{counter-increment:ln;content:counter(ln);display:inline-block;width:3.2em;padding-right:1.4em;text-align:right;color:#6e7681;user-select:none;-webkit-user-select:none}\n.dd-post .dd-vsc .l:hover{background:#2a2d2e}.dd-post .dd-vsc .l:hover::before{color:#c6c6c6}\n.dd-post .dd-vsc .k,.dd-post .dd-vsc .kn{color:#c586c0}.dd-post .dd-vsc .kc{color:#569cd6}.dd-post .dd-vsc .s{color:#ce9178}.dd-post .dd-vsc .c{color:#6a9955}.dd-post .dd-vsc .m{color:#b5cea8}.dd-post .dd-vsc .nf{color:#dcdcaa}.dd-post .dd-vsc .nc{color:#4ec9b0}.dd-post .dd-vsc .nb{color:#4ec9b0}.dd-post .dd-vsc .nd{color:#dcdcaa}.dd-post .dd-vsc .n{color:#9cdcfe}.dd-post .dd-vsc .o{color:#d4d4d4}.dd-post .dd-vsc .gp{color:#89d185;user-select:none;-webkit-user-select:none}\n.dd-post .dd-vsc-term{display:none;border-top:1px solid #3c3c3c;background:#181818}\n.dd-post .dd-vsc>.r1:checked~.p1 .dd-vsc-term,.dd-post .dd-vsc>.r2:checked~.p2 .dd-vsc-term,.dd-post .dd-vsc>.r3:checked~.p3 .dd-vsc-term{display:block;animation:ddterm .35s ease-out}\n@keyframes ddterm{from{opacity:0;transform:translateY(6px)}to{opacity:1;transform:none}}\n.dd-post .dd-term-head{display:flex;gap:18px;align-items:center;font-size:11px;letter-spacing:.06em;color:#8b8b8b;padding:7px 16px 0}\n.dd-post .dd-term-head span:first-child{color:#e7e7e7;border-bottom:1px solid #e7e7e7;padding-bottom:4px}\n.dd-post .dd-term-head .dd-term-note{margin-left:auto;letter-spacing:0;text-transform:none;color:#d7ba7d;border:0;font-size:11.5px}\n.dd-post .dd-vsc-term pre{background:#181818;color:#cccccc;border:0;margin:0;padding:10px 16px 14px;border-radius:0}\n.dd-post .dd-vsc-status{display:flex;flex-wrap:wrap;gap:4px 14px;background:#007acc;color:#fff;font-size:11.5px;padding:4px 12px}\n.dd-post .dd-vsc-status .sp{flex:1}\n.dd-post .dd-vsc-termonly pre.dd-vsc-code{background:#181818;padding:12px 16px}\n@media(max-width:640px){.dd-post .dd-vsc-title{padding-right:0;text-align:left}.dd-post .dd-vsc-status span:nth-child(n+4){display:none}.dd-post .dd-vsc .l::before{width:2.2em;padding-right:1em}}\n.dd-post .dd-vsc-hint{font-size:.93em;color:var(--muted);margin-top:-.6em}\n.dd-post .dd-next{border:1px dashed var(--violet);border-radius:12px;padding:16px 20px;margin:1.6em 0;background:#f7f4ff}\n<\/style>\n<div class=\"dd-hero\">\n<svg viewBox=\"0 0 1200 520\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" role=\"img\" aria-label=\"Generative AI series Part 11 banner: a question goes to a retriever that pulls matching chunks from your documents, the chunks and question go into the LLM, which returns an answer with sources\">\n<defs>\n<linearGradient id=\"ddbg\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"1\"><stop offset=\"0\" stop-color=\"#0b1020\"\/><stop offset=\"1\" stop-color=\"#151b3b\"\/><\/linearGradient>\n<linearGradient id=\"ddac\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"0\"><stop offset=\"0\" stop-color=\"#3178c6\"\/><stop offset=\"1\" stop-color=\"#7c5cff\"\/><\/linearGradient>\n<radialGradient id=\"ddglow\" cx=\".5\" cy=\".5\" r=\".5\"><stop offset=\"0\" stop-color=\"#7c5cff\" stop-opacity=\".45\"\/><stop offset=\"1\" stop-color=\"#7c5cff\" stop-opacity=\"0\"\/><\/radialGradient>\n<\/defs>\n<rect x=\"0\" y=\"-60\" width=\"1200\" height=\"640\" fill=\"url(#ddbg)\"\/>\n<circle cx=\"1000\" cy=\"110\" r=\"250\" fill=\"url(#ddglow)\"\/>\n<g opacity=\".12\" stroke=\"#9fb2ff\"><path d=\"M0 100H1200M0 200H1200M0 300H1200M0 400H1200M200 0V520M400 0V520M600 0V520M800 0V520M1000 0V520\"\/><\/g>\n<rect x=\"60\" y=\"46\" width=\"140\" height=\"38\" rx=\"19\" fill=\"url(#ddac)\"\/>\n<text x=\"130\" y=\"71\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"18\" font-weight=\"700\" fill=\"#fff\">PART 11<\/text>\n<text x=\"216\" y=\"71\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"18\" fill=\"#aab4e8\">Generative AI Series \u00b7 DevDojo<\/text>\n<text x=\"60\" y=\"148\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"58\" font-weight=\"800\" fill=\"#ffffff\">RAG Fundamentals<\/text>\n<text x=\"60\" y=\"204\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"40\" font-weight=\"800\" fill=\"url(#ddac)\">Retrieval-Augmented Generation<\/text>\n<text x=\"60\" y=\"246\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"23\" fill=\"#c9d1ff\">Let an LLM answer from your own documents, with sources<\/text>\n<g font-family=\"Segoe UI,Arial,sans-serif\">\n<g class=\"node\"><rect x=\"50\" y=\"320\" width=\"200\" height=\"110\" rx=\"16\" fill=\"#1b2250\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"150\" y=\"355\" text-anchor=\"middle\" font-size=\"15\" fill=\"#aac8f0\">Question<\/text><text x=\"150\" y=\"390\" text-anchor=\"middle\" font-size=\"17\" fill=\"#e8ebff\">&#8220;Can I get<\/text><text x=\"150\" y=\"414\" text-anchor=\"middle\" font-size=\"17\" fill=\"#e8ebff\">a refund?&#8221;<\/text><\/g>\n<g class=\"node\"><rect x=\"330\" y=\"290\" width=\"230\" height=\"170\" rx=\"16\" fill=\"#0b1020\" stroke=\"#61dafb\" stroke-width=\"2\"\/><text x=\"445\" y=\"320\" text-anchor=\"middle\" font-size=\"16\" font-weight=\"700\" fill=\"#a8ecfb\">1 \u00b7 Retrieve<\/text>\n<rect x=\"352\" y=\"336\" width=\"186\" height=\"30\" rx=\"8\" fill=\"#1b2250\"\/><text x=\"364\" y=\"356\" font-size=\"13\" fill=\"#c9d1ff\">pricing.md<\/text>\n<rect x=\"352\" y=\"374\" width=\"186\" height=\"30\" rx=\"8\" fill=\"#2a2160\" stroke=\"#7c5cff\"\/><text x=\"364\" y=\"394\" font-size=\"13\" fill=\"#fff\">refunds.md &#10003;<\/text>\n<rect x=\"352\" y=\"412\" width=\"186\" height=\"30\" rx=\"8\" fill=\"#1b2250\"\/><text x=\"364\" y=\"432\" font-size=\"13\" fill=\"#c9d1ff\">support.md<\/text><\/g>\n<g class=\"node\"><rect x=\"640\" y=\"320\" width=\"220\" height=\"110\" rx=\"20\" fill=\"url(#ddac)\"\/><text x=\"750\" y=\"362\" text-anchor=\"middle\" font-size=\"16\" fill=\"#dfe3ff\">2 \u00b7 Augment + Generate<\/text><text x=\"750\" y=\"398\" text-anchor=\"middle\" font-size=\"28\" font-weight=\"800\" fill=\"#fff\">LLM<\/text><\/g>\n<g class=\"node\"><rect x=\"940\" y=\"310\" width=\"220\" height=\"130\" rx=\"16\" fill=\"#1b2250\" stroke=\"#89e051\" stroke-width=\"2\"\/><text x=\"1050\" y=\"342\" text-anchor=\"middle\" font-size=\"15\" fill=\"#b8f0a0\">Answer<\/text><text x=\"1050\" y=\"376\" text-anchor=\"middle\" font-size=\"16\" fill=\"#e8ebff\">&#8220;Yes, within 7<\/text><text x=\"1050\" y=\"400\" text-anchor=\"middle\" font-size=\"16\" fill=\"#e8ebff\">days of purchase<\/text><text x=\"1050\" y=\"424\" text-anchor=\"middle\" font-size=\"16\" fill=\"#b8f0a0\">[refunds.md]&#8221;<\/text><\/g>\n<\/g>\n<path id=\"ddf1\" d=\"M250 375 L330 375\" fill=\"none\" stroke=\"url(#ddac)\" stroke-width=\"3\" stroke-dasharray=\"8 8\" opacity=\".8\"\/>\n<path id=\"ddf2\" d=\"M560 375 L640 375\" fill=\"none\" stroke=\"url(#ddac)\" stroke-width=\"3\" stroke-dasharray=\"8 8\" opacity=\".8\"\/>\n<path id=\"ddf3\" d=\"M860 375 L940 375\" fill=\"none\" stroke=\"url(#ddac)\" stroke-width=\"3\" stroke-dasharray=\"8 8\" opacity=\".8\"\/>\n<circle r=\"6\" fill=\"#61dafb\"><animateMotion dur=\"1.2s\" repeatCount=\"indefinite\"><mpath href=\"#ddf1\"\/><\/animateMotion><\/circle>\n<circle r=\"6\" fill=\"#7c5cff\"><animateMotion dur=\"1.2s\" begin=\".4s\" repeatCount=\"indefinite\"><mpath href=\"#ddf2\"\/><\/animateMotion><\/circle>\n<circle r=\"6\" fill=\"#89e051\"><animateMotion dur=\"1.2s\" begin=\".8s\" repeatCount=\"indefinite\"><mpath href=\"#ddf3\"\/><\/animateMotion><\/circle>\n<circle r=\"4\" fill=\"#ffffff\"><animateMotion dur=\"1.2s\" begin=\".6s\" repeatCount=\"indefinite\"><mpath href=\"#ddf1\"\/><\/animateMotion><\/circle>\n<text x=\"60\" y=\"505\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"16\" fill=\"#7f89b5\">devdojo.co.in \u00b7 Beginner friendly \u00b7 Python + Node.js examples<\/text>\n<\/svg>\n<\/div>\n\n<div class=\"dd-series\">Series: Generative AI <span>Part 11 of 30<\/span><\/div>\n<p class=\"dd-prereq\"><strong>What you should know first:<\/strong> read <a href=\"https:\/\/devdojo.co.in\/?p=310\">Part 10: Vector Databases<\/a> and <a href=\"https:\/\/devdojo.co.in\/?p=306\">Part 9: Embeddings Explained<\/a>. You need Python 3.10+ or Node.js 20+ and, for the free local examples, <a href=\"https:\/\/devdojo.co.in\/?p=281\">Ollama (Part 6)<\/a>.<\/p>\n\n<p>LLMs know a lot about the world, but they know nothing about <em>your<\/em> company&#8217;s refund policy, your product docs or last week&#8217;s meeting notes. Ask anyway, and they may confidently make something up. <strong>RAG (Retrieval-Augmented Generation)<\/strong> fixes this: before the model answers, you <strong>retrieve<\/strong> the most relevant pieces of your own documents and put them into the prompt. In this post you will learn the <strong>RAG fundamentals<\/strong>: how the pipeline works, how to write a RAG prompt that cites sources and says &#8220;I don&#8217;t know&#8221;, and how to build a working RAG app in Python and Node.js using the embeddings and vector database skills from Parts 9 and 10.<\/p>\n\n<div class=\"dd-summary\">\n<strong class=\"lbl\">Quick Summary<\/strong>\n<ul>\n<li><strong>RAG<\/strong> = <strong>R<\/strong>etrieve relevant chunks from your documents + <strong>A<\/strong>ugment the prompt with them + let the LLM <strong>G<\/strong>enerate an answer.<\/li>\n<li>It works like an <strong>open-book exam<\/strong>: the model doesn&#8217;t need to memorise your data, it reads the right page before answering.<\/li>\n<li>Every RAG app has two phases: <strong>indexing<\/strong> (chunk, embed, store, done once) and <strong>querying<\/strong> (embed question, retrieve top-k, prompt, answer).<\/li>\n<li>A good RAG prompt says &#8220;answer only from the context&#8221;, numbers the chunks for <strong>citations<\/strong>, and allows &#8220;I don&#8217;t know&#8221;.<\/li>\n<li>RAG is usually the first choice for private or changing data. It is cheaper and easier to update than fine-tuning.<\/li>\n<\/ul>\n<\/div>\n\n<nav class=\"dd-toc\" aria-label=\"Table of contents\">\n<strong>In this post<\/strong>\n<ol>\n<li><a href=\"#what-is-rag\">What is RAG?<\/a><\/li>\n<li><a href=\"#why-rag\">Why LLMs need RAG<\/a><\/li>\n<li><a href=\"#how-rag-works\">How RAG works: two phases<\/a><\/li>\n<li><a href=\"#step-explorer\">RAG step explorer<\/a><\/li>\n<li><a href=\"#rag-from-scratch\">RAG from scratch (no API key)<\/a><\/li>\n<li><a href=\"#rag-prompt\">Writing a good RAG prompt<\/a><\/li>\n<li><a href=\"#rag-ollama\">A real RAG app with Ollama<\/a><\/li>\n<li><a href=\"#rag-chroma\">RAG with Chroma and OpenAI<\/a><\/li>\n<li><a href=\"#rag-vs\">RAG vs fine-tuning vs long context<\/a><\/li>\n<li><a href=\"#errors\">Common errors and fixes<\/a><\/li>\n<li><a href=\"#practice\">Practice task<\/a><\/li>\n<li><a href=\"#faq\">FAQ<\/a><\/li>\n<\/ol>\n<\/nav>\n\n<details class=\"dd-roadmap\">\n<summary>Full Generative AI roadmap (30 parts)<\/summary>\n<h4>Beginner<\/h4>\n<ol start=\"1\">\n<li><a href=\"https:\/\/devdojo.co.in\/?p=215\">What Is Generative AI and How Do LLMs Work?<\/a><\/li>\n<li><a href=\"https:\/\/devdojo.co.in\/?p=219\">Tokens, Context Windows and Model Parameters<\/a><\/li>\n<li><a href=\"https:\/\/devdojo.co.in\/?p=223\">Your First LLM API Calls in Python and Node.js (OpenAI, Anthropic, Gemini)<\/a><\/li>\n<li><a href=\"https:\/\/devdojo.co.in\/?p=240\">Prompt Engineering Basics: Roles, Instructions and Examples<\/a><\/li>\n<li><a href=\"https:\/\/devdojo.co.in\/?p=244\">Temperature, Top-p and Controlling Model Output<\/a><\/li>\n<li><a href=\"https:\/\/devdojo.co.in\/?p=281\">Run LLMs Locally with Ollama<\/a><\/li>\n<li><a href=\"https:\/\/devdojo.co.in\/?p=284\">Streaming Responses and Building a Simple Chat UI<\/a><\/li>\n<li><a href=\"https:\/\/devdojo.co.in\/?p=303\">Structured Outputs: Getting Reliable JSON from LLMs<\/a><\/li>\n<\/ol>\n<h4>Intermediate<\/h4>\n<ol start=\"9\">\n<li><a href=\"https:\/\/devdojo.co.in\/?p=306\">Embeddings Explained: Turning Text into Vectors<\/a><\/li>\n<li><a href=\"https:\/\/devdojo.co.in\/?p=310\">Vector Databases: pgvector, Chroma and Pinecone<\/a><\/li>\n<li><strong>RAG Fundamentals: Retrieval-Augmented Generation<\/strong><span class=\"here\">You are here<\/span><\/li>\n<li>Document Loading and Chunking Strategies<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Better RAG: Hybrid Search, Re-ranking and Query Rewriting<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Function Calling and Tool Use<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Conversation Memory and Long Context Management<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Multimodal AI: Vision, Images and Audio Inputs<span class=\"soon\">Coming soon<\/span><\/li>\n<li>How Image Generation Works: Diffusion Models Basics<span class=\"soon\">Coming soon<\/span><\/li>\n<li>LangChain vs LlamaIndex: When to Use a Framework<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Testing LLM Apps: Evals, Metrics and Test Sets<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Cutting Cost and Latency: Caching, Batching and Model Choice<span class=\"soon\">Coming soon<\/span><\/li>\n<\/ol>\n<h4>Advanced<\/h4>\n<ol start=\"21\">\n<li>Fine-Tuning vs Prompting vs RAG: Choosing the Right Tool<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Fine-Tuning an Open Model with LoRA<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Transformers Deep Dive: Attention Explained Simply<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Reasoning Models and Thinking Time<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Guardrails, Safety and Prompt Injection Defence<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Monitoring and Tracing LLM Apps in Production<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Serving Open Models: vLLM, Quantization and GPUs<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Building a Production GenAI App End to End<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Responsible AI: Privacy, Licensing and Bias<span class=\"soon\">Coming soon<\/span><\/li>\n<li>Final Project: Build Your Own AI Knowledge Assistant<span class=\"soon\">Coming soon<\/span><\/li>\n<\/ol>\n<\/details>\n\n<h2 id=\"what-is-rag\">What is RAG (Retrieval-Augmented Generation)?<\/h2>\n<p><strong>Retrieval-Augmented Generation (RAG)<\/strong> is a pattern where your app first <em>searches<\/em> your own data for information related to the user&#8217;s question, then gives that information to the LLM together with the question. The model writes its answer based on what it was given, not just on what it remembers from training.<\/p>\n<p>Think of it as an <strong>open-book exam<\/strong>. Without RAG, the model answers from memory and may guess. With RAG, you hand it the right pages of the textbook first. The term comes from a <a href=\"https:\/\/arxiv.org\/abs\/2005.11401\" rel=\"noopener\" target=\"_blank\">2020 research paper by Lewis et al.<\/a>, and today it powers most &#8220;chat with your docs&#8221;, customer support bots and internal knowledge assistants.<\/p>\n\n<h2 id=\"why-rag\">Why LLMs need RAG<\/h2>\n<div class=\"dd-cards\">\n<div class=\"dd-card\"><b>Private data<\/b>The model was never trained on your HR policy, your codebase or your customer tickets.<\/div>\n<div class=\"dd-card\"><b>Fresh data<\/b>Training data has a cut-off date. Prices, stock and policies change every week.<\/div>\n<div class=\"dd-card\"><b>Fewer made-up answers<\/b>Giving the model the facts, and telling it to stick to them, greatly reduces hallucinations.<\/div>\n<div class=\"dd-card\"><b>Citations<\/b>Users (and you) can check the source document. That builds trust.<\/div>\n<\/div>\n<p>You might ask: &#8220;Why not paste all my documents into the prompt?&#8221; For a few pages, that works. But as you learned in <a href=\"https:\/\/devdojo.co.in\/?p=219\">Part 2<\/a>, every token costs money and time, and context windows have limits. With thousands of pages, you need to pick the <em>few<\/em> chunks that matter. That picking is the &#8220;retrieval&#8221; in RAG.<\/p>\n\n<h2 id=\"how-rag-works\">How RAG works: indexing and querying<\/h2>\n<p>Every RAG system, from a weekend project to an enterprise assistant, has the same two phases:<\/p>\n<figure>\n<svg viewBox=\"0 0 900 380\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" role=\"img\" aria-label=\"RAG has two phases. Indexing, done once: load documents, split into chunks, create embeddings, store in a vector database. Querying, on every question: embed the question, retrieve top chunks, build the prompt, generate the answer.\">\n<rect width=\"900\" height=\"380\" rx=\"16\" fill=\"#0f1430\"\/>\n<g font-family=\"Segoe UI,Arial,sans-serif\">\n<text x=\"30\" y=\"44\" font-size=\"19\" font-weight=\"700\" fill=\"#a8ecfb\">Phase 1 \u00b7 Indexing (run once, or when docs change)<\/text>\n<g font-size=\"16\" fill=\"#e8ebff\" text-anchor=\"middle\">\n<rect x=\"30\" y=\"64\" width=\"180\" height=\"70\" rx=\"12\" fill=\"#1b2250\" stroke=\"#61dafb\"\/><text x=\"120\" y=\"106\">Load docs<\/text>\n<rect x=\"250\" y=\"64\" width=\"180\" height=\"70\" rx=\"12\" fill=\"#1b2250\" stroke=\"#61dafb\"\/><text x=\"340\" y=\"106\">Split into chunks<\/text>\n<rect x=\"470\" y=\"64\" width=\"180\" height=\"70\" rx=\"12\" fill=\"#1b2250\" stroke=\"#61dafb\"\/><text x=\"560\" y=\"106\">Create embeddings<\/text>\n<rect x=\"690\" y=\"64\" width=\"180\" height=\"70\" rx=\"12\" fill=\"#22306a\" stroke=\"#61dafb\" stroke-width=\"2\"\/><text x=\"780\" y=\"106\">Store in vector DB<\/text>\n<\/g>\n<g stroke=\"#61dafb\" stroke-width=\"3\"><path d=\"M210 99h40M430 99h40M650 99h40\"\/><\/g>\n<text x=\"30\" y=\"214\" font-size=\"19\" font-weight=\"700\" fill=\"#c9bfff\">Phase 2 \u00b7 Querying (every question)<\/text>\n<g font-size=\"16\" fill=\"#e8ebff\" text-anchor=\"middle\">\n<rect x=\"30\" y=\"234\" width=\"180\" height=\"70\" rx=\"12\" fill=\"#1b2250\" stroke=\"#7c5cff\"\/><text x=\"120\" y=\"276\">Embed question<\/text>\n<rect x=\"250\" y=\"234\" width=\"180\" height=\"70\" rx=\"12\" fill=\"#1b2250\" stroke=\"#7c5cff\"\/><text x=\"340\" y=\"268\">Retrieve top-k<\/text><text x=\"340\" y=\"290\" font-size=\"13\" fill=\"#c9bfff\">from vector DB<\/text>\n<rect x=\"470\" y=\"234\" width=\"180\" height=\"70\" rx=\"12\" fill=\"#1b2250\" stroke=\"#7c5cff\"\/><text x=\"560\" y=\"268\">Build prompt<\/text><text x=\"560\" y=\"290\" font-size=\"13\" fill=\"#c9bfff\">chunks + question<\/text>\n<rect x=\"690\" y=\"234\" width=\"180\" height=\"70\" rx=\"12\" fill=\"url(#f1ac)\"\/><text x=\"780\" y=\"276\" font-weight=\"700\">LLM answers<\/text>\n<\/g>\n<g stroke=\"#7c5cff\" stroke-width=\"3\"><path d=\"M210 269h40M430 269h40M650 269h40\"\/><\/g>\n<path d=\"M780 134 C 780 190, 430 180, 430 234\" fill=\"none\" stroke=\"#aab4e8\" stroke-width=\"2\" stroke-dasharray=\"6 6\"\/>\n<text x=\"640\" y=\"190\" text-anchor=\"middle\" font-size=\"13\" fill=\"#aab4e8\">search the stored vectors<\/text>\n<text x=\"450\" y=\"350\" text-anchor=\"middle\" font-size=\"15\" fill=\"#aab4e8\">Retrieval-Augmented Generation = Retrieve + Augment the prompt + Generate<\/text>\n<\/g>\n<defs><linearGradient id=\"f1ac\" x1=\"0\" x2=\"1\"><stop offset=\"0\" stop-color=\"#3178c6\"\/><stop offset=\"1\" stop-color=\"#7c5cff\"\/><\/linearGradient><\/defs>\n<\/svg>\n<figcaption>The two phases of every RAG app. Indexing prepares your documents once; querying runs for every question.<\/figcaption>\n<\/figure>\n<p><strong>Indexing<\/strong> prepares your knowledge base: you load documents, split them into <strong>chunks<\/strong>, turn each chunk into an <strong>embedding<\/strong> (Part 9) and store it in a <strong>vector database<\/strong> (Part 10). <strong>Querying<\/strong> happens for every question: embed the question, find the most similar chunks, put them into the prompt and let the LLM answer.<\/p>\n\n<h2 id=\"step-explorer\">RAG step explorer<\/h2>\n<p>Click each step to see what happens and what usually goes wrong:<\/p>\n<div class=\"dd-tabs\" role=\"group\" aria-label=\"RAG step explorer\">\n<input type=\"radio\" name=\"ti\" id=\"ti1\" checked><input type=\"radio\" name=\"ti\" id=\"ti2\"><input type=\"radio\" name=\"ti\" id=\"ti3\"><input type=\"radio\" name=\"ti\" id=\"ti4\">\n<div class=\"labels\"><label for=\"ti1\">1. Index<\/label><label for=\"ti2\">2. Retrieve<\/label><label for=\"ti3\">3. Augment<\/label><label for=\"ti4\">4. Generate<\/label><\/div>\n<div class=\"panel p1\"><p><strong>What happens:<\/strong> load your documents (PDFs, web pages, Markdown, database rows), split them into chunks of a few hundred words, create an embedding for each chunk and save it in a vector database with metadata like the file name.<\/p><p><strong>When:<\/strong> once, and again whenever documents change. <strong>Main risk:<\/strong> bad chunks (too big, too small, cut mid-sentence). Part 12 is all about this.<\/p><\/div>\n<div class=\"panel p2\"><p><strong>What happens:<\/strong> embed the user&#8217;s question with the <em>same<\/em> embedding model and ask the vector database for the top <code>k<\/code> most similar chunks (usually 3&#8211;5).<\/p><p><strong>Main risk:<\/strong> the right chunk is not found. Part 13 adds hybrid search and re-ranking to fix this.<\/p><\/div>\n<div class=\"panel p3\"><p><strong>What happens:<\/strong> build a prompt with rules (&#8220;answer only from the context&#8221;), the numbered chunks and the question. This is the &#8220;augmented&#8221; part.<\/p><p><strong>Main risk:<\/strong> stuffing too many chunks wastes tokens and can confuse the model.<\/p><\/div>\n<div class=\"panel p4\"><p><strong>What happens:<\/strong> the LLM reads the prompt and writes an answer grounded in the chunks, with citations like <code>[1]<\/code>.<\/p><p><strong>Tip:<\/strong> use a low temperature (<a href=\"https:\/\/devdojo.co.in\/?p=244\">Part 5<\/a>) for factual answers, and stream the response for a nicer UI (<a href=\"https:\/\/devdojo.co.in\/?p=284\">Part 7<\/a>).<\/p><\/div>\n<\/div>\n\n<h2 id=\"rag-from-scratch\">Build RAG from scratch (no API key needed)<\/h2>\n<p>Let&#8217;s start with the smallest possible RAG: a list of chunks, a simple word-matching &#8220;retriever&#8221; and a prompt builder. It doesn&#8217;t call an LLM yet, so you can run it right now and <em>see<\/em> the augmented prompt that would be sent.<\/p>\n<div class=\"dd-vsc dd-vsc-termonly\" role=\"group\" aria-label=\"Terminal: Terminal \u2014 setup\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">Terminal \u2014 setup<\/span><\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"c\"># Free local models (see Part 6 for installing Ollama)<\/span>\n<span class=\"gp\">$<\/span> ollama pull nomic-embed-text\n<span class=\"gp\">$<\/span> ollama pull llama3.2\n<span class=\"c\"># Python packages<\/span>\n<span class=\"gp\">$<\/span> pip install ollama numpy chromadb openai\n<span class=\"c\"># Node.js package<\/span>\n<span class=\"gp\">$<\/span> npm install ollama<\/code><\/pre>\n<\/div>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: mini_rag.py, mini_rag.mjs\">\n<input type=\"radio\" name=\"v1\" id=\"v1f1\" class=\"f1\" checked>\n<input type=\"radio\" name=\"v1\" id=\"v1f2\" class=\"f2\">\n<input type=\"checkbox\" id=\"v1r1\" class=\"r1\">\n<input type=\"checkbox\" id=\"v1r2\" class=\"r2\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">mini_rag.py \u2014 devdojo-rag-basics \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v1f1\" class=\"t1\"><b class=\"ic py\">PY<\/b>mini_rag.py<\/label>\n<label for=\"v1f2\" class=\"t2\"><b class=\"ic js\">JS<\/b>mini_rag.mjs<\/label>\n<label for=\"v1r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<label for=\"v1r2\" class=\"run run2\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">devdojo-rag-basics \u203a mini_rag.py<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"kn\">import<\/span> <span class=\"n\">re<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\"># 1. Your private knowledge base (normally: chunks from your docs)<\/span><\/span>\n<span class=\"l\"><span class=\"n\">chunks<\/span> = [<\/span>\n<span class=\"l\">    <span class=\"s\">\"DevDojo Pro costs 499 rupees per month and includes all video courses.\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">\"Refunds are available within 7 days of purchase for DevDojo Pro.\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">\"Support is available on email from 9 AM to 9 PM IST, Monday to Saturday.\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">\"Certificates are issued after you finish 80 percent of a course.\"<\/span>,<\/span>\n<span class=\"l\">]<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">words<\/span>(<span class=\"n\">text<\/span>):<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"nb\">set<\/span>(<span class=\"n\">re<\/span>.<span class=\"n\">findall<\/span>(<span class=\"s\">r\"[a-z0-9]+\"<\/span>, <span class=\"n\">text<\/span>.<span class=\"n\">lower<\/span>()))<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\"># 2. Retrieve: score each chunk by shared words (real apps use embeddings)<\/span><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">retrieve<\/span>(<span class=\"n\">question<\/span>, <span class=\"n\">k<\/span>=<span class=\"m\">2<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">q<\/span> = <span class=\"n\">words<\/span>(<span class=\"n\">question<\/span>)<\/span>\n<span class=\"l\">    <span class=\"n\">scored<\/span> = <span class=\"nb\">sorted<\/span>(<span class=\"n\">chunks<\/span>, <span class=\"n\">key<\/span>=<span class=\"k\">lambda<\/span> <span class=\"n\">c<\/span>: <span class=\"nb\">len<\/span>(<span class=\"n\">q<\/span> &amp; <span class=\"n\">words<\/span>(<span class=\"n\">c<\/span>)), <span class=\"n\">reverse<\/span>=<span class=\"kc\">True<\/span>)<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"n\">scored<\/span>[:<span class=\"n\">k<\/span>]<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\"># 3. Augment: put the retrieved chunks into the prompt<\/span><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">build_prompt<\/span>(<span class=\"n\">question<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">context<\/span> = <span class=\"s\">\"\\n\"<\/span>.<span class=\"n\">join<\/span>(<span class=\"s\">f\"[{<\/span><span class=\"n\">i<\/span><span class=\"s\">}] {<\/span><span class=\"n\">c<\/span><span class=\"s\">}\"<\/span> <span class=\"k\">for<\/span> <span class=\"n\">i<\/span>, <span class=\"n\">c<\/span> in <span class=\"nb\">enumerate<\/span>(<span class=\"n\">retrieve<\/span>(<span class=\"n\">question<\/span>), <span class=\"m\">1<\/span>))<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> (<\/span>\n<span class=\"l\">        <span class=\"s\">\"Answer using ONLY the context below. Cite sources like [1].\\n\"<\/span><\/span>\n<span class=\"l\">        <span class=\"s\">\"If the answer is not in the context, say \\\"I don't know\\\".\\n\\n\"<\/span><\/span>\n<span class=\"l\">        <span class=\"s\">f\"Context:\\n{<\/span><span class=\"n\">context<\/span><span class=\"s\">}\\n\\nQuestion: {<\/span><span class=\"n\">question<\/span><span class=\"s\">}\"<\/span><\/span>\n<span class=\"l\">    )<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"n\">build_prompt<\/span>(<span class=\"s\">\"Can I get a refund on DevDojo Pro?\"<\/span>))<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><\/div><pre><code><span class=\"gp\">$<\/span> python mini_rag.py\nAnswer using ONLY the context below. Cite sources like [1].\nIf the answer is not in the context, say \"I don't know\".\n\nContext:\n[1] DevDojo Pro costs 499 rupees per month and includes all video courses.\n[2] Refunds are available within 7 days of purchase for DevDojo Pro.\n\nQuestion: Can I get a refund on DevDojo Pro?<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 29, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 4<\/span><span>UTF-8<\/span><span>LF<\/span><span>Python<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p2\">\n<div class=\"dd-vsc-crumbs\">devdojo-rag-basics \u203a mini_rag.mjs<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ 1. Your private knowledge base (normally: chunks from your docs)<\/span><\/span>\n<span class=\"l\"><span class=\"kc\">const<\/span> <span class=\"n\">chunks<\/span> = [<\/span>\n<span class=\"l\">  <span class=\"s\">\"DevDojo Pro costs 499 rupees per month and includes all video courses.\"<\/span>,<\/span>\n<span class=\"l\">  <span class=\"s\">\"Refunds are available within 7 days of purchase for DevDojo Pro.\"<\/span>,<\/span>\n<span class=\"l\">  <span class=\"s\">\"Support is available on email from 9 AM to 9 PM IST, Monday to Saturday.\"<\/span>,<\/span>\n<span class=\"l\">  <span class=\"s\">\"Certificates are issued after you finish 80 percent of a course.\"<\/span>,<\/span>\n<span class=\"l\">];<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"kc\">const<\/span> <span class=\"n\">words<\/span> = (<span class=\"n\">text<\/span>) =&gt; new <span class=\"nb\">Set<\/span>(<span class=\"n\">text<\/span>.<span class=\"n\">toLowerCase<\/span>().<span class=\"n\">match<\/span>(<span class=\"s\">\/[a-z0-9]+\/g<\/span>));<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\">\/\/ 2. Retrieve: score each chunk by shared words (real apps use embeddings)<\/span><\/span>\n<span class=\"l\"><span class=\"kc\">function<\/span> <span class=\"n\">retrieve<\/span>(<span class=\"n\">question<\/span>, <span class=\"n\">k<\/span> = <span class=\"m\">2<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"kc\">const<\/span> <span class=\"n\">q<\/span> = <span class=\"n\">words<\/span>(<span class=\"n\">question<\/span>);<\/span>\n<span class=\"l\">  <span class=\"kc\">const<\/span> <span class=\"n\">score<\/span> = (<span class=\"n\">c<\/span>) =&gt; [...<span class=\"n\">words<\/span>(<span class=\"n\">c<\/span>)].<span class=\"n\">filter<\/span>((<span class=\"n\">w<\/span>) =&gt; <span class=\"n\">q<\/span>.<span class=\"n\">has<\/span>(<span class=\"n\">w<\/span>)).<span class=\"n\">length<\/span>;<\/span>\n<span class=\"l\">  <span class=\"k\">return<\/span> [...<span class=\"n\">chunks<\/span>].<span class=\"n\">sort<\/span>((<span class=\"n\">a<\/span>, <span class=\"n\">b<\/span>) =&gt; <span class=\"n\">score<\/span>(<span class=\"n\">b<\/span>) - <span class=\"n\">score<\/span>(<span class=\"n\">a<\/span>)).<span class=\"n\">slice<\/span>(<span class=\"m\">0<\/span>, <span class=\"n\">k<\/span>);<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\">\/\/ 3. Augment: put the retrieved chunks into the prompt<\/span><\/span>\n<span class=\"l\"><span class=\"kc\">function<\/span> <span class=\"n\">buildPrompt<\/span>(<span class=\"n\">question<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"kc\">const<\/span> <span class=\"n\">context<\/span> = <span class=\"n\">retrieve<\/span>(<span class=\"n\">question<\/span>).<span class=\"n\">map<\/span>((<span class=\"n\">c<\/span>, <span class=\"n\">i<\/span>) =&gt; <span class=\"s\">`[${<\/span><span class=\"n\">i<\/span> + <span class=\"m\">1<\/span><span class=\"s\">}] ${<\/span><span class=\"n\">c<\/span><span class=\"s\">}`<\/span>).<span class=\"n\">join<\/span>(<span class=\"s\">\"\\n\"<\/span>);<\/span>\n<span class=\"l\">  <span class=\"k\">return<\/span> [<\/span>\n<span class=\"l\">    <span class=\"s\">\"Answer using ONLY the context below. Cite sources like [1].\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">'If the answer is not in the context, say \"I don\\'t know\".'<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">\"\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">`Context:\\n${<\/span><span class=\"n\">context<\/span><span class=\"s\">}`<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">\"\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">`Question: ${<\/span><span class=\"n\">question<\/span><span class=\"s\">}`<\/span>,<\/span>\n<span class=\"l\">  ].<span class=\"n\">join<\/span>(<span class=\"s\">\"\\n\"<\/span>);<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"n\">buildPrompt<\/span>(<span class=\"s\">\"Can I get a refund on DevDojo Pro?\"<\/span>));<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><\/div><pre><code><span class=\"gp\">$<\/span> node mini_rag.mjs\nAnswer using ONLY the context below. Cite sources like [1].\nIf the answer is not in the context, say \"I don't know\".\n\nContext:\n[1] DevDojo Pro costs 499 rupees per month and includes all video courses.\n[2] Refunds are available within 7 days of purchase for DevDojo Pro.\n\nQuestion: Can I get a refund on DevDojo Pro?<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 31, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Switch between the <strong>Python<\/strong> and <strong>Node.js<\/strong> tabs and click <strong>\u25b6 Run<\/strong>. This is real output, no API key needed.<\/p>\n<div class=\"dd-warn\"><strong>Notice the bug:<\/strong> the pricing chunk ranked <em>first<\/em> for a refund question. Why? Simple word matching sees &#8220;refund&#8221; and &#8220;refunds&#8221; as different words, but counts &#8220;DevDojo&#8221; and &#8220;Pro&#8221; twice. This is exactly why real RAG apps use <strong>embeddings<\/strong>, which understand that &#8220;refund&#8221;, &#8220;refunds&#8221; and &#8220;money back&#8221; mean the same thing.<\/div>\n\n<h2 id=\"rag-prompt\">How to write a good RAG prompt<\/h2>\n<p>Retrieval gets the facts; the prompt decides how the model uses them. A reliable RAG prompt has three parts:<\/p>\n<figure>\n<svg viewBox=\"0 0 900 330\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" role=\"img\" aria-label=\"Anatomy of a RAG prompt: rules at the top, numbered context chunks in the middle, and the user question at the bottom\">\n<rect width=\"900\" height=\"330\" rx=\"16\" fill=\"#0f1430\"\/>\n<g font-family=\"Segoe UI,Arial,sans-serif\">\n<text x=\"450\" y=\"38\" text-anchor=\"middle\" font-size=\"20\" font-weight=\"700\" fill=\"#fff\">Anatomy of a RAG prompt<\/text>\n<rect x=\"60\" y=\"60\" width=\"560\" height=\"64\" rx=\"12\" fill=\"#2a2160\" stroke=\"#7c5cff\"\/><text x=\"80\" y=\"88\" font-size=\"15\" fill=\"#fff\">Answer ONLY from the context. Cite sources like [1].<\/text><text x=\"80\" y=\"110\" font-size=\"15\" fill=\"#fff\">If the answer is not there, say &#8220;I don&#8217;t know&#8221;.<\/text>\n<rect x=\"60\" y=\"136\" width=\"560\" height=\"104\" rx=\"12\" fill=\"#1b2250\" stroke=\"#61dafb\"\/><text x=\"80\" y=\"164\" font-size=\"15\" fill=\"#a8ecfb\">Context:<\/text><text x=\"80\" y=\"190\" font-size=\"15\" fill=\"#e8ebff\">[1] Refunds are available within 7 days of purchase&#8230;<\/text><text x=\"80\" y=\"216\" font-size=\"15\" fill=\"#e8ebff\">[2] DevDojo Pro costs 499 rupees per month&#8230;<\/text>\n<rect x=\"60\" y=\"252\" width=\"560\" height=\"50\" rx=\"12\" fill=\"#1b2250\" stroke=\"#89e051\"\/><text x=\"80\" y=\"283\" font-size=\"15\" fill=\"#b8f0a0\">Question: Can I get a refund on DevDojo Pro?<\/text>\n<g font-size=\"15\" fill=\"#aab4e8\"><text x=\"650\" y=\"98\">&#8592; Rules: stop guessing<\/text><text x=\"650\" y=\"192\">&#8592; Retrieved chunks<\/text><text x=\"650\" y=\"214\">   (numbered for citations)<\/text><text x=\"650\" y=\"283\">&#8592; The user&#8217;s question<\/text><\/g>\n<\/g><\/svg>\n<figcaption>A good RAG prompt has three parts: clear rules, numbered context chunks, and the question. Numbering the chunks lets the model cite its sources.<\/figcaption>\n<\/figure>\n<ul>\n<li><strong>Grounding rule:<\/strong> &#8220;Answer using ONLY the context.&#8221; Without it, the model mixes your facts with its own guesses.<\/li>\n<li><strong>Permission to say &#8220;I don&#8217;t know&#8221;:<\/strong> otherwise the model feels it must answer something, and invents it.<\/li>\n<li><strong>Citations:<\/strong> number the chunks (or use file names) and ask the model to cite them. Show the sources in your UI.<\/li>\n<\/ul>\n<div class=\"dd-tip\"><strong>Tip:<\/strong> put the rules in the <strong>system<\/strong> message and the context + question in the <strong>user<\/strong> message, as we learned in <a href=\"https:\/\/devdojo.co.in\/?p=240\">Part 4: Prompt Engineering Basics<\/a>.<\/div>\n\n<h2 id=\"rag-ollama\">A real RAG app with Ollama (free, local)<\/h2>\n<p>Now we replace word matching with real embeddings (<code>nomic-embed-text<\/code>) and add a real LLM (<code>llama3.2<\/code>), both running on your laptop through Ollama. For four chunks we keep vectors in memory; for thousands, store them in a vector database.<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: rag_ollama.py, rag_ollama.mjs\">\n<input type=\"radio\" name=\"v2\" id=\"v2f1\" class=\"f1\" checked>\n<input type=\"radio\" name=\"v2\" id=\"v2f2\" class=\"f2\">\n<input type=\"checkbox\" id=\"v2r1\" class=\"r1\">\n<input type=\"checkbox\" id=\"v2r2\" class=\"r2\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">rag_ollama.py \u2014 devdojo-rag-basics \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v2f1\" class=\"t1\"><b class=\"ic py\">PY<\/b>rag_ollama.py<\/label>\n<label for=\"v2f2\" class=\"t2\"><b class=\"ic js\">JS<\/b>rag_ollama.mjs<\/label>\n<label for=\"v2r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<label for=\"v2r2\" class=\"run run2\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">devdojo-rag-basics \u203a rag_ollama.py<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># pip install ollama numpy<\/span><\/span>\n<span class=\"l\"><span class=\"c\"># ollama pull nomic-embed-text &amp;&amp; ollama pull llama3.2<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">import<\/span> <span class=\"n\">numpy<\/span> <span class=\"k\">as<\/span> <span class=\"n\">np<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">import<\/span> <span class=\"n\">ollama<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">chunks<\/span> = [<\/span>\n<span class=\"l\">    <span class=\"s\">\"DevDojo Pro costs 499 rupees per month and includes all video courses.\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">\"Refunds are available within 7 days of purchase for DevDojo Pro.\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">\"Support is available on email from 9 AM to 9 PM IST, Monday to Saturday.\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">\"Certificates are issued after you finish 80 percent of a course.\"<\/span>,<\/span>\n<span class=\"l\">]<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">embed<\/span>(<span class=\"n\">texts<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">vecs<\/span> = <span class=\"n\">np<\/span>.<span class=\"n\">array<\/span>(<span class=\"n\">ollama<\/span>.<span class=\"n\">embed<\/span>(<span class=\"n\">model<\/span>=<span class=\"s\">\"nomic-embed-text\"<\/span>, <span class=\"nb\">input<\/span>=<span class=\"n\">texts<\/span>)[<span class=\"s\">\"embeddings\"<\/span>])<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"n\">vecs<\/span> \/ <span class=\"n\">np<\/span>.<span class=\"n\">linalg<\/span>.<span class=\"n\">norm<\/span>(<span class=\"n\">vecs<\/span>, <span class=\"n\">axis<\/span>=<span class=\"m\">1<\/span>, <span class=\"n\">keepdims<\/span>=<span class=\"kc\">True<\/span>)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\"># Indexing phase: run once, store the vectors (Part 10: use a vector DB)<\/span><\/span>\n<span class=\"l\"><span class=\"n\">chunk_vecs<\/span> = <span class=\"n\">embed<\/span>(<span class=\"n\">chunks<\/span>)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">ask<\/span>(<span class=\"n\">question<\/span>, <span class=\"n\">k<\/span>=<span class=\"m\">2<\/span>):<\/span>\n<span class=\"l\">    <span class=\"c\"># Retrieval: find the k most similar chunks<\/span><\/span>\n<span class=\"l\">    <span class=\"n\">scores<\/span> = <span class=\"n\">chunk_vecs<\/span> @ <span class=\"n\">embed<\/span>([<span class=\"n\">question<\/span>])[<span class=\"m\">0<\/span>]<\/span>\n<span class=\"l\">    <span class=\"n\">top<\/span> = <span class=\"n\">np<\/span>.<span class=\"n\">argsort<\/span>(-<span class=\"n\">scores<\/span>)[:<span class=\"n\">k<\/span>]<\/span>\n<span class=\"l\">    <span class=\"n\">context<\/span> = <span class=\"s\">\"\\n\"<\/span>.<span class=\"n\">join<\/span>(<span class=\"s\">f\"[{<\/span><span class=\"n\">n<\/span><span class=\"s\">}] {<\/span><span class=\"n\">chunks<\/span>[<span class=\"n\">i<\/span>]<span class=\"s\">}\"<\/span> <span class=\"k\">for<\/span> <span class=\"n\">n<\/span>, <span class=\"n\">i<\/span> in <span class=\"nb\">enumerate<\/span>(<span class=\"n\">top<\/span>, <span class=\"m\">1<\/span>))<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\">    <span class=\"c\"># Augmented generation<\/span><\/span>\n<span class=\"l\">    <span class=\"n\">response<\/span> = <span class=\"n\">ollama<\/span>.<span class=\"n\">chat<\/span>(<span class=\"n\">model<\/span>=<span class=\"s\">\"llama3.2\"<\/span>, <span class=\"n\">messages<\/span>=[<\/span>\n<span class=\"l\">        {<span class=\"s\">\"role\"<\/span>: <span class=\"s\">\"system\"<\/span>, <span class=\"s\">\"content\"<\/span>:<\/span>\n<span class=\"l\">            <span class=\"s\">\"Answer using ONLY the context. Cite sources like [1]. \"<\/span><\/span>\n<span class=\"l\">            <span class=\"s\">\"If the answer is not in the context, say \\\"I don't know\\\".\"<\/span>},<\/span>\n<span class=\"l\">        {<span class=\"s\">\"role\"<\/span>: <span class=\"s\">\"user\"<\/span>, <span class=\"s\">\"content\"<\/span>: <span class=\"s\">f\"Context:\\n{<\/span><span class=\"n\">context<\/span><span class=\"s\">}\\n\\nQuestion: {<\/span><span class=\"n\">question<\/span><span class=\"s\">}\"<\/span>},<\/span>\n<span class=\"l\">    ], <span class=\"n\">options<\/span>={<span class=\"s\">\"temperature\"<\/span>: <span class=\"m\">0<\/span>})<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"n\">response<\/span>[<span class=\"s\">\"message\"<\/span>][<span class=\"s\">\"content\"<\/span>]<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"n\">ask<\/span>(<span class=\"s\">\"Can I get my money back if I don't like DevDojo Pro?\"<\/span>))<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"n\">ask<\/span>(<span class=\"s\">\"Do you offer a mobile app?\"<\/span>))<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Example output \u2013 your model may word it differently<\/span><\/div><pre><code><span class=\"gp\">$<\/span> python rag_ollama.py\nYes. Refunds are available within 7 days of purchase for DevDojo Pro [1].\nI don't know. The context does not mention a mobile app.<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 36, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 4<\/span><span>UTF-8<\/span><span>LF<\/span><span>Python<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p2\">\n<div class=\"dd-vsc-crumbs\">devdojo-rag-basics \u203a rag_ollama.mjs<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ npm install ollama<\/span><\/span>\n<span class=\"l\"><span class=\"c\">\/\/ ollama pull nomic-embed-text &amp;&amp; ollama pull llama3.2<\/span><\/span>\n<span class=\"l\"><span class=\"k\">import<\/span> <span class=\"n\">ollama<\/span> <span class=\"k\">from<\/span> <span class=\"s\">\"ollama\"<\/span>;<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"kc\">const<\/span> <span class=\"n\">chunks<\/span> = [<\/span>\n<span class=\"l\">  <span class=\"s\">\"DevDojo Pro costs 499 rupees per month and includes all video courses.\"<\/span>,<\/span>\n<span class=\"l\">  <span class=\"s\">\"Refunds are available within 7 days of purchase for DevDojo Pro.\"<\/span>,<\/span>\n<span class=\"l\">  <span class=\"s\">\"Support is available on email from 9 AM to 9 PM IST, Monday to Saturday.\"<\/span>,<\/span>\n<span class=\"l\">  <span class=\"s\">\"Certificates are issued after you finish 80 percent of a course.\"<\/span>,<\/span>\n<span class=\"l\">];<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"kc\">const<\/span> <span class=\"n\">embed<\/span> = <span class=\"k\">async<\/span> (<span class=\"n\">input<\/span>) =&gt;<\/span>\n<span class=\"l\">  (<span class=\"k\">await<\/span> <span class=\"n\">ollama<\/span>.<span class=\"n\">embed<\/span>({ <span class=\"n\">model<\/span>: <span class=\"s\">\"nomic-embed-text\"<\/span>, <span class=\"n\">input<\/span> })).<span class=\"n\">embeddings<\/span>;<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"kc\">const<\/span> <span class=\"n\">cosine<\/span> = (<span class=\"n\">a<\/span>, <span class=\"n\">b<\/span>) =&gt; {<\/span>\n<span class=\"l\">  <span class=\"kc\">let<\/span> <span class=\"n\">dot<\/span> = <span class=\"m\">0<\/span>, <span class=\"n\">na<\/span> = <span class=\"m\">0<\/span>, <span class=\"n\">nb<\/span> = <span class=\"m\">0<\/span>;<\/span>\n<span class=\"l\">  <span class=\"k\">for<\/span> (<span class=\"kc\">let<\/span> <span class=\"n\">i<\/span> = <span class=\"m\">0<\/span>; <span class=\"n\">i<\/span> &lt; <span class=\"n\">a<\/span>.<span class=\"n\">length<\/span>; <span class=\"n\">i<\/span>++) { <span class=\"n\">dot<\/span> += <span class=\"n\">a<\/span>[<span class=\"n\">i<\/span>] * <span class=\"n\">b<\/span>[<span class=\"n\">i<\/span>]; <span class=\"n\">na<\/span> += <span class=\"n\">a<\/span>[<span class=\"n\">i<\/span>] ** <span class=\"m\">2<\/span>; <span class=\"n\">nb<\/span> += <span class=\"n\">b<\/span>[<span class=\"n\">i<\/span>] ** <span class=\"m\">2<\/span>; }<\/span>\n<span class=\"l\">  <span class=\"k\">return<\/span> <span class=\"n\">dot<\/span> \/ <span class=\"nb\">Math<\/span>.<span class=\"n\">sqrt<\/span>(<span class=\"n\">na<\/span> * <span class=\"n\">nb<\/span>);<\/span>\n<span class=\"l\">};<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\">\/\/ Indexing phase: run once, store the vectors (Part 10: use a vector DB)<\/span><\/span>\n<span class=\"l\"><span class=\"kc\">const<\/span> <span class=\"n\">chunkVecs<\/span> = <span class=\"k\">await<\/span> <span class=\"n\">embed<\/span>(<span class=\"n\">chunks<\/span>);<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">async<\/span> <span class=\"kc\">function<\/span> <span class=\"n\">ask<\/span>(<span class=\"n\">question<\/span>, <span class=\"n\">k<\/span> = <span class=\"m\">2<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"c\">\/\/ Retrieval: find the k most similar chunks<\/span><\/span>\n<span class=\"l\">  <span class=\"kc\">const<\/span> [<span class=\"n\">qVec<\/span>] = <span class=\"k\">await<\/span> <span class=\"n\">embed<\/span>(<span class=\"n\">question<\/span>);<\/span>\n<span class=\"l\">  <span class=\"kc\">const<\/span> <span class=\"n\">top<\/span> = <span class=\"n\">chunks<\/span><\/span>\n<span class=\"l\">    .<span class=\"n\">map<\/span>((<span class=\"n\">text<\/span>, <span class=\"n\">i<\/span>) =&gt; ({ <span class=\"n\">text<\/span>, <span class=\"n\">score<\/span>: <span class=\"n\">cosine<\/span>(<span class=\"n\">qVec<\/span>, <span class=\"n\">chunkVecs<\/span>[<span class=\"n\">i<\/span>]) }))<\/span>\n<span class=\"l\">    .<span class=\"n\">sort<\/span>((<span class=\"n\">a<\/span>, <span class=\"n\">b<\/span>) =&gt; <span class=\"n\">b<\/span>.<span class=\"n\">score<\/span> - <span class=\"n\">a<\/span>.<span class=\"n\">score<\/span>)<\/span>\n<span class=\"l\">    .<span class=\"n\">slice<\/span>(<span class=\"m\">0<\/span>, <span class=\"n\">k<\/span>);<\/span>\n<span class=\"l\">  <span class=\"kc\">const<\/span> <span class=\"n\">context<\/span> = <span class=\"n\">top<\/span>.<span class=\"n\">map<\/span>((<span class=\"n\">c<\/span>, <span class=\"n\">n<\/span>) =&gt; <span class=\"s\">`[${<\/span><span class=\"n\">n<\/span> + <span class=\"m\">1<\/span><span class=\"s\">}] ${<\/span><span class=\"n\">c<\/span>.<span class=\"n\">text<\/span><span class=\"s\">}`<\/span>).<span class=\"n\">join<\/span>(<span class=\"s\">\"\\n\"<\/span>);<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\">  <span class=\"c\">\/\/ Augmented generation<\/span><\/span>\n<span class=\"l\">  <span class=\"kc\">const<\/span> <span class=\"n\">res<\/span> = <span class=\"k\">await<\/span> <span class=\"n\">ollama<\/span>.<span class=\"n\">chat<\/span>({<\/span>\n<span class=\"l\">    <span class=\"n\">model<\/span>: <span class=\"s\">\"llama3.2\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">messages<\/span>: [<\/span>\n<span class=\"l\">      { <span class=\"n\">role<\/span>: <span class=\"s\">\"system\"<\/span>, <span class=\"n\">content<\/span>:<\/span>\n<span class=\"l\">        <span class=\"s\">'Answer using ONLY the context. Cite sources like [1]. If the answer is not in the context, say \"I don\\'t know\".'<\/span> },<\/span>\n<span class=\"l\">      { <span class=\"n\">role<\/span>: <span class=\"s\">\"user\"<\/span>, <span class=\"n\">content<\/span>: <span class=\"s\">`Context:\\n${<\/span><span class=\"n\">context<\/span><span class=\"s\">}\\n\\nQuestion: ${<\/span><span class=\"n\">question<\/span><span class=\"s\">}`<\/span> },<\/span>\n<span class=\"l\">    ],<\/span>\n<span class=\"l\">    <span class=\"n\">options<\/span>: { <span class=\"n\">temperature<\/span>: <span class=\"m\">0<\/span> },<\/span>\n<span class=\"l\">  });<\/span>\n<span class=\"l\">  <span class=\"k\">return<\/span> <span class=\"n\">res<\/span>.<span class=\"n\">message<\/span>.<span class=\"n\">content<\/span>;<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"k\">await<\/span> <span class=\"n\">ask<\/span>(<span class=\"s\">\"Can I get my money back if I don't like DevDojo Pro?\"<\/span>));<\/span>\n<span class=\"l\"><span class=\"n\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"k\">await<\/span> <span class=\"n\">ask<\/span>(<span class=\"s\">\"Do you offer a mobile app?\"<\/span>));<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Example output \u2013 your model may word it differently<\/span><\/div><pre><code><span class=\"gp\">$<\/span> node rag_ollama.mjs\nYes. Refunds are available within 7 days of purchase for DevDojo Pro [1].\nI don't know. The context does not mention a mobile app.<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 47, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">A complete RAG app in about 40 lines, running 100% locally with <a href=\"https:\/\/devdojo.co.in\/?p=281\">Ollama<\/a>. Click the tabs, then <strong>\u25b6 Run<\/strong>.<\/p>\n<p>Two things to notice in the output: the first answer uses the refund chunk even though the question says &#8220;money back&#8221;, not &#8220;refund&#8221; (embeddings at work), and the second question is <em>not<\/em> in our documents, so the model says &#8220;I don&#8217;t know&#8221; instead of inventing a mobile app.<\/p>\n\n<h2 id=\"rag-chroma\">RAG with Chroma and OpenAI<\/h2>\n<p>In <a href=\"https:\/\/devdojo.co.in\/?p=310\">Part 10<\/a> you stored vectors in Chroma. Here is the same idea as a small but realistic app: chunks are stored once with a <code>source<\/code> in their metadata, and the answer cites the file name. Chroma uses a built-in local embedding model by default, and OpenAI generates the answer.<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: rag_chroma.py\">\n<input type=\"radio\" name=\"v3\" id=\"v3f1\" class=\"f1\" checked>\n<input type=\"checkbox\" id=\"v3r1\" class=\"r1\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">rag_chroma.py \u2014 devdojo-rag-basics \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v3f1\" class=\"t1\"><b class=\"ic py\">PY<\/b>rag_chroma.py<\/label>\n<label for=\"v3r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">devdojo-rag-basics \u203a rag_chroma.py<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># pip install chromadb openai   |   export OPENAI_API_KEY=...<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">import<\/span> <span class=\"n\">chromadb<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">from<\/span> <span class=\"n\">openai<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">OpenAI<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">client<\/span> = <span class=\"n\">OpenAI<\/span>()<\/span>\n<span class=\"l\"><span class=\"n\">db<\/span> = <span class=\"n\">chromadb<\/span>.<span class=\"n\">PersistentClient<\/span>(<span class=\"n\">path<\/span>=<span class=\"s\">\".\/rag_db\"<\/span>)<\/span>\n<span class=\"l\"><span class=\"n\">docs<\/span> = <span class=\"n\">db<\/span>.<span class=\"n\">get_or_create_collection<\/span>(<span class=\"s\">\"devdojo_help\"<\/span>)  <span class=\"c\"># default local embeddings<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\"># Indexing: add chunks once, with metadata for citations<\/span><\/span>\n<span class=\"l\"><span class=\"n\">docs<\/span>.<span class=\"n\">upsert<\/span>(<\/span>\n<span class=\"l\">    <span class=\"n\">ids<\/span>=[<span class=\"s\">\"pricing-1\"<\/span>, <span class=\"s\">\"refunds-1\"<\/span>, <span class=\"s\">\"support-1\"<\/span>],<\/span>\n<span class=\"l\">    <span class=\"n\">documents<\/span>=[<\/span>\n<span class=\"l\">        <span class=\"s\">\"DevDojo Pro costs 499 rupees per month and includes all video courses.\"<\/span>,<\/span>\n<span class=\"l\">        <span class=\"s\">\"Refunds are available within 7 days of purchase for DevDojo Pro.\"<\/span>,<\/span>\n<span class=\"l\">        <span class=\"s\">\"Support is available on email from 9 AM to 9 PM IST, Monday to Saturday.\"<\/span>,<\/span>\n<span class=\"l\">    ],<\/span>\n<span class=\"l\">    <span class=\"n\">metadatas<\/span>=[{<span class=\"s\">\"source\"<\/span>: <span class=\"s\">\"pricing.md\"<\/span>}, {<span class=\"s\">\"source\"<\/span>: <span class=\"s\">\"refunds.md\"<\/span>}, {<span class=\"s\">\"source\"<\/span>: <span class=\"s\">\"support.md\"<\/span>}],<\/span>\n<span class=\"l\">)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">ask<\/span>(<span class=\"n\">question<\/span>: <span class=\"nb\">str<\/span>) -&gt; <span class=\"nb\">str<\/span>:<\/span>\n<span class=\"l\">    <span class=\"n\">hits<\/span> = <span class=\"n\">docs<\/span>.<span class=\"n\">query<\/span>(<span class=\"n\">query_texts<\/span>=[<span class=\"n\">question<\/span>], <span class=\"n\">n_results<\/span>=<span class=\"m\">2<\/span>)<\/span>\n<span class=\"l\">    <span class=\"n\">context<\/span> = <span class=\"s\">\"\\n\"<\/span>.<span class=\"n\">join<\/span>(<\/span>\n<span class=\"l\">        <span class=\"s\">f\"[{<\/span><span class=\"n\">m<\/span>[<span class=\"s\">'source'<\/span>]<span class=\"s\">}] {<\/span><span class=\"n\">d<\/span><span class=\"s\">}\"<\/span> <span class=\"k\">for<\/span> <span class=\"n\">d<\/span>, <span class=\"n\">m<\/span> in <span class=\"nb\">zip<\/span>(<span class=\"n\">hits<\/span>[<span class=\"s\">\"documents\"<\/span>][<span class=\"m\">0<\/span>], <span class=\"n\">hits<\/span>[<span class=\"s\">\"metadatas\"<\/span>][<span class=\"m\">0<\/span>])<\/span>\n<span class=\"l\">    )<\/span>\n<span class=\"l\">    <span class=\"n\">response<\/span> = <span class=\"n\">client<\/span>.<span class=\"n\">responses<\/span>.<span class=\"n\">create<\/span>(<\/span>\n<span class=\"l\">        <span class=\"n\">model<\/span>=<span class=\"s\">\"gpt-5-mini\"<\/span>,<\/span>\n<span class=\"l\">        <span class=\"n\">instructions<\/span>=<span class=\"s\">\"Answer only from the context. Cite the [source]. \"<\/span><\/span>\n<span class=\"l\">                     <span class=\"s\">\"If it is not in the context, say you don't know.\"<\/span>,<\/span>\n<span class=\"l\">        <span class=\"nb\">input<\/span>=<span class=\"s\">f\"Context:\\n{<\/span><span class=\"n\">context<\/span><span class=\"s\">}\\n\\nQuestion: {<\/span><span class=\"n\">question<\/span><span class=\"s\">}\"<\/span>,<\/span>\n<span class=\"l\">    )<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"n\">response<\/span>.<span class=\"n\">output_text<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"n\">ask<\/span>(<span class=\"s\">\"How long do I have to ask for a refund?\"<\/span>))<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Example output \u2013 your model may word it differently<\/span><\/div><pre><code><span class=\"gp\">$<\/span> python rag_chroma.py\nYou can ask for a refund within 7 days of purchase [refunds.md].<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 33, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 4<\/span><span>UTF-8<\/span><span>LF<\/span><span>Python<\/span><\/div>\n<\/div>\n<\/div>\n<div class=\"dd-note\"><strong>Going further:<\/strong> our standalone <a href=\"https:\/\/devdojo.co.in\/?p=198\">RAG tutorial with Python and pgvector<\/a> shows a bigger project with PostgreSQL. In this series, the next two parts improve each RAG step one by one: chunking (Part 12) and smarter retrieval (Part 13).<\/div>\n\n<h2 id=\"rag-vs\">RAG vs fine-tuning vs long context<\/h2>\n<div class=\"dd-tablewrap\"><table>\n<thead><tr><th>Approach<\/th><th>Best for<\/th><th>Updating data<\/th><th>Cost<\/th><\/tr><\/thead>\n<tbody>\n<tr><td><strong>RAG<\/strong><\/td><td>Private or changing knowledge, answers with sources<\/td><td>Easy: re-index the changed document<\/td><td>Low per question<\/td><\/tr>\n<tr><td><strong>Long context<\/strong> (paste everything)<\/td><td>A few documents, one-off analysis<\/td><td>Easy: paste the new version<\/td><td>High when documents are large<\/td><\/tr>\n<tr><td><strong>Fine-tuning<\/strong><\/td><td>Teaching a style, format or narrow skill<\/td><td>Hard: train again<\/td><td>Training cost up front<\/td><\/tr>\n<\/tbody><\/table><\/div>\n<p>Rule of thumb: <strong>start with RAG for knowledge, use fine-tuning for behaviour.<\/strong> We compare them in depth in Part 21.<\/p>\n\n<h2 id=\"errors\">Common errors and fixes<\/h2>\n<div class=\"dd-tablewrap\"><table>\n<thead><tr><th>Problem<\/th><th>Likely cause<\/th><th>Fix<\/th><\/tr><\/thead>\n<tbody>\n<tr><td>Answer ignores your documents<\/td><td>No grounding rule, or context placed after a long chat history<\/td><td>Add &#8220;answer only from the context&#8221; to the system message; put context right before the question<\/td><\/tr>\n<tr><td>Model invents facts when docs don&#8217;t have the answer<\/td><td>Prompt never allows &#8220;I don&#8217;t know&#8221;<\/td><td>Explicitly allow and ask for &#8220;I don&#8217;t know&#8221;; use temperature 0<\/td><\/tr>\n<tr><td>Right document exists but is never retrieved<\/td><td>Chunks too big or cut badly; <code>k<\/code> too small<\/td><td>Better chunking (Part 12), try <code>k=4<\/code>, hybrid search (Part 13)<\/td><\/tr>\n<tr><td>Dimension mismatch error from the vector DB<\/td><td>Documents and question embedded with different models<\/td><td>Use the same embedding model for indexing and querying<\/td><\/tr>\n<tr><td><code>model \"llama3.2\" not found<\/code><\/td><td>Model not downloaded in Ollama<\/td><td>Run <code>ollama pull llama3.2<\/code><\/td><\/tr>\n<tr><td>Answers are slow and expensive<\/td><td>Too many or too large chunks in every prompt<\/td><td>Send 3&#8211;5 focused chunks; cache answers to repeated questions<\/td><\/tr>\n<\/tbody><\/table><\/div>\n\n<h2 id=\"best-practices\">RAG best practices for beginners<\/h2>\n<ul>\n<li><strong>Store metadata<\/strong> (file name, page, URL, date) with every chunk so you can cite and filter.<\/li>\n<li><strong>Show sources in the UI.<\/strong> A clickable &#8220;[refunds.md]&#8221; is the fastest way to earn user trust.<\/li>\n<li><strong>Keep a small test set:<\/strong> 10&#8211;20 real questions with expected answers. Re-run them after every change.<\/li>\n<li><strong>Re-index when documents change<\/strong>, and use stable IDs (like <code>refunds-1<\/code>) with <code>upsert<\/code> so you don&#8217;t create duplicates.<\/li>\n<li><strong>Treat retrieved text as data, not instructions.<\/strong> A document that says &#8220;ignore previous instructions&#8221; should not control your bot (more in Part 25).<\/li>\n<\/ul>\n\n<h2 id=\"practice\">Practice task<\/h2>\n<div class=\"dd-tip\">\n<p><strong>Build a &#8220;College Help Desk&#8221; RAG bot:<\/strong><\/p>\n<ol>\n<li>Write 6&#8211;8 short chunks about an imaginary college: fees, hostel timings, library rules, exam dates, placement cell.<\/li>\n<li>Start from <code>rag_ollama.py<\/code> or <code>rag_ollama.mjs<\/code> and replace the chunks.<\/li>\n<li>Ask 5 questions: 3 that are answered in the chunks and 2 that are not. Check that the bot cites a chunk for the first 3 and says &#8220;I don&#8217;t know&#8221; for the last 2.<\/li>\n<li>Bonus: print the retrieved chunks and their similarity scores before the answer, so you can see <em>why<\/em> the bot answered the way it did.<\/li>\n<\/ol>\n<\/div>\n\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>RAG is the most practical way to make an LLM useful with <em>your<\/em> data. You index your documents once, retrieve the most relevant chunks for each question, and give them to the model with clear rules and citations. With the embeddings and vector databases from Parts 9 and 10, you now have every piece of a real RAG app.<\/p>\n\n<div class=\"dd-next\"><strong>Next part &#8594; Part 12: Document Loading and Chunking Strategies.<\/strong> RAG is only as good as its chunks. Next we load real PDFs, web pages and Markdown files, and compare chunking strategies (fixed size, overlap, by headings) to see which gives the best answers.<\/div>\n\n<div class=\"dd-cta\">\n<h3>New posts every day on DevDojo<\/h3>\n<p>Follow the full <strong>Generative AI series<\/strong> from basics to production. Bookmark <a href=\"https:\/\/devdojo.co.in\/\">devdojo.co.in<\/a> and open the roadmap above to see what is coming next.<\/p>\n<\/div>\n\n<h2 id=\"faq\">FAQ<\/h2>\n<details><summary>What does RAG stand for?<\/summary><p>RAG stands for Retrieval-Augmented Generation: retrieve relevant information from your own data, augment (add it to) the prompt, and let the LLM generate an answer from it.<\/p><\/details>\n<details><summary>Do I need a vector database for RAG?<\/summary><p>Not for small projects. A few hundred chunks can live in memory, as in our Ollama example. For thousands of chunks, or data shared by many users, use a vector database like Chroma, pgvector or Pinecone (see Part 10).<\/p><\/details>\n<details><summary>Does RAG stop hallucinations completely?<\/summary><p>No, but it reduces them a lot. Combine good retrieval, a grounding rule, permission to say &#8220;I don&#8217;t know&#8221;, low temperature and citations, and test with real questions.<\/p><\/details>\n<details><summary>Is RAG better than fine-tuning?<\/summary><p>For adding knowledge, especially private or frequently changing knowledge, RAG is usually the better first choice. Fine-tuning is better for teaching a style, format or narrow skill. Many production apps use both.<\/p><\/details>\n<details><summary>Can I build RAG without paying for an API?<\/summary><p>Yes. With Ollama you can run both the embedding model (<code>nomic-embed-text<\/code>) and the chat model (<code>llama3.2<\/code>) for free on your own computer, as shown above.<\/p><\/details>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Learn RAG fundamentals: how retrieval-augmented generation works, how to write a RAG prompt with citations, and build a RAG app in Python and Node.js.<\/p>\n","protected":false},"author":1,"featured_media":312,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[170,89,115,86,172,112],"class_list":["post-313","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai","tag-chromadb","tag-embeddings","tag-ollama","tag-rag","tag-retrieval-augmented-generation","tag-series-generative-ai"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.4 - aioseo.com -->\n\t<meta name=\"description\" content=\"Learn RAG fundamentals: how retrieval-augmented generation works, how to write a RAG prompt with citations, and build a RAG app in Python and Node.js.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Lokendra\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.4\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"DevDojo - Empowering Developers\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"RAG Fundamentals: Retrieval-Augmented Generation Explained\" \/>\n\t\t<meta property=\"og:description\" content=\"Learn RAG fundamentals: how retrieval-augmented generation works, how to write a RAG prompt with citations, and build a RAG app in Python and Node.js.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-11-29T14:39:49+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-11-29T14:39:49+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"RAG Fundamentals: Retrieval-Augmented Generation Explained\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Learn RAG fundamentals: how retrieval-augmented generation works, how to write a RAG prompt with citations, and build a RAG app in Python and Node.js.\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/11\\\/29\\\/genai-series-part-11-rag-fundamentals\\\/#blogposting\",\"name\":\"RAG Fundamentals: Retrieval-Augmented Generation Explained\",\"headline\":\"Generative AI Part 11: RAG Fundamentals: Retrieval-Augmented Generation Explained\",\"author\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/genai-series-part-11-rag-fundamentals.jpg\",\"width\":1200,\"height\":630,\"caption\":\"RAG fundamentals diagram: a question is used to retrieve matching document chunks, which go into an LLM that answers with sources\"},\"datePublished\":\"2026-11-29T14:39:49+00:00\",\"dateModified\":\"2026-11-29T14:39:49+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/11\\\/29\\\/genai-series-part-11-rag-fundamentals\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/11\\\/29\\\/genai-series-part-11-rag-fundamentals\\\/#webpage\"},\"articleSection\":\"AI, ChromaDB, Embeddings, Ollama, RAG, Retrieval-Augmented Generation, Series: Generative AI\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/11\\\/29\\\/genai-series-part-11-rag-fundamentals\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/devdojo.co.in\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/ai\\\/#listItem\",\"name\":\"AI\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/ai\\\/#listItem\",\"position\":2,\"name\":\"AI\",\"item\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/ai\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/11\\\/29\\\/genai-series-part-11-rag-fundamentals\\\/#listItem\",\"name\":\"Generative AI Part 11: RAG Fundamentals: Retrieval-Augmented Generation Explained\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/11\\\/29\\\/genai-series-part-11-rag-fundamentals\\\/#listItem\",\"position\":3,\"name\":\"Generative AI Part 11: RAG Fundamentals: Retrieval-Augmented Generation Explained\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/ai\\\/#listItem\",\"name\":\"AI\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\",\"name\":\"DevDojo\",\"description\":\"Empowering Developers\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/\",\"name\":\"Lokendra\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/11\\\/29\\\/genai-series-part-11-rag-fundamentals\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/99f102f0cc9080237ce54b91304fd1b3b6fa29925cbaeba93cd796872e58b45c?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Lokendra\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/11\\\/29\\\/genai-series-part-11-rag-fundamentals\\\/#webpage\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/11\\\/29\\\/genai-series-part-11-rag-fundamentals\\\/\",\"name\":\"RAG Fundamentals: Retrieval-Augmented Generation Explained\",\"description\":\"Learn RAG fundamentals: how retrieval-augmented generation works, how to write a RAG prompt with citations, and build a RAG app in Python and Node.js.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/11\\\/29\\\/genai-series-part-11-rag-fundamentals\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/genai-series-part-11-rag-fundamentals.jpg\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/11\\\/29\\\/genai-series-part-11-rag-fundamentals\\\/#mainImage\",\"width\":1200,\"height\":630,\"caption\":\"RAG fundamentals diagram: a question is used to retrieve matching document chunks, which go into an LLM that answers with sources\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/11\\\/29\\\/genai-series-part-11-rag-fundamentals\\\/#mainImage\"},\"datePublished\":\"2026-11-29T14:39:49+00:00\",\"dateModified\":\"2026-11-29T14:39:49+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#website\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/\",\"name\":\"DevDojo\",\"description\":\"Empowering Developers\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"RAG Fundamentals: Retrieval-Augmented Generation Explained","description":"Learn RAG fundamentals: how retrieval-augmented generation works, how to write a RAG prompt with citations, and build a RAG app in Python and Node.js.","canonical_url":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/#blogposting","name":"RAG Fundamentals: Retrieval-Augmented Generation Explained","headline":"Generative AI Part 11: RAG Fundamentals: Retrieval-Augmented Generation Explained","author":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"publisher":{"@id":"https:\/\/devdojo.co.in\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/devdojo.co.in\/wp-content\/uploads\/2026\/11\/genai-series-part-11-rag-fundamentals.jpg","width":1200,"height":630,"caption":"RAG fundamentals diagram: a question is used to retrieve matching document chunks, which go into an LLM that answers with sources"},"datePublished":"2026-11-29T14:39:49+00:00","dateModified":"2026-11-29T14:39:49+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/#webpage"},"isPartOf":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/#webpage"},"articleSection":"AI, ChromaDB, Embeddings, Ollama, RAG, Retrieval-Augmented Generation, Series: Generative AI"},{"@type":"BreadcrumbList","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/#listItem","position":1,"name":"Home","item":"https:\/\/devdojo.co.in\/","nextItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/#listItem","name":"AI"}},{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/#listItem","position":2,"name":"AI","item":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/","nextItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/#listItem","name":"Generative AI Part 11: RAG Fundamentals: Retrieval-Augmented Generation Explained"},"previousItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/#listItem","position":3,"name":"Generative AI Part 11: RAG Fundamentals: Retrieval-Augmented Generation Explained","previousItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/#listItem","name":"AI"}}]},{"@type":"Organization","@id":"https:\/\/devdojo.co.in\/#organization","name":"DevDojo","description":"Empowering Developers","url":"https:\/\/devdojo.co.in\/"},{"@type":"Person","@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author","url":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/","name":"Lokendra","image":{"@type":"ImageObject","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/99f102f0cc9080237ce54b91304fd1b3b6fa29925cbaeba93cd796872e58b45c?s=96&d=mm&r=g","width":96,"height":96,"caption":"Lokendra"}},{"@type":"WebPage","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/#webpage","url":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/","name":"RAG Fundamentals: Retrieval-Augmented Generation Explained","description":"Learn RAG fundamentals: how retrieval-augmented generation works, how to write a RAG prompt with citations, and build a RAG app in Python and Node.js.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/devdojo.co.in\/#website"},"breadcrumb":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/#breadcrumblist"},"author":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"creator":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"image":{"@type":"ImageObject","url":"https:\/\/devdojo.co.in\/wp-content\/uploads\/2026\/11\/genai-series-part-11-rag-fundamentals.jpg","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/#mainImage","width":1200,"height":630,"caption":"RAG fundamentals diagram: a question is used to retrieve matching document chunks, which go into an LLM that answers with sources"},"primaryImageOfPage":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/#mainImage"},"datePublished":"2026-11-29T14:39:49+00:00","dateModified":"2026-11-29T14:39:49+00:00"},{"@type":"WebSite","@id":"https:\/\/devdojo.co.in\/#website","url":"https:\/\/devdojo.co.in\/","name":"DevDojo","description":"Empowering Developers","inLanguage":"en-US","publisher":{"@id":"https:\/\/devdojo.co.in\/#organization"}}]},"og:locale":"en_US","og:site_name":"DevDojo - Empowering Developers","og:type":"article","og:title":"RAG Fundamentals: Retrieval-Augmented Generation Explained","og:description":"Learn RAG fundamentals: how retrieval-augmented generation works, how to write a RAG prompt with citations, and build a RAG app in Python and Node.js.","og:url":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/","article:published_time":"2026-11-29T14:39:49+00:00","article:modified_time":"2026-11-29T14:39:49+00:00","twitter:card":"summary_large_image","twitter:title":"RAG Fundamentals: Retrieval-Augmented Generation Explained","twitter:description":"Learn RAG fundamentals: how retrieval-augmented generation works, how to write a RAG prompt with citations, and build a RAG app in Python and Node.js."},"aioseo_meta_data":{"post_id":"313","title":"RAG Fundamentals: Retrieval-Augmented Generation Explained","description":"Learn RAG fundamentals: how retrieval-augmented generation works, how to write a RAG prompt with citations, and build a RAG app in Python and Node.js.","keywords":null,"keyphrases":{"focus":{"keyphrase":"RAG fundamentals","score":69},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":"RAG Fundamentals: Retrieval-Augmented Generation Explained","og_description":"Learn RAG fundamentals: how retrieval-augmented generation works, how to write a RAG prompt with citations, and build a RAG app in Python and Node.js.","og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":0,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"created":"2026-11-23 13:05:18","updated":"2026-11-29 14:39:49","focus_keyword":"RAG fundamentals","additional_keywords":null,"truseo_locale":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/devdojo.co.in\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/devdojo.co.in\/index.php\/category\/ai\/\" title=\"AI\">AI<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tGenerative AI Part 11: RAG Fundamentals: Retrieval-Augmented Generation Explained\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/devdojo.co.in\/"},{"label":"AI","link":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/"},{"label":"Generative AI Part 11: RAG Fundamentals: Retrieval-Augmented Generation Explained","link":"https:\/\/devdojo.co.in\/index.php\/2026\/11\/29\/genai-series-part-11-rag-fundamentals\/"}],"_links":{"self":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/comments?post=313"}],"version-history":[{"count":1,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/313\/revisions"}],"predecessor-version":[{"id":316,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/313\/revisions\/316"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/media\/312"}],"wp:attachment":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/media?parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/categories?post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/tags?post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}