Why Are Advanced RAG Methods Crucial for the Future of AI?
Currently working as a Solution Architect at MongoDB, I was inspired to write this article by engaging dialogues with my colleagues Fabian Valle, Brian Leonard, Gabriel Paranthoen, Benjamin Flast and Henry Weller.
Free link here => pleasse help to like this Linkedin post.
Introduction
Retrieval-augmented generation (RAG) represents a significant advancement in the field of generative AI, combining efficient data retrieval with the power of large language models.
At its core, RAG operates by employing vector search to mine relevant and existing data, combining this retrieved information with the user's query, and then processing it through a large language model like ChatGPT.
This RAG method ensures that the generated responses are not just precise but also reflect current information, substantially reducing inaccuracies or "hallucinations" in the output.
However, as the landscape of AI applications expands, the demands placed on RAG are becoming more complex and varied. The basic RAG framework, while robust, may be no longer enough in addressing the nuanced needs of diverse industries and evolving use cases. This is where advanced RAG techniques come into play. These enhanced methods are tailored to cater to specific challenges, offering more precision, adaptability, and efficiency in information processing.
Understanding RAG Techniques
The Essence of Basic RAG
Retrieval-augmented generation (RAG) combines data management with intelligent querying to enhance AI's response accuracy.
- Data preparation: It begins with the user uploading data, which is then ‘chunked' and stored with embeddings, establishing a foundation for retrieval.
- Retrieval: Once a question is posed, the system employs vector search techniques to mine through the stored data, pinpointing relevant information.
- LLM query: The retrieved information is then used to provide context for the Language Model (LLM), which prepares the final prompt by melding the context with the question. The result is an answer generated based on the rich, contextualized data provided, demonstrating RAG's ability to produce reliable, informed responses.
The entire process, encapsulated in this diagram, underlines RAG's emphasis on reliable data handling and contextually aware answer generation, which are pivotal for advanced AI applications.

If you want to know more about the basic RAG, you can refer to my previous article.
From Zero to Hero: Building a Generative AI Chatbot with MongoDB and Langchain
As AI technology progressed, so did the capabilities of RAG. Advanced RAG techniques have emerged, pushing the boundaries of what these models can achieve. These advancements are not just about better retrieval or more fluent generation. They encompass a range of improvements, including an enhanced understanding of context, more sophisticated handling of nuanced queries, and the ability to integrate diverse data sources seamlessly.
Technique 1: Self-Querying Retrieval
Self-querying retrieval is a cutting-edge technique in AI-driven database systems, enhancing data querying with natural language understanding. For example, in case you have a product catalog dataset, you want to search "a black leather mini skirt less than 20 dollars" Not only do you want to run a semantic search for the product description but also you can leverage a filter on the subcategory and price of the product.


- Natural Language Query Processing: The process starts with an LLM interpreting the user's natural language query, extracting intent and context.
- Metadata Field Information: To implement this, it's crucial to provide information upfront about the metadata fields in the documents. This metadata, defining the structure and attributes of the data, guides the construction of effective queries and filters, ensuring accurate and relevant search results.
- Query Construction: Next, the LLM constructs a structured query incorporating both semantic elements for vector search and metadata filters for precision.
- Executing the Query: This structured query is applied to MongoDB's vector search, filtering results for both semantic similarity and metadata relevance.
By constructing structured queries from natural language, self-querying retrieval ensures both efficiency and precision in data fetching, as it can consider semantic elements and metadata simultaneously.
import OpenAI
import pymongo
from bson.json_util import dumps
# OpenAI API key setup
openai.api_key = 'your-api-key'
# Connect to MongoDB
client = pymongo.MongoClient('mongodb://localhost:27017/')
db = client['your_database']
collection = db['your_collection']
# Function to use GPT-3.5 for interpreting natural language query and outputting a structured query
def interpret_query_with_gpt(query):
response = openai.Completion.create(
model="gpt-3.5-turbo",
prompt=f"Translate the following natural language query into a MongoDB vector search query:nn'{query}'",
max_tokens=300
)
return response.choices[0].message.content
# Function to execute MongoDB vector search query
def execute_query(query):
structured_query = eval(query) # Caution: Use eval carefully
results = collection.aggregate([structured_query])
return dumps(list(results), indent=4)
# Example usage
natural_language_query = "Find documents related to AI advancements"
structured_query = interpret_query_with_gpt(natural_language_query)
results = execute_query(structured_query)
print(results)
Technique 2: Parent-Child Relationship in Advanced RAG (aka auto merging)
In advanced RAG systems, the concept of parent-child relationships takes data retrieval to a new level. This approach involves segmenting large documents into smaller, manageable parts – the parent documents and their respective child documents.
- Parent-Child Document Dynamics: Large documents are broken down into parent and child documents. Parent documents provide a broader context, while child documents offer specific details.
- Vectorization for Precision: Each child document is vectorized, creating a unique digital profile that aids in precise data retrieval.
- Query Processing and Contextual Responses: When a query is received, it's matched against these vectorized child documents. The system not only retrieves the most relevant child document but also brings in the parent document for additional context. This approach ensures that responses are not only precise but also rich in contextual information.
- Enhanced LLM Integration: The detailed information from the child and parent documents is then fed into a Large Language Model (LLM), like ChatGPT, for generating responses that are both accurate and context-aware.
- Implementation in MongoDB: Leveraging MongoDB's vector search, this technique offers a refined method for navigating large datasets, ensuring quick and contextually rich responses.
This technique addresses the limitations of basic RAG by providing a more nuanced and contextually rich approach to data retrieval, crucial for complex queries where understanding the broader context is key.

