ipfrs-semantic 0.2.0

Semantic search with HNSW vector indexing for content-addressed data
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//! Multi-modal embedding support for unified semantic search across text, image, and audio.
//!
//! This module provides infrastructure for:
//! - Unified embedding space across modalities
//! - Cross-modal similarity search
//! - Modality-specific distance metrics
//! - Embedding projection and alignment

use crate::{DistanceMetric, VectorIndex};
use ipfrs_core::{Cid, Error, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Supported modalities for embeddings
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Modality {
    /// Text embeddings (e.g., from BERT, GPT)
    Text,
    /// Image embeddings (e.g., from ResNet, CLIP)
    Image,
    /// Audio embeddings (e.g., from Wav2Vec, CLAP)
    Audio,
    /// Video embeddings (e.g., from VideoMAE)
    Video,
    /// Code embeddings (e.g., from CodeBERT)
    Code,
}

impl Modality {
    /// Get the default embedding dimension for this modality
    pub fn default_dim(&self) -> usize {
        match self {
            Modality::Text => 768,  // BERT-base
            Modality::Image => 512, // ResNet-50
            Modality::Audio => 768, // Wav2Vec 2.0
            Modality::Video => 768, // VideoMAE
            Modality::Code => 768,  // CodeBERT
        }
    }

    /// Get the recommended distance metric for this modality
    pub fn default_metric(&self) -> DistanceMetric {
        match self {
            Modality::Text => DistanceMetric::Cosine,
            Modality::Image => DistanceMetric::L2,
            Modality::Audio => DistanceMetric::Cosine,
            Modality::Video => DistanceMetric::L2,
            Modality::Code => DistanceMetric::Cosine,
        }
    }
}

/// Multi-modal embedding with modality information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiModalEmbedding {
    /// The embedding vector
    pub vector: Vec<f32>,
    /// The modality this embedding belongs to
    pub modality: Modality,
    /// Optional metadata about the embedding source
    pub metadata: HashMap<String, String>,
}

impl MultiModalEmbedding {
    /// Create a new multi-modal embedding
    pub fn new(vector: Vec<f32>, modality: Modality) -> Self {
        Self {
            vector,
            modality,
            metadata: HashMap::new(),
        }
    }

    /// Add metadata to the embedding
    pub fn with_metadata(mut self, key: String, value: String) -> Self {
        self.metadata.insert(key, value);
        self
    }

    /// Get the dimension of the embedding
    pub fn dim(&self) -> usize {
        self.vector.len()
    }
}

/// Configuration for multi-modal index
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiModalConfig {
    /// Target dimension for unified embedding space
    pub unified_dim: usize,
    /// Whether to project embeddings to unified dimension
    pub project_to_unified: bool,
    /// Modality-specific weights for cross-modal search
    pub modality_weights: HashMap<Modality, f32>,
}

impl Default for MultiModalConfig {
    fn default() -> Self {
        let mut weights = HashMap::new();
        weights.insert(Modality::Text, 1.0);
        weights.insert(Modality::Image, 1.0);
        weights.insert(Modality::Audio, 1.0);
        weights.insert(Modality::Video, 1.0);
        weights.insert(Modality::Code, 1.0);

        Self {
            unified_dim: 768,
            project_to_unified: false,
            modality_weights: weights,
        }
    }
}

/// Multi-modal index for unified semantic search
pub struct MultiModalIndex {
    /// Separate indices for each modality
    indices: HashMap<Modality, VectorIndex>,
    /// Configuration
    config: MultiModalConfig,
    /// Projection matrices for each modality (if using unified embedding space)
    projections: HashMap<Modality, Vec<Vec<f32>>>,
}

impl MultiModalIndex {
    /// Create a new multi-modal index
    pub fn new(config: MultiModalConfig) -> Self {
        Self {
            indices: HashMap::new(),
            config,
            projections: HashMap::new(),
        }
    }

    /// Register a modality with the index
    pub fn register_modality(&mut self, modality: Modality, dim: usize) -> Result<()> {
        let metric = modality.default_metric();

        // If projection is enabled, create index with unified dimension
        // Otherwise, use the modality's dimension
        let index_dim = if self.config.project_to_unified {
            self.config.unified_dim
        } else {
            dim
        };

        let index = VectorIndex::new(index_dim, metric, 16, 200)?;
        self.indices.insert(modality, index);

        // Initialize projection matrix if needed
        if self.config.project_to_unified && dim != self.config.unified_dim {
            self.init_projection(modality, dim)?;
        }

        Ok(())
    }

