graph_retriever.utils¶
Utilities used in graph_retriever and related packages.
math ¶
Math utility functions for vector operations.
cosine_similarity ¶
cosine_similarity(X: Matrix, Y: Matrix) -> ndarray
Compute row-wise cosine similarity between two equal-width matrices.
PARAMETER | DESCRIPTION |
---|---|
X
|
A matrix of shape (m, n), where
TYPE:
|
Y
|
A matrix of shape (p, n), where
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
A matrix of shape (m, p) containing the cosine similarity scores
between each row of |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the number of columns in |
Notes
- If the
simsimd
library is available, it will be used for performance optimization. Otherwise, the function falls back to a NumPy implementation. - Divide-by-zero and invalid values in similarity calculations are replaced with 0.0 in the output.
Source code in packages/graph-retriever/src/graph_retriever/utils/math.py
cosine_similarity_top_k ¶
cosine_similarity_top_k(
X: Matrix,
Y: Matrix,
top_k: int | None,
score_threshold: float | None = None,
) -> tuple[list[tuple[int, int]], list[float]]
Row-wise cosine similarity with optional top-k and score threshold filtering.
PARAMETER | DESCRIPTION |
---|---|
X
|
A matrix of shape (m, n), where
TYPE:
|
Y
|
A matrix of shape (p, n), where
TYPE:
|
top_k
|
Max number of results to return.
TYPE:
|
score_threshold
|
Minimum score to return.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[tuple[int, int]]
|
Two-tuples of indices |
list[float]
|
The corresponding cosine similarities. |
Source code in packages/graph-retriever/src/graph_retriever/utils/math.py
merge ¶
amerge
async
¶
amerge(
*async_iterables: AsyncIterator[T],
queue_size: int = 10,
) -> AsyncIterator[T]
Merge async iterables into a single async iterator.
Elements are yielded in the order they become available.
PARAMETER | DESCRIPTION |
---|---|
async_iterables
|
The async iterators to merge.
TYPE:
|
queue_size
|
Number of elements to buffer in the queue.
TYPE:
|
YIELDS | DESCRIPTION |
---|---|
AsyncIterator[T]
|
The elements of the iterators as they become available. |
Source code in packages/graph-retriever/src/graph_retriever/utils/merge.py
run_in_executor ¶
run_in_executor
async
¶
run_in_executor(
executor: Executor | None,
func: Callable[P, T],
*args: args,
**kwargs: kwargs,
) -> T
Run a function in an executor.
PARAMETER | DESCRIPTION |
---|---|
executor
|
The executor to run in.
TYPE:
|
func
|
The function.
TYPE:
|
*args
|
The positional arguments to the function.
TYPE:
|
kwargs
|
The keyword arguments to the function.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
T
|
The output of the function. |
RAISES | DESCRIPTION |
---|---|
RuntimeError
|
If the function raises a StopIteration. |
Source code in packages/graph-retriever/src/graph_retriever/utils/run_in_executor.py
top_k ¶
top_k ¶
Select the top-k contents from the given contet.
PARAMETER | DESCRIPTION |
---|---|
contents
|
The content from which to select the top-K. |
embedding
|
The embedding we're looking for. |
k
|
The number of items to select.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[Content]
|
Top-K by similarity. All results will have their |