Python">from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
# Initialize the text splitters for parent and child documents
parent_splitter = RecursiveCharacterTextSplitter(chunk_size=2000)
child_splitter = RecursiveCharacterTextSplitter(chunk_size=200)
# Function to process PDF document and split it into chunks
def process_pdf(file):
loader = PyPDFLoader(file.name)
docs = loader.load()
parent_docs = parent_splitter.split_documents(docs)
# Process parent documents
for parent_doc in parent_docs:
parent_doc_content = parent_doc.page_content.replace('n', ' ')
parent_id = collection.insert_one({
'document_type': 'parent',
'content': parent_doc_content
}).inserted_id
# Process child documents
child_docs = child_splitter.split_documents([parent_doc])
for child_doc in child_docs:
child_doc_content = child_doc.page_content.replace('n', ' ')
child_embedding = embeddings.embed_documents([child_doc_content])[0]
collection.insert_one({
'document_type': 'child',
'content': child_doc_content,
'embedding': child_embedding,
'parent_ref': parent_id
})
return "PDF processing complete"
# Function to embed a query and perform a vector search
def query_and_display(query):
query_embedding = embeddings.embed_documents([query])[0]
# Retrieve relevant child documents based on query
child_docs = collection.aggregate([{
"$vectorSearch": {
"index": "vector_index",
"path": "embedding",
"queryVector": query_embedding,
"numCandidates": 10
}
}])
# Fetch corresponding parent documents for additional context
parent_docs = [collection.find_one({"_id": doc['parent_ref']}) for doc in child_docs]
return parent_docs, child_docs
from langchain.llms import OpenAI
# Initialize the OpenAI client
openai_client = OpenAI(api_key=OPENAI_API_KEY)
# Function to generate a response from the LLM
def generate_response(query, parent_docs, child_docs):
response_content = " ".join([doc['content'] for doc in parent_docs if doc])
chat_completion = openai_client.chat.completions.create(
messages=[{"role": "user", "content": query}],
model="gpt-3.5-turbo"
)
return chat_completion.choices[0].message.content
For the complete code snippet, you can refer to my previous article:
Byebye Basic RAG: Embracing Advanced Retrieval with MongoDB Vector Search
Technique 3: Interactive RAG – Question-Answering
Developed by Fabian Valle, MongoDB Sales Innovation Program Lead, Interactive RAG represents the forefront of AI-driven search capabilities. This technique enhances traditional RAG by allowing users to actively influence the retrieval process in real-time, which makes for a more tailored and precise information discovery.

- Dynamic Retrieval Strategy: Users can adjust retrieval parameters on-the-fly, such as chunk size or the number of sources, to optimize results for their specific queries.
- Function Calling for Enhanced Interactivity: The integration of function calling APIs allows the RAG system to interact with external data sources and services, providing up-to-date and relevant information.
- Interactive Question-Answering: This feature empowers users to ask questions in natural language, which the system then processes using a vector search to find the most relevant information, followed by a language model like GPT-3.5 or GPT-4 to generate an informed response.
- Continuous Learning: The Interactive RAG system learns from each interaction, improving its knowledge base over time, which ensures that subsequent answers are more accurate and contextual.
This third technique showcases how advanced RAG methods can be crucial for future AI applications, offering a dynamic, adaptive, and user-centric approach to information retrieval and processing. You can find detailed code snippets in Fabian's blog.
Interactive RAG with MongoDB Atlas + Function Calling API | MongoDB
Technique 4: Contextual compression in Advanced RAG
Contextual compression tackles the challenge of retrieving relevant information from documents filled with irrelevant text. It adapts to the unpredictability of queries by compressing documents based on query context, ensuring that only pertinent information is passed through the language model, which enhances response quality and reduces costs.
- Contextual Compression Mechanics: This method optimizes the retrieval process by compressing the retrieved documents based on the context of the query, meaning that it only returns the information most pertinent to the user's request.
- Efficient Data Handling: By compressing documents contextually, the system minimizes the load on the language model, leading to faster and more cost-effective operations.
- Implementation with Document Compressors: Using base retrievers and document compressors like the LLMChainExtractor from Longchain, the system filters through the initial documents to shorten content or omit documents entirely based on their relevance to the query.
- Enhanced Query Relevance: The result is a set of compressed documents that contain highly relevant information, which the language model can use to generate precise answers without sifting through extraneous content.
This approach, highlighted in Brian Leonard's work as a Principal Solutions Architect at MongoDB, showcases the utility of Langchain python code in creating efficient and focused AI retrieval systems. For a deep dive into contextual compression, Leonard's blog offers valuable insights and examples.
Semantic Search Made Easy With LangChain and MongoDB | MongoDB
Conclusion
In wrapping up, we've explored the landscape of advanced RAG methods, exploring their pivotal role in the AI revolution. Techniques like Self-Querying Retrieval, Parent-Child Relationships, Interactive RAG, and Contextual Compression show us the art of the possible when we blend human-like understanding with machine precision. With guidance from AI thought leaders and the practical applications they've pioneered, we stand on the cusp of a future where AI doesn't just answer our questions – it understands them, context, and all. This is the future advanced RAG is guide us towards a more intuitive, responsive, and accurate AI.