Advanced RAG Techniques in R2R: HybridRAG and Beyond
Covering the advanced RAG options in R2R
In the rapidly evolving field of information retrieval and natural language processing, R2R continues to push boundaries by incorporating cutting-edge techniques. Today, we're excited to highlight our implementation of HybridRAG and other advanced features that significantly enhance the capabilities of Retrieval Augmented Generation (RAG) systems. This blog post will explore how these advanced techniques work, their benefits, and how they're seamlessly integrated into R2R.
HybridRAG: Combining Knowledge Graphs and Vector Retrieval#
HybridRAG is an innovative approach that combines the strengths of knowledge graphs (GraphRAG) with traditional vector-based retrieval methods (VectorRAG). It addresses two key challenges faced by individual RAG systems:
- Answering questions that require understanding complex relationships between different chunks of information
- Providing responses that necessitate a global context, drawing from the entire dataset
Figure 1: HybridRAG architecture combining knowledge graphs and vector retrieval
By integrating both knowledge graph structures and vector-based retrieval into the RAG pipeline, HybridRAG preserves complex relationships between different pieces of information while also leveraging the broad coverage of vector search, allowing for more nuanced and context-aware retrieval.
How HybridRAG Works in R2R#
Figure 2: Simplified system architecture of R2R, illustrating the connection of the vector database and knowledge graph, and more.
R2R's implementation of HybridRAG involves several key steps:
-
Vector Database Creation: Documents ingested into R2R are chunked and embedded using an embedding model, then stored in a vector database for similarity search.
-
Knowledge Graph Construction: Entities and relationships are extracted from the text to build a comprehensive knowledge graph. Microsoft's GraphRAG approach is then implemented to cluster the entities and relationships into communities to augment the knowledge graph.
-
Hybrid Retrieval:
- Vector-based Retrieval: Performs similarity search within the vector database to identify relevant document chunks.
- Graph-based Retrieval: Searches over entities, relationships, and communities in the augmented knowledge graph.
-
Context Combination: Contextual information retrieved from both vector search and knowledge graph traversal is combined to provide a rich, multi-faceted context for the query.
-
Response Generation: The combined context is used by a large language model to generate the final response, leveraging both broad similarity-based information and structured relationship data.
Implementing HybridRAG in R2R#
Integrating HybridRAG into your R2R workflow is straightforward. Here's an example of how to use both vector search and knowledge graph search in a single query:
from r2r import R2RClientclient = R2RClient("http://localhost:7272")result = client.search(query="Who is Aristotle?",vector_search_settings={"use_vector_search": True,"filters": {"document_id": {"eq": "3e157b3a-8469-51db-90d9-52e7d896b49b"}},"search_limit": 20,"use_hybrid_search": True},kg_search_settings={"use_kg_search": True,"kg_search_type": "local","kg_search_level": 0,"kg_search_generation_config": {"model": "gpt-4-turbo","temperature": 0.7,},"max_llm_queries_for_global_search": 250})
For a comprehensive explanation of available runtime configurations, please refer to our Python SDK documentation. The settings demonstrated above can also be applied to the RAG endpoint of a deployed R2R system, enabling it to utilize the enhanced search capabilities.
Benefits of HybridRAG#
-
Enhanced Context Understanding: By combining vector-based similarity and knowledge graph relationships, HybridRAG provides a more comprehensive view of the data.
-
Improved Answer Accuracy: The integration of structured relationships from the knowledge graph with broad coverage from vector search allows for more precise and relevant information retrieval.
-
Global and Local Context Awareness: Vector search provides a global view of the dataset, while knowledge graphs capture local, structured relationships.
-
Flexible Retrieval: The hybrid approach allows R2R to adapt to different types of queries, using the most appropriate retrieval method for each situation.
-
Scalability: HybridRAG can handle large and complex datasets more effectively than individual RAG systems.
-
Better Performance on Various Question Types: As shown in research, HybridRAG outperforms both VectorRAG and GraphRAG individually on metrics such as faithfulness, answer relevance, and context recall.
Performance Metrics#
Based on research, HybridRAG shows impressive performance compared to individual VectorRAG and GraphRAG approaches:
- Faithfulness: HybridRAG (0.96) matches GraphRAG (0.96) and outperforms VectorRAG (0.94)
- Answer Relevance: HybridRAG (0.96) significantly outperforms both VectorRAG (0.91) and GraphRAG (0.89)
- Context Recall: HybridRAG (1.0) matches VectorRAG (1.0) and outperforms GraphRAG (0.85)
While there's a slight trade-off in Context Precision, the overall performance of HybridRAG demonstrates its effectiveness in providing accurate and comprehensive responses.
Additional Advanced RAG Techniques in R2R#
In addition to HybridRAG, R2R supports other advanced RAG techniques that can be easily configured at runtime:
1. HyDE (Hypothetical Document Embeddings)#
HyDE enhances retrieval by generating and embedding hypothetical documents based on the query.
Figure 3: A schematic showing how HyDE is used to boost RAG quality. Output from HyDE will flow through whatever search settings are given in the input config, including GraphRAG.
Here's how to use it:
hyde_response = client.rag("What are the main themes in Shakespeare's plays?",vector_search_settings={"search_strategy": "hyde","search_limit": 10})
2. RAG-Fusion#
RAG-Fusion improves retrieval quality by combining results from multiple search iterations.
Figure 4: RAG fusion works by performing reciprocal rank fusion on outputs from a number of search queries.
Here's an example:
rag_fusion_response = client.rag("Explain the theory of relativity",vector_search_settings={"search_strategy": "rag_fusion","search_limit": 20})
Hybrid Search Implementation#
R2R also offers a powerful hybrid search feature that combines traditional keyword-based searching with modern semantic understanding. This approach is particularly effective for complex queries where both specific terms and overall meaning are crucial.
Figure 5: Hybrid search combines vector search with full text search for enhanced relevance.
The hybrid search process in R2R works as follows:
- Full-Text Search: Utilizes PostgreSQL's full-text search capabilities.
- Semantic Search: Performs vector similarity search using the query's embedded representation.
- Reciprocal Rank Fusion (RRF): Merges results from full-text and semantic searches.
- Result Ranking: Orders final results based on the combined RRF score.
Here's an example of how to use hybrid search in R2R:
vector_settings = {"use_hybrid_search": True,"search_limit": 20,"filters": {"category": {"$eq": "philosophy"}},"hybrid_search_settings": {"full_text_weight": 1.0,"semantic_weight": 5.0,"full_text_limit": 200,"rrf_k": 50}}results = client.search(query="Who was Aristotle?",vector_search_settings=vector_settings)
Retrieval SDK#
R2R provides a powerful Retrieval SDK that makes it easy to implement these advanced techniques in your applications. Here's a snippet from the SDK showcasing the flexibility of the rag method:
async def rag(client,query: str,rag_generation_config: Optional[Union[dict, GenerationConfig]] = None,vector_search_settings: Optional[Union[dict, VectorSearchSettings]] = None,kg_search_settings: Optional[Union[dict, KGSearchSettings]] = None,task_prompt_override: Optional[str] = None,include_title_if_available: Optional[bool] = False,) -> Union[RAGResponse, AsyncGenerator[RAGResponse, None]]: # Implementation details...
This method allows you to easily configure various aspects of the RAG process, including generation settings, vector search parameters, knowledge graph search settings, and more.
Conclusion#
R2R's implementation of HybridRAG, along with other advanced RAG techniques and hybrid search, represents a significant advancement in our quest to provide more accurate, contextually relevant, and insightful information retrieval. By combining the strengths of knowledge graphs with vector-based retrieval and other cutting-edge approaches, R2R offers a powerful solution for handling complex queries and large datasets, particularly in domains with specialized terminology and intricate relationships.
The flexibility of R2R allows you to experiment with different strategies, settings, and combinations of techniques to find the optimal configuration for your specific use case. Whether you're working on question-answering systems, document analysis, or any other NLP task that requires sophisticated information retrieval, R2R provides the tools and techniques to enhance your results.
We encourage you to explore the capabilities of these advanced techniques in R2R and experience the enhanced performance in your information retrieval tasks. As always, we're committed to pushing the boundaries of what's possible in natural language processing and information retrieval.
Stay tuned for more updates and innovations from the R2R team!
Was this helpful?