Skip to main content

Overview

The PageIndex RAG module implements a RAG pipeline using the PageIndex retrieval API. Unlike the other RAG modules that use local vector stores, this module leverages PageIndex’s cloud-based document retrieval service. Module: src.rag.pageindex Source: src/rag/pageindex.py

Configuration

Environment Variables

Required environment variables in .env:

Default Models

Prompt Template

Uses the standard medical expert prompt (same as Simple RAG).

Core Functions

_wait_for_retrieval_completion

Polls PageIndex retrieval endpoint until completion or timeout.
str
required
The retrieval ID returned from PageIndex submit_query
int
default:"120"
Maximum wait time for retrieval completion
float
default:"2.0"
Poll interval for checking retrieval status
Dict[str, Any]
Complete retrieval result from PageIndex API
Raises:
  • RuntimeError: If retrieval status is “failed” or “error”
  • TimeoutError: If timeout is reached before completion

_extract_contexts_from_retrieval

Converts PageIndex retrieval payload into the List[str] context format expected by RAGAS.
Dict[str, Any]
required
The retrieval result from PageIndex API
List[str]
List of formatted context strings, each containing:
  • Title of the retrieved node
  • Top 2 relevant content snippets

_format_contexts

Formats contexts into a prompt-ready block.
List[str]
required
List of context strings to format
str
Formatted string with numbered documents

process_pageindex_query

Processes a query with PageIndex retrieval and OpenAI answer synthesis.
str
required
User question
ChatOpenAI
default:"None"
Optional custom answer model. Defaults to gpt-4o with temperature=0
str
default:"None"
Optional PageIndex document id. If None, uses PAGEINDEX_DOC_ID from .env
bool
default:"False"
Whether to enable PageIndex deeper retrieval mode
int
default:"120"
Max wait time for retrieval completion
float
default:"2.0"
Poll interval for retrieval status
Dict[str, Any]
Dictionary containing:
  • answer (str): Generated answer
  • contexts (List[str]): Extracted context strings
  • retrieved_nodes (List[dict]): Full PageIndex retrieval nodes
  • retrieval_result (dict): Complete PageIndex API response
  • metrics (dict):
    • input_tokens (int): Input tokens for answer generation
    • output_tokens (int): Output tokens generated
    • total_tokens (int): Total tokens
    • usage_source (str): Source of usage data
    • cost (float): Cost in USD
    • cost_source (str): Source of cost calculation
    • retrieval_id (str): PageIndex retrieval ID
    • doc_id (str): PageIndex document ID used

query_for_evaluation

Wrapper function compatible with the benchmark evaluator contract.
str
required
The question to process
str
default:"None"
Optional model name for answer generation. Defaults to “gpt-4o”
str
default:"None"
Optional PageIndex document id to override PAGEINDEX_DOC_ID
ChatOpenAI
default:"None"
Optional custom LLM instance (takes precedence over llm_model)
bool
default:"False"
Optional PageIndex retrieval mode for deeper analysis
Dict[str, Any]
Dictionary containing:
  • question (str): Original question
  • answer (str): Generated answer
  • contexts (List[str]): Retrieved context strings
  • source_documents (List[dict]): PageIndex retrieved nodes
  • metadata (dict): Comprehensive metadata including:
    • num_contexts (int): Number of contexts
    • retrieval_method (str): “pageindex”
    • llm_model (str): Model name used
    • provider (str): Provider name
    • model_id (str): Full model ID
    • execution_time (float): Total execution time
    • input_tokens (int): Input tokens used
    • output_tokens (int): Output tokens generated
    • total_cost (float): Total cost in USD
    • tokens_used (int): Total tokens
    • usage_source (str): Usage data source
    • cost_source (str): Cost calculation source
    • doc_id (str): PageIndex document ID
    • retrieval_id (str): PageIndex retrieval ID
    • pageindex_thinking (bool): Whether thinking mode was enabled

Usage Example

Pipeline Flow

  1. Submit Query: Submits query to PageIndex API with document ID
  2. Wait for Completion: Polls retrieval status every 2 seconds (default) until complete
  3. Extract Contexts: Extracts relevant content snippets from PageIndex nodes
  4. Format: Formats contexts into numbered documents
  5. Generate Answer: Uses OpenAI LLM to generate answer based on contexts
  6. Track: Captures token usage, cost, and retrieval metadata

PageIndex Retrieval Modes

Standard Mode (thinking=False)

Fast retrieval mode for straightforward queries.

Thinking Mode (thinking=True)

Deeper analysis mode for complex queries. May take longer but provides more comprehensive results.

Error Handling

The module handles several error conditions:

Key Features

  • Cloud-based retrieval: Uses PageIndex API instead of local vector store
  • Asynchronous polling: Waits for retrieval completion with configurable timeout
  • Thinking mode: Optional deeper analysis for complex queries
  • Flexible document selection: Can override document ID per query
  • Automatic context extraction: Extracts top 2 snippets per node
  • Full metadata tracking: Includes retrieval ID and document ID in results
  • Compatible interface: Matches the contract of other RAG modules

When to Use PageIndex

PageIndex RAG is ideal when:
  • You want to avoid managing local vector stores
  • Documents are already indexed in PageIndex
  • You need cloud-based, managed retrieval
  • You want to leverage PageIndex’s advanced retrieval features
  • You need to query multiple document collections dynamically