/** * Search Reranking Functions * * Implements BM25 text scoring, cosine similarity, Reciprocal Rank Fusion (RRF), * Weighted RRF, and Maximal Marginal Relevance (MMR) for combining multiple * ranking signals with diversity. * * Cosine similarity delegates to the Rust WASM core for performance. * BM25, RRF, and higher-order combinators remain in TypeScript because * they operate on Maps/objects that are expensive to serialize across the * WASM boundary. */ /** * Compute cosine similarity between two vectors * * Delegates to the Rust WASM core for SIMD-style inner-product computation. * * @param a - First vector * @param b - Second vector * @returns Similarity score in range [-1, 1] */ export declare function cosineSimilarity(a: number[], b: number[]): number; /** * BM25 Scorer for text relevance */ export declare class BM25Scorer { private stats; /** * Tokenize text into terms */ private tokenize; /** * Index documents for BM25 scoring * * @param documents - Array of {id, text} documents */ indexDocuments(documents: Array<{ id: string; text: string; }>): void; /** * Compute IDF (Inverse Document Frequency) for a term */ private idf; /** * Compute BM25 score for a document given a query * * @param query - Query text * @param docId - Document ID (must be indexed) * @param docText - Document text (for term frequency calculation) * @returns BM25 score */ score(query: string, docId: string, docText: string): number; /** * Quick BM25 score without indexing (uses document text directly) * * Less accurate but useful for one-off scoring. * * @param query - Query text * @param doc - Document text * @returns Approximate BM25 score */ quickScore(query: string, doc: string): number; /** * Get the number of indexed documents */ getDocCount(): number; /** * Get average document length */ getAvgDocLength(): number; } /** * Reciprocal Rank Fusion (RRF) for combining rankings * * RRF is a simple and effective method for merging multiple ranked lists. * Formula: RRF(d) = sum(1 / (k + rank_i(d))) for all rankings i * * @param rankings - Array of ranked item arrays (each array is a ranking) * @param k - RRF constant (default: 60) * @returns Map of item -> combined RRF score */ export declare function rrfFusion(rankings: Array>, k?: number): Map; /** * Normalize scores to [0, 1] range * * @param scores - Array of scores * @returns Normalized scores */ export declare function normalizeScores(scores: number[]): number[]; /** * Combine multiple score signals with weights * * @param signals - Array of {id, score} arrays * @param weights - Weight for each signal (must sum to 1) * @returns Combined and sorted results */ export declare function combineSignals(signals: Array>, weights: number[]): Array<{ id: string; score: number; }>; /** * Default BM25 scorer instance for convenience */ export declare const bm25Scorer: BM25Scorer; /** * Compute BM25 score using the default scorer * * @param query - Query text * @param doc - Document text * @returns BM25 score */ export declare function bm25Score(query: string, doc: string): number; /** * Weighted Reciprocal Rank Fusion for combining rankings with per-signal weights. * * Like standard RRF, but each ranking list's contribution is multiplied by * its weight, allowing callers to emphasize or de-emphasize specific signals. * * @param rankings - Array of ranked item arrays (each array is a ranking, sorted by score desc) * @param weights - Weight for each ranking list (same length as rankings) * @param k - RRF smoothing constant (default 60) * @returns Map of item id -> weighted RRF score */ export declare function weightedRrfFusion(rankings: Array>, weights: number[], k?: number): Map; /** * Apply Maximal Marginal Relevance to promote diversity in results. * * MMR re-orders a ranked list of candidates so that highly similar candidates * are spread out. The algorithm greedily selects the candidate that maximizes: * * MMR(d) = lambda * relevance(d) - (1 - lambda) * max_sim(d, selected) * * where: * - relevance(d) = position-based score (1.0 for first, linearly decreasing) * - max_sim(d, selected) = max cosine similarity between d and any already * selected candidate (0 if no embeddings available) * * @param candidates - Candidates in relevance order (best first), with optional embeddings * @param lambda - Trade-off between relevance and diversity (default 0.7) * @param topK - Number of results to return (default 8) * @returns - Re-ordered candidates with diversity */ export declare function applyMMR(candidates: T[], lambda?: number, topK?: number): T[]; /** * Result from the WASM rerank pipeline. */ export interface WasmRankedResult { id: string; text: string; score: number; cosine_score: number; bm25_score: number; decay_score: number; } /** * Rerank candidates using the Rust WASM BM25 + Cosine + RRF fusion pipeline. * * This is the high-performance path for managed-service search. It takes * decrypted candidates and returns top-K ranked results in a single WASM call. * * @param query - Search query text * @param queryEmbedding - Query embedding vector * @param candidates - Array of {id, text, embedding, timestamp} candidates * @param topK - Number of results to return * @returns Ranked results from WASM */ export declare function wasmRerank(query: string, queryEmbedding: number[], candidates: Array<{ id: string; text: string; embedding: number[]; timestamp?: string; }>, topK: number): WasmRankedResult[]; //# sourceMappingURL=rerank.d.ts.map