graph_retriever.strategies¶
Strategies determine which nodes are selected during traversal.
Eager
dataclass
¶
Eager(
*,
select_k: int = DEFAULT_SELECT_K,
start_k: int = 4,
adjacent_k: int = 10,
max_traverse: int | None = None,
max_depth: int | None = None,
k: int = DEFAULT_SELECT_K,
_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 |
---|---|
select_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:
|
k
|
Deprecated: Use
TYPE:
|
__post_init__ ¶
Allow passing the deprecated 'k' value instead of 'select_k'.
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
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
finalize_nodes ¶
Finalize the selected nodes.
This method is called before returning the final set of nodes. It allows the strategy to perform any final processing or re-ranking of the selected nodes.
PARAMETER | DESCRIPTION |
---|---|
selected
|
The selected nodes to be finalized |
RETURNS | DESCRIPTION |
---|---|
Iterable[Node]
|
Finalized nodes. |
Notes
- The default implementation returns the first
self.select_k
selected nodes without any additional processing.
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
iteration ¶
iteration(
nodes: Iterable[Node], tracker: NodeTracker
) -> None
Process the newly discovered nodes on each iteration.
This method should call tracker.traverse()
and/or tracker.select()
as appropriate to update the nodes that need to be traversed in this iteration
or selected at the end of the retrieval, respectively.
PARAMETER | DESCRIPTION |
---|---|
nodes
|
The newly discovered nodes found from either: - the initial vector store retrieval - incoming edges from nodes chosen for traversal in the previous iteration |
tracker
|
The tracker object to manage the traversal and selection of nodes.
TYPE:
|
Notes
- This method is called once for each iteration of the traversal.
- In order to stop iterating either choose to not traverse any additional nodes or don't select any additional nodes for output.
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,
*,
select_k: int = DEFAULT_SELECT_K,
start_k: int = 4,
adjacent_k: int = 10,
max_traverse: int | None = None,
max_depth: int | None = None,
k: int = DEFAULT_SELECT_K,
_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 |
---|---|
select_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:
|
k
|
Deprecated: Use
TYPE:
|
__post_init__ ¶
Allow passing the deprecated 'k' value instead of 'select_k'.
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
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 ¶
finalize_nodes ¶
Finalize the selected nodes.
This method is called before returning the final set of nodes. It allows the strategy to perform any final processing or re-ranking of the selected nodes.
PARAMETER | DESCRIPTION |
---|---|
selected
|
The selected nodes to be finalized |
RETURNS | DESCRIPTION |
---|---|
Iterable[Node]
|
Finalized nodes. |
Notes
- The default implementation returns the first
self.select_k
selected nodes without any additional processing.
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
iteration ¶
iteration(
nodes: Iterable[Node], tracker: NodeTracker
) -> None
Add candidates to the consideration set.
Source code in packages/graph-retriever/src/graph_retriever/strategies/mmr.py
NodeTracker ¶
Helper class initiating node selection and traversal.
Call .select(nodes) to add nodes to the result set. Call .traverse(nodes) to add nodes to the next traversal. Call .select_and_traverse(nodes) to add nodes to the result set and the next traversal.
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
select ¶
Select nodes to be included in the result set.
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
select_and_traverse ¶
Select nodes to be included in the result set and the next traversal.
RETURNS | DESCRIPTION |
---|---|
Number of nodes added for traversal.
|
|
Notes
- Nodes are only added for traversal if they have not been visited before.
- Nodes are only added for traversal if they do not exceed the maximum depth.
- If no new nodes are chosen for traversal, or selected for output, then the traversal will stop.
- Traversal will also stop if the number of selected nodes reaches the select_k limit.
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
traverse ¶
Select nodes to be included in the next traversal.
RETURNS | DESCRIPTION |
---|---|
Number of nodes added for traversal.
|
|
Notes
- Nodes are only added if they have not been visited before.
- Nodes are only added if they do not exceed the maximum depth.
- If no new nodes are chosen for traversal, or selected for output, then the traversal will stop.
- Traversal will also stop if the number of selected nodes reaches the select_k limit.
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
Scored
dataclass
¶
Scored(
scorer: Callable[[Node], float],
_nodes: list[_ScoredNode] = list(),
per_iteration_limit: int | None = None,
*,
select_k: int = DEFAULT_SELECT_K,
start_k: int = 4,
adjacent_k: int = 10,
max_traverse: int | None = None,
max_depth: int | None = None,
k: int = DEFAULT_SELECT_K,
_query_embedding: list[float] = list(),
)
Bases: Strategy
Scored traversal strategy.
This strategy uses a scoring function to select nodes using a local maximum approach. In each iteration, it chooses the top scoring nodes available and then traverses the connected nodes.
PARAMETER | DESCRIPTION |
---|---|
scorer
|
A callable function that returns the score of a node. |
select_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:
|
per_iteration_limit
|
Maximum number of nodes to select and traverse during a single iteration.
TYPE:
|
k
|
Deprecated: Use
TYPE:
|
__post_init__ ¶
Allow passing the deprecated 'k' value instead of 'select_k'.
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
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
finalize_nodes ¶
Finalize the selected nodes.
This method is called before returning the final set of nodes. It allows the strategy to perform any final processing or re-ranking of the selected nodes.
PARAMETER | DESCRIPTION |
---|---|
selected
|
The selected nodes to be finalized |
RETURNS | DESCRIPTION |
---|---|
Iterable[Node]
|
Finalized nodes. |
Notes
- The default implementation returns the first
self.select_k
selected nodes without any additional processing.
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
iteration ¶
iteration(
nodes: Iterable[Node], tracker: NodeTracker
) -> None
Process the newly discovered nodes on each iteration.
This method should call tracker.traverse()
and/or tracker.select()
as appropriate to update the nodes that need to be traversed in this iteration
or selected at the end of the retrieval, respectively.
PARAMETER | DESCRIPTION |
---|---|
nodes
|
The newly discovered nodes found from either: - the initial vector store retrieval - incoming edges from nodes chosen for traversal in the previous iteration |
tracker
|
The tracker object to manage the traversal and selection of nodes.
TYPE:
|
Notes
- This method is called once for each iteration of the traversal.
- In order to stop iterating either choose to not traverse any additional nodes or don't select any additional nodes for output.
Source code in packages/graph-retriever/src/graph_retriever/strategies/scored.py
Strategy
dataclass
¶
Strategy(
*,
select_k: int = DEFAULT_SELECT_K,
start_k: int = 4,
adjacent_k: int = 10,
max_traverse: int | None = None,
max_depth: int | None = None,
k: int = DEFAULT_SELECT_K,
_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 |
---|---|
select_k
|
Maximum number of nodes to select and return during traversal.
TYPE:
|
start_k
|
Number of nodes to fetch via similarity for starting the traversal. Added to any initial roots provided to the traversal.
TYPE:
|
adjacent_k
|
Number of nodes to fetch for each outgoing edge.
TYPE:
|
max_traverse
|
Maximum number of nodes to traverse outgoing edges from before returning.
If
TYPE:
|
max_depth
|
Maximum traversal depth. If
TYPE:
|
k
|
Deprecated: Use
TYPE:
|
__post_init__ ¶
Allow passing the deprecated 'k' value instead of 'select_k'.
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
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
finalize_nodes ¶
Finalize the selected nodes.
This method is called before returning the final set of nodes. It allows the strategy to perform any final processing or re-ranking of the selected nodes.
PARAMETER | DESCRIPTION |
---|---|
selected
|
The selected nodes to be finalized |
RETURNS | DESCRIPTION |
---|---|
Iterable[Node]
|
Finalized nodes. |
Notes
- The default implementation returns the first
self.select_k
selected nodes without any additional processing.
Source code in packages/graph-retriever/src/graph_retriever/strategies/base.py
iteration
abstractmethod
¶
iteration(
*, nodes: Iterable[Node], tracker: NodeTracker
) -> None
Process the newly discovered nodes on each iteration.
This method should call tracker.traverse()
and/or tracker.select()
as appropriate to update the nodes that need to be traversed in this iteration
or selected at the end of the retrieval, respectively.
PARAMETER | DESCRIPTION |
---|---|
nodes
|
The newly discovered nodes found from either: - the initial vector store retrieval - incoming edges from nodes chosen for traversal in the previous iteration |
tracker
|
The tracker object to manage the traversal and selection of nodes.
TYPE:
|
Notes
- This method is called once for each iteration of the traversal.
- In order to stop iterating either choose to not traverse any additional nodes or don't select any additional nodes for output.