RAG
Retrieval Augmented Generation Large models generate answers based on information retrieved from data sources
Basic RAG Concepts
In a nutshell, its purpose is to quickly retrieve the accurate data we need from a large dataset.
Retrieval
Retrieval is the process of obtaining or retrieving data or information from a storage location. In the context of databases or search engines, it refers to retrieving relevant data based on a specific query.
Vector Similarity Search
Vector similarity search involves comparing vectors (lists of numbers) to determine how similar they are. It is commonly used in machine learning and artificial intelligence to find the most similar items in a database based on a given input.
Vector Database
A vector database is a database designed to store vectors, typically used with machine learning models. These databases are optimized for vector similarity search.
Chunking
Chunking is the process of splitting input data (such as text) into smaller, manageable chunks. This can make processing more efficient, especially in contexts such as natural language processing, where the input may be very long.
Embeddings / Vectors
In machine learning, embeddings refer to converting discrete items (such as words or products) into continuous vectors. These vectors capture the semantic meaning or relationships between items in a way that algorithms can process.
K Nearest Neighbors (KNN)
K Nearest Neighbors (KNN) is a type of algorithm used for classification and regression. Given an input, KNN finds the “k” training samples closest to that input and makes a decision based on their values or labels.
Standard RAG

- Split the text into small chunks.
- Then use some kind of Transformer Encoder model to convert these chunks into vectors.
- Collect these vectors into an index, and finally create a prompt for a large language model that instructs the model to answer the user’s query based on the context we found in the search step.
- We use the same Encoder model to convert the user’s query into a vector.
- Then search this query vector against the index to find the top k most relevant results.
- Extract the corresponding text chunks from our database and feed them into the LLM as context for processing.
Retriever
In a RAG-LLM setup, the retriever can capture more complex semantic relationships between queries and documents, leading to more accurate retrieval results. The retriever can embed documents and queries into a high-dimensional vector space, where the distance between vectors corresponds to the relevance of a document to a query. Documents that are less relevant to the input query will have a larger “distance” and should therefore be treated as irrelevant and ignored.
The retriever takes the input query, uses a query encoder to convert it into a vector, and then finds the most similar document vectors in the corpus. The documents associated with those vectors are then passed to the generator.
An important aspect to consider during retrieval is the accuracy of the retrieved documents. Retrieval accuracy can usually be quantified with distance metrics, such as vector similarity in the context of machine learning models. Essentially, documents unrelated to the input query will show a larger “distance” from the query vector, indicating their irrelevance. This distance serves as a metric for determining which documents are relevant to the query.
Generator
In a RAG-LLM setup, the generator is a large Transformer model, such as GPT3.5, GPT4, Llama2, Falcon, PaLM, and BERT. The generator takes the input query and the retrieved documents and generates a response.
The retrieved documents and the input query are concatenated and fed into the generator. The generator then uses this combined input to generate a response. The retrieved documents provide additional context and information that help the generator produce more informed and accurate responses and reduce hallucinations.
Advanced RAG

The green elements in the figure represent the core RAG techniques we will explore further, while the blue parts are related text.
Query Transformation
Query transformation is a set of techniques that use a large language model as a reasoning engine to adjust user input, with the goal of improving retrieval quality.

For complex queries, a large language model can split them into multiple subqueries.
For example, when you ask: — “On GitHub, which of the two frameworks, LangChain and LlamaIndex, is more popular?”, we are unlikely to find a direct comparison between them in the corpus text, so it makes sense to break this question into two simpler, more specific subqueries: _— “How many stars does LangChain have on GitHub?” — “How many stars does LlamaIndex have on GitHub?”
These subqueries run simultaneously. The retrieved information is then aggregated into a statement for the large language model to synthesize a final answer to the original query.
Query Routing
Query routing means that after receiving a user’s query, the large language model decides what to do next.
Common approaches include summarizing the query, searching a specific data index, or trying several different processing methods and combining their results into one answer.
Another role of the query router is to choose the data storage location for handling the user query. These data storage locations can vary widely, such as traditional vector stores, graph databases, or relational databases, or different levels of indexing systems.
Setting up a query router involves deciding which choices it can make.
Fusion Retrieval
Combining traditional keyword-based search with modern semantic or vector search
Traditional search uses sparse retrieval algorithms such as tf-idf or the industry-standard BM25, while modern search uses semantic or vector methods. Combining the two can produce excellent retrieval results.
How to properly fuse retrieval results from these two different similarity scores is an issue typically solved with the Reciprocal Rank Fusion (RRF) algorithm, which effectively reranks the retrieval results to produce the final output.

In LangChain, this approach is implemented through the Ensemble Retriever, a class that combines multiple retrievers you define, such as a faiss-based vector index and a BM25-based retriever, and uses the RRF algorithm to rerank the results.
Reranking Postprocessing
After applying the retrieval algorithms described above, we obtain initial search results. The next step is to further refine them by filtering, reordering, or transforming them.
In LlamaIndex, you can find a variety of postprocessors that can filter based on similarity scores, keywords, or metadata, or use other models such as large language models, Sentence-Transformers cross-encoders, and Cohere’s rerank interface to reorder results. They can even operate based on the recency of dates in metadata—basically everything you can think of.
Knowledge sources: