<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sahil Sharma</title>
    <description>The latest articles on DEV Community by Sahil Sharma (@sahil_sharma_3043).</description>
    <link>https://dev.to/sahil_sharma_3043</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4270404%2Fe175e8b6-aa1c-479e-98da-d29b3227018f.png</url>
      <title>DEV Community: Sahil Sharma</title>
      <link>https://dev.to/sahil_sharma_3043</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sahil_sharma_3043"/>
    <language>en</language>
    <item>
      <title>RAG Explained Simply: How AI Reads a 700-Page Book to Answer Your Question</title>
      <dc:creator>Sahil Sharma</dc:creator>
      <pubDate>Sat, 28 Nov 2026 09:28:41 +0000</pubDate>
      <link>https://dev.to/sahil_sharma_3043/rag-explained-simply-how-ai-reads-a-700-page-book-to-answer-your-question-5chk</link>
      <guid>https://dev.to/sahil_sharma_3043/rag-explained-simply-how-ai-reads-a-700-page-book-to-answer-your-question-5chk</guid>
      <description>&lt;h2&gt;
  
  
  The Problem: A 700-Page Book and One Exam Question
&lt;/h2&gt;

&lt;p&gt;Imagine you are a student. Your Software Engineering exam is tomorrow. You have a 700-page textbook on your desk, and one question in your head:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What are the disadvantages of the Waterfall model?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You open ChatGPT (or any LLM) and ask it. You get an answer, but there are three problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It is not from YOUR book.&lt;/strong&gt; Your professor wants the answer the way your textbook explains it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It might be wrong.&lt;/strong&gt; LLMs sometimes make things up with full confidence. This is called &lt;em&gt;hallucination&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You can't paste 700 pages into the chat.&lt;/strong&gt; It is too big, too slow and too expensive.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So what if the AI could first &lt;em&gt;find the right pages&lt;/em&gt; in your book, &lt;em&gt;read only those&lt;/em&gt;, and then answer?&lt;/p&gt;

&lt;p&gt;That is exactly what &lt;strong&gt;RAG&lt;/strong&gt; does.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is RAG?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;RAG = Retrieval-Augmented Generation.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Break the name into three words and you already understand it:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Word&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;th&gt;In our example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Retrieval&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Find the relevant information&lt;/td&gt;
&lt;td&gt;Find the pages about the Waterfall model&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Augmented&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Add that information to the question&lt;/td&gt;
&lt;td&gt;Attach those pages to the student's question&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Generation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Let the LLM write the answer&lt;/td&gt;
&lt;td&gt;The LLM answers using those pages&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The simplest analogy:&lt;/strong&gt; RAG turns a &lt;em&gt;closed-book exam&lt;/em&gt; into an &lt;em&gt;open-book exam&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Without RAG, the LLM answers from memory. With RAG, the LLM first opens the right pages of the book, and then answers.&lt;/p&gt;

&lt;p&gt;Here is the full architecture I drew for this blog:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fo7t4j40tqiw7q1fl5set.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fo7t4j40tqiw7q1fl5set.png" alt="RAG Architecture" width="800" height="536"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The diagram has two parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The vertical flow (top to bottom):&lt;/strong&gt; preparing the book. This happens &lt;strong&gt;once&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The horizontal flow (left to right):&lt;/strong&gt; answering a question. This happens &lt;strong&gt;every time&lt;/strong&gt; a student asks something.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's walk through both.&lt;/p&gt;

&lt;h2&gt;
  
  
  Phase 1: Preparing the Book (Indexing)
&lt;/h2&gt;

&lt;p&gt;This is the vertical flow in the diagram. Think of it as making a super-smart index for the book, before anyone asks anything.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Concerned Doc
&lt;/h3&gt;

&lt;p&gt;This is our source of knowledge: the &lt;strong&gt;700-page Software Engineering book&lt;/strong&gt; (as a PDF, for example).&lt;/p&gt;

&lt;p&gt;It could be anything: company docs, legal files, a product manual, your college notes. RAG works the same way for all of them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Chunks and Metadata
&lt;/h3&gt;