    /// Initialize random projection matrix for dimensionality reduction/expansion
    fn init_projection(&mut self, modality: Modality, from_dim: usize) -> Result<()> {
        let to_dim = self.config.unified_dim;

        // Use random projection (Johnson-Lindenstrauss lemma)
        // Each element ~ N(0, 1/to_dim)
        let mut projection = Vec::with_capacity(from_dim);

        use rand::RngExt;
        let mut rng = rand::rng();
        let scale = (1.0 / to_dim as f32).sqrt();

        for _ in 0..from_dim {
            let mut row = Vec::with_capacity(to_dim);
            for _ in 0..to_dim {
                // Sample from standard normal, then scale
                let val: f32 = rng.random_range(-1.0..1.0);
                row.push(val * scale);
            }
            projection.push(row);
        }

        self.projections.insert(modality, projection);
        Ok(())
    }

    /// Project embedding to unified dimension
    fn project_embedding(&self, embedding: &[f32], modality: Modality) -> Vec<f32> {
        if !self.config.project_to_unified {
            return embedding.to_vec();
        }

        if let Some(projection) = self.projections.get(&modality) {
            let mut result = vec![0.0; self.config.unified_dim];

            for (i, row) in projection.iter().enumerate() {
                if i >= embedding.len() {
                    break;
                }
                for (j, &proj_val) in row.iter().enumerate() {
                    result[j] += embedding[i] * proj_val;
                }
            }

            // Normalize
            let norm: f32 = result.iter().map(|x| x * x).sum::<f32>().sqrt();
            if norm > 0.0 {
                for val in &mut result {
                    *val /= norm;
                }
            }

            result
        } else {
            embedding.to_vec()
        }
    }

    /// Add an embedding to the index
    pub fn add(&mut self, cid: Cid, embedding: MultiModalEmbedding) -> Result<()> {
        // Project embedding first to avoid borrowing issues
        let projected = self.project_embedding(&embedding.vector, embedding.modality);

        let index = self.indices.get_mut(&embedding.modality).ok_or_else(|| {
            Error::InvalidInput(format!("Modality {:?} not registered", embedding.modality))
        })?;

        index.insert(&cid, &projected)?;

        Ok(())
    }

    /// Search within a specific modality
    pub fn search_modality(
        &self,
        query: &MultiModalEmbedding,
        k: usize,
        ef_search: Option<usize>,
    ) -> Result<Vec<(Cid, f32)>> {
        let index = self.indices.get(&query.modality).ok_or_else(|| {
            Error::InvalidInput(format!("Modality {:?} not registered", query.modality))
        })?;

        let projected = self.project_embedding(&query.vector, query.modality);
        let ef_search = ef_search.unwrap_or(50);

        let results = index.search(&projected, k, ef_search)?;
        Ok(results.into_iter().map(|r| (r.cid, r.score)).collect())
    }

    /// Cross-modal search: search across all modalities
    pub fn search_cross_modal(
        &self,
        query: &MultiModalEmbedding,
        k: usize,
        ef_search: Option<usize>,
    ) -> Result<Vec<(Cid, f32, Modality)>> {
        let mut all_results = Vec::new();
        let projected_query = self.project_embedding(&query.vector, query.modality);
        let ef_search = ef_search.unwrap_or(50);

        // Search each modality
        for (modality, index) in &self.indices {
            let weight = self
                .config
                .modality_weights
                .get(modality)
                .copied()
                .unwrap_or(1.0);

            match index.search(&projected_query, k * 2, ef_search) {
                Ok(results) => {
                    for result in results {
                        // Apply modality weight to score
                        let weighted_score = result.score * weight;
                        all_results.push((result.cid, weighted_score, *modality));
                    }
                }
                Err(_) => continue,
            }
        }

        // Sort by score and take top k
        all_results.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        all_results.truncate(k);

        Ok(all_results)
    }

    /// Get statistics for each modality
    pub fn stats(&self) -> HashMap<Modality, ModalityStats> {
        let mut stats = HashMap::new();

        for (modality, index) in &self.indices {
            stats.insert(
                *modality,
                ModalityStats {
                    num_embeddings: index.len(),
                    dimension: index.dimension(),
                    metric: modality.default_metric(),
                },
            );
        }

        stats
    }

    /// Get the number of embeddings in a specific modality
    pub fn len_for_modality(&self, modality: Modality) -> usize {
        self.indices
            .get(&modality)
            .map(|idx| idx.len())
            .unwrap_or(0)
    }

    /// Check if the index is empty
    pub fn is_empty(&self) -> bool {
        self.indices.values().all(|idx| idx.is_empty())
    }

