oxirs-embed 0.2.4

Knowledge graph embeddings with TransE, ComplEx, and custom models
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
617
618
//! Advanced embedding server example demonstrating caching, API endpoints, and model management
//!
//! This example shows how to set up a production-ready embedding service with:
//! - Multi-level caching for performance optimization
//! - RESTful API endpoints for various operations
//! - Model registry and version management
//! - Performance monitoring and health checks

use anyhow::Result;
use oxirs_embed::{
    caching::{CacheConfig, CacheManager},
    model_registry::{ModelRegistry, ResourceAllocation},
    models::{GNNConfig, GNNEmbedding, GNNType, TransE},
    EmbeddingModel, ModelConfig, NamedNode, Triple,
};
use std::collections::HashMap;
use std::sync::Arc;
use tempfile::tempdir;
use tokio::sync::RwLock;
use tracing::info;
use uuid::Uuid;

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize logging (simple console output)
    println!("📄 Logging initialized");

    info!("🚀 Starting Advanced OxiRS Embedding Server Example");

    // Create a comprehensive embedding service demonstration
    let demo = EmbeddingServerDemo::new().await?;
    demo.run().await?;

    Ok(())
}

/// Comprehensive embedding server demonstration
struct EmbeddingServerDemo {
    /// Model registry for managing models
    registry: Arc<ModelRegistry>,
    /// Cache manager for performance optimization
    cache_manager: Arc<CacheManager>,
    /// Currently loaded models
    models: Arc<RwLock<HashMap<Uuid, Arc<dyn EmbeddingModel + Send + Sync>>>>,
}

impl EmbeddingServerDemo {
    /// Create a new demo instance
    async fn new() -> Result<Self> {
        info!("📋 Initializing embedding server components...");

        // Create temporary directory for model storage
        let temp_dir = tempdir()?;
        let registry = Arc::new(ModelRegistry::new(temp_dir.path().to_path_buf()));

        // Configure caching with optimized settings
        let cache_config = CacheConfig {
            l1_max_size: 5_000,  // Hot embeddings
            l2_max_size: 25_000, // Computation results
            l3_max_size: 50_000, // Similarity cache
            ttl_seconds: 1800,   // 30 minutes TTL
            enable_warming: true,
            enable_compression: true,
            max_memory_mb: 512, // 512MB cache limit
            ..Default::default()
        };

        let mut cache_manager = CacheManager::new(cache_config);
        cache_manager.start().await?;
        let cache_manager = Arc::new(cache_manager);

        let models = Arc::new(RwLock::new(HashMap::new()));

        Ok(Self {
            registry,
            cache_manager,
            models,
        })
    }

    /// Run the complete demonstration
    async fn run(&self) -> Result<()> {
        info!("🔧 Setting up demonstration models and data...");

        // 1. Create and register multiple embedding models
        self.setup_models().await?;

        // 2. Demonstrate caching capabilities
        self.demonstrate_caching().await?;

        // 3. Simulate API operations
        self.simulate_api_operations().await?;

        // 4. Demonstrate API endpoints
        self.demonstrate_api_endpoints().await?;

        // 5. Demonstrate advanced features
        self.demonstrate_advanced_features().await?;

        info!("✅ Embedding server demonstration completed successfully!");
        Ok(())
    }

    /// Set up and register multiple embedding models
    async fn setup_models(&self) -> Result<()> {
        info!("🤖 Creating and training embedding models...");

        // Create sample knowledge graph data
        let sample_triples = vec![
            (
                "http://example.org/Alice",
                "http://example.org/knows",
                "http://example.org/Bob",
            ),
            (
                "http://example.org/Bob",
                "http://example.org/knows",
                "http://example.org/Charlie",
            ),
            (
                "http://example.org/Charlie",
                "http://example.org/worksAt",
                "http://example.org/Company",
            ),
            (
                "http://example.org/Alice",
                "http://example.org/livesIn",
                "http://example.org/City",
            ),
            (
                "http://example.org/Bob",
                "http://example.org/likes",
                "http://example.org/Pizza",
            ),
            (
                "http://example.org/Charlie",
                "http://example.org/plays",
                "http://example.org/Guitar",
            ),
            (
                "http://example.org/Alice",
                "http://example.org/speaks",
                "http://example.org/English",
            ),
            (
                "http://example.org/Bob",
                "http://example.org/drives",
                "http://example.org/Car",
            ),
        ];

        // 1. Create and train TransE model
        let transe_model = self.create_transe_model(&sample_triples).await?;
        let transe_id = self
            .register_model("TransE Social Network Model", "TransE", transe_model)
            .await?;

        // 2. Create and train GNN model
        let gnn_model = self.create_gnn_model(&sample_triples).await?;
        let _gnn_id = self
            .register_model("GNN Social Network Model", "GNNEmbedding", gnn_model)
            .await?;

        // 3. Promote TransE model to production
        self.registry.promote_to_production(transe_id).await?;

        info!("✅ Successfully created and registered {} models", 2);
        Ok(())
    }

