Skip to content

latent.rag.optimize

RAG pipeline optimizer — search-based and agentic.

Searches over RAG configurations (chunk size, top_k, alpha, backend, etc.) to find the best pipeline for a given dataset and metric.

Classes

RAGOptimizationResult

RAGOptimizationResult()

Optimization result extended with the best RAG pipeline configuration.

RAGOptimizer

RAGOptimizer(search_space: RAGSearchSpace, documents: list[str], eval_data: list[dict[str, Any]], metrics: list[RAGMetric], strategy: str = 'bayesian', agent_model: str | None = None, prompt_optimizer: Any | None = None, tracker: ExperimentTracker | None = None, lower_is_better: bool = False)

Search-based optimizer for RAG pipeline hyperparameters.

Supports "grid", "random", and "bayesian" strategies. Bayesian optimization requires optuna (pip install latent[optimizers]).

RAGSearchSpace

RAGSearchSpace(chunk_sizes: list[int] = (lambda: [256, 512, 1024])(), chunk_overlaps: list[int] = (lambda: [50, 100, 200])(), embedding_providers: list[str] | None = None, embedding_models: list[str] | None = None, top_k: list[int] = (lambda: [3, 5, 10])(), backends: list[str] = (lambda: ['chroma', 'hybrid', 'bm25'])(), alpha: list[float] = (lambda: [0.3, 0.5, 0.7])(), score_thresholds: list[float] = (lambda: [0.0, 0.3, 0.5])(), raptor_tree_depths: list[int] | None = None, raptor_summary_models: list[str] | None = None)

Search space for RAG pipeline hyperparameter optimization.

Parameters are split into two groups:

Index-time (require re-chunking + re-embedding): - chunk_sizes — token/character budget per chunk - chunk_overlaps — overlap between consecutive chunks - embedding_providers — e.g. ["openai", "voyage"] - embedding_models — e.g. ["text-embedding-3-small"]

Query-time (cheap, no re-indexing needed): - top_k — number of chunks to retrieve - backends — retrieval backend names - alpha — hybrid fusion weight (only applies to hybrid backend) - score_thresholds — minimum similarity score to keep a chunk

Methods

RAGOptimizer.optimize

optimize(max_trials: int = 20) -> RAGOptimizationResult

Run the optimizer and return the best configuration found.

RAGSearchSpace.index_configs

index_configs() -> list[dict[str, Any]]

Return cartesian product of index-time parameters.

Filters out combinations where chunk_overlap >= chunk_size.

RAGSearchSpace.query_configs

query_configs() -> list[dict[str, Any]]

Return cartesian product of query-time parameters.

Alpha is only included for the hybrid backend. Duplicate configurations (after alpha is stripped for non-hybrid backends) are removed using a seen set.

RAGSearchSpace.to_dict

to_dict() -> dict[str, Any]

Serialize all fields for agent tool responses.