Module query

Module query 

Source
Expand description

§Query Processing Module

Advanced query understanding, transformation, and optimization for RAG systems.

This module provides state-of-the-art query processing techniques to improve retrieval accuracy and relevance. It implements multiple strategies for query enhancement including rewriting, expansion, decomposition, and hypothetical document generation.

§Features

  • Query Rewriting: Transform queries for better retrieval
  • Query Expansion: Add synonyms and related terms
  • Query Classification: Understand query intent and type
  • Query Decomposition: Break complex queries into sub-queries
  • HyDE: Generate hypothetical documents for improved retrieval

§Examples

§Query Rewriting

use rrag::query::{QueryRewriter, RewriteStrategy};

let rewriter = QueryRewriter::new()
    .with_strategy(RewriteStrategy::Semantic)
    .build();

let rewritten = rewriter.rewrite("What's RAG?").await?;
assert!(rewritten.alternatives.contains(&"What is Retrieval Augmented Generation?".to_string()));

§Query Decomposition

use rrag::query::{QueryDecomposer, DecompositionStrategy};

let decomposer = QueryDecomposer::new();

let query = "Compare the performance of BERT and GPT-3 on sentiment analysis";
let sub_queries = decomposer.decompose(query).await?;

// Results in sub-queries like:
// - "BERT performance on sentiment analysis"
// - "GPT-3 performance on sentiment analysis"
// - "Comparison between BERT and GPT-3"

§HyDE (Hypothetical Document Embeddings)

use rrag::query::{HyDEGenerator, HyDEConfig};

let hyde = HyDEGenerator::new(HyDEConfig::default());

let query = "How does photosynthesis work?";
let hypothetical_docs = hyde.generate(query).await?;

// Use hypothetical documents for retrieval
for doc in hypothetical_docs.documents {
    tracing::debug!("Hypothetical answer: {}", doc.content);
}

Re-exports§

pub use classifier::ClassificationResult;
pub use classifier::QueryClassifier;
pub use classifier::QueryIntent;
pub use classifier::QueryType;
pub use decomposer::DecompositionStrategy;
pub use decomposer::QueryDecomposer;
pub use decomposer::SubQuery;
pub use expander::ExpansionConfig;
pub use expander::ExpansionResult;
pub use expander::ExpansionStrategy;
pub use expander::QueryExpander;
pub use hyde::HyDEConfig;
pub use hyde::HyDEGenerator;
pub use hyde::HyDEResult;
pub use rewriter::QueryRewriteConfig;
pub use rewriter::QueryRewriter;
pub use rewriter::RewriteResult;
pub use rewriter::RewriteStrategy;

Modules§

classifier
Query Classifier
decomposer
Query Decomposer
expander
Query Expander
hyde
HyDE (Hypothetical Document Embeddings)
rewriter
Query Rewriter

Structs§

QueryProcessingMetadata
Metadata about query processing
QueryProcessingResult
Complete query processing result
QueryProcessor
Main query processor that orchestrates all query enhancement techniques
QueryProcessorConfig
Configuration for the query processor