    /// Create and train a TransE model
    async fn create_transe_model(
        &self,
        triples: &[(&str, &str, &str)],
    ) -> Result<Box<dyn EmbeddingModel + Send + Sync>> {
        let config = ModelConfig::default()
            .with_dimensions(64)
            .with_learning_rate(0.01)
            .with_max_epochs(50)
            .with_batch_size(8)
            .with_seed(42);

        let mut model = TransE::new(config);

        // Add triples to model
        for &(s, p, o) in triples {
            let triple = Triple::new(NamedNode::new(s)?, NamedNode::new(p)?, NamedNode::new(o)?);
            model.add_triple(triple)?;
        }

        // Train the model
        info!("🏋️ Training TransE model...");
        let stats = model.train(Some(10)).await?;
        info!(
            "TransE training completed: {} epochs, final loss: {:.6}",
            stats.epochs_completed, stats.final_loss
        );

        Ok(Box::new(model))
    }

    /// Create and train a GNN model
    async fn create_gnn_model(
        &self,
        triples: &[(&str, &str, &str)],
    ) -> Result<Box<dyn EmbeddingModel + Send + Sync>> {
        let config = GNNConfig {
            base_config: ModelConfig::default()
                .with_dimensions(64)
                .with_learning_rate(0.01)
                .with_max_epochs(50),
            gnn_type: GNNType::GCN,
            num_layers: 2,
            hidden_dimensions: vec![32, 64],
            dropout: 0.1,
            ..Default::default()
        };

        let mut model = GNNEmbedding::new(config);

        // Add triples to model
        for &(s, p, o) in triples {
            let triple = Triple::new(NamedNode::new(s)?, NamedNode::new(p)?, NamedNode::new(o)?);
            model.add_triple(triple)?;
        }

        // Train the model
        info!("🏋️ Training GNN model...");
        let stats = model.train(Some(10)).await?;
        info!(
            "GNN training completed: {} epochs, final loss: {:.6}",
            stats.epochs_completed, stats.final_loss
        );

        Ok(Box::new(model))
    }

    /// Register a model in the registry
    async fn register_model(
        &self,
        name: &str,
        model_type: &str,
        model: Box<dyn EmbeddingModel + Send + Sync>,
    ) -> Result<Uuid> {
        // Register model in registry
        let model_id = self
            .registry
            .register_model(
                name.to_string(),
                model_type.to_string(),
                "demo-user".to_string(),
                format!("Demo {model_type} model for embedding server"),
            )
            .await?;

        // Register version
        let version_id = self
            .registry
            .register_version(
                model_id,
                "1.0.0".to_string(),
                "demo-user".to_string(),
                "Initial trained version".to_string(),
                model.config().clone(),
                HashMap::from([
                    ("accuracy".to_string(), 0.95),
                    ("training_time".to_string(), 120.0),
                ]),
            )
            .await?;

        // Deploy the version
        let _deployment_id = self
            .registry
            .deploy_version(
                version_id,
                ResourceAllocation {
                    cpu_cores: 2.0,
                    memory_gb: 4.0,
                    gpu_count: 0,
                    gpu_memory_gb: 0.0,
                    max_concurrent_requests: 100,
                },
            )
            .await?;

        // Store model in our local registry
        {
            let mut models = self.models.write().await;
            models.insert(version_id, Arc::from(model));
        }

        info!(
            "📝 Registered model '{}' with version ID: {}",
            name, version_id
        );
        Ok(version_id)
    }