    /// Total number of embeddings across all modalities
    pub fn total_len(&self) -> usize {
        self.indices.values().map(|idx| idx.len()).sum()
    }
}

/// Statistics for a specific modality
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModalityStats {
    /// Number of embeddings in this modality
    pub num_embeddings: usize,
    /// Dimension of embeddings
    pub dimension: usize,
    /// Distance metric used
    pub metric: DistanceMetric,
}

/// Alignment between two modalities for cross-modal search
pub struct ModalityAlignment {
    /// Source modality
    #[allow(dead_code)]
    source: Modality,
    /// Target modality
    #[allow(dead_code)]
    target: Modality,
    /// Learned transformation matrix (source_dim × target_dim)
    transform: Vec<Vec<f32>>,
}

impl ModalityAlignment {
    /// Create a new modality alignment
    pub fn new(source: Modality, target: Modality, source_dim: usize, target_dim: usize) -> Self {
        // Initialize with identity-like transformation
        let mut transform = vec![vec![0.0; target_dim]; source_dim];
        let min_dim = source_dim.min(target_dim);

        for (i, row) in transform.iter_mut().enumerate().take(min_dim) {
            row[i] = 1.0;
        }

        Self {
            source,
            target,
            transform,
        }
    }

    /// Learn alignment from paired examples
    ///
    /// This is a simplified version - in practice, you'd use CCA, GCCA, or neural networks
    pub fn learn_from_pairs(&mut self, pairs: &[(Vec<f32>, Vec<f32>)]) -> Result<()> {
        if pairs.is_empty() {
            return Err(Error::InvalidInput("No pairs provided".into()));
        }

        // Simplified learning: use average mapping
        // In practice, use Canonical Correlation Analysis (CCA) or neural networks
        let source_dim = pairs[0].0.len();
        let target_dim = pairs[0].1.len();

        let mut transform = vec![vec![0.0; target_dim]; source_dim];

        for (source_vec, target_vec) in pairs {
            for (i, &source_val) in source_vec.iter().enumerate().take(source_dim) {
                for (j, &target_val) in target_vec.iter().enumerate().take(target_dim) {
                    transform[i][j] += source_val * target_val;
                }
            }
        }

        // Normalize by number of pairs
        let n = pairs.len() as f32;
        for row in &mut transform {
            for val in row {
                *val /= n;
            }
        }

        self.transform = transform;
        Ok(())
    }

