Strategies
RAG strategy implementations.
CRAG(retriever, response_synthesizer)
Bases: Strategy
Corrective RAG with relevance-based routing.
Pipeline pattern: Retrieve ↓ Decide ├─ relevant → [Rerank?] → Generate ├─ irrelevant → WebRetrieve → [Rerank?] → Generate └─ ambiguous → FilterChunks → [Rerank?] → Generate
Component usage in this pattern:
1. relevance_evaluator is optional. When configured, it is used in the
Decide stage to evaluate retrieved chunks and set retrieval_decision:
- relevant when all chunks pass.
- irrelevant when no chunks pass.
- ambiguous when only part of the chunks pass.
It also stores the filtered subset in filtered_chunks. When it is not
configured, CRAG falls back to the relevant path.
2. web_retriever is used only in the irrelevant branch as fallback
retrieval. If absent, the branch degrades to generating from the original
retrieved chunks.
3. reranker is optional and applied inside each generate branch before
synthesis, so the selected branch chunks are reranked prior to response
generation.
Initializes a new CRAG strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retriever
|
BaseRetriever
|
Primary retriever over the main corpus. |
required |
response_synthesizer
|
ResponseSynthesizer
|
Component used to generate the final answer. |
required |
MapReduceRAG(transformer, retriever, response_synthesizer, fuse_fn='rrf', top_k=5)
Bases: Strategy
Map-reduce based RAG strategy.
The pipeline follows the pattern: Expand query → Retrieve and fuse (map_reduce) → [Reranker?] → Generate.
Attributes:
| Name | Type | Description |
|---|---|---|
reranker |
BaseReranker | None
|
Optional reranker component. |
Initializes a new MapReduceRAG strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transformer
|
BaseQueryTransformer
|
The query transformer component. |
required |
retriever
|
BaseRetriever
|
The retriever component for fetching chunks. |
required |
response_synthesizer
|
ResponseSynthesizer
|
The response synthesizer component. |
required |
fuse_fn
|
str | Callable[[list[list[Chunk]]], list[Chunk]]
|
Fusion strategy name or custom callable. Defaults to "rrf". |
'rrf'
|
top_k
|
int
|
Default top-k retrieval count. Defaults to 5. |
5
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any required dependency is None, or top_k is less than 1. |
arun(question, top_k=None, **kwargs)
async
Async entry point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The user query. |
required |
top_k
|
int | None
|
Number of top results to retrieve. If None, uses the constructor default. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional retrieval parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Final state dict with response. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If top_k is less than 1. |
run(question, top_k=None, **kwargs)
Synchronous entry point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The user query. |
required |
top_k
|
int | None
|
Number of top results to retrieve. If None, uses the constructor default. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional retrieval parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Final state dict with response. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If top_k is less than 1. |
NaiveRAG(retriever, response_synthesizer)
Bases: Strategy
Linear RAG strategy with optional slots.
The pipeline follows the pattern: [QueryExpansionPipeline?] → Retrieve → [RelevanceFilter?] → [Reranker?] → Generate
When query_expansion is set, the bare retriever step is replaced by the QueryExpansionPipeline subgraph.
Attributes:
| Name | Type | Description |
|---|---|---|
query_expansion |
QueryExpansionPipeline | None
|
Optional query expansion pipeline. |
reranker |
BaseReranker | None
|
Optional reranker component. |
relevance_filter |
BaseRelevanceFilter | None
|
Optional relevance filter component. |
Initializes a new NaiveRAG strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retriever
|
BaseRetriever
|
The retriever component for fetching chunks. |
required |
response_synthesizer
|
ResponseSynthesizer
|
The response synthesizer component for generating responses. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RoutedStrategy()
Bases: CompositeStrategy
Composite strategy that routes queries to one of multiple branches.
RoutedStrategy supports classifier mode and router mode. Classifier mode
evaluates registered classifiers in registration order and selects the first
matching branch (first-match semantics). Router mode delegates branch-name
selection to a single BaseRouter. The selected branch name is written into
state and dispatched via a switch step.
Initializes a new RoutedStrategy.
add_branch(name, classifier=None, branch=None)
Adds a routed branch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique branch name. |
required |
classifier
|
Callable[[str], bool | Awaitable[bool]] | None
|
Predicate used for classifier-mode route selection. Must be omitted in router mode. |
None
|
branch
|
Strategy | Pipeline | None
|
Branch strategy or pipeline. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
RoutedStrategy |
'RoutedStrategy'
|
This strategy instance for fluent chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any of the following is true: 1. A branch uses the reserved no-match sentinel name. 2. A branch with the same name has already been registered. 3. Classifier configuration does not match the selected routing mode. |
set_router(router)
Configures this strategy to use router mode.
Calling this method again while the strategy is already in router mode replaces the router, preserves registered branches, and invalidates the cached pipeline so the next build uses the new router.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
router
|
BaseRouter
|
Router used to select a registered branch name. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RoutedStrategy |
'RoutedStrategy'
|
This strategy instance for fluent chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If classifier branches are already registered. |