MCPcopy Create free account
hub / github.com/alibaba/zvec / BM25EmbeddingFunction

Class BM25EmbeddingFunction

python/zvec/extension/bm25_embedding_function.py:24–375  ·  view source on GitHub ↗

BM25-based sparse embedding function using DashText SDK. This class provides text-to-sparse-vector embedding capabilities using the DashText library with BM25 algorithm. BM25 (Best Matching 25) is a probabilistic retrieval function used for lexical search and document ranking based

Source from the content-addressed store, hash-verified

22
23
24class BM25EmbeddingFunction(SparseEmbeddingFunction[TEXT]):
25 """BM25-based sparse embedding function using DashText SDK.
26
27 This class provides text-to-sparse-vector embedding capabilities using
28 the DashText library with BM25 algorithm. BM25 (Best Matching 25) is a
29 probabilistic retrieval function used for lexical search and document
30 ranking based on term frequency and inverse document frequency.
31
32 BM25 generates sparse vectors where each dimension corresponds to a term in
33 the vocabulary, and the value represents the BM25 score for that term. It's
34 particularly effective for:
35
36 - Lexical search and keyword matching
37 - Document ranking and information retrieval
38 - Combining with dense embeddings for hybrid search
39 - Traditional IR tasks where exact term matching is important
40
41 This implementation uses DashText's SparseVectorEncoder, which provides
42 efficient BM25 computation for Chinese and English text using either a
43 built-in encoder or custom corpus training.
44
45 Args:
46 corpus (Optional[list[str]], optional): List of documents to train the
47 BM25 encoder. If provided, creates a custom encoder trained on this
48 corpus for better domain-specific accuracy. If ``None``, uses the
49 built-in encoder. Defaults to ``None``.
50 encoding_type (Literal["query", "document"], optional): Encoding mode
51 for text processing. Use ``"query"`` for search queries (default) and
52 ``"document"`` for document indexing. This distinction optimizes the
53 BM25 scoring for asymmetric retrieval tasks. Defaults to ``"query"``.
54 language (Literal["zh", "en"], optional): Language for built-in encoder.
55 Only used when corpus is None. ``"zh"`` for Chinese (trained on Chinese
56 Wikipedia), ``"en"`` for English. Defaults to ``"zh"``.
57 b (float, optional): Document length normalization parameter for BM25.
58 Range [0, 1]. 0 means no normalization, 1 means full normalization.
59 Only used with custom corpus. Defaults to ``0.75``.
60 k1 (float, optional): Term frequency saturation parameter for BM25.
61 Higher values give more weight to term frequency. Only used with
62 custom corpus. Defaults to ``1.2``.
63 **kwargs: Additional parameters for DashText encoder customization.
64
65 Attributes:
66 corpus_size (int): Number of documents in the training corpus (0 if using built-in encoder).
67 encoding_type (str): The encoding type being used ("query" or "document").
68 language (str): The language of the built-in encoder ("zh" or "en").
69
70 Raises:
71 ValueError: If corpus is provided but empty or contains non-string elements.
72 TypeError: If input to ``embed()`` is not a string.
73 RuntimeError: If DashText encoder initialization or training fails.
74
75 Note:
76 - Requires Python 3.10, 3.11, or 3.12
77 - Requires the ``dashtext`` package: ``pip install dashtext``
78 - Two encoder options available:
79
80 1. **Built-in encoder** (no corpus needed): Pre-trained models for
81 Chinese (zh) and English (en), good generalization, works out-of-the-box

Calls

no outgoing calls