latent.flows.rag_research_flow.flow¶
rag_research_flow — RAG pipeline optimization with flow+task pattern.
Functions¶
rag_research_flow¶
rag_research_flow(eval_fn: RAGEvalFn | None = None, metrics: list | None = None, baseline_config: dict[str, Any] | None = None, max_trials: int | None = None, max_eval_configs: int | None = None, embedding_provider: str | None = None, embedding_model: str | None = None, persist_base: str | None = None, chunk_sizes: list[int] | None = None, chunk_overlaps: list[int] | None = None, backends: list[str] | None = None, alphas: list[float] | None = None, rerankers: list[str] | None = None, query_expansions: list[str] | None = None, llm_model: str | None = None) -> dict[str, Any]
Run RAG pipeline optimization with MLflow logging.
Two-phase flow+task pattern:
1. search — Optuna Bayesian optimization over RAG component space.
2. eval — Validate top configs via caller-supplied eval_fn.
The corpus and the eval set are catalog datasets, not parameters, because
Prefect Cloud rejects flow-run parameters over 512 KB. By default the
corpus is data/rag_research/input/documents.json, a JSON list of
strings; override the entry in flows/rag_research/catalog.yaml to point
at a directory of text or markdown files instead. The eval set is
data/rag_research/input/eval_data.json, a non-empty JSON list of dicts,
each with a "query" key.
Args:
eval_fn: Async callable (config, eval_data) -> EvalReport.
If None, eval phase is skipped.
metrics: RAGMetric instances. Defaults to [ContentRecallMetric(k=7), ContentMRRMetric()].
baseline_config: Baseline pipeline config for Optuna seeding.
max_trials: Search phase trial budget.
max_eval_configs: Number of top configs to validate in eval phase.
embedding_provider: Embedding provider (e.g. "voyage").
embedding_model: Embedding model name.
persist_base: Directory for caching pre-indexed adapters.
chunk_sizes: Chunk sizes to explore.
chunk_overlaps: Chunk overlaps to explore.
backends: Backend types to explore.
alphas: Hybrid alpha values to explore.
rerankers: Rerankers to explore, from "none", "cross_encoder"
and "llm". Defaults to ["none", "cross_encoder"].
query_expansions: Query expansions to explore, from "none",
"rewriter" and "hyde". Defaults to ["none"].
llm_model: Provider-prefixed litellm model (e.g. "bedrock/...")
for every LLM component. Required when rerankers or
query_expansions names one.
Returns:
Dict with report (StatisticalReport), markdown (str),
optimization (OptimizationReport), and scalar result fields.
search_configs¶
search_configs(documents: list[str] | dict[str, str], eval_data: list[dict[str, Any]], metrics: list, max_trials: int = 15, baseline_config: dict[str, Any] | None = None, persist_base: str | None = None, embedding_provider: str = 'voyage', embedding_model: str = 'voyage-3', chunk_sizes: list[int] | None = None, chunk_overlaps: list[int] | None = None, backends: list[str] | None = None, alphas: list[float] | None = None, rerankers: list[str] | None = None, query_expansions: list[str] | None = None, llm_model: str | None = None) -> list[Experiment]
Search phase: Optuna Bayesian optimization over RAG component space.
documents and eval_data are the catalog datasets of the same names,
loaded here rather than passed through the flow, because Prefect Cloud
rejects flow-run parameters over 512 KB. A json.JSON list of strings is
used as is; a text.Text/text.Markdown directory contributes one
document per file. eval_data is a json.JSON list of dicts, each
with a "query" key.
validate_configs¶
validate_configs(eval_data: list[dict[str, Any]], configs: list[dict[str, Any]], eval_fn: RAGEvalFn) -> list[Experiment]
Eval phase: score each config with eval_fn on the eval_data dataset.
eval_data loads from the catalog for the same reason as in
:func:search_configs. A config whose eval_fn raises is recorded as a
failed experiment; the others still run.