&lt;p&gt;An LLM can't read 700 pages at once, and we don't want it to. So we &lt;strong&gt;cut the book into small pieces called chunks&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A chunk is usually a few hundred words (for example, a paragraph or half a page).&lt;/li&gt;
&lt;li&gt;Chunks often &lt;strong&gt;overlap a little&lt;/strong&gt;, so a sentence cut in the middle is not lost.&lt;/li&gt;
&lt;li&gt;Our 700-page book might become around &lt;strong&gt;3,000 to 5,000 chunks&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With every chunk, we also save &lt;strong&gt;metadata&lt;/strong&gt;, which is extra info about where the chunk came from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"The Waterfall model is rigid. Once a phase is completed, going back to change requirements is costly..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"chapter"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Chapter 2: Software Process Models"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"page"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;47&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Metadata helps later, when we want to show &lt;em&gt;"Source: Chapter 2, Page 47"&lt;/em&gt; in the answer.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 3: Embedding Model
&lt;/h3&gt;

&lt;p&gt;Computers don't understand meaning in words. They understand &lt;strong&gt;numbers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;embedding model&lt;/strong&gt; converts each chunk into a long list of numbers called a &lt;strong&gt;vector&lt;/strong&gt; (for example, 768 or 1536 numbers). This vector represents the &lt;em&gt;meaning&lt;/em&gt; of the text.&lt;/p&gt;

&lt;p&gt;The magic part: &lt;strong&gt;text with similar meaning gets similar numbers.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Disadvantages of the Waterfall model"&lt;/li&gt;
&lt;li&gt;"Problems with the linear sequential SDLC approach"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These two sentences use different words, but their vectors will be very close to each other. That is how RAG searches by &lt;em&gt;meaning&lt;/em&gt;, not by exact keywords.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 4: Vector DB
&lt;/h3&gt;

&lt;p&gt;Now we store every chunk's &lt;strong&gt;vector + original text + metadata&lt;/strong&gt; in a &lt;strong&gt;Vector Database&lt;/strong&gt; (like Pinecone, Qdrant, Chroma, or pgvector).&lt;/p&gt;

&lt;p&gt;A Vector DB is built for one special job: &lt;em&gt;"Given a vector, find me the most similar vectors, fast."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And that's it. &lt;strong&gt;The book is now ready.&lt;/strong&gt; This whole phase runs once. You only redo it when the book changes.&lt;/p&gt;
&lt;h2&gt;
  
  
  Phase 2: Answering the Student (Retrieval + Generation)
&lt;/h2&gt;

&lt;p&gt;This is the horizontal flow at the bottom of the diagram. It runs &lt;strong&gt;every time&lt;/strong&gt; someone asks a question.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1: User asks a question
&lt;/h3&gt;

&lt;p&gt;The student types:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What are the disadvantages of the Waterfall model?"&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Step 2: The question goes through the Embedding Model
&lt;/h3&gt;

&lt;p&gt;The question is converted into a vector, exactly like the chunks were.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; this must be the &lt;strong&gt;same embedding model&lt;/strong&gt; used in Phase 1. That's why the diagram shows the Embedding Model twice. If you use different models, the numbers won't match, and the search will return garbage. It's like searching an English index with a French dictionary.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 3: Search the Vector DB
&lt;/h3&gt;

&lt;p&gt;The Vector DB compares the question's vector with all ~5,000 chunk vectors and returns the &lt;strong&gt;top few closest matches&lt;/strong&gt; (usually 3 to 5). This is called &lt;strong&gt;similarity search&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In a few milliseconds, out of 700 pages, it finds something like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chunk from &lt;strong&gt;page 47&lt;/strong&gt;: "Waterfall is rigid, going back to a previous phase is costly..."&lt;/li&gt;
&lt;li&gt;Chunk from &lt;strong&gt;page 48&lt;/strong&gt;: "Working software is only available late in the life cycle..."&lt;/li&gt;
&lt;li&gt;Chunk from &lt;strong&gt;page 112&lt;/strong&gt;: "Comparison: Agile handles changing requirements better than Waterfall..."&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Step 4: Retrieved Doc (Chunks)
&lt;/h3&gt;

