lethe-core-rust
A high-performance hybrid retrieval engine that combines BM25 lexical search with vector similarity using z-score fusion. Lethe Core provides state-of-the-art context selection for conversational AI and retrieval-augmented generation (RAG) systems.
Features
- Hybrid Retrieval: Combines BM25 lexical search with vector similarity for optimal relevance
- Z-Score Fusion: Normalizes and fuses scores using statistical z-score transformation (α=0.5, β=0.5)
- Hero Configuration: Pre-tuned parameters achieving parity with splade baseline performance
- Gamma Boosting: Context-aware score boosting for code, errors, and technical content
- Chunking Pipeline: Intelligent text segmentation with sentence-level granularity
- Async-First: Built on Tokio for high-performance concurrent operations
- Extensible: Modular architecture supporting custom embedding services and repositories
Quick Start
Add this to your Cargo.toml:
[]
= "0.1.0"
Basic Usage
use ;
async
Advanced Usage with Full Pipeline
use ;
use Arc;
async
Z-Score Fusion Algorithm
Lethe Core implements a sophisticated z-score fusion algorithm that normalizes and combines scores from different retrieval methods:
- Score Normalization: BM25 scores are normalized to [0,1], cosine similarities from [-1,1] to [0,1]
- Z-Score Calculation: Each score set is converted to z-scores (mean=0, std=1)
- Weighted Fusion: Combined using
hybrid_score = α * z_bm25 + β * z_vector - Gamma Boosting: Context-aware multipliers for code, error, and technical content
Hero Configuration
The hero configuration provides optimal parameters validated against splade baseline performance:
- α = 0.5, β = 0.5: Equal weighting of lexical and semantic signals
- k_initial = 200: Large candidate pool for comprehensive coverage
- k_final = 5: Focused results optimized for Recall@5 metrics
- Diversification = "splade": Advanced diversification matching baseline method
- Gamma boosting disabled: Clean z-score fusion without latent multipliers
Performance Characteristics
- Concurrent Retrieval: BM25 and vector search run in parallel using
tokio::try_join! - Memory Efficient: Streaming chunking and lazy evaluation
- Scalable: Handles large document collections with efficient indexing
- Fast Fusion: O(n) z-score calculation and fusion
- Low Latency: Sub-millisecond fusion for typical candidate sets
Feature Flags
[]
= { = "0.1.0", = ["ollama"] }
- default: Basic functionality with fallback embedding service
- fallback: Fallback embedding service for testing (included in default)
- ollama: Integration with Ollama embedding service
Architecture
Lethe Core follows a modular architecture:
lethe-shared: Common types, errors, and utilitieslethe-domain: Core business logic and serviceslethe-infrastructure: External integrations and adapters
Key Types
Candidate: Search result with document ID, score, and metadataHybridRetrievalConfig: Configuration for retrieval parametersHybridRetrievalService: Main service orchestrating hybrid searchChunk: Text segment with tokenization and metadataEmbeddingVector: Vector representation with similarity operations
Examples
The repository includes several examples:
- Basic Usage: Simple z-score fusion example
- Hero Configuration: Using pre-tuned optimal parameters
- Custom Config: Advanced configuration with custom parameters
- Full Pipeline: End-to-end chunking and retrieval
Testing
Run the test suite:
Run with logging enabled:
RUST_LOG=debug
Golden Parity Snapshots
The hero configuration has been validated against golden parity snapshots ensuring consistent performance with the TypeScript implementation. Key validation points:
- Z-score mathematical properties (mean ≈ 0, std ≈ 1)
- Fusion calculation accuracy to 10 decimal places
- Candidate ordering and scoring consistency
- Performance parity with splade baseline
Contributing
Contributions are welcome! Please ensure:
- All tests pass:
cargo test - Code is formatted:
cargo fmt - Linting passes:
cargo clippy - Documentation builds:
cargo doc --no-deps
License
This project is licensed under the MIT License - see the LICENSE file for details.
Related Projects
Citation
If you use Lethe Core in your research, please cite: