rrag 0.1.0-alpha.2

High-performance Rust framework for Retrieval-Augmented Generation with pluggable components, async-first design, and comprehensive observability
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! # Semantic Vector Search
//!
//! High-performance semantic search using vector embeddings and similarity metrics.
//! Supports multiple similarity algorithms and optimization techniques.

use crate::{Document, Embedding, EmbeddingProvider, RragResult, SearchResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Semantic retriever configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SemanticConfig {
    /// Similarity metric to use
    pub similarity_metric: SimilarityMetric,

    /// Embedding dimension
    pub embedding_dimension: usize,

    /// Whether to normalize embeddings
    pub normalize_embeddings: bool,

    /// Index type for efficient search
    pub index_type: IndexType,

    /// Number of clusters for IVF index
    pub num_clusters: Option<usize>,

    /// Number of probes for IVF search
    pub num_probes: Option<usize>,

    /// Enable GPU acceleration if available
    pub use_gpu: bool,
}

impl Default for SemanticConfig {
    fn default() -> Self {
        Self {
            similarity_metric: SimilarityMetric::Cosine,
            embedding_dimension: 768,
            normalize_embeddings: true,
            index_type: IndexType::Flat,
            num_clusters: None,
            num_probes: None,
            use_gpu: false,
        }
    }
}

/// Similarity metrics for vector comparison
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SimilarityMetric {
    /// Cosine similarity (angle between vectors)
    Cosine,
    /// Euclidean distance (L2 norm)
    Euclidean,
    /// Dot product (inner product)
    DotProduct,
    /// Manhattan distance (L1 norm)
    Manhattan,
}

/// Index types for efficient search
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum IndexType {
    /// Flat index (brute force search)
    Flat,
    /// Inverted File Index (clustering-based)
    IVF,
    /// Hierarchical Navigable Small World
    HNSW,
    /// Locality Sensitive Hashing
    LSH,
}

/// Vector document for semantic search
#[derive(Debug, Clone)]
struct VectorDocument {
    /// Document ID
    id: String,

    /// Original content
    content: String,

    /// Document embedding
    embedding: Embedding,

    /// Normalized embedding (if applicable)
    normalized_embedding: Option<Vec<f32>>,

    /// Metadata
    metadata: HashMap<String, serde_json::Value>,
}

/// Semantic retriever implementation
pub struct SemanticRetriever {
    /// Configuration
    config: SemanticConfig,

    /// Document storage
    documents: Arc<RwLock<HashMap<String, VectorDocument>>>,

    /// Embedding service
    embedding_service: Arc<dyn EmbeddingProvider>,

    /// Index for efficient search (simplified for this example)
    index: Arc<RwLock<VectorIndex>>,
}

/// Simplified vector index
struct VectorIndex {
    /// Document IDs in order
    doc_ids: Vec<String>,

    /// Embeddings matrix (row-major)
    embeddings: Vec<Vec<f32>>,

    /// Index type
    index_type: IndexType,
}

impl SemanticRetriever {
    /// Create a new semantic retriever
    pub fn new(config: SemanticConfig, embedding_service: Arc<dyn EmbeddingProvider>) -> Self {
        Self {
            config,
            documents: Arc::new(RwLock::new(HashMap::new())),
            embedding_service,
            index: Arc::new(RwLock::new(VectorIndex {
                doc_ids: Vec::new(),
                embeddings: Vec::new(),
                index_type: IndexType::Flat,
            })),
        }
    }

    /// Index a document with semantic embedding
    pub async fn index_document(&self, doc: &Document) -> RragResult<()> {
        // Generate embedding for the document
        let embedding = self.embedding_service.embed_text(&doc.content).await?;

        // Normalize if configured
        let normalized = if self.config.normalize_embeddings {
            Some(Self::normalize_vector(&embedding.vector))
        } else {
            None
        };

        let vector_doc = VectorDocument {
            id: doc.id.clone(),
            content: doc.content.to_string(),
            embedding: embedding.clone(),
            normalized_embedding: normalized,
            metadata: doc.metadata.clone(),
        };

        // Store document
        let mut documents = self.documents.write().await;
        documents.insert(doc.id.clone(), vector_doc);

        // Update index
        let mut index = self.index.write().await;
        index.doc_ids.push(doc.id.clone());
        index.embeddings.push(if self.config.normalize_embeddings {
            Self::normalize_vector(&embedding.vector)
        } else {
            embedding.vector
        });

        Ok(())
    }

    /// Search for similar documents
    pub async fn search(
        &self,
        query: &str,
        limit: usize,
        min_score: Option<f32>,
    ) -> RragResult<Vec<SearchResult>> {
        // Generate query embedding
        let query_embedding = self.embedding_service.embed_text(query).await?;

        let query_vector = if self.config.normalize_embeddings {
            Self::normalize_vector(&query_embedding.vector)
        } else {
            query_embedding.vector
        };

        // Perform search
        let index = self.index.read().await;
        let documents = self.documents.read().await;

        let mut scores: Vec<(String, f32)> = Vec::new();

        // Calculate similarities
        for (i, doc_embedding) in index.embeddings.iter().enumerate() {
            let similarity = self.calculate_similarity(&query_vector, doc_embedding);

            if let Some(threshold) = min_score {
                if similarity < threshold {
                    continue;
                }
            }

            scores.push((index.doc_ids[i].clone(), similarity));
        }

        // Sort by similarity
        scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
        scores.truncate(limit);

        // Build results
        let results: Vec<SearchResult> = scores
            .into_iter()
            .enumerate()
            .filter_map(|(rank, (doc_id, score))| {
                documents.get(&doc_id).map(|doc| SearchResult {
                    id: doc_id,
                    content: doc.content.clone(),
                    score,
                    rank,
                    metadata: doc.metadata.clone(),
                    embedding: Some(doc.embedding.clone()),
                })
            })
            .collect();

        Ok(results)
    }

    /// Search with pre-computed embedding
    pub async fn search_by_embedding(
        &self,
        embedding: &Embedding,
        limit: usize,
        min_score: Option<f32>,
    ) -> RragResult<Vec<SearchResult>> {
        let query_vector = if self.config.normalize_embeddings {
            Self::normalize_vector(&embedding.vector)
        } else {
            embedding.vector.clone()
        };

        let index = self.index.read().await;
        let documents = self.documents.read().await;

        let mut scores: Vec<(String, f32)> = Vec::new();

        for (i, doc_embedding) in index.embeddings.iter().enumerate() {
            let similarity = self.calculate_similarity(&query_vector, doc_embedding);

            if let Some(threshold) = min_score {
                if similarity < threshold {
                    continue;
                }
            }

            scores.push((index.doc_ids[i].clone(), similarity));
        }

        scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
        scores.truncate(limit);

        let results: Vec<SearchResult> = scores
            .into_iter()
            .enumerate()
            .filter_map(|(rank, (doc_id, score))| {
                documents.get(&doc_id).map(|doc| SearchResult {
                    id: doc_id,
                    content: doc.content.clone(),
                    score,
                    rank,
                    metadata: doc.metadata.clone(),
                    embedding: Some(doc.embedding.clone()),
                })
            })
            .collect();

        Ok(results)
    }

    /// Calculate similarity between two vectors
    fn calculate_similarity(&self, vec1: &[f32], vec2: &[f32]) -> f32 {
        match self.config.similarity_metric {
            SimilarityMetric::Cosine => Self::cosine_similarity(vec1, vec2),
            SimilarityMetric::Euclidean => {
                let distance = Self::euclidean_distance(vec1, vec2);
                1.0 / (1.0 + distance) // Convert distance to similarity
            }
            SimilarityMetric::DotProduct => Self::dot_product(vec1, vec2),
            SimilarityMetric::Manhattan => {
                let distance = Self::manhattan_distance(vec1, vec2);
                1.0 / (1.0 + distance) // Convert distance to similarity
            }
        }
    }

    /// Cosine similarity between two vectors
    fn cosine_similarity(vec1: &[f32], vec2: &[f32]) -> f32 {
        let dot = Self::dot_product(vec1, vec2);
        let norm1 = vec1.iter().map(|x| x * x).sum::<f32>().sqrt();
        let norm2 = vec2.iter().map(|x| x * x).sum::<f32>().sqrt();

        if norm1 == 0.0 || norm2 == 0.0 {
            0.0
        } else {
            dot / (norm1 * norm2)
        }
    }

    /// Dot product of two vectors
    fn dot_product(vec1: &[f32], vec2: &[f32]) -> f32 {
        vec1.iter().zip(vec2.iter()).map(|(a, b)| a * b).sum()
    }

    /// Euclidean distance between two vectors
    fn euclidean_distance(vec1: &[f32], vec2: &[f32]) -> f32 {
        vec1.iter()
            .zip(vec2.iter())
            .map(|(a, b)| (a - b).powi(2))
            .sum::<f32>()
            .sqrt()
    }

    /// Manhattan distance between two vectors
    fn manhattan_distance(vec1: &[f32], vec2: &[f32]) -> f32 {
        vec1.iter()
            .zip(vec2.iter())
            .map(|(a, b)| (a - b).abs())
            .sum()
    }

    /// Normalize a vector to unit length
    fn normalize_vector(vec: &[f32]) -> Vec<f32> {
        let norm = vec.iter().map(|x| x * x).sum::<f32>().sqrt();

        if norm == 0.0 {
            vec.to_vec()
        } else {
            vec.iter().map(|x| x / norm).collect()
        }
    }

    /// Batch index multiple documents
    pub async fn index_batch(&self, documents: Vec<Document>) -> RragResult<()> {
        // Generate embedding requests
        let requests: Vec<crate::EmbeddingRequest> = documents
            .iter()
            .map(|doc| crate::EmbeddingRequest::new(&doc.id, doc.content.as_ref()))
            .collect();

        let embedding_batch = self.embedding_service.embed_batch(requests).await?;

        let mut docs_map = self.documents.write().await;
        let mut index = self.index.write().await;

        for doc in documents.iter() {
            if let Some(embedding) = embedding_batch.embeddings.get(&doc.id) {
                let normalized = if self.config.normalize_embeddings {
                    Some(Self::normalize_vector(&embedding.vector))
                } else {
                    None
                };

                let vector_doc = VectorDocument {
                    id: doc.id.clone(),
                    content: doc.content.to_string(),
                    embedding: embedding.clone(),
                    normalized_embedding: normalized.clone(),
                    metadata: doc.metadata.clone(),
                };

                docs_map.insert(doc.id.clone(), vector_doc);
                index.doc_ids.push(doc.id.clone());
                index
                    .embeddings
                    .push(normalized.unwrap_or_else(|| embedding.vector.clone()));
            }
        }

        Ok(())
    }

    /// Clear the index
    pub async fn clear(&self) -> RragResult<()> {
        let mut documents = self.documents.write().await;
        let mut index = self.index.write().await;

        documents.clear();
        index.doc_ids.clear();
        index.embeddings.clear();

        Ok(())
    }

    /// Get index statistics
    pub async fn stats(&self) -> HashMap<String, serde_json::Value> {
        let documents = self.documents.read().await;
        let _index = self.index.read().await;

        let mut stats = HashMap::new();
        stats.insert("total_documents".to_string(), documents.len().into());
        stats.insert(
            "embedding_dimension".to_string(),
            self.config.embedding_dimension.into(),
        );
        stats.insert(
            "index_type".to_string(),
            format!("{:?}", self.config.index_type).into(),
        );
        stats.insert(
            "similarity_metric".to_string(),
            format!("{:?}", self.config.similarity_metric).into(),
        );

        let memory_size = documents.len() * self.config.embedding_dimension * 4; // 4 bytes per f32
        stats.insert("index_memory_bytes".to_string(), memory_size.into());

        stats
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::embeddings::MockEmbeddingService;

    #[tokio::test]
    async fn test_semantic_search() {
        let mock_service = Arc::new(MockEmbeddingService::new());
        let retriever = SemanticRetriever::new(SemanticConfig::default(), mock_service);

        let docs = vec![
            Document::with_id(
                "1",
                "Machine learning is a subset of artificial intelligence",
            ),
            Document::with_id("2", "Deep learning uses neural networks"),
            Document::with_id(
                "3",
                "Natural language processing enables computers to understand text",
            ),
        ];

        retriever.index_batch(docs).await.unwrap();

        let results = retriever
            .search("AI and machine learning", 2, Some(0.5))
            .await
            .unwrap();
        assert!(!results.is_empty());
    }
}