&lt;p&gt;These few chunks are the &lt;strong&gt;retrieved context&lt;/strong&gt;. Instead of 700 pages, we now have maybe 2 pages of exactly relevant text.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 5: LLM
&lt;/h3&gt;

&lt;p&gt;Now comes the &lt;em&gt;Augmented&lt;/em&gt; part. We build a prompt that combines the retrieved chunks with the student's question:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You are a helpful study assistant.
Answer the question using ONLY the context below.
If the answer is not in the context, say "I don't know".

Context:
[Page 47] Waterfall is rigid, going back to a previous phase is costly...
[Page 48] Working software is only available late in the life cycle...
[Page 112] Agile handles changing requirements better than Waterfall...

Question: What are the disadvantages of the Waterfall model?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The LLM reads this small, focused prompt and writes the answer. It is no longer guessing from memory. It is answering &lt;strong&gt;from the book&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 6: Response
&lt;/h3&gt;

&lt;p&gt;The student gets something like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The main disadvantages of the Waterfall model are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It is rigid. Changing requirements after a phase is done is costly.&lt;/li&gt;
&lt;li&gt;Working software comes very late in the cycle.&lt;/li&gt;
&lt;li&gt;It handles changing requirements poorly compared to Agile.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Source: Chapter 2, Pages 47, 48 and 112&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Accurate, from the textbook, and with page numbers to verify. That's RAG.&lt;/p&gt;
&lt;h2&gt;
  
  
  Deep Dive: How the Vector DB Finds the Right Chunks (Semantic Search + Top-K)
&lt;/h2&gt;

&lt;p&gt;Step 3 of Phase 2 is the heart of RAG. If this step picks the wrong pages, even the best LLM will give a wrong answer. So let's open it up.&lt;/p&gt;
&lt;h3&gt;
  
  
  Keyword Search vs Semantic Search
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Keyword search&lt;/th&gt;
&lt;th&gt;Semantic search&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Matches by&lt;/td&gt;
&lt;td&gt;Exact words&lt;/td&gt;
&lt;td&gt;Meaning&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Query: "Waterfall drawbacks"&lt;/td&gt;
&lt;td&gt;Misses a page that says "limitations of the linear model"&lt;/td&gt;
&lt;td&gt;Finds it, because the meaning is the same&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Query: "bug"&lt;/td&gt;
&lt;td&gt;Can match an insect page&lt;/td&gt;
&lt;td&gt;Understands the software context&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Works with&lt;/td&gt;
&lt;td&gt;Text&lt;/td&gt;
&lt;td&gt;Vectors (numbers)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Old-school search (like Ctrl+F) looks for the same words. &lt;strong&gt;Semantic search looks for the same meaning.&lt;/strong&gt; That is why RAG uses vectors.&lt;/p&gt;
&lt;h3&gt;
  
  
  Think of Vectors as Points on a Map
&lt;/h3&gt;

&lt;p&gt;Imagine every chunk of the book as a dot on a giant map.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chunks about &lt;strong&gt;Waterfall&lt;/strong&gt; sit close together in one area.&lt;/li&gt;
&lt;li&gt;Chunks about &lt;strong&gt;Agile and Scrum&lt;/strong&gt; sit in a nearby area (related topic).&lt;/li&gt;
&lt;li&gt;Chunks about &lt;strong&gt;Software Testing&lt;/strong&gt; sit further away.&lt;/li&gt;
&lt;li&gt;Chunks about &lt;strong&gt;UML diagrams&lt;/strong&gt; sit in a completely different corner.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When the student asks a question, the question also becomes a dot on this map. Semantic search simply asks: &lt;strong&gt;"Which book dots are closest to the question dot?"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Real vectors have hundreds or thousands of dimensions, not 2. But the idea is exactly the same: &lt;strong&gt;close = similar meaning.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  How "Close" is Measured: Cosine Similarity
&lt;/h3&gt;

&lt;p&gt;The most common way to measure closeness is &lt;strong&gt;cosine similarity&lt;/strong&gt;. It checks the &lt;strong&gt;angle&lt;/strong&gt; between two vectors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Same direction (small angle) = very similar meaning, score close to &lt;strong&gt;1&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;At a right angle = unrelated, score close to &lt;strong&gt;0&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Opposite direction = opposite meaning, score close to &lt;strong&gt;-1&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The formula (A = question vector, B = chunk vector):&lt;/p&gt;