    /// Demonstrate caching capabilities
    async fn demonstrate_caching(&self) -> Result<()> {
        info!("🗄️ Demonstrating caching capabilities...");

        // Get production model for caching demo
        let models = self.models.read().await;
        let model = models.values().next().expect("No models available").clone();
        drop(models);

        // Warm up cache with frequently accessed entities
        let frequent_entities = vec![
            "http://example.org/Alice".to_string(),
            "http://example.org/Bob".to_string(),
            "http://example.org/Charlie".to_string(),
        ];

        let warmed_count = self
            .cache_manager
            .warm_cache(model.as_ref(), frequent_entities)
            .await?;
        info!("🔥 Cache warmed with {} entities", warmed_count);

        // Precompute common operations
        let common_queries = vec![
            (
                "http://example.org/Alice".to_string(),
                "http://example.org/knows".to_string(),
            ),
            (
                "http://example.org/Bob".to_string(),
                "http://example.org/knows".to_string(),
            ),
        ];

        let precomputed_count = self
            .cache_manager
            .precompute_common_operations(model.as_ref(), common_queries)
            .await?;
        info!("⚡ Precomputed {} common operations", precomputed_count);

        // Demonstrate cache performance
        let start = std::time::Instant::now();

        // First access (cache miss)
        let _embedding1 = self.cache_manager.get_embedding("http://example.org/Alice");
        let miss_time = start.elapsed();

        // Second access (cache hit)
        let start = std::time::Instant::now();
        let _embedding2 = self.cache_manager.get_embedding("http://example.org/Alice");
        let hit_time = start.elapsed();

        info!(
            "📊 Cache performance: miss={:?}, hit={:?}",
            miss_time, hit_time
        );

        // Display cache statistics
        let stats = self.cache_manager.get_stats();
        info!(
            "📈 Cache stats: hits={}, misses={}, hit_rate={:.2}%",
            stats.total_hits,
            stats.total_misses,
            stats.hit_rate * 100.0
        );

        Ok(())
    }

    /// Demonstrate API endpoints (simulated)
    async fn demonstrate_api_endpoints(&self) -> Result<()> {
        info!("🌐 Demonstrating API functionality...");

        info!("📚 Would provide these endpoints:");
        info!("  POST /api/v1/embed              - Generate single embedding");
        info!("  POST /api/v1/embed/batch        - Generate batch embeddings");
        info!("  POST /api/v1/score              - Score a triple");
        info!("  POST /api/v1/predict            - Make predictions");
        info!("  GET  /api/v1/models             - List available models");
        info!("  GET  /api/v1/health             - System health");
        info!("  GET  /api/v1/cache/stats        - Cache statistics");

        Ok(())
    }

    /// Simulate API operations for demonstration
    async fn simulate_api_operations(&self) -> Result<()> {
        info!("🎭 Simulating API operations...");

        let models = self.models.read().await;
        let model = models.values().next().expect("No models available").clone();
        drop(models);

        // Simulate embedding generation
        let entities = vec!["http://example.org/Alice", "http://example.org/Bob"];
        for entity in entities {
            match model.get_entity_embedding(entity) {
                Ok(embedding) => {
                    info!(
                        "🔢 Generated embedding for {}: {} dimensions",
                        entity, embedding.dimensions
                    );
                }
                Err(e) => {
                    info!("❌ Failed to generate embedding for {}: {}", entity, e);
                }
            }
        }

        // Simulate triple scoring
        let score = model.score_triple(
            "http://example.org/Alice",
            "http://example.org/knows",
            "http://example.org/Bob",
        )?;
        info!("🎯 Triple score for (Alice, knows, Bob): {:.4}", score);

        // Simulate predictions
        let predictions =
            model.predict_objects("http://example.org/Alice", "http://example.org/knows", 3)?;
        info!(
            "🔮 Top 3 predictions for (Alice, knows, ?): {:?}",
            predictions
        );

        Ok(())
    }

    /// Demonstrate advanced features
    async fn demonstrate_advanced_features(&self) -> Result<()> {
        info!("🚀 Demonstrating advanced features...");

        // 1. Model comparison
        self.compare_models().await?;

        // 2. Cache optimization
        self.optimize_cache_performance().await?;

        // 3. Monitoring and health checks
        self.demonstrate_monitoring().await?;

        Ok(())
    }

    /// Compare different model performance
    async fn compare_models(&self) -> Result<()> {
        info!("📊 Comparing model performance...");

        let models = self.models.read().await;

        if models.len() >= 2 {
            let model_performances = models
                .iter()
                .map(|(id, model)| {
                    let stats = model.get_stats();
                    (
                        id,
                        stats.model_type,
                        stats.num_entities,
                        stats.num_relations,
                        stats.dimensions,
                    )
                })
                .collect::<Vec<_>>();

            for (id, model_type, entities, relations, dims) in model_performances {
                info!(
                    "🤖 Model {}: type={}, entities={}, relations={}, dims={}",
                    id, model_type, entities, relations, dims
                );
            }
        }

        Ok(())
    }

