GenAIHub
← Back to RAG

RAPTOR

Recursive Abstractive Processing and Thematic Organization for Retrieval

🎧

Listen to this Explanation

Enjoy a clear, AI-narrated audio version (Solo Mode).

Overview

RAPTOR is an advanced information retrieval and question-answering system that combines hierarchical document summarization, embedding-based retrieval, and contextual answer generation. It efficiently handles large document collections by creating a multi-level tree of summaries, allowing for both broad and detailed information retrieval.

"RAPTOR creates a hierarchical structure of document summaries, allowing it to navigate between high-level concepts and specific details as needed, providing contextually appropriate answers at any level of abstraction."

RAPTOR Process Flowchart

RAPTOR Process Flow Diagram

Flowchart showing the RAPTOR hierarchical tree building and retrieval process

Motivation

Traditional retrieval systems often struggle with large document sets:

Missing Important Details

Systems may overlook crucial specific information in large document sets

Information Overload

Getting overwhelmed by irrelevant information when context is too broad

Hierarchical Navigation

RAPTOR creates a tree structure to navigate between concepts and details

Multi-Level Abstraction

Retrieves from the most appropriate level based on query needs

Key Components

Tree Building

Creates hierarchical structure of document summaries at multiple levels.

Embedding & Clustering

Uses GMM to organize documents based on semantic similarity.

Vectorstore

FAISS store for efficient similarity search across all tree levels.

Hierarchical Retrieval

Traverses tree from top to bottom following parent-child links.

Contextual Compression

Extracts only relevant parts from retrieved documents.

Answer Generation

Produces coherent responses from compressed context.

RAPTOR Process Flow

Offline: Tree Building

Top Summary
Summary
Summary
Sum
Sum
Sum
Sum
Doc
Doc
Doc
Doc
Doc
Doc

Each level: Embed β†’ Cluster (GMM) β†’ Summarize

Online: Query Process

1 User query β†’ Embed query
2 Start at top level (summaries)
3 Retrieve similar nodes
4 Follow to child nodes (drill down)
5 Contextual compression
6 Generate answer

Traverses top-down through tree levels

Implementation Example

# RAPTOR Implementation
from sklearn.mixture import GaussianMixture
from langchain.vectorstores import FAISS

def build_raptor_tree(texts, max_levels=3):
    """Build hierarchical tree of summaries."""
    results = {}
    current_texts = texts
    
    for level in range(1, max_levels + 1):
        # Embed and cluster
        embeddings = embed_texts(current_texts)
        n_clusters = min(10, len(current_texts) // 2)
        
        gm = GaussianMixture(n_components=n_clusters)
        cluster_labels = gm.fit_predict(embeddings)
        
        # Store level results
        results[level-1] = {'texts': current_texts, 'clusters': cluster_labels}
        
        # Generate summaries for each cluster
        summaries = []
        for cluster_id in range(n_clusters):
            cluster_texts = [t for t, c in zip(current_texts, cluster_labels) if c == cluster_id]
            summary = summarize_texts(cluster_texts)
            summaries.append(summary)
        
        current_texts = summaries
        if len(current_texts) <= 1:
            break
    
    return results

def hierarchical_retrieval(query, vectorstore, max_level):
    """Retrieve from top level down to original documents."""
    all_docs = []
    
    for level in range(max_level, -1, -1):
        level_docs = vectorstore.similarity_search(
            query, 
            filter={'level': level}
        )
        all_docs.extend(level_docs)
        
        # Follow to child documents
        child_ids = [doc.metadata.get('child_ids', []) for doc in level_docs]
    
    return all_docs

Benefits of RAPTOR

Scalability

Handles large document collections by working with summaries at different levels.

Flexibility

Provides both high-level overviews and specific details as needed.

Context-Awareness

Retrieves from the most appropriate level of abstraction.

Efficiency

Uses embeddings and vectorstore for fast retrieval at any level.

Traceability

Maintains links between summaries and original documents for verification.

Multi-Level Access

Query can access any level from abstract summaries to raw documents.

Ideal Use Cases

Large Document Sets

When dealing with hundreds or thousands of documents that need organization.

Variable Detail Queries

When users need both overview answers and detailed specifics.

Research & Analysis

Academic papers, reports, or technical documentation requiring exploration.

Related Topics

Test Your Knowledge

Score 8/10 or higher to pass