Skip to content

Commit

Permalink
feat: BM25 Ranking Function implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tazarov committed Feb 9, 2024
1 parent 1391065 commit 8efadb6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
24 changes: 24 additions & 0 deletions chromadb/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,30 @@ class QueryResult(TypedDict):
distances: Optional[List[List[float]]]


Rank = Union[int, float]


class RankerScore(TypedDict):
ranker_id: str
rank: Rank


class RankerQueryResult(QueryResult):
ranks: Optional[List[List[RankerScore]]]


Rankable = Union[str, int, QueryResult]
R = TypeVar("R", bound=Rankable, contravariant=True)


class RankingFunction(Protocol[R]):
def get_id(self) -> str:
...

def __call__(self, results: R) -> RankerQueryResult:
...


class IndexMetadata(TypedDict):
dimensionality: int
# The current number of elements in the index (total = additions - deletes)
Expand Down
11 changes: 11 additions & 0 deletions chromadb/utils/ranking_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from chromadb.api.types import RankingFunction


class BM25ServerSideRankingFunction(RankingFunction):
def __init__(self, k1=1.2, b=0.75):
self.k1 = k1
self.b = b

def rank(self, query, documents):
# ... (implementation of the BM25 ranking function)
return ranked_documents

0 comments on commit 8efadb6

Please sign in to comment.