&lt;p&gt;

&lt;/p&gt;
&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;cosine&amp;nbsp;similarity&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;B&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen"&gt;∥&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mclose"&gt;∥&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;∥&lt;/span&gt;&lt;span class="mord mathnormal"&gt;B&lt;/span&gt;&lt;span class="mclose"&gt;∥&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;⋅&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;B&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord sqrt"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span class="svg-align"&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mop"&gt;&lt;span class="mop op-symbol small-op"&gt;∑&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;span class="mrel mtight"&gt;=&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="hide-tail"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord sqrt"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span class="svg-align"&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mop"&gt;&lt;span class="mop op-symbol small-op"&gt;∑&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;span class="mrel mtight"&gt;=&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;B&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="hide-tail"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mop"&gt;&lt;span class="mop op-symbol small-op"&gt;∑&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;span class="mrel mtight"&gt;=&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;B&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;



&lt;p&gt;In simple words: multiply the matching numbers of both vectors, add them up, then divide by their lengths.&lt;/p&gt;

&lt;p&gt;Other measures you will see in Vector DB settings:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;What it measures&lt;/th&gt;
&lt;th&gt;Better score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Cosine similarity&lt;/td&gt;
&lt;td&gt;Angle between vectors&lt;/td&gt;
&lt;td&gt;Higher&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dot product&lt;/td&gt;
&lt;td&gt;Angle + length together&lt;/td&gt;
&lt;td&gt;Higher&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Euclidean (L2) distance&lt;/td&gt;
&lt;td&gt;Straight-line distance between points&lt;/td&gt;
&lt;td&gt;Lower&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Rule of thumb: use the metric your embedding model recommends. Most text models work best with cosine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Scoring Chunks for Our Question
&lt;/h3&gt;

&lt;p&gt;Question: &lt;em&gt;"What are the disadvantages of the Waterfall model?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The Vector DB compares it with the chunks and gets scores like these (illustrative numbers):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Rank&lt;/th&gt;
&lt;th&gt;Chunk (page)&lt;/th&gt;
&lt;th&gt;Text preview&lt;/th&gt;
&lt;th&gt;Cosine score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Page 47&lt;/td&gt;
&lt;td&gt;"Waterfall is rigid, going back to a phase is costly..."&lt;/td&gt;
&lt;td&gt;0.89&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Page 48&lt;/td&gt;
&lt;td&gt;"Working software comes late in the life cycle..."&lt;/td&gt;
&lt;td&gt;0.84&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Page 112&lt;/td&gt;
&lt;td&gt;"Agile handles changing requirements better than Waterfall..."&lt;/td&gt;
&lt;td&gt;0.78&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Page 46&lt;/td&gt;
&lt;td&gt;"Phases of the Waterfall model: requirements, design..."&lt;/td&gt;
&lt;td&gt;0.71&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Page 301&lt;/td&gt;
&lt;td&gt;"Unit testing checks individual modules..."&lt;/td&gt;
&lt;td&gt;0.22&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Notice page 46. It is &lt;em&gt;about&lt;/em&gt; Waterfall, but it describes the phases, not the disadvantages. So it scores a bit lower. That is semantic search understanding the exact intent of the question.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Top-K?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Top-K = "give me the K best-matching chunks."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If K = 3, the Vector DB returns ranks 1, 2 and 3 (pages 47, 48, 112) and ignores the rest. These K chunks become the context for the LLM.&lt;/p&gt;