    /// Transform a source embedding to target modality space
    pub fn transform_embedding(&self, source: &[f32]) -> Vec<f32> {
        let target_dim = self.transform[0].len();
        let mut result = vec![0.0; target_dim];

        for (i, row) in self.transform.iter().enumerate() {
            if i >= source.len() {
                break;
            }
            for (j, &val) in row.iter().enumerate() {
                result[j] += source[i] * val;
            }
        }

        // Normalize
        let norm: f32 = result.iter().map(|x| x * x).sum::<f32>().sqrt();
        if norm > 0.0 {
            for val in &mut result {
                *val /= norm;
            }
        }

        result
    }
}

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

    fn generate_test_cid(index: usize) -> Cid {
        use multihash_codetable::{Code, MultihashDigest};
        let data = format!("multimodal_test_{}", index);
        let hash = Code::Sha2_256.digest(data.as_bytes());
        Cid::new_v1(0x55, hash)
    }

    #[test]
    fn test_modality_defaults() {
        assert_eq!(Modality::Text.default_dim(), 768);
        assert_eq!(Modality::Image.default_dim(), 512);
        assert_eq!(Modality::Text.default_metric(), DistanceMetric::Cosine);
    }

    #[test]
    fn test_multimodal_embedding_creation() {
        let vec = vec![0.1, 0.2, 0.3];
        let emb = MultiModalEmbedding::new(vec.clone(), Modality::Text);

        assert_eq!(emb.vector, vec);
        assert_eq!(emb.modality, Modality::Text);
        assert_eq!(emb.dim(), 3);
    }

    #[test]
    fn test_multimodal_index_creation() {
        let config = MultiModalConfig::default();
        let mut index = MultiModalIndex::new(config);

        assert!(index.is_empty());
        assert_eq!(index.total_len(), 0);

        // Register modalities
        index
            .register_modality(Modality::Text, 768)
            .expect("test: register Text modality dim 768 should succeed");
        index
            .register_modality(Modality::Image, 512)
            .expect("test: register Image modality dim 512 should succeed");

        assert_eq!(index.len_for_modality(Modality::Text), 0);
        assert_eq!(index.len_for_modality(Modality::Image), 0);
    }

    #[test]
    fn test_add_and_search_single_modality() {
        let config = MultiModalConfig::default();
        let mut index = MultiModalIndex::new(config);
        index
            .register_modality(Modality::Text, 3)
            .expect("test: register Text modality dim 3 should succeed");

        // Add embeddings
        let cid1 = generate_test_cid(1);
        let emb1 = MultiModalEmbedding::new(vec![1.0, 0.0, 0.0], Modality::Text);
        index
            .add(cid1, emb1)
            .expect("test: add Text embedding cid1 should succeed");

        let cid2 = generate_test_cid(2);
        let emb2 = MultiModalEmbedding::new(vec![0.0, 1.0, 0.0], Modality::Text);
        index
            .add(cid2, emb2)
            .expect("test: add Text embedding cid2 should succeed");

        assert_eq!(index.len_for_modality(Modality::Text), 2);

        // Search
        let query = MultiModalEmbedding::new(vec![0.9, 0.1, 0.0], Modality::Text);
        let results = index
            .search_modality(&query, 1, None)
            .expect("test: single-modality search should succeed");

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].0, cid1);
    }

    #[test]
    fn test_cross_modal_search() {
        let config = MultiModalConfig::default();
        let mut index = MultiModalIndex::new(config);

        index
            .register_modality(Modality::Text, 3)
            .expect("test: register Text modality dim 3 should succeed");
        index
            .register_modality(Modality::Image, 3)
            .expect("test: register Image modality dim 3 should succeed");

        // Add text embedding
        let cid1 = generate_test_cid(3);
        let emb1 = MultiModalEmbedding::new(vec![1.0, 0.0, 0.0], Modality::Text);
        index
            .add(cid1, emb1)
            .expect("test: add Text embedding cid1 should succeed");

        // Add image embedding
        let cid2 = generate_test_cid(4);
        let emb2 = MultiModalEmbedding::new(vec![0.0, 1.0, 0.0], Modality::Image);
        index
            .add(cid2, emb2)
            .expect("test: add Image embedding cid2 should succeed");

        // Cross-modal search from text
        let query = MultiModalEmbedding::new(vec![0.9, 0.1, 0.0], Modality::Text);
        let results = index
            .search_cross_modal(&query, 2, None)
            .expect("test: cross-modal search should succeed");

        assert!(!results.is_empty());
    }

    #[test]
    fn test_modality_alignment() {
        let mut alignment = ModalityAlignment::new(Modality::Text, Modality::Image, 3, 3);

        // Create some paired examples
        let pairs = vec![
            (vec![1.0, 0.0, 0.0], vec![0.9, 0.1, 0.0]),
            (vec![0.0, 1.0, 0.0], vec![0.1, 0.9, 0.0]),
        ];

        alignment
            .learn_from_pairs(&pairs)
            .expect("test: learn_from_pairs with valid aligned pairs should succeed");

        // Transform a source embedding
        let source = vec![1.0, 0.0, 0.0];
        let transformed = alignment.transform_embedding(&source);

        assert_eq!(transformed.len(), 3);
        assert!(transformed[0] > 0.5); // Should be close to target space
    }

    #[test]
    fn test_modality_stats() {
        let config = MultiModalConfig::default();
        let mut index = MultiModalIndex::new(config);

        index
            .register_modality(Modality::Text, 768)
            .expect("test: register Text modality dim 768 should succeed");
        index
            .register_modality(Modality::Image, 512)
            .expect("test: register Image modality dim 512 should succeed");

        let stats = index.stats();

        assert_eq!(stats.len(), 2);
        assert_eq!(
            stats
                .get(&Modality::Text)
                .expect("test: Text modality should be present in stats")
                .dimension,
            768
        );
        assert_eq!(
            stats
                .get(&Modality::Image)
                .expect("test: Image modality should be present in stats")
                .dimension,
            512
        );
    }

    #[test]
    fn test_projection() {
        let config = MultiModalConfig {
            project_to_unified: true,
            unified_dim: 512,
            ..Default::default()
        };

        let mut index = MultiModalIndex::new(config);
        index
            .register_modality(Modality::Text, 768)
            .expect("test: register Text modality dim 768 should succeed");

        // Add an embedding (should be projected from 768 to 512)
        let cid = generate_test_cid(5);
        let emb = MultiModalEmbedding::new(vec![0.5; 768], Modality::Text);
        index
            .add(cid, emb)
            .expect("test: add Text embedding with projection should succeed");

        assert_eq!(index.len_for_modality(Modality::Text), 1);
    }
}