Class: BM25Index
Defined in: packages/agentos/src/cognition/rag/search/BM25Index.ts:152
BM25 sparse keyword index for hybrid retrieval.
Dense embeddings excel at semantic similarity but miss exact keyword matches (e.g., error codes, function names, product IDs). BM25 catches these by scoring documents based on term frequency, inverse document frequency, and document length normalization.
Examples
const index = new BM25Index({ k1: 1.5, b: 0.75 });
index.addDocuments([
{ id: 'doc-1', text: 'TypeScript compiler error TS2304' },
{ id: 'doc-2', text: 'JavaScript runtime TypeError explanation' },
{ id: 'doc-3', text: 'Fix error TS2304 by adding type declarations' },
]);
const results = index.search('error TS2304', 5);
// results[0].id === 'doc-3' (exact match on "error" + "TS2304")
// results[1].id === 'doc-1' (exact match on "error" + "TS2304")
const hybrid = new HybridSearcher(vectorStore, embeddingManager, bm25Index, {
denseWeight: 0.7,
sparseWeight: 0.3,
});
const results = await hybrid.search('What does error TS2304 mean?');
Constructors
Constructor
new BM25Index(
config?):BM25Index
Defined in: packages/agentos/src/cognition/rag/search/BM25Index.ts:203
Creates a new BM25 index.
Parameters
config?
Optional BM25 tuning parameters.