memory.ranking¶
memory.ranking ¶
Rank-fusion helpers shared across search surfaces.
Pure ranking math with no storage or embedding dependencies, so both the memory and zettelkasten servers can import it.
rrf_fuse ¶
rrf_fuse(rankings: list[list[str]], k: int | None = None, weights: list[float] | None = None) -> dict[str, float]
Fuse several ranked id-lists into one score map via Reciprocal Rank Fusion.
Each entry in rankings is a list of ids already ordered best-first (rank
1 is the top hit). An id's fused score is the sum over the lists it appears
in of weight * 1 / (k + rank), with rank starting at 1. Ids appearing
in multiple lists naturally rise, without the input score scales needing to be
comparable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rankings
|
list[list[str]]
|
One id-list per retrieval channel, each best-first. |
required |
k
|
int | None
|
Damping constant (default 60, or |
None
|
weights
|
list[float] | None
|
Optional per-channel multipliers, one per entry in |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Mapping of id -> fused score. Sort descending to get the fused ranking. |
Source code in memory/ranking.py
rrf_order ¶
rrf_order(rankings: list[list[str]], k: int | None = None, weights: list[float] | None = None) -> list[str]
Return ids fused by :func:rrf_fuse, ordered best-first.
Ties (equal fused score) are broken by best rank achieved in any single
channel, then by id, so the order is deterministic across runs. weights
is threaded through to :func:rrf_fuse (see there); a switched-off (weight
0.0) channel contributes nothing to the tie-break either, staying
consistent with the fused scores.