graph_retriever.strategies¶
Strategies determine which nodes are selected during traversal.
Eager
dataclass
¶
Eager(
_nodes: list[Node] = list(),
*,
k: int = 5,
start_k: int = 4,
adjacent_k: int = 10,
max_depth: int | None = None,
_query_embedding: list[float] = list(),
)
Bases: Strategy
Eager traversal strategy (breadth-first).
This strategy selects all discovered nodes at each traversal step. It ensures breadth-first traversal by processing nodes layer by layer, which is useful for scenarios where all nodes at the current depth should be explored before proceeding to the next depth.
PARAMETER | DESCRIPTION |
---|---|
k
|
Maximum number of nodes to retrieve during traversal.
TYPE:
|
start_k
|
Number of documents to fetch via similarity for starting the traversal. Added to any initial roots provided to the traversal.
TYPE:
|
adjacent_k
|
Number of documents to fetch for each outgoing edge.
TYPE:
|
max_depth
|
Maximum traversal depth. If
TYPE:
|
build
staticmethod
¶
Build a strategy for a retrieval operation.
Combines a base strategy with any provided keyword arguments to create a customized traversal strategy.
PARAMETER | DESCRIPTION |
---|---|
base_strategy
|
The base strategy to start with.
TYPE:
|
kwargs
|
Additional configuration options for the strategy.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Strategy
|
A configured strategy instance. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If 'strategy' is set incorrectly or extra arguments are invalid. |
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
discover_nodes ¶
Add discovered nodes to the strategy.
This method updates the strategy's state with nodes discovered during the traversal process.
PARAMETER | DESCRIPTION |
---|---|
nodes
|
Discovered nodes keyed by their IDs. |
finalize_nodes ¶
select_nodes ¶
Select discovered nodes to visit in the next iteration.
This method determines which nodes will be traversed next. If it returns
an empty list, traversal ends even if fewer than k
nodes have been selected.
PARAMETER | DESCRIPTION |
---|---|
limit
|
Maximum number of nodes to select.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Iterable[Node]
|
Selected nodes for the next iteration. Traversal ends if this is empty. |
Mmr
dataclass
¶
Mmr(
lambda_mult: float = 0.5,
min_mmr_score: float = NEG_INF,
_selected_ids: list[str] = list(),
_candidate_id_to_index: dict[str, int] = dict(),
_candidates: list[_MmrCandidate] = list(),
_best_score: float = NEG_INF,
_best_id: str | None = None,
*,
k: int = 5,
start_k: int = 4,
adjacent_k: int = 10,
max_depth: int | None = None,
_query_embedding: list[float] = list(),
)
Bases: Strategy
Maximal Marginal Relevance (MMR) traversal strategy.
This strategy selects nodes by balancing relevance to the query and diversity
among the results. It uses a lambda_mult
parameter to control the trade-off
between relevance and redundancy. Nodes are scored based on their similarity
to the query and their distance from already selected nodes.
PARAMETER | DESCRIPTION |
---|---|
k
|
Maximum number of nodes to retrieve during traversal.
TYPE:
|
start_k
|
Number of documents to fetch via similarity for starting the traversal. Added to any initial roots provided to the traversal.
TYPE:
|
adjacent_k
|
Number of documents to fetch for each outgoing edge.
TYPE:
|
max_depth
|
Maximum traversal depth. If
TYPE:
|
lambda_mult
|
Controls the trade-off between relevance and diversity. A value closer to 1 prioritizes relevance, while a value closer to 0 prioritizes diversity. Must be between 0 and 1 (inclusive).
TYPE:
|
min_mmr_score
|
Only nodes with a score greater than or equal to this value will be selected.
TYPE:
|
build
staticmethod
¶
Build a strategy for a retrieval operation.
Combines a base strategy with any provided keyword arguments to create a customized traversal strategy.
PARAMETER | DESCRIPTION |
---|---|
base_strategy
|
The base strategy to start with.
TYPE:
|
kwargs
|
Additional configuration options for the strategy.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Strategy
|
A configured strategy instance. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If 'strategy' is set incorrectly or extra arguments are invalid. |
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
candidate_ids ¶
discover_nodes ¶
Add candidates to the consideration set.
Source code in packages/graph-retriever/src/graph_retriever/strategies/mmr.py
finalize_nodes ¶
select_nodes ¶
Select and pop the best item being considered.
Updates the consideration set based on it.
RETURNS | DESCRIPTION |
---|---|
A tuple containing the ID of the best item.
|
|
Source code in packages/graph-retriever/src/graph_retriever/strategies/mmr.py
Scored
dataclass
¶
Scored(
scorer: Callable[[Node], float],
_nodes: list[_ScoredNode] = list(),
per_iteration_limit: int | None = None,
*,
k: int = 5,
start_k: int = 4,
adjacent_k: int = 10,
max_depth: int | None = None,
_query_embedding: list[float] = list(),
)
Bases: Strategy
Strategy selecing nodes using a scoring function.
build
staticmethod
¶
Build a strategy for a retrieval operation.
Combines a base strategy with any provided keyword arguments to create a customized traversal strategy.
PARAMETER | DESCRIPTION |
---|---|
base_strategy
|
The base strategy to start with.
TYPE:
|
kwargs
|
Additional configuration options for the strategy.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Strategy
|
A configured strategy instance. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If 'strategy' is set incorrectly or extra arguments are invalid. |
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
discover_nodes ¶
Add discovered nodes to the strategy.
This method updates the strategy's state with nodes discovered during the traversal process.
PARAMETER | DESCRIPTION |
---|---|
nodes
|
Discovered nodes keyed by their IDs. |
finalize_nodes ¶
select_nodes ¶
Select discovered nodes to visit in the next iteration.
This method determines which nodes will be traversed next. If it returns
an empty list, traversal ends even if fewer than k
nodes have been selected.
PARAMETER | DESCRIPTION |
---|---|
limit
|
Maximum number of nodes to select.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Iterable[Node]
|
Selected nodes for the next iteration. Traversal ends if this is empty. |
Source code in packages/graph-retriever/src/graph_retriever/strategies/scored.py
Strategy
dataclass
¶
Strategy(
*,
k: int = 5,
start_k: int = 4,
adjacent_k: int = 10,
max_depth: int | None = None,
_query_embedding: list[float] = list(),
)
Bases: ABC
Interface for configuring node selection and traversal strategies.
This base class defines how nodes are selected, traversed, and finalized during a graph traversal. Implementations can customize behaviors like limiting the depth of traversal, scoring nodes, or selecting the next set of nodes for exploration.
PARAMETER | DESCRIPTION |
---|---|
k
|
Maximum number of nodes to retrieve during traversal.
TYPE:
|
start_k
|
Number of documents to fetch via similarity for starting the traversal. Added to any initial roots provided to the traversal.
TYPE:
|
adjacent_k
|
Number of documents to fetch for each outgoing edge.
TYPE:
|
max_depth
|
Maximum traversal depth. If
TYPE:
|
build
staticmethod
¶
Build a strategy for a retrieval operation.
Combines a base strategy with any provided keyword arguments to create a customized traversal strategy.
PARAMETER | DESCRIPTION |
---|---|
base_strategy
|
The base strategy to start with.
TYPE:
|
kwargs
|
Additional configuration options for the strategy.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Strategy
|
A configured strategy instance. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If 'strategy' is set incorrectly or extra arguments are invalid. |
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
discover_nodes
abstractmethod
¶
Add discovered nodes to the strategy.
This method updates the strategy's state with nodes discovered during the traversal process.
PARAMETER | DESCRIPTION |
---|---|
nodes
|
Discovered nodes keyed by their IDs. |
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
finalize_nodes ¶
select_nodes
abstractmethod
¶
Select discovered nodes to visit in the next iteration.
This method determines which nodes will be traversed next. If it returns
an empty list, traversal ends even if fewer than k
nodes have been selected.
PARAMETER | DESCRIPTION |
---|---|
limit
|
Maximum number of nodes to select.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Iterable[Node]
|
Selected nodes for the next iteration. Traversal ends if this is empty. |