&lt;p&gt;Choosing K is a balance:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;K value&lt;/th&gt;
&lt;th&gt;What happens&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Too small (K = 1)&lt;/td&gt;
&lt;td&gt;You may miss important info. Our answer would only have 1 disadvantage.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Balanced (K = 3 to 5)&lt;/td&gt;
&lt;td&gt;Enough context, little noise. A good default.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Too big (K = 20)&lt;/td&gt;
&lt;td&gt;Irrelevant chunks sneak in, the LLM gets confused, and token cost goes up.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Extra Controls Used with Top-K
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Similarity threshold:&lt;/strong&gt; only keep chunks above a minimum score (for example 0.75). If nothing passes, the bot can say "I don't know" instead of guessing. In our table, page 301 (0.22) would be dropped even if K was 5.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Metadata filtering:&lt;/strong&gt; search only inside certain chunks, like &lt;code&gt;chapter = "Chapter 2"&lt;/code&gt;. This is where the metadata from Phase 1 pays off.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Re-ranking:&lt;/strong&gt; fetch more chunks first (say top 20), then use a smarter re-ranker model to pick the best 3 to 5. More accurate, a bit slower.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MMR (Maximal Marginal Relevance):&lt;/strong&gt; avoids picking 3 chunks that all say the same thing, so the context covers more points.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hybrid search:&lt;/strong&gt; combine keyword search + semantic search. Useful for exact terms like error codes, names or formulas ("COCOMO", "IEEE 830").&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How is it So Fast? (ANN Search)
&lt;/h3&gt;

&lt;p&gt;For our book (~5,000 chunks), the DB could simply compare the question with every chunk. This is called &lt;strong&gt;brute-force&lt;/strong&gt; or &lt;strong&gt;exact search&lt;/strong&gt;, and it's fine at small scale.&lt;/p&gt;

&lt;p&gt;But real apps can have &lt;strong&gt;millions of chunks&lt;/strong&gt;. Comparing with all of them for every question is too slow. So Vector DBs use &lt;strong&gt;ANN (Approximate Nearest Neighbor)&lt;/strong&gt; search.&lt;/p&gt;

&lt;p&gt;The most popular ANN method is &lt;strong&gt;HNSW (Hierarchical Navigable Small World)&lt;/strong&gt;. Think of it like travelling across India:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First take a &lt;strong&gt;flight&lt;/strong&gt; to the right state (top layer, few long jumps).&lt;/li&gt;
&lt;li&gt;Then a &lt;strong&gt;train&lt;/strong&gt; to the right city (middle layer).&lt;/li&gt;
&lt;li&gt;Then an &lt;strong&gt;auto&lt;/strong&gt; to the exact street (bottom layer, short precise steps).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You never check every house in the country, but you still reach the right address. HNSW does the same with vectors: it jumps through layers of a graph and reaches the closest chunks in milliseconds.&lt;/p&gt;

&lt;p&gt;"Approximate" means it may very rarely miss the absolute best match, but it is thousands of times faster. For RAG, that trade-off is almost always worth it.&lt;/p&gt;

&lt;p&gt;Other ANN index types you might see: &lt;strong&gt;IVF&lt;/strong&gt; (group vectors into clusters, search only the closest clusters) and &lt;strong&gt;PQ&lt;/strong&gt; (compress vectors to save memory).&lt;/p&gt;

&lt;h3&gt;
  
  
  Semantic Search + Top-K in JavaScript (Brute-Force Version)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;cosineSimilarity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;dot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;normA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;normB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;dot&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;normA&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;normB&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dot&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;normA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;normB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;semanticSearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;queryVector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;topK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;minScore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.75&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;chunks&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;cosineSimilarity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;queryVector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;minScore&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// similarity threshold&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;// best match first&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;topK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                            &lt;span class="c1"&gt;// keep only Top-K&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A real Vector DB does the same job, just with an ANN index (like HNSW) instead of looping over every chunk.&lt;/p&gt;

