Skip to main content

Overview

The HyDE (Hypothetical Document Embeddings) RAG module implements a two-stage RAG pipeline. It first generates a hypothetical document that would perfectly answer the user’s query, then uses that document for semantic search. This approach can improve retrieval accuracy by searching for detailed content rather than a short query. Module: src.rag.hyde Source: src/rag/hyde.py

Configuration

Default Models

Vector Store

Prompt Templates

HyDE Document Generation Prompt

Answer Generation Prompt

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

Functions

generate_hypothetical_document

Generates a hypothetical document based on the user’s query.
str
required
The user’s question
Dict[str, Any]
Dictionary containing:
  • document (str): The generated hypothetical document
  • input_tokens (int): Input tokens used
  • 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

format_docs

Formats the retrieved documents to be included in the final prompt.
List[Any]
required
A list of retrieved LangChain Document objects
str
A formatted string containing the content of the documents

process_hyde_query

Processes a query using the full HyDE RAG pipeline.
str
required
The user’s question
ChatOpenAI
default:"None"
Custom model for hypothetical document generation
ChatOpenAI
default:"None"
Custom model for answer generation
Dict[str, Any]
Dictionary containing:
  • answer (str): The final generated answer
  • contexts (List[str]): Retrieved document contents
  • hypothetical_document (str): The generated hypothetical document
  • hyde_metrics (dict): Metrics for HyDE generation
    • input_tokens (int)
    • output_tokens (int)
    • cost (float)
    • usage_source (str)
    • cost_source (str)
  • answer_metrics (dict): Metrics for answer generation
    • input_tokens (int)
    • output_tokens (int)
    • cost (float)
    • usage_source (str)
    • cost_source (str)
  • total_cost (float): Combined cost
  • total_input_tokens (int): Combined input tokens
  • total_output_tokens (int): Combined output tokens
  • usage_sources (List[str]): Sources of usage data
  • cost_sources (List[str]): Sources of cost calculations

query_for_evaluation

A wrapper function for RAG evaluation frameworks like Ragas.
str
required
The question to process
str
default:"None"
The name of the LLM model to use for HyDE generation. Defaults to “gpt-3.5-turbo”
str
default:"None"
The name of the LLM model to use for answer generation. Defaults to “gpt-4o”
BaseChatModel
default:"None"
Pre-configured LLM for HyDE. Takes precedence over hyde_model
BaseChatModel
default:"None"
Pre-configured LLM for answer. Takes precedence over answer_model
dict
Dictionary containing:
  • question (str): Original question
  • answer (str): Generated answer
  • contexts (List[str]): Retrieved document contents
  • metadata (dict): Comprehensive metadata including:
    • execution_time (float): Total execution time in seconds
    • input_tokens (int): Total input tokens (HyDE + Answer)
    • output_tokens (int): Total output tokens (HyDE + Answer)
    • total_cost (float): Total cost in USD
    • retrieval_method (str): “hyde”
    • llm_hyde_model (str): Model used for HyDE generation
    • llm_answer_model (str): Model used for answer generation
    • hyde_provider (str): Provider for HyDE model
    • answer_provider (str): Provider for answer model
    • hyde_model_id (str): Full HyDE model ID
    • answer_model_id (str): Full answer model ID
    • hyde_cost (float): Cost for HyDE generation
    • answer_cost (float): Cost for answer generation
    • usage_source (str): Combined usage sources
    • cost_source (str): Cost calculation source

Usage Example

Pipeline Flow

  1. Generate HyDE: Uses gpt-3.5-turbo (temperature=0.7) to generate a detailed hypothetical document that would answer the question
  2. Retrieve: Uses the hypothetical document (not the original query) to perform semantic search and retrieve the top 5 most relevant actual documents
  3. Format: Formats retrieved documents with metadata
  4. Generate Answer: Uses gpt-4o (temperature=0) to generate the final answer based on retrieved context
  5. Track: Captures separate metrics for both HyDE and answer generation

Key Features

  • Two-stage retrieval: Generates hypothetical content first, then searches
  • Improved semantic matching: Searches with detailed content vs. short query
  • Dual model tracking: Separate metrics for HyDE and answer generation
  • Creative HyDE generation: Uses higher temperature (0.7) for document generation
  • Precise answer generation: Uses temperature 0 for final answer
  • Comprehensive cost tracking: Tracks costs for both stages

When to Use HyDE

HyDE works best when:
  • User queries are short or ambiguous
  • You need to bridge vocabulary gaps between query and documents
  • Documents use different terminology than typical user queries
  • You want to improve recall for conceptual questions