PageIndex

repository·main·Indexed 12 days ago

https://github.com/vectifyai/pageindex

A reasoning-based, vectorless RAG engine for complex professional documents. PageIndex replaces traditional vector similarity search and chunking with hierarchical tree indexing and LLM-driven agentic search. It offers a Python SDK (v0.2.9), a high-speed 'Flash' mode for tree structure generation via layout statistics, and support for vision-based RAG that reasons over page images without OCR.

Tokens
20.8K
Snippets
76
Records
91
Agent score
99%

What's inside PageIndex

  1. What is PageIndex and how does it work?

    main

    PageIndex is a vectorless, reasoning-based RAG (Retrieval-Augmented Generation) system designed for long, complex professional documents (e.g., legal, financial, technical).

    Unlike traditional RAG that relies on vector similarity search and artificial text chunking, PageIndex uses a two-step process that mimics human expert navigation:

    1. Tree Index Generation: It transforms a document into a hierarchical tree structure index (similar to a semantic Table of Contents).
    2. Reasoning-based Retrieval: It uses LLMs to perform agentic tree search over that index. This allows the model to reason its way to relevant sections based on context, rather than just finding semantically similar text fragments.

    Key Advantages:

    • No Vector DB: Uses document structure and reasoning instead of vector similarity.
    • No Chunking: Organizes documents into natural, semantic sections.
    • Traceability: Results are grounded in explicit page and section references.
    • Context-Aware: Retrieval can incorporate conversation history and domain knowledge.
  2. Understand the PageIndex tree structure format

    main

    The PageIndex tree structure is a hierarchical JSON representation of a document's semantic organization. Each node in the tree represents a section and contains metadata used by LLMs for navigation.

    Key fields in a node include:

    • title: The semantic title of the section.
    • node_id: A unique identifier for the node.
    • start_index / end_index: The page range covered by this section.
    • summary: A concise summary of the section's content.
    • nodes: An array of child nodes representing sub-sections.
    {
      "title": "Financial Stability",
      "node_id": "0006",
      "start_index": 21,
      "end_index": 22,
      "summary": "The Federal Reserve ...",
      "nodes": [
        {
          "title": "Monitoring Financial Vulnerabilities",
          "node_id": "0007",
          "start_index": 22,
          "end_index": 28,
          "summary": "The Federal Reserve's monitoring ..."
        }
      ]
    }
  3. Use PageIndex Flash for fast tree generation

    main

    PageIndex Flash (pageindex/flash) is a preview feature that generates tree structures from PDFs in seconds using heuristic-based extraction instead of an LLM. The LLM is only used for generating node summaries.

    To run in Flash mode:

    python3 run_pageindex.py --flash --pdf_path /path/to/your/document.pdf

    To refine the tree structure for more efficient retrieval, add the --optimize flag to perform an LLM expansion pass.

  4. Implement Vision-based RAG with PageIndex

    main

    A vision-based, vectorless RAG pipeline uses PageIndex to navigate document hierarchies without traditional OCR or vector databases. The workflow follows these steps:

    1. Tree Generation: Submit a PDF to PageIndex to build a hierarchical tree of nodes (titles, summaries, and page ranges).
    2. Reasoning-based Retrieval: Instead of vector similarity, pass the document tree structure and a user query to a Vision-Language Model (VLM). Ask the VLM to identify relevant node_ids based on the tree's summaries.
    3. Visual Context Extraction: Use the retrieved node_ids to find the corresponding page numbers via a node map. Extract these specific pages as images (e.g., using PyMuPDF).
    4. Answer Generation: Pass the user query and the extracted page images to a multimodal VLM (like GPT-4o) to generate an answer based purely on visual context.
    # Conceptual workflow summary
    # 1. Generate Tree
    doc_id = pi_client.submit_document(pdf_path)["doc_id"]
    tree = pi_client.get_tree(doc_id, node_summary=True)['result']
    
    # 2. Reasoning Retrieval (via VLM)
    # Prompt VLM with tree structure to get node_list
    # ...
    
    # 3. Visual Context
    # Extract images for retrieved nodes
    # ...
    
    # 4. Final Answer
    # answer = await call_vlm(query, retrieved_page_images)
  5. Example Pipeline for Metadata-based Retrieval

    main

    To build a retrieval system using metadata, follow this four-step pipeline:

    1. PageIndex Tree Generation: Upload your documents into PageIndex to generate a unique doc_id for every document.
    2. Set up SQL tables: In your own database, store the document metadata alongside its corresponding PageIndex doc_id.
    3. Query to SQL: Use an LLM to translate a natural language user request into a SQL query. This query should target your database to fetch the relevant doc_ids based on the metadata.
    4. Retrieve with PageIndex: Pass the retrieved doc_ids into the PageIndex retrieval API to perform the final reasoning-based RAG on those specific documents.
  6. Explore Vectorless RAG notebooks

    main

    PageIndex provides two primary cookbook notebooks for hands-on experimentation with reasoning-based RAG (Retrieval-Augmented Generation):

    1. Vectorless RAG notebook: A minimal example demonstrating reasoning-based RAG without the need for vectors or chunking. It focuses on human-like retrieval.
    2. Vision-based Vectorless RAG notebook: A reasoning-native RAG pipeline that retrieves and reasons directly over page images without requiring OCR.

    Both notebooks are available via GitHub or can be opened directly in Google Colab for immediate testing.

    https://colab.research.google.com/github/VectifyAI/PageIndex/blob/main/cookbook/pageindex_RAG_simple.ipynb
    https://colab.research.google.com/github/VectifyAI/PageIndex/blob/main/cookbook/vision_RAG_pageindex.ipynb
  7. Explore PageIndex quickstart examples

    main

    There are several ways to get started with PageIndex depending on your use case:

    • Agentic Vectorless RAG: A complete example using the OpenAI Agents SDK with a self-hosted PageIndex instance.
    • Vectorless RAG (Notebook): A minimal, hands-on Jupyter/Colab notebook for reasoning-based RAG.
    • Vision-based Vectorless RAG: A RAG pipeline that works directly over PDF page images without requiring OCR.
    • PageIndex Flash: An ultra-fast preview for generating tree structures from PDFs.
  8. Implement semantic document search using PageIndex

    main

    For documents covering diverse topics, you can combine vector-based semantic search with PageIndex to improve retrieval accuracy. Instead of retrieving raw chunks, use vector search to identify relevant documents first, then use PageIndex to perform structured retrieval within those documents.

    The Semantic Search Pipeline

    1. Chunking and Embedding: Divide your documents into chunks. Use an embedding model to convert these chunks into vectors and store them in a vector database alongside their corresponding doc_id.
    2. Vector Search: For a given query, perform a vector search to retrieve the top-K most relevant chunks and their associated doc_ids.
    3. Compute Document Score: Calculate a relevance score for each document to prioritize those with high-quality matches. Use the following formula to aggregate chunk scores while preventing large documents from dominating purely by volume:

    $$\text{DocScore}=\frac{1}{\sqrt{N+1}}\sum_{n=1}^N \text{ChunkScore}(n)$$

    Where:

    • $N$ is the number of content chunks associated with the document.
    • $\text{ChunkScore}(n)$ is the relevance score of chunk $n$.
    • The $\sqrt{N+1}$ denominator provides diminishing returns, favoring documents with fewer, highly relevant chunks over documents with many weakly relevant ones.
    1. Retrieve with PageIndex: Select the documents with the highest DocScore and use their doc_id to perform structured retrieval via the PageIndex retrieval API.
  9. Integrate expert knowledge or user preferences into tree search

    main

    Unlike vector-based RAG, PageIndex allows you to incorporate domain-specific rules or user preferences by injecting them directly into the LLM tree search prompt. This makes node search more targeted without requiring embedding model fine-tuning.

    Implementation Pipeline

    1. Preference Retrieval: Retrieve relevant user preferences or expert knowledge snippets (e.g., via keyword matching or semantic similarity) based on the incoming query.
    2. Enhanced Tree Search: Include the retrieved preference in the prompt alongside the query and the PageIndex_Tree.

    Example Prompt Pattern:

    prompt = f"""
    You are given a question and a tree structure of a document.
    You need to find all nodes that are likely to contain the answer.
    
    Query: {query}
    
    Document tree structure:  {PageIndex_Tree}
    
    Expert Knowledge of relevant sections: {Preference}
    
    Reply in the following JSON format:
    {{
      "thinking": <reasoning about which nodes are relevant>,
      "node_list": [node_id1, node_id2, ...]
    }}
    """

    Example Preference Input:

    "If the query mentions EBITDA adjustments, prioritize Item 7 (MD&A) and footnotes in Item 8 (Financial Statements) in 10-K reports."

  10. Run the Agentic Vectorless RAG demo

    main

    To see an end-to-end example of agentic vectorless RAG using the OpenAI Agents SDK and self-hosted PageIndex, follow these steps:

    1. Install the optional dependency:
    pip3 install openai-agents
    1. Run the demo script:
    python3 examples/agentic_vectorless_rag_demo.py
    pip3 install openai-agents
    python3 examples/agentic_vectorless_rag_demo.py
  11. Deployment options for PageIndex

    main

    Depending on your needs, you can use PageIndex in several ways:

    • Self-host: Run the open-source repository locally using standard PDF parsing.
    • Cloud Service: Use the production-grade pipeline via the PageIndex Chat Platform, or integrate via MCP or API. This option provides enhanced OCR and tree building.
    • Enterprise: Dedicated or private deployments (VPC, on-prem) available via contact.