&lt;h3&gt;
  
  
  Semantic Search in One Line
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Turn the question into a vector, find the K closest chunk vectors by cosine similarity, and hand those chunks to the LLM.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The Whole Flow in One Table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;Box in the diagram&lt;/th&gt;
&lt;th&gt;What happens&lt;/th&gt;
&lt;th&gt;Book example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Concerned Doc&lt;/td&gt;
&lt;td&gt;Load the source&lt;/td&gt;
&lt;td&gt;The 700-page SE book&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Chunks and Metadata&lt;/td&gt;
&lt;td&gt;Split into small pieces + save info&lt;/td&gt;
&lt;td&gt;~5,000 chunks with chapter and page&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Embedding Model&lt;/td&gt;
&lt;td&gt;Text to vectors (meaning as numbers)&lt;/td&gt;
&lt;td&gt;Each chunk becomes a vector&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Vector DB&lt;/td&gt;
&lt;td&gt;Store vectors for fast search&lt;/td&gt;
&lt;td&gt;Book is now searchable by meaning&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;User&lt;/td&gt;
&lt;td&gt;Asks a question&lt;/td&gt;
&lt;td&gt;"Disadvantages of Waterfall?"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Embedding Model&lt;/td&gt;
&lt;td&gt;Question to vector (same model!)&lt;/td&gt;
&lt;td&gt;Question becomes a vector&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Vector DB&lt;/td&gt;
&lt;td&gt;Similarity search, top-k chunks&lt;/td&gt;
&lt;td&gt;Finds pages 47, 48, 112&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Retrieved Doc (Chunks)&lt;/td&gt;
&lt;td&gt;Relevant context&lt;/td&gt;
&lt;td&gt;~2 pages instead of 700&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;LLM&lt;/td&gt;
&lt;td&gt;Question + context to answer&lt;/td&gt;
&lt;td&gt;Writes a grounded answer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;Response&lt;/td&gt;
&lt;td&gt;Shown to the user&lt;/td&gt;
&lt;td&gt;Answer with page sources&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Steps 1 to 4 run &lt;strong&gt;once&lt;/strong&gt;. Steps 5 to 10 run &lt;strong&gt;for every question&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The same thing in (pseudo) JavaScript
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Phase 1: run once&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;chunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;splitIntoChunks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;overlap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;embed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;vectorDB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Phase 2: run for every question&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;askRAG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;qVector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;embed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// same model!&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;topChunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;vectorDB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qVector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;topK&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;topChunks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="sb"&gt;`[Page ${c.page}] ${c.text}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`Answer using only this context:\n${context}\n\nQuestion: ${question}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the full idea of RAG in around 15 lines.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Not Just Paste the Whole Book?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Approach&lt;/th&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Ask the LLM directly&lt;/td&gt;
&lt;td&gt;Answers from general memory, may hallucinate, not from your book&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Paste all 700 pages&lt;/td&gt;
&lt;td&gt;Too many tokens, slow, costly, and LLMs miss details in very long inputs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fine-tune the LLM on the book&lt;/td&gt;
&lt;td&gt;Expensive, needs retraining when the book changes, still no page sources&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;RAG&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Sends only the relevant pages, cheap, up to date, and can cite sources&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Where RAG Can Go Wrong
&lt;/h2&gt;

&lt;p&gt;RAG is simple, but not magic. Watch out for these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bad chunking:&lt;/strong&gt; chunks too small lose context, chunks too big add noise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wrong chunks retrieved:&lt;/strong&gt; if search finds the wrong pages, the LLM gives a wrong answer with confidence.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Different embedding models:&lt;/strong&gt; question and chunks must use the same model.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stale data:&lt;/strong&gt; if the book gets a new edition, you must re-index it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most real-world RAG work is about fixing these four things.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Big Thank You
&lt;/h2&gt;

&lt;p&gt;Everything I explained here, I learned from &lt;strong&gt;Akshay Saini&lt;/strong&gt; and his &lt;strong&gt;Namaste AI&lt;/strong&gt; course.&lt;/p&gt;

&lt;p&gt;Akshay has a rare talent: he takes a topic that sounds scary, like RAG, embeddings or vector databases, and makes it feel simple and obvious. The way he explains the &lt;em&gt;why&lt;/em&gt; behind every step is what made this architecture click for me.&lt;/p&gt;

&lt;p&gt;Thank you, Akshay, for teaching with so much clarity and passion. If you want to learn AI engineering from the basics, the Namaste AI course is a great place to start.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;One line to remember:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;RAG = find the right pages first, then let the LLM answer from them.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is the open-book exam for AI. And now, you can explain it to anyone.&lt;/p&gt;

&lt;p&gt;If this helped you, drop a like or a comment. Tell me what you want me to break down next: chunking strategies, vector databases, or building a RAG app in Node.js?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy learning!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>beginners</category>
      <category>llm</category>
      <category>rag</category>
    </item>
  </channel>
</rss>