    /// Optimize cache performance
    async fn optimize_cache_performance(&self) -> Result<()> {
        info!("⚡ Optimizing cache performance...");

        // Get current memory usage
        let memory_usage = self.cache_manager.estimate_memory_usage();
        info!("💾 Current cache memory usage: {} bytes", memory_usage);

        // Display detailed cache statistics
        let stats = self.cache_manager.get_stats();
        info!("📈 Detailed cache statistics:");
        info!(
            "  L1 Cache: {}/{} entries, {} hits, {} misses",
            stats.l1_stats.size,
            stats.l1_stats.capacity,
            stats.l1_stats.hits,
            stats.l1_stats.misses
        );
        info!(
            "  L2 Cache: {}/{} entries, {} hits, {} misses",
            stats.l2_stats.size,
            stats.l2_stats.capacity,
            stats.l2_stats.hits,
            stats.l2_stats.misses
        );
        info!(
            "  L3 Cache: {}/{} entries, {} hits, {} misses",
            stats.l3_stats.size,
            stats.l3_stats.capacity,
            stats.l3_stats.hits,
            stats.l3_stats.misses
        );
        info!(
            "  Total time saved: {:.2} seconds",
            stats.total_time_saved_seconds
        );

        Ok(())
    }

    /// Demonstrate monitoring and health checks
    async fn demonstrate_monitoring(&self) -> Result<()> {
        info!("🏥 Demonstrating monitoring and health checks...");

        // Model health simulation
        let models = self.models.read().await;
        for (id, model) in models.iter() {
            let stats = model.get_stats();
            let health_status = if stats.is_trained {
                "healthy"
            } else {
                "unhealthy"
            };
            info!("💊 Model {} health: {}", id, health_status);
        }

        // Cache health
        let cache_stats = self.cache_manager.get_stats();
        let cache_health = if cache_stats.hit_rate > 0.5 {
            "optimal"
        } else {
            "needs warming"
        };
        info!(
            "🗄️ Cache health: {} (hit rate: {:.1}%)",
            cache_health,
            cache_stats.hit_rate * 100.0
        );

        // System metrics simulation
        info!("📊 System metrics:");
        info!("  🔧 Loaded models: {}", models.len());
        info!("  💾 Memory usage: {} MB", memory_usage() / 1024 / 1024);
        info!("  🏃 Uptime: simulated");
        info!("  📡 Request rate: simulated");

        Ok(())
    }
}

// Simulate memory usage for demonstration
fn memory_usage() -> usize {
    256 * 1024 * 1024 // 256 MB simulated
}

/// Helper function to create sample configuration
#[allow(dead_code)]
fn create_production_config() -> ModelConfig {
    ModelConfig::default()
        .with_dimensions(128)
        .with_learning_rate(0.005)
        .with_max_epochs(1000)
        .with_batch_size(512)
}

/// Utility function for performance benchmarking
#[allow(dead_code)]
async fn benchmark_operation<F, T>(name: &str, operation: F) -> Result<T>
where
    F: std::future::Future<Output = Result<T>>,
{
    let start = std::time::Instant::now();
    let result = operation.await?;
    let duration = start.elapsed();
    info!("⏱️ {}: completed in {:?}", name, duration);
    Ok(result)
}

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

    #[tokio::test]
    async fn test_embedding_server_demo_creation() {
        let demo = EmbeddingServerDemo::new().await;
        assert!(demo.is_ok());
    }

    #[tokio::test]
    async fn test_model_creation() {
        let demo = EmbeddingServerDemo::new().await.unwrap();
        let triples = vec![(
            "http://example.org/A",
            "http://example.org/rel",
            "http://example.org/B",
        )];

        let model = demo.create_transe_model(&triples).await;
        assert!(model.is_ok());

        let model = model.unwrap();
        assert!(model.is_trained());
    }

    #[test]
    fn test_production_config() {
        let config = create_production_config();
        assert_eq!(config.dimensions, 128);
        assert_eq!(config.learning_rate, 0.005);
        assert_eq!(config.max_epochs, 1000);
        assert_eq!(config.batch_size, 512);
    }
}