<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Vector Databases | Ziyang Lin</title><link>https://ziyanglin.netlify.app/en/tags/vector-databases/</link><atom:link href="https://ziyanglin.netlify.app/en/tags/vector-databases/index.xml" rel="self" type="application/rss+xml"/><description>Vector Databases</description><generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><lastBuildDate>Mon, 30 Jun 2025 10:00:00 +0000</lastBuildDate><image><url>https://ziyanglin.netlify.app/img/icon-192.png</url><title>Vector Databases</title><link>https://ziyanglin.netlify.app/en/tags/vector-databases/</link></image><item><title>Retrieval-Augmented Generation (RAG): A Comprehensive Technical Analysis</title><link>https://ziyanglin.netlify.app/en/post/rag-technical-documentation/</link><pubDate>Mon, 30 Jun 2025 10:00:00 +0000</pubDate><guid>https://ziyanglin.netlify.app/en/post/rag-technical-documentation/</guid><description>&lt;h2 id="1-macro-overview-why-rag">1. Macro Overview: Why RAG?&lt;/h2>
&lt;h3 id="11-what-is-rag">1.1 What is RAG?&lt;/h3>
&lt;p>RAG, or Retrieval-Augmented Generation, is a technical framework that combines information retrieval from external knowledge bases with the powerful generative capabilities of large language models (LLMs). In simple terms, when a user asks a question, a RAG system first retrieves the most relevant information snippets from a vast, updatable knowledge base (such as company internal documents, product manuals, or the latest web information), and then &amp;ldquo;feeds&amp;rdquo; this information along with the original question to the language model, enabling it to generate answers based on precise, up-to-date context.&lt;/p>
&lt;p>To use an analogy: Imagine a student taking an open-book exam. This student (the LLM) has already learned a lot of knowledge (pre-training data), but when answering very specific questions or those involving the latest information, they can refer to reference books (external knowledge base). RAG is this &amp;ldquo;open-book&amp;rdquo; process, allowing the LLM to consult the most recent and authoritative materials when answering questions, thus providing more accurate and comprehensive answers.&lt;/p>
&lt;h3 id="12-rags-core-value-solving-llms-inherent-limitations">1.2 RAG's Core Value: Solving LLM's Inherent Limitations&lt;/h3>
&lt;p>Despite their power, large language models have several inherent limitations that RAG technology specifically addresses.&lt;/p>
&lt;p>&lt;strong>Limitation 1: Knowledge Cut-off&lt;/strong>&lt;/p>
&lt;p>An LLM's knowledge is frozen at the time of its last training. For example, a model completed in early 2023 cannot answer questions about events that occurred after that point. RAG completely solves this problem by introducing an external knowledge base that can be updated at any time. Companies can update their knowledge bases with the latest product information, financial reports, market dynamics, etc., and the RAG system can immediately leverage this new knowledge to answer questions.&lt;/p>
&lt;p>&lt;strong>Limitation 2: Hallucination&lt;/strong>&lt;/p>
&lt;p>When LLMs encounter questions outside their knowledge domain or with uncertain answers, they sometimes &amp;ldquo;confidently make things up,&amp;rdquo; fabricating facts and producing what are known as &amp;ldquo;hallucinations.&amp;rdquo; RAG greatly constrains model output by providing clear, fact-based reference materials. The model is required to answer based on the retrieved context, which effectively defines the scope of its response, significantly reducing the probability of hallucinations.&lt;/p>
&lt;p>&lt;strong>Limitation 3: Lack of Domain-Specific Knowledge&lt;/strong>&lt;/p>
&lt;p>General-purpose LLMs often perform poorly when handling specialized questions in specific industries or enterprises. For example, they don't understand a company's internal processes or the technical specifications of particular products. Through RAG, enterprises can build a specialized knowledge base containing internal regulations, technical documentation, customer support records, and more. This equips the LLM with domain expert knowledge, enabling it to handle highly specialized Q&amp;amp;A tasks.&lt;/p>
&lt;p>&lt;strong>Limitation 4: Lack of Transparency &amp;amp; Interpretability&lt;/strong>&lt;/p>
&lt;p>The answer generation process of traditional LLMs is a &amp;ldquo;black box&amp;rdquo; - we cannot know what information they based their conclusions on. This is fatal in fields requiring high credibility, such as finance, healthcare, and law. The RAG architecture naturally enhances transparency because the system can clearly show &amp;ldquo;I derived this answer based on these documents (Source 1, Source 2&amp;hellip;)&amp;quot;. Users can trace and verify the sources of information, greatly enhancing trust in the answers.&lt;/p>
&lt;h3 id="13-rags-macro-workflow">1.3 RAG's Macro Workflow&lt;/h3>
&lt;p>At the highest level, RAG's workflow can be depicted as a simple yet elegant architecture.&lt;/p>
&lt;pre>&lt;code class="language-mermaid">graph TD
A[&amp;quot;User Query&amp;quot;] --&amp;gt; B{RAG System};
B --&amp;gt; C[&amp;quot;Retrieve&amp;quot;];
C --&amp;gt; D[&amp;quot;External Knowledge Base&amp;quot;];
D --&amp;gt; C;
C --&amp;gt; E[&amp;quot;Augment&amp;quot;];
A --&amp;gt; E;
E --&amp;gt; F[&amp;quot;Generate&amp;quot;];
F --&amp;gt; G[LLM];
G --&amp;gt; F;
F --&amp;gt; H[&amp;quot;Final Answer with Sources&amp;quot;];
&lt;/code>&lt;/pre>
&lt;p>This workflow can be interpreted as:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Retrieve&lt;/strong>: After receiving a user's question, the system first converts it into a format suitable for searching (such as a vector), then quickly matches and retrieves the most relevant information snippets from the knowledge base.&lt;/li>
&lt;li>&lt;strong>Augment&lt;/strong>: The system integrates the retrieved information snippets with the user's original question into a richer &amp;ldquo;prompt.&amp;rdquo;&lt;/li>
&lt;li>&lt;strong>Generate&lt;/strong>: This enhanced prompt is sent to the LLM, guiding it to generate a content-rich and accurate answer based on the provided context, along with sources of information.&lt;/li>
&lt;/ol>
&lt;p>Through this process, RAG successfully transforms the LLM from a &amp;ldquo;closed-world scholar&amp;rdquo; into an &amp;ldquo;open-world, verifiable expert.&amp;rdquo;&lt;/p>
&lt;h2 id="2-rag-core-architecture-dual-process-analysis">2. RAG Core Architecture: Dual Process Analysis&lt;/h2>
&lt;p>The lifecycle of a RAG system can be clearly divided into two core processes:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Offline Process: Indexing&lt;/strong>: This is a preprocessing stage responsible for transforming raw data sources into a knowledge base ready for quick retrieval. This process typically runs in the background and is triggered whenever the knowledge base content needs updating.&lt;/li>
&lt;li>&lt;strong>Online Process: Retrieval &amp;amp; Generation&lt;/strong>: This is the real-time process of user interaction with the system, responsible for retrieving information from the index based on user input and generating answers.&lt;/li>
&lt;/ol>
&lt;p>Below, we'll analyze these two processes through detailed diagrams and explanations.&lt;/p>
&lt;h3 id="21-offline-process-indexing">2.1 Offline Process: Indexing&lt;/h3>
&lt;p>The goal of this process is to transform unstructured or semi-structured raw data into structured, easily queryable indices.&lt;/p>
&lt;pre>&lt;code class="language-mermaid">graph TD
subgraph &amp;quot;Offline Indexing Pipeline&amp;quot;
A[&amp;quot;Data Sources&amp;quot;] --&amp;gt; B[&amp;quot;Load&amp;quot;];
B --&amp;gt; C[&amp;quot;Split/Chunk&amp;quot;];
C --&amp;gt; D[&amp;quot;Embed&amp;quot;];
D --&amp;gt; E[&amp;quot;Store/Index&amp;quot;];
end
A --&amp;gt; A_Details(&amp;quot;e.g.: PDFs, .txt, .md, Notion, Confluence, databases&amp;quot;);
B --&amp;gt; B_Details(&amp;quot;Using data loaders, e.g., LlamaIndex Readers&amp;quot;);
C --&amp;gt; C_Details(&amp;quot;Strategies: fixed size, recursive splitting, semantic chunking&amp;quot;);
D --&amp;gt; D_Details(&amp;quot;Using Embedding models, e.g., BERT, Sentence-BERT, a-e-5-large-v2&amp;quot;);
E --&amp;gt; E_Details(&amp;quot;Store in vector databases, e.g., Chroma, Pinecone, FAISS&amp;quot;);
&lt;/code>&lt;/pre>
&lt;p>&lt;strong>Process Details:&lt;/strong>&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Load&lt;/strong>: The system first needs to load original documents from various specified data sources. These sources can be diverse, such as PDF files, Markdown documents, web pages, Notion pages, database records, etc. Modern RAG frameworks (like LlamaIndex, LangChain) provide rich data loader ecosystems to simplify this process.&lt;/li>
&lt;li>&lt;strong>Split/Chunk&lt;/strong>: Due to the limited context window of language models, directly embedding a long document (like a PDF with hundreds of pages) as a single vector performs poorly and loses many details. Therefore, it's essential to split long texts into smaller, semantically complete chunks. The chunking strategy is crucial and directly affects retrieval precision.&lt;/li>
&lt;li>&lt;strong>Embed&lt;/strong>: This is the core step of transforming textual information into machine-understandable mathematical representations. The system uses a pre-trained embedding model to map each text chunk to a high-dimensional vector. This vector captures the semantic information of the text, with semantically similar text chunks being closer to each other in the vector space.&lt;/li>
&lt;li>&lt;strong>Store/Index&lt;/strong>: Finally, the system stores the vector representations of all text chunks along with their metadata (such as source document, chapter, page number, etc.) in a specialized database, typically a vector database. Vector databases are specially optimized to support efficient similarity searches across massive-scale vector data.&lt;/li>
&lt;/ol>
&lt;h3 id="22-online-process-retrieval--generation">2.2 Online Process: Retrieval &amp;amp; Generation&lt;/h3>
&lt;p>This process is triggered when a user submits a query, with the goal of generating precise, evidence-based answers in real-time.&lt;/p>
&lt;pre>&lt;code class="language-mermaid">graph TD
A[&amp;quot;User Query&amp;quot;] --&amp;gt; B[&amp;quot;Embed Query&amp;quot;];
B --&amp;gt; C[&amp;quot;Vector Search&amp;quot;];
C &amp;lt;--&amp;gt; D[&amp;quot;Vector Database&amp;quot;];
C --&amp;gt; E[&amp;quot;Get Top-K Chunks&amp;quot;];
E --&amp;gt; F[&amp;quot;(Optional) Re-ranking&amp;quot;];
A &amp;amp; F --&amp;gt; G[&amp;quot;Build Prompt&amp;quot;];
G --&amp;gt; H[&amp;quot;LLM Generation&amp;quot;];
H --&amp;gt; I[&amp;quot;Final Answer&amp;quot;];
&lt;/code>&lt;/pre>
&lt;p>&lt;strong>Process Details:&lt;/strong>&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Embed Query&lt;/strong>: When a user inputs a question, the system uses the &lt;strong>same embedding model&lt;/strong> as in the indexing phase to convert this question into a query vector.&lt;/li>
&lt;li>&lt;strong>Vector Search&lt;/strong>: The system takes this query vector and performs a similarity search in the vector database. The most common algorithm is &amp;ldquo;K-Nearest Neighbors&amp;rdquo; (KNN), aiming to find the K text chunk vectors closest to the query vector in the vector space.&lt;/li>
&lt;li>&lt;strong>Get Top-K Chunks&lt;/strong>: Based on the search results, the system retrieves the original content of these K most relevant text chunks from the database. These K text chunks form the core context for answering the question.&lt;/li>
&lt;li>&lt;strong>Re-ranking (Optional)&lt;/strong>: In some advanced RAG systems, there's an additional re-ranking step. This is because high vector similarity doesn't always equate to high relevance to the question. A re-ranker is a lighter-weight model that re-examines the relevance of these Top-K text chunks to the original question and reorders them, selecting the highest quality ones as the final context.&lt;/li>
&lt;li>&lt;strong>Build Prompt&lt;/strong>: The system combines the original question and the filtered context information according to a predefined template into a complete prompt. This prompt typically includes instructions like: &amp;ldquo;Please answer this question based on the following context information. Question: [&amp;hellip;] Context: [&amp;hellip;]&amp;quot;.&lt;/li>
&lt;li>&lt;strong>LLM Generation&lt;/strong>: Finally, this enhanced prompt is sent to the large language model (LLM). The LLM, following the instructions, comprehensively utilizes its internal knowledge and the provided context to generate a fluent, accurate, and information-rich answer. The system can also cite the sources of the context, enhancing the credibility of the answer.&lt;/li>
&lt;/ol>
&lt;h2 id="3-indexing-deep-dive">3. Indexing Deep Dive&lt;/h2>
&lt;p>Indexing is the cornerstone of RAG systems. The quality of this process directly determines the effectiveness of subsequent retrieval and generation phases. A well-designed indexing process ensures that information in the knowledge base is accurately and completely transformed into retrievable units. Let's explore each component in depth.&lt;/p>
&lt;h3 id="31-data-loading">3.1 Data Loading&lt;/h3>
&lt;p>The first step is to load raw data from various sources into the processing pipeline.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Loaders&lt;/strong>: Modern RAG frameworks provide powerful loader ecosystems. For example, LangChain's &lt;code>Document Loaders&lt;/code> support loading data from over 100 different sources, including:
&lt;ul>
&lt;li>&lt;strong>Files&lt;/strong>: &lt;code>TextLoader&lt;/code> (plain text), &lt;code>PyPDFLoader&lt;/code> (PDF), &lt;code>JSONLoader&lt;/code>, &lt;code>CSVLoader&lt;/code>, &lt;code>UnstructuredFileLoader&lt;/code> (capable of processing Word, PowerPoint, HTML, XML, and other formats).&lt;/li>
&lt;li>&lt;strong>Web Content&lt;/strong>: &lt;code>WebBaseLoader&lt;/code> (web scraping), &lt;code>YoutubeLoader&lt;/code> (loading YouTube video captions).&lt;/li>
&lt;li>&lt;strong>Collaboration Platforms&lt;/strong>: &lt;code>NotionDirectoryLoader&lt;/code>, &lt;code>ConfluenceLoader&lt;/code>.&lt;/li>
&lt;li>&lt;strong>Databases&lt;/strong>: &lt;code>AzureCosmosDBLoader&lt;/code>, &lt;code>PostgresLoader&lt;/code>.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>Choosing the right loader allows enterprises to easily integrate their existing knowledge assets into RAG systems without complex data format conversions.&lt;/p>
&lt;h3 id="32-text-splitting--chunking">3.2 Text Splitting / Chunking&lt;/h3>
&lt;p>&lt;strong>Why is chunking necessary?&lt;/strong>
Directly vectorizing an entire document (like a PDF with hundreds of pages) is impractical for three reasons:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Context Length Limitations&lt;/strong>: Most embedding models and LLMs have token input limits.&lt;/li>
&lt;li>&lt;strong>Noise Issues&lt;/strong>: A single vector representing a lengthy document contains too many topics and details, diluting the semantic information and making it difficult to precisely match specific user questions during retrieval.&lt;/li>
&lt;li>&lt;strong>Retrieval Cost&lt;/strong>: Feeding an entire document as context to an LLM consumes substantial computational resources and costs.&lt;/li>
&lt;/ol>
&lt;p>Therefore, splitting documents into semantically related chunks is a crucial step. &lt;strong>The quality of chunks determines the ceiling of RAG performance.&lt;/strong>&lt;/p>
&lt;h4 id="321-core-parameters-chunksize-and-chunkoverlap">3.2.1 Core Parameters: &lt;code>chunk_size&lt;/code> and &lt;code>chunk_overlap&lt;/code>&lt;/h4>
&lt;ul>
&lt;li>&lt;code>chunk_size&lt;/code>: Defines the size of each text block, typically calculated in character count or token count. Choosing this value requires balancing &amp;ldquo;information density&amp;rdquo; and &amp;ldquo;context completeness.&amp;rdquo; Too small may fragment complete semantics; too large may introduce excessive noise.&lt;/li>
&lt;li>&lt;code>chunk_overlap&lt;/code>: Defines the number of characters (or tokens) that overlap between adjacent text blocks. Setting overlap can effectively prevent cutting off a complete sentence or paragraph at block boundaries, ensuring semantic continuity.&lt;/li>
&lt;/ul>
&lt;h4 id="322-mainstream-chunking-strategies">3.2.2 Mainstream Chunking Strategies&lt;/h4>
&lt;p>The choice of chunking strategy depends on the structure and content of the document.&lt;/p>
&lt;p>&lt;strong>Strategy 1: Character Splitting&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Representative&lt;/strong>: &lt;code>CharacterTextSplitter&lt;/code>&lt;/li>
&lt;li>&lt;strong>Principle&lt;/strong>: This is the simplest direct method. It splits text based on a fixed character (like &lt;code>\n\n&lt;/code> newline) and then forcibly chunks according to the preset &lt;code>chunk_size&lt;/code>.&lt;/li>
&lt;li>&lt;strong>Advantages&lt;/strong>: Simple, fast, low computational cost.&lt;/li>
&lt;li>&lt;strong>Disadvantages&lt;/strong>: Completely ignores the semantics and logical structure of the text, easily breaking sentences in the middle or abruptly cutting off complete concept descriptions.&lt;/li>
&lt;li>&lt;strong>Applicable Scenarios&lt;/strong>: Suitable for texts with no obvious structure or where semantic coherence is not a high requirement.&lt;/li>
&lt;/ul>
&lt;pre>&lt;code class="language-python"># Example: CharacterTextSplitter
from langchain_text_splitters import CharacterTextSplitter
text_splitter = CharacterTextSplitter(
separator=&amp;quot;\n\n&amp;quot;,
chunk_size=1000,
chunk_overlap=200,
length_function=len,
)
&lt;/code>&lt;/pre>
&lt;p>&lt;strong>Strategy 2: Recursive Character Splitting&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Representative&lt;/strong>: &lt;code>RecursiveCharacterTextSplitter&lt;/code>&lt;/li>
&lt;li>&lt;strong>Principle&lt;/strong>: This is currently the most commonly used and recommended strategy. It attempts to split recursively according to a set of preset separators (like &lt;code>[&amp;quot;\n\n&amp;quot;, &amp;quot;\n&amp;quot;, &amp;quot; &amp;quot;, &amp;quot;&amp;quot;]&lt;/code>). It first tries to split using the first separator (&lt;code>\n\n&lt;/code>, paragraph); if the resulting blocks are still larger than &lt;code>chunk_size&lt;/code>, it continues using the next separator (&lt;code>\n&lt;/code>, line) to split these large blocks, and so on until the block size meets requirements.&lt;/li>
&lt;li>&lt;strong>Advantages&lt;/strong>: Makes the greatest effort to maintain the integrity of paragraphs, sentences, and other semantic units, striking a good balance between universality and effectiveness.&lt;/li>
&lt;li>&lt;strong>Disadvantages&lt;/strong>: Still based on character rules rather than true semantic understanding.&lt;/li>
&lt;li>&lt;strong>Applicable Scenarios&lt;/strong>: The preferred strategy for the vast majority of scenarios.&lt;/li>
&lt;/ul>
&lt;pre>&lt;code class="language-python"># Example: RecursiveCharacterTextSplitter
from langchain_text_splitters import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
)
&lt;/code>&lt;/pre>
&lt;p>&lt;strong>Strategy 3: Token-Based Splitting&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Representative&lt;/strong>: &lt;code>TokenTextSplitter&lt;/code>, &lt;code>CharacterTextSplitter.from_tiktoken_encoder&lt;/code>&lt;/li>
&lt;li>&lt;strong>Principle&lt;/strong>: It calculates &lt;code>chunk_size&lt;/code> by token count rather than character count. This is more consistent with how language models process text and allows for more precise control over the length of content input to the model.&lt;/li>
&lt;li>&lt;strong>Advantages&lt;/strong>: More precise control over cost and input length for model API calls.&lt;/li>
&lt;li>&lt;strong>Disadvantages&lt;/strong>: Computation is slightly more complex than character splitting.&lt;/li>
&lt;li>&lt;strong>Applicable Scenarios&lt;/strong>: When strict control over costs and API call input lengths is needed.&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Strategy 4: Semantic Chunking&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Principle&lt;/strong>: This is a more advanced experimental method. Instead of being based on fixed rules, it's based on understanding the semantics of the text. The splitter calculates embedding similarity between sentences and splits when it detects that the semantic difference between adjacent sentences exceeds a threshold.&lt;/li>
&lt;li>&lt;strong>Advantages&lt;/strong>: Can generate highly semantically consistent text blocks, theoretically the best splitting method.&lt;/li>
&lt;li>&lt;strong>Disadvantages&lt;/strong>: Very high computational cost, as it requires multiple embedding calculations during the splitting phase.&lt;/li>
&lt;li>&lt;strong>Applicable Scenarios&lt;/strong>: Scenarios requiring extremely high retrieval quality, regardless of computational cost.&lt;/li>
&lt;/ul>
&lt;h3 id="33-embedding">3.3 Embedding&lt;/h3>
&lt;p>Embedding is the process of transforming text chunks into high-dimensional numerical vectors, which serve as mathematical representations of the text's semantics.&lt;/p>
&lt;h4 id="331-embedding-model-selection">3.3.1 Embedding Model Selection&lt;/h4>
&lt;p>The choice of embedding model directly affects retrieval quality and system cost.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Closed-Source Commercial Models (e.g., OpenAI)&lt;/strong>:
&lt;ul>
&lt;li>&lt;strong>Representatives&lt;/strong>: &lt;code>text-embedding-ada-002&lt;/code>, &lt;code>text-embedding-3-small&lt;/code>, &lt;code>text-embedding-3-large&lt;/code>&lt;/li>
&lt;li>&lt;strong>Advantages&lt;/strong>: Powerful performance, typically ranking high in various evaluation benchmarks, simple to use (API calls).&lt;/li>
&lt;li>&lt;strong>Disadvantages&lt;/strong>: Requires payment, data must be sent to third-party servers, privacy risks exist.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;pre>&lt;code class="language-python"># Example: Using OpenAI Embeddings
from langchain_openai import OpenAIEmbeddings
embeddings_model = OpenAIEmbeddings(model=&amp;quot;text-embedding-3-small&amp;quot;)
&lt;/code>&lt;/pre>
&lt;ul>
&lt;li>&lt;strong>Open-Source Models (e.g., Hugging Face)&lt;/strong>:
&lt;ul>
&lt;li>&lt;strong>Representatives&lt;/strong>: &lt;code>sentence-transformers/all-mpnet-base-v2&lt;/code> (English general), &lt;code>bge-large-zh-v1.5&lt;/code> (Chinese), &lt;code>m3e-large&lt;/code> (Chinese-English) etc.&lt;/li>
&lt;li>&lt;strong>Advantages&lt;/strong>: Free, can be deployed locally, no data privacy leakage risk, numerous fine-tuned models available for specific languages or domains.&lt;/li>
&lt;li>&lt;strong>Disadvantages&lt;/strong>: Requires self-management of model deployment and computational resources, performance may have some gap compared to top commercial models.&lt;/li>
&lt;li>&lt;strong>MTEB Leaderboard&lt;/strong>: The Massive Text Embedding Benchmark (MTEB) is a public leaderboard for evaluating and comparing the performance of different embedding models, an important reference for selecting open-source models.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;pre>&lt;code class="language-python"># Example: Using open-source models from Hugging Face
from langchain_huggingface import HuggingFaceEmbeddings
model_name = &amp;quot;sentence-transformers/all-mpnet-base-v2&amp;quot;
embeddings_model = HuggingFaceEmbeddings(model_name=model_name)
&lt;/code>&lt;/pre>
&lt;p>&lt;strong>Core Principle&lt;/strong>: Throughout the entire RAG process, &lt;strong>the same embedding model must be used in both the indexing phase and the online retrieval phase&lt;/strong>. Otherwise, the query vectors and document vectors will exist in different vector spaces, making meaningful similarity comparisons impossible.&lt;/p>
&lt;h2 id="4-retrieval-technology-deep-dive">4. Retrieval Technology Deep Dive&lt;/h2>
&lt;p>Retrieval is the &amp;ldquo;heart&amp;rdquo; of RAG systems. Finding the most relevant contextual information is the prerequisite for generating high-quality answers. If the retrieved content is irrelevant or inaccurate, even the most powerful LLM will be ineffective - this is the so-called &amp;ldquo;Garbage In, Garbage Out&amp;rdquo; principle.&lt;/p>
&lt;p>Retrieval technology has evolved from traditional keyword matching to modern semantic vector search, and has now developed various advanced strategies to address complex challenges in different scenarios.&lt;/p>
&lt;h3 id="41-traditional-foundation-sparse-retrieval">4.1 Traditional Foundation: Sparse Retrieval&lt;/h3>
&lt;p>Sparse retrieval is a classic information retrieval method based on word frequency statistics, independent of deep learning models. Its core idea is that the more times a word appears in a specific document and the fewer times it appears across all documents, the more representative that word is for that document.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Representative Algorithms&lt;/strong>: &lt;strong>TF-IDF&lt;/strong> &amp;amp; &lt;strong>BM25 (Best Match 25)&lt;/strong>&lt;/li>
&lt;li>&lt;strong>Principle Brief (using BM25 as an example)&lt;/strong>:
&lt;ol>
&lt;li>&lt;strong>Term Frequency (TF)&lt;/strong>: Calculate the frequency of each query term in the document.&lt;/li>
&lt;li>&lt;strong>Inverse Document Frequency (IDF)&lt;/strong>: Measure the &amp;ldquo;rarity&amp;rdquo; of a term. Rarer terms have higher weights.&lt;/li>
&lt;li>&lt;strong>Document Length Penalty&lt;/strong>: Penalize overly long documents to prevent them from getting artificially high scores just because they contain more words.&lt;/li>
&lt;/ol>
&lt;/li>
&lt;li>&lt;strong>Advantages&lt;/strong>:
&lt;ul>
&lt;li>&lt;strong>Precise Keyword Matching&lt;/strong>: Performs excellently for queries containing specific terms, abbreviations, or product models (like &amp;ldquo;iPhone 15 Pro&amp;rdquo;).&lt;/li>
&lt;li>&lt;strong>Strong Interpretability&lt;/strong>: Score calculation logic is clear, easy to understand and debug.&lt;/li>
&lt;li>&lt;strong>Fast Computation&lt;/strong>: No complex model inference required.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Disadvantages&lt;/strong>:
&lt;ul>
&lt;li>&lt;strong>Cannot Understand Semantics&lt;/strong>: Unable to handle synonyms, near-synonyms, or conceptual relevance. For example, searching for &amp;ldquo;Apple phone&amp;rdquo; won't match documents containing &amp;ldquo;iPhone&amp;rdquo;.&lt;/li>
&lt;li>&lt;strong>&amp;ldquo;Vocabulary Gap&amp;rdquo; Problem&lt;/strong>: Relies on literal matching between queries and documents.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Applicable Scenarios&lt;/strong>: As part of hybrid retrieval, handling keyword and proper noun matching.&lt;/li>
&lt;/ul>
&lt;h3 id="42-modern-core-dense-retrieval--vector-search">4.2 Modern Core: Dense Retrieval / Vector Search&lt;/h3>
&lt;p>Dense retrieval is the mainstream technology in current RAG systems. It uses deep learning models (the embedding models we discussed earlier) to encode the semantic information of text into dense vectors, enabling retrieval based on &amp;ldquo;semantic similarity&amp;rdquo; rather than &amp;ldquo;literal similarity&amp;rdquo;.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Core Idea&lt;/strong>: Semantically similar texts have vectors that are close to each other in multidimensional space.&lt;/li>
&lt;li>&lt;strong>Workflow&lt;/strong>:
&lt;ol>
&lt;li>Offline: Vectorize all document chunks and store them in a vector database.&lt;/li>
&lt;li>Online: Vectorize the user query.&lt;/li>
&lt;li>In the vector database, calculate the distance/similarity between the query vector and all document vectors (such as cosine similarity, Euclidean distance).&lt;/li>
&lt;li>Return the Top-K document chunks with the closest distances.&lt;/li>
&lt;/ol>
&lt;/li>
&lt;/ul>
&lt;h4 id="421-approximate-nearest-neighbor-ann-search">4.2.1 Approximate Nearest Neighbor (ANN) Search&lt;/h4>
&lt;p>Since performing exact &amp;ldquo;nearest neighbor&amp;rdquo; searches among millions or even billions of vectors is extremely computationally expensive, the industry widely adopts &lt;strong>Approximate Nearest Neighbor (ANN)&lt;/strong> algorithms. ANN sacrifices minimal precision in exchange for query speed improvements of several orders of magnitude.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;strong>Mainstream ANN Algorithm&lt;/strong>: &lt;strong>HNSW (Hierarchical Navigable Small World)&lt;/strong>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>HNSW Principle Brief&lt;/strong>: It constructs a hierarchical graph structure. In the higher-level graph, it performs rough, large-step searches to quickly locate the target area; then in the lower-level graph, it performs fine, small-step searches to finally find the nearest neighbor vectors. This is like finding an address in a city - first determining which district (higher level), then which street (lower level).&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Advantages&lt;/strong>:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Powerful Semantic Understanding&lt;/strong>: Can cross literal barriers to understand concepts and intentions.&lt;/li>
&lt;li>&lt;strong>High Recall Rate&lt;/strong>: Can retrieve more semantically relevant documents with different wording.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Disadvantages&lt;/strong>:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Keyword Insensitivity&lt;/strong>: Sometimes less effective than sparse retrieval for matching specific keywords or proper nouns.&lt;/li>
&lt;li>&lt;strong>Strong Dependence on Embedding Models&lt;/strong>: Effectiveness completely depends on the quality of the embedding model.&lt;/li>
&lt;li>&lt;strong>&amp;ldquo;Black Box&amp;rdquo; Problem&lt;/strong>: The process of generating and matching vectors is less intuitive than sparse retrieval.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h3 id="43-powerful-combination-hybrid-search">4.3 Powerful Combination: Hybrid Search&lt;/h3>
&lt;p>Since sparse retrieval and dense retrieval each have their own strengths and weaknesses, the most natural idea is to combine them to leverage their respective advantages. Hybrid search was born for this purpose.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Implementation Method&lt;/strong>:
&lt;ol>
&lt;li>&lt;strong>Parallel Execution&lt;/strong>: Simultaneously process user queries using sparse retrieval (like BM25) and dense retrieval (vector search).&lt;/li>
&lt;li>&lt;strong>Score Fusion&lt;/strong>: Obtain two sets of results and their corresponding scores.&lt;/li>
&lt;li>&lt;strong>Result Re-ranking&lt;/strong>: Use a fusion algorithm (such as &lt;strong>Reciprocal Rank Fusion, RRF&lt;/strong>) to merge the two sets of results and re-rank them based on the fused scores to get the final Top-K results. The RRF algorithm gives higher weight to documents that rank high in different retrieval methods.&lt;/li>
&lt;/ol>
&lt;/li>
&lt;/ul>
&lt;pre>&lt;code class="language-mermaid">graph TD
subgraph &amp;quot;Hybrid Search&amp;quot;
A[&amp;quot;User Query&amp;quot;] --&amp;gt; B[&amp;quot;BM25 Retriever&amp;quot;];
A --&amp;gt; C[&amp;quot;Vector Retriever&amp;quot;];
B --&amp;gt; D[&amp;quot;Sparse Results (Top-K)&amp;quot;];
C --&amp;gt; E[&amp;quot;Dense Results (Top-K)&amp;quot;];
D &amp;amp; E --&amp;gt; F{&amp;quot;Fusion &amp;amp; Reranking (e.g., RRF)&amp;quot;};
F --&amp;gt; G[&amp;quot;Final Ranked Results&amp;quot;];
end
&lt;/code>&lt;/pre>
&lt;ul>
&lt;li>&lt;strong>Advantages&lt;/strong>: Balances the precision of keyword matching and the breadth of semantic understanding, achieving better results than single retrieval methods in most scenarios.&lt;/li>
&lt;li>&lt;strong>Applicable Scenarios&lt;/strong>: Almost all RAG applications requiring high-quality retrieval.&lt;/li>
&lt;/ul>
&lt;h3 id="44-frontier-exploration-advanced-retrieval-strategies">4.4 Frontier Exploration: Advanced Retrieval Strategies&lt;/h3>
&lt;p>To address more complex query intentions and data structures, academia and industry have developed a series of advanced retrieval strategies.&lt;/p>
&lt;h4 id="441-contextual-compression--reranking">4.4.1 Contextual Compression &amp;amp; Re-ranking&lt;/h4>
&lt;p>&lt;strong>Problem&lt;/strong>: The Top-K document chunks returned by vector search may only partially contain content truly relevant to the question, and some high-ranking blocks might actually be &amp;ldquo;false positives.&amp;rdquo; Directly feeding this redundant or irrelevant information to the LLM increases noise and cost.&lt;/p>
&lt;p>&lt;strong>Solution&lt;/strong>: Add an intermediate &amp;ldquo;filtering&amp;rdquo; and &amp;ldquo;sorting&amp;rdquo; layer between retrieval and generation.&lt;/p>
&lt;pre>&lt;code class="language-mermaid">graph TD
A[&amp;quot;Initial Retrieval&amp;quot;] --&amp;gt; B[&amp;quot;Top-K Documents&amp;quot;];
B --&amp;gt; C{&amp;quot;Compressor / Re-ranker&amp;quot;};
UserQuery --&amp;gt; C;
C --&amp;gt; D[&amp;quot;Filtered &amp;amp; Re-ranked Documents&amp;quot;];
D --&amp;gt; E[&amp;quot;LLM Generation&amp;quot;];
&lt;/code>&lt;/pre>
&lt;ul>
&lt;li>&lt;strong>Implementation Method&lt;/strong>: Using LangChain's &lt;code>ContextualCompressionRetriever&lt;/code>.
&lt;ul>
&lt;li>&lt;strong>&lt;code>LLMChainExtractor&lt;/code>&lt;/strong>: Uses an LLM to judge whether each document chunk is relevant to the query and only extracts relevant sentences.&lt;/li>
&lt;li>&lt;strong>&lt;code>EmbeddingsFilter&lt;/code>&lt;/strong>: Recalculates the similarity between query vectors and document chunk vectors, filtering out documents below a certain threshold.&lt;/li>
&lt;li>&lt;strong>Re-ranker&lt;/strong>: This is currently the most effective and commonly used approach. It uses a lighter-weight &lt;strong>cross-encoder&lt;/strong> model specifically trained to calculate relevance scores. Unlike the bi-encoder used in the retrieval phase (which encodes queries and documents separately), a cross-encoder receives both the query and document chunk as input simultaneously, enabling more fine-grained relevance judgment. Common re-rankers include &lt;code>Cohere Rerank&lt;/code>, &lt;code>BAAI/bge-reranker-*&lt;/code>, and models provided by open-source or cloud service vendors.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h4 id="442-selfquerying-retriever">4.4.2 Self-Querying Retriever&lt;/h4>
&lt;p>&lt;strong>Problem&lt;/strong>: User queries are typically in natural language but may contain filtering requirements for &lt;strong>metadata&lt;/strong>. For example: &amp;ldquo;Recommend some science fiction movies released after 2000 with ratings above 8.5?&amp;rdquo;&lt;/p>
&lt;p>&lt;strong>Solution&lt;/strong>: Let the LLM itself &amp;ldquo;translate&amp;rdquo; natural language queries into structured query statements containing metadata filtering conditions.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Workflow&lt;/strong>:
&lt;ol>
&lt;li>User inputs a natural language query.&lt;/li>
&lt;li>&lt;code>SelfQueryingRetriever&lt;/code> sends the query to the LLM.&lt;/li>
&lt;li>Based on predefined metadata field information (such as &lt;code>year&lt;/code>, &lt;code>rating&lt;/code>, &lt;code>genre&lt;/code>), the LLM generates a structured query containing:
&lt;ul>
&lt;li>&lt;code>query&lt;/code>: The keyword part for vector search (&amp;ldquo;science fiction movies&amp;rdquo;).&lt;/li>
&lt;li>&lt;code>filter&lt;/code>: Conditions for metadata filtering (&lt;code>year &amp;gt; 2000 AND rating &amp;gt; 8.5&lt;/code>).&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>The retriever uses this structured query to perform a &amp;ldquo;filter first, then search&amp;rdquo; operation on the vector database, greatly narrowing the search scope and improving precision.&lt;/li>
&lt;/ol>
&lt;/li>
&lt;/ul>
&lt;pre>&lt;code class="language-python"># Core settings for Self-Querying in LangChain
metadata_field_info = [
AttributeInfo(name=&amp;quot;genre&amp;quot;, ...),
AttributeInfo(name=&amp;quot;year&amp;quot;, ...),
AttributeInfo(name=&amp;quot;rating&amp;quot;, ...),
]
retriever = SelfQueryRetriever.from_llm(
llm,
vectorstore,
document_content_description,
metadata_field_info,
)
&lt;/code>&lt;/pre>
&lt;h4 id="443-multivector-retriever">4.4.3 Multi-Vector Retriever&lt;/h4>
&lt;p>&lt;strong>Problem&lt;/strong>: A single vector struggles to perfectly summarize a longer document chunk, especially when the chunk contains multiple subtopics.&lt;/p>
&lt;p>&lt;strong>Solution&lt;/strong>: Generate &lt;strong>multiple&lt;/strong> vectors representing different aspects for each document chunk, rather than a single vector.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Implementation Methods&lt;/strong>:
&lt;ol>
&lt;li>&lt;strong>Smaller Sub-chunks&lt;/strong>: Further split the original document chunk into smaller sentences or paragraphs, and generate vectors for these small chunks.&lt;/li>
&lt;li>&lt;strong>Summary Vectors&lt;/strong>: Use an LLM to generate a summary for each document chunk, then vectorize the summary.&lt;/li>
&lt;li>&lt;strong>Hypothetical Question Vectors&lt;/strong>: Use an LLM to pose several possible questions about each document chunk, then vectorize these questions.&lt;/li>
&lt;/ol>
&lt;/li>
&lt;/ul>
&lt;p>During querying, the query vector matches with all these sub-vectors (sub-chunks, summaries, questions). Once a match is successful, what's returned is the &lt;strong>complete original document chunk&lt;/strong> it belongs to. This leverages both the precision of fine-grained matching and ensures that the context provided to the final LLM is complete.&lt;/p>
&lt;h4 id="444-parent-document-retriever">4.4.4 Parent Document Retriever&lt;/h4>
&lt;p>This is a common implementation of the multi-vector retriever. It splits documents into &amp;ldquo;parent chunks&amp;rdquo; and &amp;ldquo;child chunks.&amp;rdquo; Indexing and retrieval happen on the smaller &amp;ldquo;child chunks,&amp;rdquo; but what's ultimately returned to the LLM is the larger &amp;ldquo;parent chunk&amp;rdquo; that the child belongs to. This solves the &amp;ldquo;context loss&amp;rdquo; problem, ensuring that the LLM sees a more complete linguistic context when generating answers.&lt;/p>
&lt;h4 id="445-graph-rag">4.4.5 Graph RAG&lt;/h4>
&lt;p>&lt;strong>Problem&lt;/strong>: Traditional RAG views knowledge as independent text blocks, ignoring the complex, web-like relationships between knowledge points.&lt;/p>
&lt;p>&lt;strong>Solution&lt;/strong>: Build the knowledge base into a &lt;strong>Knowledge Graph&lt;/strong>, where entities are nodes and relationships are edges.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;strong>Workflow&lt;/strong>:&lt;/p>
&lt;ol>
&lt;li>During querying, the system first identifies the core entities in the query.&lt;/li>
&lt;li>It then explores neighboring nodes and relationships related to these entities in the graph, forming a subgraph containing rich structured information.&lt;/li>
&lt;li>This subgraph information is linearized (converted to text) and provided to the LLM as context.&lt;/li>
&lt;/ol>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Advantages&lt;/strong>: Can answer more complex questions requiring multi-hop reasoning (e.g., &amp;ldquo;Who is A's boss's wife?&amp;quot;), providing deeper context than &amp;ldquo;text blocks.&amp;rdquo;&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Implementation Case: Graphiti/Zep&lt;/strong>:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Introduction&lt;/strong>: &lt;a href="https://github.com/getzep/graphiti">Graphiti&lt;/a> is a temporal knowledge graph architecture designed specifically for LLM Agents, seamlessly integrating Neo4j's graph database capabilities with LLM's natural language processing abilities.&lt;/li>
&lt;li>&lt;strong>Core Features&lt;/strong>:
&lt;ul>
&lt;li>&lt;strong>Temporal Awareness&lt;/strong>: Each node and relationship carries timestamp attributes, enabling tracking of how entity states change over time.&lt;/li>
&lt;li>&lt;strong>Automatic Schema Inference&lt;/strong>: No need to predefine entity types and relationships; the system can automatically infer appropriate graph structures from conversations.&lt;/li>
&lt;li>&lt;strong>Multi-hop Reasoning&lt;/strong>: Supports complex relationship path queries, capable of discovering indirectly associated information.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Application Scenarios&lt;/strong>: Particularly suitable for multi-turn dialogue systems requiring long-term memory and temporal reasoning, such as customer support, personal assistants, and other scenarios needing to &amp;ldquo;remember&amp;rdquo; user historical interactions.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h4 id="446-agentic-rag--adaptive-rag">4.4.6 Agentic RAG / Adaptive RAG&lt;/h4>
&lt;p>This is the latest evolutionary direction of RAG, endowing RAG systems with certain &amp;ldquo;thinking&amp;rdquo; and &amp;ldquo;decision-making&amp;rdquo; capabilities, allowing them to adaptively select the best retrieval strategy based on the complexity of the question.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Core Idea&lt;/strong>: Transform the traditional linear RAG process into a dynamic process driven by an LLM Agent that can loop and iterate.&lt;/li>
&lt;li>&lt;strong>Possible Workflow&lt;/strong>:
&lt;ol>
&lt;li>&lt;strong>Question Analysis&lt;/strong>: The Agent first analyzes the user's question. Is this a simple question or a complex one? Does it need keyword matching or semantic search?&lt;/li>
&lt;li>&lt;strong>Strategy Selection&lt;/strong>:
&lt;ul>
&lt;li>If the question is simple, directly perform vector search.&lt;/li>
&lt;li>If the question contains metadata, switch to Self-Querying.&lt;/li>
&lt;li>If the question is ambiguous, the Agent might first rewrite the question (Query Rewriting), generating several different query variants and executing them separately.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Result Reflection &amp;amp; Iteration&lt;/strong>: The Agent examines the preliminary retrieved results. If the results are not ideal (e.g., low relevance or conflicting information), it can decide to:
&lt;ul>
&lt;li>&lt;strong>Query Again&lt;/strong>: Use different keywords or strategies to retrieve again.&lt;/li>
&lt;li>&lt;strong>Web Search&lt;/strong>: If the internal knowledge base doesn't have an answer, it can call search engine tools to find information online.&lt;/li>
&lt;li>&lt;strong>Multi-step Reasoning&lt;/strong>: Break down complex questions into several sub-questions, retrieving and answering step by step.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ol>
&lt;/li>
&lt;/ul>
&lt;p>Agentic RAG is no longer a fixed pipeline but a flexible, intelligent framework, representing the future direction of RAG development.&lt;/p>
&lt;h2 id="5-generation-phase-the-final-touch">5. Generation Phase: The Final Touch&lt;/h2>
&lt;p>The generation phase is the endpoint of the RAG process and the ultimate manifestation of its value. In this phase, the system combines the &amp;ldquo;essence&amp;rdquo; context obtained from previous retrieval, filtering, and re-ranking with the user's original question to form a final prompt, which is then sent to the large language model (LLM) to generate an answer.&lt;/p>
&lt;h3 id="51-core-task-effective-prompt-engineering">5.1 Core Task: Effective Prompt Engineering&lt;/h3>
&lt;p>The core task of this phase is &lt;strong>Prompt Engineering&lt;/strong>. A well-designed prompt template can clearly instruct the LLM on its task, ensuring it thinks and answers along the right track.&lt;/p>
&lt;p>A typical RAG prompt template structure is as follows:&lt;/p>
&lt;pre>&lt;code class="language-text">You are a professional, rigorous Q&amp;amp;A assistant. Please answer the user's question based on the context information provided below.
Your answer must be completely based on the given context, and you are prohibited from using your internal knowledge for any supplementation or imagination.
If there is not enough information in the context to answer the question, please clearly state &amp;quot;Based on the available information, I cannot answer this question.&amp;quot;
At the end of your answer, please list all the context source IDs you referenced.
---
[Context Information]
{context}
---
[User Question]
{question}
---
[Your Answer]
&lt;/code>&lt;/pre>
&lt;h4 id="511-template-key-elements-analysis">5.1.1 Template Key Elements Analysis&lt;/h4>
&lt;ul>
&lt;li>&lt;strong>Persona&lt;/strong>: &amp;ldquo;You are a professional, rigorous Q&amp;amp;A assistant.&amp;rdquo; This helps set the tone and style of the LLM's output.&lt;/li>
&lt;li>&lt;strong>Core Instruction&lt;/strong>: &amp;ldquo;Please answer the user's question based on the context information provided below.&amp;rdquo; This is the most critical task instruction.&lt;/li>
&lt;li>&lt;strong>Constraints &amp;amp; Guardrails&lt;/strong>:
&lt;ul>
&lt;li>&amp;ldquo;Must be completely based on the given context, prohibited from&amp;hellip; supplementation or imagination.&amp;rdquo; -&amp;gt; This is key to suppressing model hallucinations.&lt;/li>
&lt;li>&amp;ldquo;If there is not enough information, please clearly state&amp;hellip;&amp;rdquo; -&amp;gt; This defines the model's &amp;ldquo;escape route&amp;rdquo; when information is insufficient, preventing it from guessing.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Attribution/Citation&lt;/strong>: &amp;ldquo;Please list all the context source IDs you referenced.&amp;rdquo; -&amp;gt; This is the foundation for answer explainability and credibility.&lt;/li>
&lt;li>&lt;strong>Placeholders&lt;/strong>:
&lt;ul>
&lt;li>&lt;code>{context}&lt;/code>: This will be filled with the content of multiple document chunks (chunks) obtained from the retrieval phase, after processing.&lt;/li>
&lt;li>&lt;code>{question}&lt;/code>: This will be filled with the user's original question.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h3 id="52-context-and-question-fusion">5.2 Context and Question Fusion&lt;/h3>
&lt;p>When the system fills multiple document chunks (e.g., Top-5 chunks) into the &lt;code>{context}&lt;/code> placeholder, these chunks are packaged together with the original question and sent to the LLM. The LLM reads the entire enhanced prompt and then:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Understands the Question&lt;/strong>: Clarifies the user's query intent.&lt;/li>
&lt;li>&lt;strong>Locates Information&lt;/strong>: Searches for sentences and paragraphs directly related to the question within the provided multiple context blocks.&lt;/li>
&lt;li>&lt;strong>Synthesizes &amp;amp; Refines&lt;/strong>: Integrates, understands, and refines scattered information points found from different context blocks.&lt;/li>
&lt;li>&lt;strong>Generates an Answer&lt;/strong>: Based on the refined information, generates a final answer using fluent, coherent natural language.&lt;/li>
&lt;li>&lt;strong>Cites Sources&lt;/strong>: According to instructions, includes the document sources that the answer is based on.&lt;/li>
&lt;/ol>
&lt;p>Through this carefully designed &amp;ldquo;open-book exam&amp;rdquo; process, the RAG system ultimately generates a high-quality answer that combines both the LLM's powerful language capabilities and fact-based information.&lt;/p>
&lt;h2 id="6-rag-evaluation-framework-how-to-measure-system-quality">6. RAG Evaluation Framework: How to Measure System Quality?&lt;/h2>
&lt;p>Building a RAG system is just the first step. Scientifically and quantitatively evaluating its performance, and continuously iterating and optimizing based on this evaluation, is equally important. A good evaluation framework can help us diagnose whether the system's bottleneck is in the retrieval module (&amp;ldquo;not found&amp;rdquo;) or in the generation module (&amp;ldquo;not well expressed&amp;rdquo;).&lt;/p>
&lt;p>Industry-leading RAG evaluation frameworks, such as &lt;strong>RAGAS (RAG Assessment)&lt;/strong> and &lt;strong>TruLens&lt;/strong>, provide a series of metrics to score RAG system performance from different dimensions.&lt;/p>
&lt;h3 id="61-core-evaluation-dimensions">6.1 Core Evaluation Dimensions&lt;/h3>
&lt;p>RAG evaluation can be divided into two levels: &lt;strong>component level&lt;/strong> (evaluating retrieval and generation separately) and &lt;strong>end-to-end level&lt;/strong> (evaluating the quality of the final answer).&lt;/p>
&lt;pre>&lt;code class="language-mermaid">graph TD
subgraph &amp;quot;RAG Evaluation Dimensions&amp;quot;
A(&amp;quot;Evaluation&amp;quot;) --&amp;gt; B[&amp;quot;Component-Level Evaluation&amp;quot;];
A --&amp;gt; C[&amp;quot;End-to-End Evaluation&amp;quot;];
B --&amp;gt; B1[&amp;quot;Retriever Quality Evaluation&amp;quot;];
B --&amp;gt; B2[&amp;quot;Generator Quality Evaluation&amp;quot;];
B1 --&amp;gt; B1_Metrics(&amp;quot;Context Precision, Context Recall&amp;quot;);
B2 --&amp;gt; B2_Metrics(&amp;quot;Faithfulness&amp;quot;);
C --&amp;gt; C_Metrics(&amp;quot;Answer Relevancy, Answer Correctness&amp;quot;);
end
&lt;/code>&lt;/pre>
&lt;h3 id="62-key-evaluation-metrics-using-ragas-as-an-example">6.2 Key Evaluation Metrics (Using RAGAS as an Example)&lt;/h3>
&lt;p>Below we explain in detail several core metrics in the RAGAS framework. These metrics do not require manually annotated reference answers (Reference-Free), greatly reducing evaluation costs.&lt;/p>
&lt;h4 id="621-evaluating-generation-quality">6.2.1 Evaluating Generation Quality&lt;/h4>
&lt;p>&lt;strong>Metric 1: Faithfulness&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Definition&lt;/strong>: Measures the extent to which the generated answer is completely based on the provided context. High faithfulness means that every statement in the answer can find evidence in the context.&lt;/li>
&lt;li>&lt;strong>Evaluation Method&lt;/strong>: RAGAS uses an LLM to analyze the answer, breaking it down into a series of statements. Then, for each statement, it verifies in the context whether there is evidence supporting that statement. The final score is (number of statements supported by the context) / (total number of statements).&lt;/li>
&lt;li>&lt;strong>Problem Diagnosed&lt;/strong>: This metric is the &lt;strong>core indicator for measuring &amp;ldquo;model hallucination&amp;rdquo;&lt;/strong>. A low score means the generator (LLM) is freely making up information that doesn't exist in the context.&lt;/li>
&lt;li>&lt;strong>Data Required&lt;/strong>: &lt;code>question&lt;/code>, &lt;code>answer&lt;/code>, &lt;code>context&lt;/code>.&lt;/li>
&lt;/ul>
&lt;h4 id="622-evaluating-both-retrieval-and-generation-quality">6.2.2 Evaluating Both Retrieval and Generation Quality&lt;/h4>
&lt;p>&lt;strong>Metric 2: Answer Relevancy&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Definition&lt;/strong>: Measures the relevance of the generated answer to the user's original question. An answer faithful to the context might still be off-topic.&lt;/li>
&lt;li>&lt;strong>Evaluation Method&lt;/strong>: RAGAS uses an Embedding model to measure the semantic similarity between the question and answer. It also uses an LLM to identify &amp;ldquo;noise&amp;rdquo; or irrelevant sentences in the answer and penalizes them.&lt;/li>
&lt;li>&lt;strong>Problem Diagnosed&lt;/strong>: A low score means that although the answer may be based on the context, it doesn't directly or effectively answer the user's question, or it contains too much irrelevant information.&lt;/li>
&lt;li>&lt;strong>Data Required&lt;/strong>: &lt;code>question&lt;/code>, &lt;code>answer&lt;/code>.&lt;/li>
&lt;/ul>
&lt;h4 id="623-evaluating-retrieval-quality">6.2.3 Evaluating Retrieval Quality&lt;/h4>
&lt;p>&lt;strong>Metric 3: Context Precision&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Definition&lt;/strong>: Measures how much of the retrieved context is truly relevant to the question - the &amp;ldquo;signal-to-noise ratio.&amp;rdquo;&lt;/li>
&lt;li>&lt;strong>Evaluation Method&lt;/strong>: RAGAS analyzes the context sentence by sentence and has an LLM judge whether each sentence is necessary for answering the user's question. The final score is (number of sentences deemed useful) / (total number of sentences in the context).&lt;/li>
&lt;li>&lt;strong>Problem Diagnosed&lt;/strong>: A low score (high &lt;code>1 - Context Precision&lt;/code> value) indicates that the retriever returned many irrelevant &amp;ldquo;noise&amp;rdquo; documents, which interferes with the generator's judgment and increases costs. This suggests that the &lt;strong>retrieval algorithm needs optimization&lt;/strong>.&lt;/li>
&lt;li>&lt;strong>Data Required&lt;/strong>: &lt;code>question&lt;/code>, &lt;code>context&lt;/code>.&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Metric 4: Context Recall&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Definition&lt;/strong>: Measures whether the retrieved context contains all the necessary information to answer the question.&lt;/li>
&lt;li>&lt;strong>Evaluation Method&lt;/strong>: This metric requires a &lt;strong>manually annotated reference answer (Ground Truth)&lt;/strong> as a benchmark. RAGAS has an LLM analyze this reference answer and judge whether each sentence in it can find support in the retrieved context.&lt;/li>
&lt;li>&lt;strong>Problem Diagnosed&lt;/strong>: A low score means the retriever &lt;strong>failed to find&lt;/strong> key information needed to answer the question, indicating &amp;ldquo;missed retrievals.&amp;rdquo; This might suggest that the document chunking strategy is unreasonable, or the Embedding model cannot understand the query well.&lt;/li>
&lt;li>&lt;strong>Data Required&lt;/strong>: &lt;code>question&lt;/code>, &lt;code>ground_truth&lt;/code> (reference answer), &lt;code>context&lt;/code>.&lt;/li>
&lt;/ul>
&lt;h3 id="63-using-evaluation-to-guide-iteration">6.3 Using Evaluation to Guide Iteration&lt;/h3>
&lt;p>By comprehensively evaluating a RAG system using the above metrics, we can get a clear performance profile and make targeted optimizations:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Low Faithfulness Score&lt;/strong>: The problem is in the &lt;strong>generator&lt;/strong>. Need to optimize the Prompt, add stronger constraints, or switch to an LLM with stronger instruction-following capabilities.&lt;/li>
&lt;li>&lt;strong>Low Answer Relevancy Score&lt;/strong>: The problem could be in either the generator or retriever. Need to check if the Prompt is guiding the model off-topic, or if the retrieved content is of poor quality.&lt;/li>
&lt;li>&lt;strong>Low Context Precision Score&lt;/strong>: The problem is in the &lt;strong>retriever&lt;/strong>. Indicates that the recalled documents are of poor quality with much noise. Can try better retrieval strategies, such as adding a Re-ranker to filter irrelevant documents.&lt;/li>
&lt;li>&lt;strong>Low Context Recall Score&lt;/strong>: The problem is in the &lt;strong>retriever&lt;/strong>. Indicates that key information wasn't found. Need to check if the Chunking strategy is fragmenting key information, or try methods like Multi-Query to expand the retrieval scope.&lt;/li>
&lt;/ul>
&lt;p>Through the &amp;ldquo;evaluate-diagnose-optimize&amp;rdquo; closed loop, we can continuously improve the overall performance of the RAG system.&lt;/p>
&lt;h2 id="7-challenges-and-future-outlook">7. Challenges and Future Outlook&lt;/h2>
&lt;p>Although RAG has greatly expanded the capabilities of large language models and has become the de facto standard for building knowledge-intensive applications, it still faces some challenges while also pointing to exciting future development directions.&lt;/p>
&lt;h3 id="71-current-challenges">7.1 Current Challenges&lt;/h3>
&lt;ol>
&lt;li>&lt;strong>&amp;ldquo;Needle-in-a-Haystack&amp;rdquo; Problem&lt;/strong>: As LLM context windows grow larger (e.g., million-level tokens), precisely finding and utilizing key information in lengthy, noisy contexts becomes increasingly difficult. Research shows that LLM performance when processing long contexts is affected by the position of information within them, with issues like &amp;ldquo;middle neglect.&amp;rdquo;&lt;/li>
&lt;li>&lt;strong>Imperfect Chunking&lt;/strong>: How to optimally split documents remains an open question. Existing rule-based or simple semantic splitting methods may damage information integrity or introduce irrelevant context, affecting retrieval and generation quality.&lt;/li>
&lt;li>&lt;strong>Evaluation Complexity and Cost&lt;/strong>: Although frameworks like RAGAS provide automated evaluation metrics, building a comprehensive, reliable evaluation set still requires significant human effort. Especially in domains requiring fine judgment, machine evaluation results may differ from human perception.&lt;/li>
&lt;li>&lt;strong>Integration of Structured and Multimodal Data&lt;/strong>: Knowledge in the real world isn't just text. How to efficiently integrate tables, charts, images, audio, and other multimodal information, and enable RAG systems to understand and utilize them, is an actively explored area.&lt;/li>
&lt;li>&lt;strong>Production Environment Complexity&lt;/strong>: Deploying a RAG prototype to a production environment requires considering data updates, permission management, version control, cost monitoring, low-latency responses, and a series of engineering challenges.&lt;/li>
&lt;/ol>
&lt;h3 id="72-future-outlook">7.2 Future Outlook&lt;/h3>
&lt;ol>
&lt;li>&lt;strong>Smarter Indexing&lt;/strong>: Future indexing processes will no longer be simple &amp;ldquo;split-vectorize&amp;rdquo; operations. They will more deeply understand document structures, automatically build knowledge graphs, identify entities and relationships, generate multi-level, multi-perspective representations (such as summaries, questions), creating a richer, more queryable knowledge network.&lt;/li>
&lt;li>&lt;strong>Adaptive Retrieval&lt;/strong>: As demonstrated by Agentic RAG, future RAG systems will have stronger autonomy. They can dynamically decide whether to perform simple vector searches or execute complex multi-step queries, or even call external tools (such as search engines, calculators, APIs) to obtain information based on the specific situation of the question. Retrieval will evolve from a fixed step to a flexible, agent-driven process.&lt;/li>
&lt;li>&lt;strong>LLM as Part of RAG&lt;/strong>: As LLM capabilities strengthen, they will participate more deeply in every aspect of RAG. Not just in the generation phase, but also in indexing (generating metadata, summaries), querying (query rewriting, expansion), retrieval (as a re-ranker), and other phases, playing a core role.&lt;/li>
&lt;li>&lt;strong>End-to-End Optimization&lt;/strong>: Future frameworks may allow end-to-end joint fine-tuning of various RAG components (Embedding models, LLM generators, etc.), making the entire system highly optimized for a specific task or domain, rather than simply piecing together individual components.&lt;/li>
&lt;li>&lt;strong>Native Multimodal RAG&lt;/strong>: RAG will natively support understanding and retrieving content like images, audio, and video. Users can ask questions like &amp;ldquo;Find me that picture of &amp;lsquo;a cat playing piano&amp;rsquo;&amp;rdquo; and the system can directly perform semantic retrieval in multimedia databases and return results.&lt;/li>
&lt;/ol>
&lt;p>In summary, RAG is evolving from a relatively fixed &amp;ldquo;retrieve-augment-generate&amp;rdquo; pipeline to a more dynamic, intelligent, adaptive knowledge processing framework. It will continue to serve as the key bridge connecting large language models with the vast external world, continuously unleashing AI's application potential across various industries in the foreseeable future.&lt;/p></description></item></channel></rss>