bm25
A Rust crate that computes BM25 embeddings for information retrieval. You can use these embeddings in vector databases that support the storage of sparse vectors, e.g. Qdrant, Pinecone, Milvus, etc.
This crate also contains a light-weight, in-memory full-text search engine built on top of the embedder.
The BM25 algorithm
BM25 is an algorithm for scoring the relevance of a query to documents in a corpus. You can make this scoring more efficient by pre-computing a 'sparse embedding' of each document. You can use these sparse embeddings directly, or upload them to a vector database and query them from there.
BM25 assumes that you know the average (meaningful) word count of your documents ahead of time. This crate provides utilities to compute this. If this assumption doesn't hold for your use-case, you have two options: (1) make a sensible guess (e.g. based on a sample); or (2) configure the algorithm to disregard document length. The former is recommended if most of your documents are around the same size.
BM25 has three hyperparameters: b, k1 and avgdl. These terms match the formula given on
Wikipedia. avgdl ('average document length') is the aforementioned average meaningful word count;
you should always provide a value for this and the crate can fit this for you. b controls
document length normalization; 0 means no normalisation (length will not affect score) while 1
means full normalisation. If you know avgdl, 0.75 is typically a good choice for b. If
you're guessing avgdl, you can use a slightly lower b to reduce the effect of document length
on score. If you have no idea what avgdl is, set b to 0. k1 controls how much weight is
given to recurring tokens. For almost all use-cases, a value of 1.2 is suitable.
Getting started
Add bm25 to your project with
Embed some text
The best way to embed some text is to fit an embedder to your corpus.
use ;
let corpus = ;
let embedder: Embedder = with_fit_to_corpus
.build;
let embedding = embedder.embed;
assert_eq!
For cases where you don't have the full corpus ahead of time, but have an approximate idea of the
average meaningful word count you expect, you can construct an embedder with your avgdl guess.
By default, this crate will detect the language of your input text to embed it accordingly. You
can configure the embedder to embed with a specific language if you know it in advance.
use ;
let embedder: Embedder = with_avgdl
.language_mode // `Detect` is the default language mode
.build;
If you want to disregard document length altogether, set b to 0.
use ;
let embedder: Embedder = with_avgdl // if b = 0, avgdl has no effect
.b
.build;
You can customise the dimensionality of your sparse vector via the generic parameter.
Supported values are usize, u32 and u64. You can also use your own type (and also
inject your own embedding function) by implementing the EmbeddingDimension trait.
use ;
let text = "cup of tea";
// Embed into a u32-dimensional space
let embedder = with_avgdl.build;
let embedding = embedder.embed;
assert_eq!;
// Embed into a u64-dimensional space
let embedder = with_avgdl.build;
let embedding = embedder.embed;
assert_eq!;
// Embed into a usize-dimensional space
let embedder = with_avgdl.build;
let embedding = embedder.embed;
assert_eq!;
;
// Embed into a MyType-dimensional space
let embedder = with_avgdl.build;
let embedding = embedder.embed;
assert_eq!;
Search
This crate includes a light-weight, in-memory full-text search engine built on top of the embedder.
use ;
let corpus = ;
let search_engine = with_corpus
.build;
let limit = 3;
let search_results = search_engine.search;
assert_eq!;
You can also construct a search engine with documents (allowing you to customise the id type and value), or with an average document length.
use ;
// Build a search engine from documents
let search_engine = with_documents
.build;
// Build a search engine from avgdl
let search_engine = with_avgdl.build;
You can also upsert or remove documents from the search engine. Note that mutating the search corpus
by upserting or removing documents will change the true value of avgdl. The more avgdl drifts
from its true value, the less accurate the BM25 scores will be.
use ;
let mut search_engine = with_avgdl.build;
let document_id = 42;
let document = Document ;
search_engine.upsert;
assert_eq!;
search_engine.remove;
assert_eq!;
Working with a large corpus
If your corpus is large, fitting an embedder and generating embeddings can be slow. Fortunately,
these tasks can both be trivially parallelised via the parallelism feature, which implements
data parallelism using Rayon.