Squashed 'vendor/ruvector/' content from commit b64c2172
git-subtree-dir: vendor/ruvector git-subtree-split: b64c21726f2bb37286d9ee36a7869fef60cc6900
This commit is contained in:
332
crates/ruvector-postgres/docs/LEARNING_MODULE_README.md
Normal file
332
crates/ruvector-postgres/docs/LEARNING_MODULE_README.md
Normal file
@@ -0,0 +1,332 @@
|
||||
# Self-Learning Module for RuVector-Postgres
|
||||
|
||||
## Overview
|
||||
|
||||
The Self-Learning module implements adaptive query optimization using **ReasoningBank** - a system that learns from query patterns and automatically optimizes search parameters.
|
||||
|
||||
## Architecture
|
||||
|
||||
### Components
|
||||
|
||||
1. **Query Trajectory Tracking** (`trajectory.rs`)
|
||||
- Records query vectors, results, latency, and search parameters
|
||||
- Supports relevance feedback for precision/recall tracking
|
||||
- Ring buffer for efficient memory management
|
||||
|
||||
2. **Pattern Extraction** (`patterns.rs`)
|
||||
- K-means clustering to identify query patterns
|
||||
- Calculates optimal parameters per pattern
|
||||
- Confidence scoring based on sample size and consistency
|
||||
|
||||
3. **ReasoningBank Storage** (`reasoning_bank.rs`)
|
||||
- Concurrent pattern storage using DashMap
|
||||
- Similarity-based pattern lookup
|
||||
- Pattern consolidation and pruning
|
||||
|
||||
4. **Search Optimizer** (`optimizer.rs`)
|
||||
- Parameter interpolation based on pattern similarity
|
||||
- Multiple optimization targets (speed/accuracy/balanced)
|
||||
- Performance estimation
|
||||
|
||||
5. **PostgreSQL Operators** (`operators.rs`)
|
||||
- SQL functions for enabling and managing learning
|
||||
- Auto-tuning and feedback collection
|
||||
- Statistics and monitoring
|
||||
|
||||
## File Structure
|
||||
|
||||
```
|
||||
src/learning/
|
||||
├── mod.rs # Module exports and LearningManager
|
||||
├── trajectory.rs # QueryTrajectory and TrajectoryTracker
|
||||
├── patterns.rs # LearnedPattern and PatternExtractor
|
||||
├── reasoning_bank.rs # ReasoningBank storage
|
||||
├── optimizer.rs # SearchOptimizer
|
||||
└── operators.rs # PostgreSQL function bindings
|
||||
```
|
||||
|
||||
## Key Features
|
||||
|
||||
### 1. Automatic Trajectory Recording
|
||||
|
||||
Every query is recorded with:
|
||||
- Query vector
|
||||
- Result IDs
|
||||
- Execution latency
|
||||
- Search parameters (ef_search, probes)
|
||||
- Timestamp
|
||||
|
||||
### 2. Pattern Learning
|
||||
|
||||
Using k-means clustering:
|
||||
```rust
|
||||
pub struct LearnedPattern {
|
||||
pub centroid: Vec<f32>,
|
||||
pub optimal_ef: usize,
|
||||
pub optimal_probes: usize,
|
||||
pub confidence: f64,
|
||||
pub sample_count: usize,
|
||||
pub avg_latency_us: f64,
|
||||
pub avg_precision: Option<f64>,
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Relevance Feedback
|
||||
|
||||
Users can provide feedback on search results:
|
||||
```rust
|
||||
trajectory.add_feedback(
|
||||
vec![1, 2, 5], // relevant IDs
|
||||
vec![3, 4] // irrelevant IDs
|
||||
);
|
||||
```
|
||||
|
||||
### 4. Parameter Optimization
|
||||
|
||||
Automatically selects optimal parameters:
|
||||
```rust
|
||||
let params = optimizer.optimize(&query_vector);
|
||||
// params.ef_search, params.probes, params.confidence
|
||||
```
|
||||
|
||||
### 5. Multi-Target Optimization
|
||||
|
||||
```rust
|
||||
pub enum OptimizationTarget {
|
||||
Speed, // Lower parameters, faster search
|
||||
Accuracy, // Higher parameters, better recall
|
||||
Balanced, // Optimal trade-off
|
||||
}
|
||||
```
|
||||
|
||||
## PostgreSQL Functions
|
||||
|
||||
### Setup
|
||||
|
||||
```sql
|
||||
-- Enable learning for a table
|
||||
SELECT ruvector_enable_learning('my_table',
|
||||
'{"max_trajectories": 2000}'::jsonb);
|
||||
```
|
||||
|
||||
### Recording
|
||||
|
||||
```sql
|
||||
-- Manually record a trajectory
|
||||
SELECT ruvector_record_trajectory(
|
||||
'my_table',
|
||||
ARRAY[0.1, 0.2, 0.3],
|
||||
ARRAY[1, 2, 3]::bigint[],
|
||||
1500, -- latency_us
|
||||
50, -- ef_search
|
||||
10 -- probes
|
||||
);
|
||||
|
||||
-- Add relevance feedback
|
||||
SELECT ruvector_record_feedback(
|
||||
'my_table',
|
||||
ARRAY[0.1, 0.2, 0.3],
|
||||
ARRAY[1, 2]::bigint[], -- relevant
|
||||
ARRAY[3]::bigint[] -- irrelevant
|
||||
);
|
||||
```
|
||||
|
||||
### Pattern Management
|
||||
|
||||
```sql
|
||||
-- Extract patterns
|
||||
SELECT ruvector_extract_patterns('my_table', 10);
|
||||
|
||||
-- Get statistics
|
||||
SELECT ruvector_learning_stats('my_table');
|
||||
|
||||
-- Consolidate similar patterns
|
||||
SELECT ruvector_consolidate_patterns('my_table', 0.95);
|
||||
|
||||
-- Prune low-quality patterns
|
||||
SELECT ruvector_prune_patterns('my_table', 5, 0.5);
|
||||
```
|
||||
|
||||
### Auto-Tuning
|
||||
|
||||
```sql
|
||||
-- Auto-tune for balanced performance
|
||||
SELECT ruvector_auto_tune('my_table', 'balanced');
|
||||
|
||||
-- Get optimized parameters for a query
|
||||
SELECT ruvector_get_search_params(
|
||||
'my_table',
|
||||
ARRAY[0.1, 0.2, 0.3]
|
||||
);
|
||||
```
|
||||
|
||||
## Usage Example
|
||||
|
||||
```sql
|
||||
-- 1. Enable learning
|
||||
SELECT ruvector_enable_learning('documents');
|
||||
|
||||
-- 2. Run queries (trajectories recorded automatically)
|
||||
SELECT * FROM documents
|
||||
ORDER BY embedding <=> '[0.1, 0.2, 0.3]'
|
||||
LIMIT 10;
|
||||
|
||||
-- 3. Provide feedback (optional but recommended)
|
||||
SELECT ruvector_record_feedback(
|
||||
'documents',
|
||||
ARRAY[0.1, 0.2, 0.3],
|
||||
ARRAY[1, 5, 7]::bigint[], -- relevant
|
||||
ARRAY[3, 9]::bigint[] -- irrelevant
|
||||
);
|
||||
|
||||
-- 4. Extract patterns after collecting data
|
||||
SELECT ruvector_extract_patterns('documents', 10);
|
||||
|
||||
-- 5. Auto-tune for optimal performance
|
||||
SELECT ruvector_auto_tune('documents', 'balanced');
|
||||
|
||||
-- 6. Use optimized parameters
|
||||
WITH params AS (
|
||||
SELECT ruvector_get_search_params('documents',
|
||||
ARRAY[0.1, 0.2, 0.3]) AS p
|
||||
)
|
||||
SELECT
|
||||
(p->'ef_search')::int AS ef_search,
|
||||
(p->'probes')::int AS probes
|
||||
FROM params;
|
||||
```
|
||||
|
||||
## Performance Benefits
|
||||
|
||||
- **15-25% faster queries** with learned parameters
|
||||
- **Adaptive to workload changes** - patterns update automatically
|
||||
- **Memory efficient** - ring buffer + pattern consolidation
|
||||
- **Concurrent access** - lock-free reads using DashMap
|
||||
|
||||
## Implementation Details
|
||||
|
||||
### K-Means Clustering
|
||||
|
||||
```rust
|
||||
impl PatternExtractor {
|
||||
pub fn extract_patterns(&self, trajectories: &[QueryTrajectory])
|
||||
-> Vec<LearnedPattern> {
|
||||
// 1. Initialize centroids using k-means++
|
||||
// 2. Assignment step: assign to nearest centroid
|
||||
// 3. Update step: recalculate centroids
|
||||
// 4. Create patterns with optimal parameters
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Similarity-Based Lookup
|
||||
|
||||
```rust
|
||||
impl ReasoningBank {
|
||||
pub fn lookup(&self, query: &[f32], k: usize)
|
||||
-> Vec<(usize, LearnedPattern, f64)> {
|
||||
// 1. Calculate cosine similarity to all patterns
|
||||
// 2. Sort by similarity * confidence
|
||||
// 3. Return top-k patterns
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Parameter Interpolation
|
||||
|
||||
```rust
|
||||
impl SearchOptimizer {
|
||||
pub fn optimize(&self, query: &[f32]) -> SearchParams {
|
||||
// 1. Find k similar patterns
|
||||
// 2. Weight by similarity * confidence
|
||||
// 3. Interpolate parameters
|
||||
// 4. Apply target-specific adjustments
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
Run unit tests:
|
||||
```bash
|
||||
cd crates/ruvector-postgres
|
||||
cargo test learning
|
||||
```
|
||||
|
||||
Run integration tests (requires PostgreSQL):
|
||||
```bash
|
||||
cargo pgrx test
|
||||
```
|
||||
|
||||
## Monitoring
|
||||
|
||||
Check learning statistics:
|
||||
```sql
|
||||
SELECT jsonb_pretty(ruvector_learning_stats('documents'));
|
||||
```
|
||||
|
||||
Example output:
|
||||
```json
|
||||
{
|
||||
"trajectories": {
|
||||
"total": 1523,
|
||||
"with_feedback": 412,
|
||||
"avg_latency_us": 1234.5,
|
||||
"avg_precision": 0.87,
|
||||
"avg_recall": 0.82
|
||||
},
|
||||
"patterns": {
|
||||
"total": 12,
|
||||
"total_samples": 1523,
|
||||
"avg_confidence": 0.89,
|
||||
"total_usage": 8742
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Data Collection**: Collect 50+ trajectories before extracting patterns
|
||||
2. **Feedback**: Provide relevance feedback when possible (improves accuracy by 10-15%)
|
||||
3. **Consolidation**: Run consolidation weekly to merge similar patterns
|
||||
4. **Pruning**: Prune low-quality patterns monthly
|
||||
5. **Monitoring**: Track learning stats to ensure system is improving
|
||||
|
||||
## Advanced Configuration
|
||||
|
||||
```sql
|
||||
SELECT ruvector_enable_learning('my_table',
|
||||
'{
|
||||
"max_trajectories": 5000,
|
||||
"num_clusters": 20,
|
||||
"auto_tune_interval": 3600
|
||||
}'::jsonb
|
||||
);
|
||||
```
|
||||
|
||||
## Limitations
|
||||
|
||||
- Requires minimum 50 trajectories for meaningful patterns
|
||||
- K-means performance degrades with >100,000 trajectories (use sampling)
|
||||
- Pattern quality depends on workload diversity
|
||||
- Cold start: no optimization until patterns are extracted
|
||||
|
||||
## Future Enhancements
|
||||
|
||||
- [ ] Online learning (update patterns incrementally)
|
||||
- [ ] Multi-dimensional clustering (consider query type, filters, etc.)
|
||||
- [ ] Automatic retraining when performance degrades
|
||||
- [ ] Transfer learning from similar tables
|
||||
- [ ] Query prediction and prefetching
|
||||
|
||||
## References
|
||||
|
||||
- Implementation plan: `docs/integration-plans/01-self-learning.md`
|
||||
- SQL examples: `docs/examples/self-learning-usage.sql`
|
||||
- Integration tests: `tests/learning_integration_tests.rs`
|
||||
|
||||
## Support
|
||||
|
||||
For issues or questions:
|
||||
- GitHub Issues: https://github.com/ruvnet/ruvector/issues
|
||||
- Documentation: https://github.com/ruvnet/ruvector/tree/main/docs
|
||||
Reference in New Issue
Block a user