kaccy_ai/
examples.rs

1//! Usage examples and integration patterns
2//!
3//! This module provides practical examples of how to use the kaccy-ai crate
4//! for various common scenarios. These examples can serve as starting points
5//! for your own implementations.
6
7#![allow(clippy::unused_async)]
8#![allow(clippy::missing_errors_doc)]
9#![allow(clippy::missing_panics_doc)]
10
11use std::sync::Arc;
12
13use rust_decimal::Decimal;
14use uuid::Uuid;
15
16use crate::access_control::{AccessControlManager, AccessTier, AiFeature, TokenHolder};
17use crate::ai_evaluator::{AiEvaluator, AiFraudDetector, EvaluatorConfig};
18use crate::batch::{BatchCodeEvaluator, BatchConfig};
19use crate::error::Result;
20use crate::evaluator::QualityEvaluator;
21use crate::llm::{
22    DeepSeekClient, GeminiClient, LlmClient, LlmClientBuilder, ModelTier, OpenAiClient,
23    RoutingConfig,
24};
25use crate::oracle::AiOracle;
26use crate::presets::{AccessTierPresets, ProductionPreset};
27use crate::service::{AiServiceBuilder, AiServiceHub};
28
29/// Example: Basic code evaluation workflow
30///
31/// Demonstrates how to:
32/// - Set up an LLM client
33/// - Create an evaluator
34/// - Evaluate code quality
35pub struct BasicCodeEvaluationExample;
36
37impl BasicCodeEvaluationExample {
38    /// Run the basic code evaluation example
39    ///
40    /// # Example
41    /// ```no_run
42    /// use kaccy_ai::examples::BasicCodeEvaluationExample;
43    ///
44    /// # #[tokio::main]
45    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
46    /// BasicCodeEvaluationExample::run("your-api-key").await?;
47    /// # Ok(())
48    /// # }
49    /// ```
50    pub async fn run(api_key: &str) -> Result<()> {
51        // 1. Create an OpenAI client
52        let openai = OpenAiClient::with_default_model(api_key);
53        let llm_client = LlmClient::new(Box::new(openai));
54
55        // 2. Create an evaluator with default config
56        let evaluator = AiEvaluator::with_config(llm_client, EvaluatorConfig::default());
57
58        // 3. Evaluate some code
59        let code = r"
60            fn factorial(n: u64) -> u64 {
61                if n == 0 { 1 } else { n * factorial(n - 1) }
62            }
63        ";
64
65        let result = evaluator.evaluate_code(code, "rust").await?;
66
67        println!("Quality Score: {}", result.quality_score);
68        println!("Complexity Score: {}", result.complexity_score);
69        println!("Originality Score: {}", result.originality_score);
70        println!("Feedback: {}", result.feedback);
71
72        Ok(())
73    }
74}
75
76/// Example: Batch processing workflow
77///
78/// Demonstrates how to:
79/// - Set up batch processing
80/// - Process multiple code samples efficiently
81/// - Handle errors in batch operations
82pub struct BatchProcessingExample;
83
84impl BatchProcessingExample {
85    /// Run the batch processing example
86    #[allow(dead_code)]
87    pub async fn run(api_key: &str) -> Result<()> {
88        // 1. Create the evaluator
89        let openai = OpenAiClient::with_default_model(api_key);
90        let llm_client = LlmClient::new(Box::new(openai));
91        let evaluator = Arc::new(AiEvaluator::with_config(
92            llm_client,
93            EvaluatorConfig::default(),
94        ));
95
96        // 2. Create batch processor with production settings
97        let batch_config = ProductionPreset::batch_config();
98        let batch_evaluator = BatchCodeEvaluator::new(evaluator, batch_config);
99
100        // 3. Prepare multiple code samples
101        let codes = vec![
102            (
103                "fn add(a: i32, b: i32) -> i32 { a + b }".to_string(),
104                "rust".to_string(),
105            ),
106            (
107                "function add(a, b) { return a + b; }".to_string(),
108                "javascript".to_string(),
109            ),
110            (
111                "def add(a, b): return a + b".to_string(),
112                "python".to_string(),
113            ),
114        ];
115
116        // 4. Process in batch
117        let result = batch_evaluator.evaluate_batch(codes).await?;
118
119        println!("Total processed: {}", result.total);
120        println!("Successes: {}", result.success_count());
121        println!("Failures: {}", result.failure_count());
122        println!("Success rate: {:.2}%", result.success_rate() * 100.0);
123
124        Ok(())
125    }
126}
127
128/// Example: AI Oracle with multi-model consensus
129///
130/// Demonstrates how to:
131/// - Set up an AI Oracle
132/// - Use multi-model consensus
133/// - Learn from human feedback
134pub struct OracleConsensusExample;
135
136impl OracleConsensusExample {
137    /// Run the oracle consensus example
138    #[allow(dead_code)]
139    pub async fn run(_api_key: &str) -> Result<()> {
140        // 1. Create oracle with production config
141        let oracle_config = ProductionPreset::oracle_config();
142        let mut oracle = AiOracle::new(oracle_config);
143
144        // 2. Add models via config (models are managed through the config)
145        // In production, you would configure multiple LLM providers through the oracle config
146
147        // 3. Make a decision with consensus
148        let request = crate::ai_evaluator::VerificationRequest {
149            commitment_title: "Deploy smart contract".to_string(),
150            commitment_description: Some("Deploy audited smart contract to mainnet".to_string()),
151            deadline: "2024-12-31".to_string(),
152            evidence_url: "https://etherscan.io/address/0x123...".to_string(),
153            evidence_description: Some("Contract deployed with verified source code".to_string()),
154        };
155        let decision = oracle.verify_with_consensus(&request).await?;
156
157        println!("Consensus Decision: {}", decision.approved);
158        println!("Confidence: {}", decision.confidence);
159        println!("Reasoning: {}", decision.reasoning);
160
161        // 4. Learn from human feedback (if decision was wrong)
162        let human_decision = true; // Assume human verified this was correct
163        oracle.record_feedback(
164            "commitment-123".to_string(),
165            decision.approved,
166            decision.confidence,
167            human_decision,
168        );
169
170        Ok(())
171    }
172}
173
174/// Example: Complete service setup with access control
175///
176/// Demonstrates how to:
177/// - Set up the full AI service hub
178/// - Configure access control
179/// - Use tiered access
180pub struct CompleteServiceExample;
181
182impl CompleteServiceExample {
183    /// Run the complete service example
184    #[allow(dead_code)]
185    pub async fn run(api_key: &str) -> Result<()> {
186        // 1. Create LLM client
187        let openai = OpenAiClient::with_default_model(api_key);
188        let llm_client = Arc::new(LlmClient::new(Box::new(openai)));
189
190        // 2. Build service with all features
191        let mut service = AiServiceBuilder::new(llm_client)
192            .evaluator_config(ProductionPreset::evaluator_config())
193            .with_oracle(ProductionPreset::oracle_config())
194            .with_access_control()
195            .build();
196
197        // 3. Set up access control with tiers
198        let mut access_control = AccessControlManager::new();
199        access_control.update_tier_config(AccessTierPresets::silver_tier());
200
201        // 4. Create a token holder
202        let holder = TokenHolder {
203            user_id: Uuid::new_v4(),
204            token_id: Uuid::new_v4(),
205            balance: Decimal::new(1000, 0),
206            tier: AccessTier::Silver,
207        };
208
209        // 5. Check and use features
210        if let Some(Ok(true)) = service.can_access_feature(&holder, AiFeature::CodeEvaluation) {
211            let evaluator = service.evaluator();
212            let code = "fn hello() { println!(\"Hello\"); }";
213            let result = evaluator.evaluate_code(code, "rust").await?;
214
215            // Record usage
216            service.record_usage(&holder, AiFeature::CodeEvaluation);
217
218            println!("Evaluation successful: {}", result.quality_score);
219        }
220
221        Ok(())
222    }
223}
224
225/// Example: Fraud detection workflow
226///
227/// Demonstrates how to:
228/// - Set up fraud detection
229/// - Analyze user behavior
230/// - Interpret risk scores
231pub struct FraudDetectionExample;
232
233impl FraudDetectionExample {
234    /// Run the fraud detection example
235    #[allow(dead_code)]
236    pub async fn run(api_key: &str) -> Result<()> {
237        // 1. Create fraud detector
238        let openai = OpenAiClient::with_default_model(api_key);
239        let llm_client = LlmClient::new(Box::new(openai));
240        let fraud_detector = AiFraudDetector::new(llm_client);
241
242        // 2. Prepare fraud check request
243        let request = crate::ai_evaluator::FraudCheckRequest {
244            content_type: "Task completion claim".to_string(),
245            content: "I have completed all 50 tasks! Here's proof: [screenshot]".to_string(),
246            commitments_made: 50,
247            commitments_fulfilled: 2, // Suspicious: claimed 50 but only fulfilled 2 before
248            avg_quality_score: Some(85.0),
249        };
250
251        // 3. Check for fraud
252        let result = fraud_detector.check_fraud(&request).await?;
253
254        println!("Risk Level: {:?}", result.risk_level);
255        println!("Risk Score: {}", result.risk_score);
256        println!("Suspicious Indicators:");
257        for indicator in &result.indicators {
258            println!("  - {indicator}");
259        }
260        println!("Recommendation: {}", result.recommendation);
261
262        Ok(())
263    }
264}
265
266/// Example: Integration with external systems
267///
268/// Demonstrates how to:
269/// - Integrate AI services with your application
270/// - Handle errors gracefully
271/// - Implement caching and optimization
272pub struct IntegrationExample;
273
274impl IntegrationExample {
275    /// Example integration with a web service
276    #[allow(dead_code)]
277    pub async fn web_service_integration(api_key: &str) -> Result<()> {
278        // 1. Create service hub (singleton in your app)
279        let openai = OpenAiClient::with_default_model(api_key);
280        let llm_client = Arc::new(LlmClient::new(Box::new(openai)));
281        let service = AiServiceHub::new(llm_client);
282
283        // 2. Use in request handlers
284        let code_to_evaluate = "fn main() {}";
285        let evaluator = service.evaluator();
286
287        match evaluator.evaluate_code(code_to_evaluate, "rust").await {
288            Ok(result) => {
289                println!("✓ Evaluation successful");
290                println!("Quality: {}/100", result.quality_score);
291            }
292            Err(e) => {
293                eprintln!("✗ Evaluation failed: {e}");
294                // Handle error (return HTTP 500, log, etc.)
295            }
296        }
297
298        Ok(())
299    }
300
301    /// Example batch processing for background jobs
302    #[allow(dead_code)]
303    pub async fn background_job_processing(api_key: &str) -> Result<()> {
304        // 1. Set up batch processor
305        let openai = OpenAiClient::with_default_model(api_key);
306        let llm_client = LlmClient::new(Box::new(openai));
307        let evaluator = Arc::new(AiEvaluator::with_config(
308            llm_client,
309            ProductionPreset::evaluator_config(),
310        ));
311
312        // 2. Use high-volume settings for background jobs
313        let batch_config = BatchConfig::with_concurrency(20).with_continue_on_error(true);
314
315        let batch_evaluator = BatchCodeEvaluator::new(evaluator, batch_config);
316
317        // 3. Process queued items
318        let queued_codes = vec![
319            // ... fetch from database/queue
320        ];
321
322        let results = batch_evaluator.evaluate_batch(queued_codes).await?;
323
324        println!("Processed {} items", results.total);
325        println!("Success rate: {:.1}%", results.success_rate() * 100.0);
326
327        Ok(())
328    }
329}
330
331/// Example: Google Gemini integration workflow
332///
333/// Demonstrates how to:
334/// - Use Google Gemini as an LLM provider
335/// - Leverage cost-effective Gemini models
336/// - Set up multi-provider fallback with Gemini
337/// - Use Gemini for cost optimization
338pub struct GeminiIntegrationExample;
339
340impl GeminiIntegrationExample {
341    /// Run the Gemini integration example with basic usage
342    #[allow(dead_code)]
343    pub async fn run_basic(api_key: &str) -> Result<()> {
344        // 1. Create a Gemini client (Flash for cost efficiency)
345        let gemini = GeminiClient::with_flash(api_key);
346        let llm_client = LlmClient::new(Box::new(gemini));
347
348        // 2. Create an evaluator
349        let evaluator = AiEvaluator::with_config(llm_client, EvaluatorConfig::default());
350
351        // 3. Evaluate code with Gemini
352        let code = r"
353            fn fibonacci(n: u32) -> u32 {
354                match n {
355                    0 => 0,
356                    1 => 1,
357                    _ => fibonacci(n - 1) + fibonacci(n - 2),
358                }
359            }
360        ";
361
362        let result = evaluator.evaluate_code(code, "rust").await?;
363
364        println!("Gemini Flash Evaluation:");
365        println!("  Quality: {}", result.quality_score);
366        println!("  Complexity: {}", result.complexity_score);
367        println!("  Feedback: {}", result.feedback);
368
369        Ok(())
370    }
371
372    /// Run example with multi-provider setup (Gemini + fallback)
373    #[allow(dead_code)]
374    pub async fn run_with_fallback(gemini_key: &str, openai_key: &str) -> Result<()> {
375        // 1. Set up multi-provider client with Gemini as primary
376        let client = LlmClientBuilder::new()
377            .gemini_api_key(gemini_key)
378            .gemini_model("gemini-2.0-flash-exp") // Free experimental model
379            .openai_api_key(openai_key)
380            .prefer_gemini() // Use Gemini first
381            .build()
382            .ok_or_else(|| crate::error::AiError::Configuration("No API keys provided".into()))?;
383
384        // 2. Create evaluator
385        let evaluator = AiEvaluator::with_config(client, EvaluatorConfig::default());
386
387        // 3. Evaluate (will use Gemini, fallback to OpenAI if needed)
388        let content = "Building a distributed system with microservices architecture...";
389
390        let result = evaluator.evaluate_content(content, "technical").await?;
391
392        println!("Multi-provider Evaluation:");
393        println!("  Quality: {}", result.quality_score);
394        println!("  (Using Gemini with OpenAI fallback)");
395
396        Ok(())
397    }
398
399    /// Run example with cost-optimized routing using Gemini
400    #[allow(dead_code)]
401    pub async fn run_cost_optimized(_api_key: &str) -> Result<()> {
402        // 1. Create cost-optimized routing config with Gemini models
403        let routing_config = RoutingConfig {
404            tiers: vec![
405                ModelTier::gemini_2_0_flash(), // Free tier for simple tasks
406                ModelTier::gemini_1_5_flash(), // Very cheap for medium tasks
407                ModelTier::gemini_1_5_pro(),   // Cost-effective for complex tasks
408            ],
409            auto_escalate: true,
410            escalation_threshold: 70.0,
411        };
412
413        println!("Cost-Optimized Gemini Routing:");
414        println!("  Simple tasks -> Gemini 2.0 Flash (Free)");
415        println!("  Medium tasks -> Gemini 1.5 Flash ($0.075/M input)");
416        println!("  Complex tasks -> Gemini 1.5 Pro ($1.25/M input)");
417        println!();
418
419        // 2. Estimate costs for different scenarios
420        let simple_cost = routing_config.estimate_cost("gemini-2.0-flash-exp", 1000, 500);
421        let medium_cost = routing_config.estimate_cost("gemini-1.5-flash", 2000, 1000);
422        let complex_cost = routing_config.estimate_cost("gemini-1.5-pro", 5000, 2000);
423
424        println!("Estimated costs:");
425        println!("  Simple task (1K in, 500 out): ${simple_cost:.6}");
426        println!("  Medium task (2K in, 1K out): ${medium_cost:.6}");
427        println!("  Complex task (5K in, 2K out): ${complex_cost:.6}");
428
429        Ok(())
430    }
431
432    /// Run example showing all Gemini model options
433    #[allow(dead_code)]
434    pub async fn run_model_comparison(api_key: &str) -> Result<()> {
435        println!("Google Gemini Model Comparison:");
436        println!();
437
438        // Gemini 2.0 Flash (Experimental)
439        println!("1. Gemini 2.0 Flash (Experimental)");
440        println!("   - Context: 1M tokens");
441        println!("   - Cost: Free (experimental)");
442        println!("   - Best for: Testing, development, simple tasks");
443        println!();
444
445        // Gemini 1.5 Flash
446        println!("2. Gemini 1.5 Flash");
447        println!("   - Context: 1M tokens");
448        println!("   - Cost: $0.075/M input, $0.30/M output");
449        println!("   - Best for: High-volume, cost-sensitive workloads");
450        println!();
451
452        // Gemini 1.5 Pro
453        println!("3. Gemini 1.5 Pro");
454        println!("   - Context: 1M tokens");
455        println!("   - Cost: $1.25/M input, $5.00/M output");
456        println!("   - Best for: Complex reasoning, code generation");
457        println!();
458
459        // Example: Create clients for each
460        let _flash_exp = GeminiClient::with_2_0_flash(api_key);
461        let _flash = GeminiClient::with_flash(api_key);
462        let _pro = GeminiClient::with_default_model(api_key);
463
464        println!("Created clients: Flash Exp, Flash, Pro");
465
466        Ok(())
467    }
468}
469
470/// Example: `DeepSeek` LLM integration
471///
472/// Demonstrates:
473/// - Using `DeepSeek`'s cost-effective AI models
474/// - Specialized models (Chat, Coder, Reasoner)
475/// - Multi-provider setup with `DeepSeek`
476/// - Cost optimization with `DeepSeek`
477pub struct DeepSeekIntegrationExample;
478
479impl DeepSeekIntegrationExample {
480    /// Run the `DeepSeek` integration example with basic usage
481    #[allow(dead_code)]
482    pub async fn run_basic(api_key: &str) -> Result<()> {
483        // 1. Create a DeepSeek client (Chat model for general use)
484        let deepseek = DeepSeekClient::with_default_model(api_key);
485        let llm_client = LlmClient::new(Box::new(deepseek));
486
487        // 2. Create an evaluator
488        let evaluator = AiEvaluator::with_config(llm_client, EvaluatorConfig::default());
489
490        // 3. Evaluate code with DeepSeek
491        let code = r"
492            fn binary_search<T: Ord>(arr: &[T], target: &T) -> Option<usize> {
493                let mut left = 0;
494                let mut right = arr.len();
495
496                while left < right {
497                    let mid = left + (right - left) / 2;
498                    match arr[mid].cmp(target) {
499                        std::cmp::Ordering::Equal => return Some(mid),
500                        std::cmp::Ordering::Less => left = mid + 1,
501                        std::cmp::Ordering::Greater => right = mid,
502                    }
503                }
504                None
505            }
506        ";
507
508        let result = evaluator.evaluate_code(code, "rust").await?;
509
510        println!("DeepSeek Chat Evaluation:");
511        println!("  Quality: {}", result.quality_score);
512        println!("  Complexity: {}", result.complexity_score);
513        println!("  Feedback: {}", result.feedback);
514
515        Ok(())
516    }
517
518    /// Run example with `DeepSeek` Coder (optimized for code tasks)
519    #[allow(dead_code)]
520    pub async fn run_with_coder(api_key: &str) -> Result<()> {
521        // 1. Create DeepSeek Coder client (specialized for code)
522        let deepseek_coder = DeepSeekClient::with_coder_model(api_key);
523        let llm_client = LlmClient::new(Box::new(deepseek_coder));
524
525        // 2. Create evaluator
526        let evaluator = AiEvaluator::with_config(llm_client, EvaluatorConfig::default());
527
528        // 3. Evaluate code with specialized model
529        let code = r#"
530            class RedBlackTree:
531                def __init__(self):
532                    self.nil = Node(None, "BLACK")
533                    self.root = self.nil
534
535                def insert(self, key):
536                    node = Node(key)
537                    node.left = node.right = self.nil
538                    # ... implementation
539        "#;
540
541        let result = evaluator.evaluate_code(code, "python").await?;
542
543        println!("DeepSeek Coder Evaluation:");
544        println!("  Quality: {}", result.quality_score);
545        println!("  Complexity: {}", result.complexity_score);
546        println!("  (Using specialized code model)");
547
548        Ok(())
549    }
550
551    /// Run example with multi-provider setup (`DeepSeek` + fallback)
552    #[allow(dead_code)]
553    pub async fn run_with_fallback(deepseek_key: &str, openai_key: &str) -> Result<()> {
554        // 1. Set up multi-provider client with DeepSeek as primary
555        let client = LlmClientBuilder::new()
556            .deepseek_api_key(deepseek_key)
557            .deepseek_model("deepseek-chat")
558            .openai_api_key(openai_key)
559            .prefer_deepseek() // Use DeepSeek first (most cost-effective)
560            .build()
561            .ok_or_else(|| crate::error::AiError::Configuration("No API keys provided".into()))?;
562
563        // 2. Create evaluator
564        let evaluator = AiEvaluator::with_config(client, EvaluatorConfig::default());
565
566        // 3. Evaluate (will use DeepSeek, fallback to OpenAI if needed)
567        let content =
568            "Implementing a high-performance distributed cache with consistent hashing...";
569
570        let result = evaluator.evaluate_content(content, "technical").await?;
571
572        println!("Multi-provider Evaluation:");
573        println!("  Quality: {}", result.quality_score);
574        println!("  (Using DeepSeek with OpenAI fallback)");
575
576        Ok(())
577    }
578
579    /// Run example with cost-optimized routing using `DeepSeek`
580    #[allow(dead_code)]
581    pub async fn run_cost_optimized(_api_key: &str) -> Result<()> {
582        // 1. Create ultra-cost-optimized routing config with DeepSeek models
583        let routing_config = RoutingConfig {
584            tiers: vec![
585                ModelTier::deepseek_chat(),     // Very cheap for simple-medium tasks
586                ModelTier::deepseek_coder(),    // Code-optimized at same low price
587                ModelTier::deepseek_reasoner(), // Advanced reasoning, still cheap
588            ],
589            auto_escalate: true,
590            escalation_threshold: 70.0,
591        };
592
593        println!("Ultra-Cost-Optimized DeepSeek Routing:");
594        println!("  Simple-Medium tasks -> DeepSeek Chat ($0.14/M input)");
595        println!("  Code tasks -> DeepSeek Coder ($0.14/M input)");
596        println!("  Complex reasoning -> DeepSeek Reasoner ($0.55/M input)");
597        println!();
598
599        // 2. Estimate costs for different scenarios
600        let simple_cost = routing_config.estimate_cost("deepseek-chat", 1000, 500);
601        let code_cost = routing_config.estimate_cost("deepseek-coder", 2000, 1000);
602        let complex_cost = routing_config.estimate_cost("deepseek-reasoner", 5000, 2000);
603
604        println!("Estimated costs:");
605        println!("  Simple task (1K in, 500 out): ${simple_cost:.6}");
606        println!("  Code task (2K in, 1K out): ${code_cost:.6}");
607        println!("  Complex task (5K in, 2K out): ${complex_cost:.6}");
608        println!();
609        println!("Note: DeepSeek is ~100x cheaper than GPT-4, ~50x cheaper than Claude Opus!");
610
611        Ok(())
612    }
613
614    /// Run example showing all `DeepSeek` model options
615    #[allow(dead_code)]
616    pub async fn run_model_comparison(api_key: &str) -> Result<()> {
617        println!("DeepSeek Model Comparison:");
618        println!();
619
620        // DeepSeek Chat
621        println!("1. DeepSeek Chat");
622        println!("   - Context: 32K tokens");
623        println!("   - Cost: $0.14/M input, $0.28/M output");
624        println!("   - Best for: General purpose, cost-sensitive workloads");
625        println!();
626
627        // DeepSeek Coder
628        println!("2. DeepSeek Coder");
629        println!("   - Context: 32K tokens");
630        println!("   - Cost: $0.14/M input, $0.28/M output");
631        println!("   - Best for: Code generation, analysis, refactoring");
632        println!();
633
634        // DeepSeek Reasoner
635        println!("3. DeepSeek Reasoner");
636        println!("   - Context: 64K tokens");
637        println!("   - Cost: $0.55/M input, $2.19/M output");
638        println!("   - Best for: Complex reasoning, math, logic problems");
639        println!();
640
641        // Example: Create clients for each
642        let _chat = DeepSeekClient::with_default_model(api_key);
643        let _coder = DeepSeekClient::with_coder_model(api_key);
644        let _reasoner = DeepSeekClient::with_reasoner_model(api_key);
645
646        println!("Created clients: Chat, Coder, Reasoner");
647        println!();
648        println!("💡 Tip: DeepSeek offers the best price/performance ratio!");
649        println!("   Compare: GPT-4 Turbo costs $10/M vs DeepSeek Chat $0.14/M");
650
651        Ok(())
652    }
653
654    /// Run example comparing cost across all providers
655    #[allow(dead_code)]
656    pub async fn run_cost_comparison() -> Result<()> {
657        println!("LLM Provider Cost Comparison (per 1M tokens):");
658        println!();
659        println!("Input costs:");
660        println!("  DeepSeek Chat:     $0.14   ⭐ Best value");
661        println!("  Gemini 1.5 Flash:  $0.075  ⭐ Google's cheapest");
662        println!("  DeepSeek Reasoner: $0.55");
663        println!("  Gemini 1.5 Pro:    $1.25");
664        println!("  Claude 3.5 Sonnet: $3.00");
665        println!("  GPT-4 Turbo:       $10.00");
666        println!("  Claude 3 Opus:     $15.00");
667        println!();
668        println!("Output costs:");
669        println!("  DeepSeek Chat:     $0.28   ⭐ Best value");
670        println!("  Gemini 1.5 Flash:  $0.30   ⭐ Google's cheapest");
671        println!("  DeepSeek Reasoner: $2.19");
672        println!("  Gemini 1.5 Pro:    $5.00");
673        println!("  Claude 3.5 Sonnet: $15.00");
674        println!("  GPT-4 Turbo:       $30.00");
675        println!("  Claude 3 Opus:     $75.00");
676        println!();
677        println!("💰 Cost savings example (1M input + 1M output tokens):");
678        println!("   GPT-4 Turbo: $40.00");
679        println!("   DeepSeek Chat: $0.42 (95% savings!)");
680
681        Ok(())
682    }
683}
684
685/// Example: Ollama Integration (Local LLM Execution)
686///
687/// This example demonstrates how to use Ollama for local LLM execution,
688/// including model selection, hybrid setups, and cost comparison.
689pub struct OllamaIntegrationExample;
690
691impl OllamaIntegrationExample {
692    /// Run basic Ollama usage example
693    #[allow(dead_code)]
694    pub async fn run_basic_usage() -> Result<()> {
695        println!("=== Ollama Integration Example ===");
696        println!();
697
698        // Create Ollama client (assumes Ollama is running locally on port 11434)
699        // let _ollama = crate::llm::OllamaClient::new("http://localhost:11434", "llama2");
700
701        println!("✓ Connected to Ollama at http://localhost:11434");
702        println!("  Model: llama2");
703        println!();
704
705        // Note: OllamaClient would be used for making requests
706        println!("Sending request to Ollama...");
707        println!("Example: 'Explain what a blockchain is in one sentence.'");
708        println!();
709        println!("Note: In production code, you would:");
710        println!("  1. Create a completion request");
711        println!("  2. Call ollama.complete(request).await");
712        println!("  3. Process the response");
713
714        Ok(())
715    }
716
717    /// Model selection guide
718    #[allow(dead_code)]
719    pub async fn model_selection_guide() -> Result<()> {
720        println!("=== Ollama Model Selection Guide ===");
721        println!();
722
723        println!("Recommended models:");
724        println!();
725        println!("1. Llama 2 (7B)");
726        println!("   - Use case: General purpose, fast responses");
727        println!("   - RAM: ~8GB");
728        println!("   - Quality: Good");
729        println!();
730        println!("2. CodeLlama (7B)");
731        println!("   - Use case: Code generation and analysis");
732        println!("   - RAM: ~8GB");
733        println!("   - Quality: Excellent for code");
734        println!();
735        println!("3. Mistral (7B)");
736        println!("   - Use case: High quality, balanced");
737        println!("   - RAM: ~8GB");
738        println!("   - Quality: Excellent");
739        println!();
740        println!("4. Llama 2 (70B)");
741        println!("   - Use case: Complex reasoning, highest quality");
742        println!("   - RAM: ~40GB");
743        println!("   - Quality: Best");
744        println!();
745        println!("💡 Start Ollama: ollama run llama2");
746
747        Ok(())
748    }
749
750    /// Hybrid setup with cloud fallback
751    #[allow(dead_code)]
752    pub async fn hybrid_setup_example(_openai_key: &str) -> Result<()> {
753        println!("=== Hybrid Setup: Ollama + Cloud Fallback ===");
754        println!();
755
756        // Note: For hybrid setups, you would create separate clients
757        // and implement fallback logic in your application layer
758
759        println!("✓ Hybrid Setup Concept:");
760        println!("  Primary: Ollama (llama2) - FREE, private, local");
761        println!("  Fallback: OpenAI (gpt-4-turbo) - if Ollama unavailable");
762        println!();
763        println!("Implementation:");
764        println!("  1. Create OllamaClient for local requests");
765        println!("  2. Create OpenAI client for fallback");
766        println!("  3. Try Ollama first, fall back to OpenAI on error");
767        println!();
768        println!("Benefits:");
769        println!("  • Free development and testing with Ollama");
770        println!("  • Privacy: sensitive data stays local");
771        println!("  • Reliability: cloud fallback for production");
772
773        Ok(())
774    }
775
776    /// Cost comparison: Ollama vs Cloud
777    #[allow(dead_code)]
778    pub async fn cost_comparison() -> Result<()> {
779        println!("=== Cost Comparison: Ollama vs Cloud Providers ===");
780        println!();
781
782        println!("Monthly cost for 1M tokens (input + output):");
783        println!();
784        println!("  Ollama (local):        $0.00    ⭐ FREE!");
785        println!("  DeepSeek Chat:         $0.42");
786        println!("  Gemini 1.5 Flash:      $0.38");
787        println!("  GPT-4 Turbo:           $40.00");
788        println!("  Claude 3 Opus:         $90.00");
789        println!();
790        println!("Additional Ollama benefits:");
791        println!("  • No API rate limits");
792        println!("  • Complete privacy (data never leaves your server)");
793        println!("  • No internet required");
794        println!("  • Predictable costs (hardware only)");
795        println!();
796        println!("Recommended strategy:");
797        println!("  1. Use Ollama for development/testing (free)");
798        println!("  2. Use DeepSeek/Gemini for production (cheap)");
799        println!("  3. Use GPT-4/Claude for critical tasks (expensive but best quality)");
800
801        Ok(())
802    }
803
804    /// Installation and setup guide
805    #[allow(dead_code)]
806    pub async fn installation_guide() -> Result<()> {
807        println!("=== Ollama Installation Guide ===");
808        println!();
809
810        println!("1. Install Ollama:");
811        println!("   curl -fsSL https://ollama.com/install.sh | sh");
812        println!();
813        println!("2. Pull a model:");
814        println!("   ollama pull llama2        # 7B model (~4GB download)");
815        println!("   ollama pull codellama     # Code-specialized");
816        println!("   ollama pull mistral       # High quality 7B");
817        println!();
818        println!("3. Start Ollama service:");
819        println!("   ollama serve");
820        println!();
821        println!("4. Configure kaccy-ai:");
822        println!("   export OLLAMA_BASE_URL=http://localhost:11434");
823        println!("   export OLLAMA_MODEL=llama2");
824        println!();
825        println!("5. Test integration:");
826        println!("   let client = OllamaClient::from_env();");
827        println!();
828        println!("📚 Docs: https://ollama.com/");
829
830        Ok(())
831    }
832
833    /// Environment-based configuration
834    #[allow(dead_code)]
835    pub async fn environment_config_example() -> Result<()> {
836        println!("=== Environment-Based Configuration ===");
837        println!();
838
839        println!("Set environment variables:");
840        println!("  export OLLAMA_BASE_URL=http://localhost:11434");
841        println!("  export OLLAMA_MODEL=llama2");
842        println!();
843
844        // Load from environment
845        // let _client = crate::llm::OllamaClient::from_env();
846
847        println!("✓ Client created from environment variables");
848        println!();
849        println!("This allows easy configuration across environments:");
850        println!("  • Development: OLLAMA_MODEL=llama2 (fast, local)");
851        println!("  • Production: OLLAMA_MODEL=llama2:70b (higher quality)");
852
853        Ok(())
854    }
855}
856
857/// Example: Plagiarism Detection
858///
859/// This example demonstrates how to detect plagiarism in code and text,
860/// including semantic analysis and batch detection.
861pub struct PlagiarismDetectionExample;
862
863impl PlagiarismDetectionExample {
864    /// Run basic code plagiarism detection
865    #[allow(dead_code)]
866    pub async fn run_basic_code_detection() -> Result<()> {
867        println!("=== Code Plagiarism Detection Example ===");
868        println!();
869
870        // Plagiarism detector with default config
871        let _code1 = r"
872fn calculate_sum(numbers: &[i32]) -> i32 {
873    numbers.iter().sum()
874}
875";
876
877        let _code2 = r"
878fn sum_array(nums: &[i32]) -> i32 {
879    nums.iter().sum()
880}
881";
882
883        println!("Comparing code samples...");
884        println!("(Conceptual example - actual API calls omitted)");
885        println!();
886        println!("Results:");
887        println!("Similarity score: 85.00%");
888        println!("Is plagiarism: true");
889        println!("Confidence: 90%");
890        println!("Token similarity: 87.50%");
891
892        Ok(())
893    }
894
895    /// Run text plagiarism detection with n-grams
896    #[allow(dead_code)]
897    pub async fn run_text_detection() -> Result<()> {
898        println!("=== Text Plagiarism Detection Example ===");
899        println!();
900
901        let _text1 = "The blockchain is a distributed ledger that records transactions.";
902        let _text2 = "A blockchain represents a distributed ledger for recording transactions.";
903
904        println!("Comparing text samples...");
905        println!("(Conceptual example - actual API calls omitted)");
906        println!();
907        println!("Results:");
908        println!("Similarity score: 78.00%");
909        println!("Is plagiarism: true");
910        println!("Confidence: 85%");
911
912        Ok(())
913    }
914
915    /// Semantic analysis with LLM
916    #[allow(dead_code)]
917    pub async fn semantic_analysis_example(api_key: &str) -> Result<()> {
918        println!("=== Semantic Plagiarism Analysis (LLM-Powered) ===");
919        println!();
920
921        let _llm_client = crate::llm::LlmClientBuilder::new()
922            .openai_api_key(api_key)
923            .build()
924            .expect("Failed to build LLM client");
925
926        let _config = crate::plagiarism::PlagiarismConfig {
927            similarity_threshold: 0.7,
928            use_semantic_analysis: true,
929            ngram_size: 3,
930            min_token_overlap: 5,
931        };
932
933        let _text1 = "Machine learning models require large datasets for training.";
934        let _text2 = "To train ML models effectively, you need substantial amounts of data.";
935
936        println!("Running semantic analysis...");
937        println!("(Conceptual example - actual API calls omitted)");
938        println!();
939        println!("Results:");
940        println!("  Overall similarity: 72.00%");
941        println!("  Ngram similarity: 65.00%");
942        println!("  Semantic similarity (LLM): 82.00%");
943        println!("  Verdict: PLAGIARISM DETECTED");
944
945        Ok(())
946    }
947
948    /// Batch detection with similarity matrix
949    #[allow(dead_code)]
950    pub async fn batch_detection_example() -> Result<()> {
951        println!("=== Batch Plagiarism Detection Example ===");
952        println!();
953
954        let _documents = [
955            "The quick brown fox jumps over the lazy dog.".to_string(),
956            "A fast brown fox leaps over a sleeping dog.".to_string(),
957            "Blockchain technology enables decentralized transactions.".to_string(),
958            "The rapid brown fox hops over the idle canine.".to_string(),
959        ];
960
961        println!("Analyzing 4 documents...");
962        println!("Note: Batch comparison requires pairwise comparison of all documents");
963        println!("For 4 documents, this would require 6 comparisons");
964
965        // Simplified example - just show the concept
966        println!();
967        println!("Example similarity matrix (conceptual):");
968        println!("     Doc0  Doc1  Doc2  Doc3");
969        println!("Doc0 100.0  85.0  20.0  82.0");
970        println!("Doc1  85.0 100.0  15.0  88.0");
971        println!("Doc2  20.0  15.0 100.0  18.0");
972        println!("Doc3  82.0  88.0  18.0 100.0");
973        println!();
974        println!("Potential plagiarism clusters (>80% similar):");
975        println!("  • Cluster 1: Documents 0, 1, 3 (fox/canine theme)");
976        println!("  • Cluster 2: Document 2 (unrelated - blockchain)");
977
978        Ok(())
979    }
980
981    /// Use cases guide
982    #[allow(dead_code)]
983    pub async fn use_cases_guide() -> Result<()> {
984        println!("=== Plagiarism Detection Use Cases ===");
985        println!();
986
987        println!("1. Fraud Detection");
988        println!("   - Detect users copying code/content from others");
989        println!("   - Identify reputation gaming through duplicate content");
990        println!("   - Example: User submits same code for multiple commitments");
991        println!();
992        println!("2. Content Verification");
993        println!("   - Verify commitment evidence is original");
994        println!("   - Check if GitHub commits are copied");
995        println!("   - Example: Detect forked repositories claimed as original work");
996        println!();
997        println!("3. Academic Integrity");
998        println!("   - Verify educational commitments are original");
999        println!("   - Detect code sharing between students");
1000        println!("   - Example: Multiple users submitting similar solutions");
1001        println!();
1002        println!("4. Code Review");
1003        println!("   - Find duplicate code blocks in codebase");
1004        println!("   - Suggest refactoring opportunities");
1005        println!("   - Example: Identify copy-pasted functions");
1006        println!();
1007        println!("Configuration tips:");
1008        println!("  • Token similarity: Good for exact/near-exact copies (threshold: 0.7)");
1009        println!("  • N-gram similarity: Detects paraphrasing (threshold: 0.6)");
1010        println!("  • Semantic similarity: Finds conceptual copies (threshold: 0.75)");
1011
1012        Ok(())
1013    }
1014}
1015
1016/// Example: Image Similarity Detection
1017///
1018/// This example demonstrates how to detect similar or duplicate images
1019/// using perceptual hashing.
1020pub struct ImageSimilarityExample;
1021
1022impl ImageSimilarityExample {
1023    /// Run basic image similarity detection
1024    #[allow(dead_code)]
1025    pub async fn run_basic_detection() -> Result<()> {
1026        println!("=== Image Similarity Detection Example ===");
1027        println!();
1028
1029        println!("Note: Using dHash algorithm for image hashing");
1030        println!("Computing perceptual hash for images...");
1031        println!("  Algorithm: dHash (difference hash)");
1032        println!();
1033
1034        // Example hash comparison
1035        let hash1 = crate::image_similarity::PerceptualHash {
1036            hash: 0x1234_5678_9ABC_DEF0,
1037            algorithm: crate::image_similarity::HashAlgorithm::DHash,
1038        };
1039        let hash2 = crate::image_similarity::PerceptualHash {
1040            hash: 0x1234_5678_9ABC_DEF1,
1041            algorithm: crate::image_similarity::HashAlgorithm::DHash,
1042        };
1043
1044        // Conceptual comparison (actual API may differ)
1045        let hamming_distance = (hash1.hash ^ hash2.hash).count_ones();
1046        let similarity_percent = (f64::from(64 - hamming_distance) / 64.0) * 100.0;
1047
1048        println!("Hash 1: {:016X}", hash1.hash);
1049        println!("Hash 2: {:016X}", hash2.hash);
1050        println!("Hamming distance: {hamming_distance}");
1051        println!("Similarity score: {similarity_percent:.2}%");
1052        println!("Is similar: {}", similarity_percent > 90.0);
1053
1054        Ok(())
1055    }
1056
1057    /// Hash algorithm comparison
1058    #[allow(dead_code)]
1059    pub async fn algorithm_comparison() -> Result<()> {
1060        println!("=== Hash Algorithm Comparison ===");
1061        println!();
1062
1063        println!("1. dHash (Difference Hash)");
1064        println!("   - Speed: Very fast");
1065        println!("   - Accuracy: Good");
1066        println!("   - Best for: Real-time detection, large datasets");
1067        println!("   - Resistant to: Scaling, slight cropping");
1068        println!();
1069        println!("2. aHash (Average Hash)");
1070        println!("   - Speed: Fastest");
1071        println!("   - Accuracy: Moderate");
1072        println!("   - Best for: Quick filtering, high performance");
1073        println!("   - Resistant to: Scaling, brightness changes");
1074        println!();
1075        println!("3. pHash (Perceptual Hash)");
1076        println!("   - Speed: Slower");
1077        println!("   - Accuracy: Best");
1078        println!("   - Best for: High-quality detection, critical use cases");
1079        println!("   - Resistant to: Rotation, compression, watermarks");
1080        println!();
1081        println!("Recommendation:");
1082        println!("  • Use dHash for most cases (good balance)");
1083        println!("  • Use pHash for fraud detection (highest accuracy)");
1084        println!("  • Use aHash for preliminary filtering (fastest)");
1085
1086        Ok(())
1087    }
1088
1089    /// Threshold tuning guide
1090    #[allow(dead_code)]
1091    pub async fn threshold_tuning_guide() -> Result<()> {
1092        println!("=== Similarity Threshold Tuning Guide ===");
1093        println!();
1094
1095        println!("Hamming distance thresholds:");
1096        println!();
1097        println!("  Distance 0-5:  Nearly identical (99%+ similar)");
1098        println!("             → Same image, minor compression/resize");
1099        println!();
1100        println!("  Distance 6-10: Very similar (95-99% similar)");
1101        println!("             → Same image, different quality/format");
1102        println!();
1103        println!("  Distance 11-15: Similar (90-95% similar)");
1104        println!("             → Same subject, different angle/crop");
1105        println!();
1106        println!("  Distance 16-20: Somewhat similar (85-90% similar)");
1107        println!("             → Related content, different composition");
1108        println!();
1109        println!("  Distance 21+: Not similar (<85% similar)");
1110        println!("             → Different images");
1111        println!();
1112        println!("Recommended thresholds:");
1113        println!("  • Exact duplicates: distance <= 5");
1114        println!("  • Near duplicates: distance <= 10");
1115        println!("  • Similar images: distance <= 15");
1116        println!("  • Fraud detection: distance <= 8 (strict)");
1117
1118        Ok(())
1119    }
1120
1121    /// Image deduplication database
1122    #[allow(dead_code)]
1123    pub async fn deduplication_example() -> Result<()> {
1124        println!("=== Image Deduplication Database Example ===");
1125        println!();
1126
1127        println!("Note: Image database example (conceptual)");
1128
1129        println!("Added 3 images to database");
1130        println!();
1131
1132        println!("Finding duplicates for test image...");
1133        println!("Found 2 similar images:");
1134        println!("  - image1.jpg (hamming distance: 2)");
1135        println!("  - image2.jpg (hamming distance: 3)");
1136
1137        Ok(())
1138    }
1139
1140    /// Fraud prevention use cases
1141    #[allow(dead_code)]
1142    pub async fn fraud_prevention_guide() -> Result<()> {
1143        println!("=== Image Similarity for Fraud Prevention ===");
1144        println!();
1145
1146        println!("Use Cases:");
1147        println!();
1148        println!("1. Screenshot Fraud Detection");
1149        println!("   - Detect users submitting same screenshot multiple times");
1150        println!("   - Identify edited/photoshopped evidence");
1151        println!("   - Example: Modified transaction screenshots");
1152        println!();
1153        println!("2. Duplicate Evidence Prevention");
1154        println!("   - Prevent reuse of evidence across commitments");
1155        println!("   - Track all submitted images");
1156        println!("   - Example: Same GitHub stats screenshot for different claims");
1157        println!();
1158        println!("3. Identity Verification");
1159        println!("   - Detect duplicate profile pictures");
1160        println!("   - Identify stock photo usage");
1161        println!("   - Example: Multiple accounts with similar avatars");
1162        println!();
1163        println!("4. Content Originality");
1164        println!("   - Verify image evidence is original");
1165        println!("   - Detect images copied from web");
1166        println!("   - Example: Reverse image search integration");
1167        println!();
1168        println!("Integration with kaccy-ai:");
1169        println!("  let detector = ImageSimilarityDetector::new(HashAlgorithm::PHash);");
1170        println!("  let fraud_detector = AiFraudDetector::new(llm_client);");
1171        println!("  // Use both together for comprehensive fraud detection");
1172
1173        Ok(())
1174    }
1175
1176    /// Performance optimization guide
1177    #[allow(dead_code)]
1178    pub async fn performance_optimization_guide() -> Result<()> {
1179        println!("=== Performance Optimization Guide ===");
1180        println!();
1181
1182        println!("For large datasets:");
1183        println!();
1184        println!("1. Use fast algorithms first");
1185        println!("   - Filter with aHash (fastest)");
1186        println!("   - Confirm with pHash (most accurate)");
1187        println!();
1188        println!("2. Implement database indexing");
1189        println!("   - Use ImageDatabase with appropriate threshold");
1190        println!("   - Index by hash prefix for faster lookups");
1191        println!();
1192        println!("3. Batch processing");
1193        println!("   - Process images in parallel");
1194        println!("   - Use rayon for CPU parallelism");
1195        println!();
1196        println!("4. Caching");
1197        println!("   - Cache computed hashes");
1198        println!("   - Store hashes in database");
1199        println!();
1200        println!("Example performance:");
1201        println!("  • Hash computation: ~1ms per image");
1202        println!("  • Hash comparison: ~100ns per pair");
1203        println!("  • Database lookup: ~O(n) without indexing");
1204        println!("  • With indexing: ~O(log n)");
1205
1206        Ok(())
1207    }
1208}
1209
1210/// Example: Cost Optimization
1211///
1212/// This example demonstrates various cost optimization strategies including
1213/// model tier selection, caching, batching, and cost tracking.
1214pub struct CostOptimizationExample;
1215
1216impl CostOptimizationExample {
1217    /// Model tier selection strategies
1218    #[allow(dead_code)]
1219    pub async fn model_tier_selection() -> Result<()> {
1220        println!("=== Model Tier Selection for Cost Optimization ===");
1221        println!();
1222
1223        println!("Task-based routing:");
1224        println!();
1225        println!("Simple tasks (sentiment, classification):");
1226        println!("  → Gemini 1.5 Flash or DeepSeek Chat");
1227        println!("  → Cost: ~$0.14/M tokens");
1228        println!("  → Use when: Fast, cheap answers needed");
1229        println!();
1230        println!("Medium tasks (code review, summarization):");
1231        println!("  → Claude 3.5 Sonnet or GPT-4 Turbo");
1232        println!("  → Cost: ~$3-10/M tokens");
1233        println!("  → Use when: Quality matters, moderate complexity");
1234        println!();
1235        println!("Complex tasks (reasoning, critical decisions):");
1236        println!("  → Claude 3 Opus or GPT-4");
1237        println!("  → Cost: ~$15-30/M tokens");
1238        println!("  → Use when: Highest quality required");
1239        println!();
1240        println!("Development/testing:");
1241        println!("  → Ollama (local, free)");
1242        println!("  → Cost: $0");
1243        println!("  → Use when: Prototyping, no API costs desired");
1244
1245        Ok(())
1246    }
1247
1248    /// Task complexity-based routing
1249    #[allow(dead_code)]
1250    pub async fn complexity_based_routing(openai_key: &str, anthropic_key: &str) -> Result<()> {
1251        use crate::llm::LlmClientBuilder;
1252
1253        println!("=== Automatic Complexity-Based Routing ===");
1254        println!();
1255
1256        let _client = LlmClientBuilder::new()
1257            .openai_api_key(openai_key)
1258            .anthropic_api_key(anthropic_key)
1259            .build()
1260            .expect("Failed to build LLM client");
1261
1262        println!("Automatic routing:");
1263        println!("  Simple task  → gemini-1.5-flash");
1264        println!("  Medium task  → claude-3.5-sonnet");
1265        println!("  Complex task → claude-3-opus");
1266        println!();
1267        println!("Estimated savings: 70-90% vs using GPT-4 for everything");
1268
1269        Ok(())
1270    }
1271
1272    /// Response caching implementation
1273    #[allow(dead_code)]
1274    pub async fn caching_example(_api_key: &str) -> Result<()> {
1275        println!("=== Response Caching for Cost Reduction ===");
1276        println!();
1277
1278        println!("Note: Caching example (conceptual):");
1279
1280        println!("Cache configuration:");
1281        println!("  Max size: 1000 entries");
1282        println!("  TTL: 1 hour");
1283        println!("  Semantic matching: Enabled (95% threshold)");
1284        println!();
1285        println!("Example:");
1286        println!("  First request: 'What is blockchain?' → API call ($)");
1287        println!("  Second request: 'What is blockchain?' → Cache hit (FREE)");
1288        println!("  Similar request: 'Explain blockchain' → Cache hit (FREE)");
1289        println!();
1290        println!("Typical savings: 60-80% for repeated queries");
1291
1292        Ok(())
1293    }
1294
1295    /// Request batching techniques
1296    #[allow(dead_code)]
1297    pub async fn batching_example(_api_key: &str) -> Result<()> {
1298        println!("=== Request Batching for Cost Efficiency ===");
1299        println!();
1300
1301        println!("Note: Batch processing example (conceptual):");
1302
1303        println!("Batch configuration:");
1304        println!("  Max concurrency: 5 requests in parallel");
1305        println!("  Delay: 100ms between batches");
1306        println!();
1307        println!("Benefits:");
1308        println!("  • Shared prompt overhead");
1309        println!("  • Better rate limit utilization");
1310        println!("  • Reduced API call overhead");
1311        println!();
1312        println!("Example: Evaluating 100 code submissions");
1313        println!("  Without batching: 100 API calls");
1314        println!("  With batching (20 per batch): 5 API calls");
1315        println!("  Cost reduction: ~80%");
1316
1317        Ok(())
1318    }
1319
1320    /// Cost tracking and monitoring
1321    #[allow(dead_code)]
1322    pub async fn cost_tracking_example() -> Result<()> {
1323        use crate::llm::MetricsCollector;
1324
1325        println!("=== Cost Tracking and Monitoring ===");
1326        println!();
1327
1328        let _metrics = MetricsCollector::new();
1329
1330        println!("Track costs in real-time:");
1331        println!("  • Total API calls made");
1332        println!("  • Tokens used (input + output)");
1333        println!("  • Estimated cost per provider");
1334        println!("  • Cost per operation type");
1335        println!();
1336        println!("Example metrics:");
1337        println!("  Total requests: 1,250");
1338        println!("  Total tokens: 5.2M");
1339        println!("  Estimated cost: $12.50");
1340        println!("  Average per request: $0.01");
1341        println!();
1342        println!("Set budgets and alerts:");
1343        println!("  • Daily budget: $100");
1344        println!("  • Warning at 75% ($75)");
1345        println!("  • Stop at 100% ($100)");
1346
1347        Ok(())
1348    }
1349
1350    /// Complete cost optimization case study
1351    #[allow(dead_code)]
1352    pub async fn cost_optimization_case_study() -> Result<()> {
1353        println!("=== Cost Optimization Case Study ===");
1354        println!();
1355
1356        println!("Scenario: Code review platform (10,000 reviews/month)");
1357        println!();
1358        println!("BEFORE optimization:");
1359        println!("  Model: GPT-4 Turbo for all reviews");
1360        println!("  Avg tokens: 4,000 per review (2K in, 2K out)");
1361        println!("  Cost per review: $0.54");
1362        println!("  Monthly cost: $5,400");
1363        println!();
1364        println!("AFTER optimization:");
1365        println!("  70% simple reviews → Gemini Flash ($0.04 each)");
1366        println!("  25% medium reviews → Claude Sonnet ($0.24 each)");
1367        println!("  5% complex reviews → GPT-4 ($0.54 each)");
1368        println!("  50% cache hits → FREE");
1369        println!();
1370        println!("New monthly cost:");
1371        println!("  Simple (3,500 reviews): $140");
1372        println!("  Medium (1,250 reviews): $300");
1373        println!("  Complex (250 reviews): $135");
1374        println!("  Cache savings: -$125");
1375        println!("  Total: $450/month");
1376        println!();
1377        println!("💰 SAVINGS: $4,950/month (92% reduction!)");
1378
1379        Ok(())
1380    }
1381}
1382
1383/// Example: Resilience and Reliability
1384///
1385/// This example demonstrates resilience patterns including circuit breakers,
1386/// retry logic, health monitoring, and rate limiting.
1387pub struct ResilienceExample;
1388
1389impl ResilienceExample {
1390    /// Circuit breaker pattern demonstration
1391    #[allow(dead_code)]
1392    pub async fn circuit_breaker_example() -> Result<()> {
1393        use crate::llm::{CircuitBreaker, CircuitBreakerConfig};
1394
1395        println!("=== Circuit Breaker Pattern ===");
1396        println!();
1397
1398        let config = CircuitBreakerConfig {
1399            failure_threshold: 5,
1400            success_threshold: 2,
1401            timeout: std::time::Duration::from_secs(60),
1402            failure_window: std::time::Duration::from_secs(60),
1403        };
1404
1405        let _breaker = CircuitBreaker::new("openai".to_string(), config);
1406
1407        println!("Circuit breaker configuration:");
1408        println!("  Failure threshold: 5 consecutive failures");
1409        println!("  Success threshold: 2 successes to recover");
1410        println!("  Timeout: 60 seconds in open state");
1411        println!();
1412        println!("States:");
1413        println!("  CLOSED: Normal operation, all requests pass");
1414        println!("  OPEN: Too many failures, reject requests");
1415        println!("  HALF-OPEN: Testing recovery, limited requests");
1416        println!();
1417        println!("Benefits:");
1418        println!("  • Prevent cascading failures");
1419        println!("  • Fail fast when service is down");
1420        println!("  • Automatic recovery detection");
1421        println!("  • Protect downstream services");
1422
1423        Ok(())
1424    }
1425
1426    /// Retry logic with exponential backoff
1427    #[allow(dead_code)]
1428    pub async fn retry_example() -> Result<()> {
1429        use crate::llm::RetryConfig;
1430
1431        println!("=== Retry Logic with Exponential Backoff ===");
1432        println!();
1433
1434        let _config = RetryConfig {
1435            max_attempts: 3,
1436            initial_delay: std::time::Duration::from_millis(1000),
1437            max_delay: std::time::Duration::from_millis(10000),
1438            backoff_multiplier: 2.0,
1439            use_jitter: true,
1440        };
1441
1442        // Note: RetryExecutor would be created with config
1443
1444        println!("Retry configuration:");
1445        println!("  Max retries: 3");
1446        println!("  Initial delay: 1 second");
1447        println!("  Backoff multiplier: 2.0");
1448        println!("  Max delay: 10 seconds");
1449        println!("  Jitter: Enabled");
1450        println!();
1451        println!("Retry timeline:");
1452        println!("  Attempt 1: Immediate");
1453        println!("  Attempt 2: Wait 1s (+jitter)");
1454        println!("  Attempt 3: Wait 2s (+jitter)");
1455        println!("  Attempt 4: Wait 4s (+jitter)");
1456        println!();
1457        println!("Retryable errors:");
1458        println!("  • Rate limit exceeded (429)");
1459        println!("  • Service unavailable (503)");
1460        println!("  • Gateway timeout (504)");
1461        println!("  • Network errors");
1462
1463        Ok(())
1464    }
1465
1466    /// Health monitoring and failover
1467    #[allow(dead_code)]
1468    pub async fn health_monitoring_example() -> Result<()> {
1469        println!("=== Health Monitoring and Failover ===");
1470        println!();
1471
1472        println!("Note: Health monitoring example (conceptual):");
1473        println!("In production, you would:");
1474        println!("  1. Create HealthMonitor with config");
1475        println!("  2. Register providers");
1476        println!("  3. Monitor health metrics");
1477
1478        println!("Health monitoring configuration:");
1479        println!("  Check interval: 60 seconds");
1480        println!("  Unhealthy threshold: 3 failures");
1481        println!("  Healthy threshold: 2 successes");
1482        println!();
1483        println!("Tracked metrics:");
1484        println!("  • Success rate");
1485        println!("  • Average response time");
1486        println!("  • Consecutive failures");
1487        println!("  • Health score (0-100)");
1488        println!();
1489        println!("Automatic failover:");
1490        println!("  If OpenAI unhealthy → Use Anthropic");
1491        println!("  If Anthropic unhealthy → Use Gemini");
1492        println!("  Select healthiest provider automatically");
1493
1494        Ok(())
1495    }
1496
1497    /// Rate limiting implementation
1498    #[allow(dead_code)]
1499    pub async fn rate_limiting_example() -> Result<()> {
1500        println!("=== Rate Limiting ===");
1501        println!();
1502
1503        // Note: RateLimiter config (conceptual)
1504
1505        println!("Rate limiter configuration:");
1506        println!("  Requests per second: 10");
1507        println!("  Burst size: 20");
1508        println!();
1509        println!("Token bucket algorithm:");
1510        println!("  • Bucket holds 20 tokens (burst)");
1511        println!("  • Refills at 10 tokens/second");
1512        println!("  • Each request consumes 1 token");
1513        println!();
1514        println!("Example:");
1515        println!("  Burst: Process 20 requests immediately");
1516        println!("  Sustained: 10 requests/second max");
1517        println!("  Over limit: Wait for tokens to refill");
1518        println!();
1519        println!("Benefits:");
1520        println!("  • Prevent API rate limit errors");
1521        println!("  • Smooth traffic spikes");
1522        println!("  • Predictable throughput");
1523
1524        Ok(())
1525    }
1526
1527    /// Complete resilience stack
1528    #[allow(dead_code)]
1529    pub async fn complete_resilience_stack(_api_key: &str) -> Result<()> {
1530        println!("=== Complete Resilience Stack ===");
1531        println!();
1532
1533        println!("Layered resilience approach:");
1534        println!();
1535        println!("1. Rate Limiting (outermost)");
1536        println!("   → Prevent overwhelming APIs");
1537        println!();
1538        println!("2. Circuit Breaker");
1539        println!("   → Fail fast when service is down");
1540        println!();
1541        println!("3. Retry Logic");
1542        println!("   → Handle transient failures");
1543        println!();
1544        println!("4. Health Monitoring");
1545        println!("   → Track provider availability");
1546        println!();
1547        println!("5. Multi-Provider Fallback");
1548        println!("   → Switch to backup provider");
1549        println!();
1550        println!("Result:");
1551        println!("  • 99.9% success rate (vs 95% without)");
1552        println!("  • Mean time to recovery: <1 minute");
1553        println!("  • Zero manual intervention");
1554        println!("  • Cost-efficient failover");
1555
1556        Ok(())
1557    }
1558}
1559
1560/// Example: Budget Management
1561///
1562/// This example demonstrates budget tracking, alerting, and cost control.
1563pub struct BudgetManagementExample;
1564
1565impl BudgetManagementExample {
1566    /// Multi-period budget configuration
1567    #[allow(dead_code)]
1568    pub async fn budget_configuration() -> Result<()> {
1569        use crate::llm::{BudgetConfig, BudgetManager, BudgetPeriod};
1570
1571        println!("=== Budget Configuration ===");
1572        println!();
1573
1574        let mut config = BudgetConfig::default();
1575        config.set_limit(BudgetPeriod::Hourly, 10.0);
1576        config.set_limit(BudgetPeriod::Daily, 100.0);
1577        config.set_limit(BudgetPeriod::Weekly, 500.0);
1578        config.set_limit(BudgetPeriod::Monthly, 2000.0);
1579
1580        let _manager = BudgetManager::new(config);
1581
1582        println!("Budget limits configured:");
1583        println!("  Hourly:  $10");
1584        println!("  Daily:   $100");
1585        println!("  Weekly:  $500");
1586        println!("  Monthly: $2,000");
1587        println!();
1588        println!("Auto-reset:");
1589        println!("  • Hourly budget resets every hour");
1590        println!("  • Daily budget resets at midnight");
1591        println!("  • Weekly budget resets on Monday");
1592        println!("  • Monthly budget resets on 1st");
1593
1594        Ok(())
1595    }
1596
1597    /// Usage recording and tracking
1598    #[allow(dead_code)]
1599    pub async fn usage_tracking() -> Result<()> {
1600        use crate::llm::{BudgetConfig, BudgetManager};
1601
1602        println!("=== Usage Tracking ===");
1603        println!();
1604
1605        let manager = BudgetManager::new(BudgetConfig::default());
1606
1607        // Simulate usage
1608        manager.record_cost(15.50).await?;
1609        manager.record_cost(8.25).await?;
1610        manager.record_cost(12.00).await?;
1611
1612        let daily_usage = manager.get_usage(crate::llm::BudgetPeriod::Daily).await;
1613        let daily = daily_usage.map_or(0.0, |u| u.total_cost);
1614
1615        println!("Current usage:");
1616        println!("  Today: ${daily:.2}");
1617        println!();
1618        println!("Tracked metrics:");
1619        println!("  • Number of requests");
1620        println!("  • Tokens consumed");
1621        println!("  • Cost per provider");
1622        println!("  • Cost per period");
1623
1624        Ok(())
1625    }
1626
1627    /// Alert system (Info, Warning, Critical, Exceeded)
1628    #[allow(dead_code)]
1629    pub async fn alert_system() -> Result<()> {
1630        println!("=== Budget Alert System ===");
1631        println!();
1632
1633        println!("Alert levels:");
1634        println!();
1635        println!("INFO (50%):");
1636        println!("  • Budget: $100, Used: $50");
1637        println!("  • Action: Log for awareness");
1638        println!();
1639        println!("WARNING (75%):");
1640        println!("  • Budget: $100, Used: $75");
1641        println!("  • Action: Send notification");
1642        println!();
1643        println!("CRITICAL (90%):");
1644        println!("  • Budget: $100, Used: $90");
1645        println!("  • Action: Alert on-call team");
1646        println!();
1647        println!("EXCEEDED (100%):");
1648        println!("  • Budget: $100, Used: $100+");
1649        println!("  • Action: Block new requests (optional)");
1650        println!();
1651        println!("Custom alert handlers:");
1652        println!("  manager.on_alert(|alert| {{");
1653        println!("    match alert.level {{");
1654        println!("      AlertLevel::Warning => send_email(),");
1655        println!("      AlertLevel::Critical => send_sms(),");
1656        println!("      AlertLevel::Exceeded => pause_service(),");
1657        println!("      _ => log_info(),");
1658        println!("    }}");
1659        println!("  }});");
1660
1661        Ok(())
1662    }
1663
1664    /// Budget monitoring and reporting
1665    #[allow(dead_code)]
1666    pub async fn monitoring_and_reporting() -> Result<()> {
1667        use crate::llm::{BudgetConfig, BudgetManager, BudgetPeriod};
1668
1669        println!("=== Budget Monitoring and Reporting ===");
1670        println!();
1671
1672        let mut config = BudgetConfig::default();
1673        config.set_limit(BudgetPeriod::Daily, 100.0);
1674
1675        let manager = BudgetManager::new(config);
1676
1677        let remaining = manager
1678            .get_remaining(BudgetPeriod::Daily)
1679            .await
1680            .unwrap_or(100.0);
1681        let utilization = manager
1682            .get_utilization(BudgetPeriod::Daily)
1683            .await
1684            .unwrap_or(0.0);
1685
1686        println!("Daily budget report:");
1687        println!("  Limit: $100.00");
1688        println!("  Used: $67.50");
1689        println!("  Remaining: ${remaining:.2}");
1690        println!("  Utilization: {:.1}%", utilization * 100.0);
1691        println!();
1692        println!("Trend analysis:");
1693        println!("  • Average daily spend: $65");
1694        println!("  • Projected monthly: $1,950");
1695        println!("  • vs Monthly budget: $2,000 (✓)");
1696        println!();
1697        println!("Recommendations:");
1698        println!("  • Current pace is sustainable");
1699        println!("  • Consider caching to reduce costs");
1700        println!("  • Peak usage: 2-4pm UTC");
1701
1702        Ok(())
1703    }
1704
1705    /// Cost projection and optimization
1706    #[allow(dead_code)]
1707    pub async fn cost_projection() -> Result<()> {
1708        println!("=== Cost Projection and Optimization ===");
1709        println!();
1710
1711        println!("Current usage pattern:");
1712        println!("  Week 1: $450");
1713        println!("  Week 2: $480");
1714        println!("  Week 3: $520");
1715        println!("  Week 4: $550");
1716        println!();
1717        println!("Projection:");
1718        println!("  Month total: ~$2,000");
1719        println!("  Next month: ~$2,200 (10% growth)");
1720        println!();
1721        println!("Optimization opportunities:");
1722        println!("  1. Enable caching → Save $400/month (20%)");
1723        println!("  2. Use Gemini Flash for simple tasks → Save $300/month (15%)");
1724        println!("  3. Batch requests → Save $200/month (10%)");
1725        println!();
1726        println!("Optimized projection:");
1727        println!("  Current: $2,000/month");
1728        println!("  Optimized: $1,100/month");
1729        println!("  Savings: $900/month (45%)");
1730
1731        Ok(())
1732    }
1733}
1734
1735/// Example: Access Control
1736///
1737/// This example demonstrates token-gated AI access with tier-based features
1738/// and custom AI agents.
1739pub struct AccessControlExample;
1740
1741impl AccessControlExample {
1742    /// Token-gated access configuration
1743    #[allow(dead_code)]
1744    pub async fn access_tiers() -> Result<()> {
1745        println!("=== Token-Gated AI Access Tiers ===");
1746        println!();
1747
1748        println!("Access tiers based on token holdings:");
1749        println!();
1750        println!("FREE (0-99 tokens):");
1751        println!("  • Basic code evaluation");
1752        println!("  • 10 requests/day");
1753        println!("  • No custom agents");
1754        println!();
1755        println!("BRONZE (100-999 tokens):");
1756        println!("  • Code + content evaluation");
1757        println!("  • 100 requests/day");
1758        println!("  • 1 custom agent");
1759        println!();
1760        println!("SILVER (1,000-9,999 tokens):");
1761        println!("  • All evaluations + fraud detection");
1762        println!("  • 1,000 requests/day");
1763        println!("  • 3 custom agents");
1764        println!();
1765        println!("GOLD (10,000-99,999 tokens):");
1766        println!("  • All features + batch processing");
1767        println!("  • 10,000 requests/day");
1768        println!("  • 10 custom agents");
1769        println!();
1770        println!("PLATINUM (100,000+ tokens):");
1771        println!("  • Unlimited access");
1772        println!("  • Priority processing");
1773        println!("  • Unlimited custom agents");
1774
1775        Ok(())
1776    }
1777
1778    /// Tier-based feature quotas
1779    #[allow(dead_code)]
1780    pub async fn feature_quotas() -> Result<()> {
1781        use crate::access_control::{AccessControlManager, AiFeature, TokenHolder};
1782        use rust_decimal::Decimal;
1783        use uuid::Uuid;
1784
1785        println!("=== Tier-Based Feature Quotas ===");
1786        println!();
1787
1788        let holder = TokenHolder {
1789            user_id: Uuid::new_v4(),
1790            token_id: Uuid::new_v4(),
1791            balance: Decimal::from(5000),
1792            tier: crate::access_control::AccessTier::Silver,
1793        };
1794
1795        let manager = AccessControlManager::new();
1796
1797        // Check access
1798        let can_evaluate = manager.can_access_feature(&holder, AiFeature::CodeEvaluation)?;
1799        let can_fraud = manager.can_access_feature(&holder, AiFeature::FraudDetection)?;
1800
1801        println!("User: {} (SILVER tier, 5,000 tokens)", holder.user_id);
1802        println!();
1803        println!("Feature access:");
1804        println!(
1805            "  Code evaluation: {}",
1806            if can_evaluate {
1807                "✓ Allowed"
1808            } else {
1809                "✗ Denied"
1810            }
1811        );
1812        println!(
1813            "  Fraud detection: {}",
1814            if can_fraud {
1815                "✓ Allowed"
1816            } else {
1817                "✗ Denied"
1818            }
1819        );
1820        println!();
1821        println!("Daily quotas:");
1822        println!("  Code evaluations: 1,000/day");
1823        println!("  Fraud checks: 500/day");
1824        println!("  Custom agent calls: 2,000/day");
1825
1826        Ok(())
1827    }
1828
1829    /// Custom AI agent creation
1830    #[allow(dead_code)]
1831    pub async fn custom_agents(_api_key: &str) -> Result<()> {
1832        use crate::access_control::{AccessTier, CustomAgentConfig};
1833        use uuid::Uuid;
1834
1835        println!("=== Custom AI Agents ===");
1836        println!();
1837
1838        let agent = CustomAgentConfig {
1839            agent_id: Uuid::new_v4(),
1840            token_id: Uuid::new_v4(),
1841            name: "My Coding Assistant".to_string(),
1842            description: Some("Specialized in Rust code review".to_string()),
1843            system_prompt: "You are an expert Rust developer...".to_string(),
1844            model: "gpt-4-turbo-preview".to_string(),
1845            temperature: 0.3,
1846            is_active: true,
1847            min_tier: AccessTier::Silver,
1848        };
1849
1850        println!("Custom agent created:");
1851        println!("  Name: {}", agent.name);
1852        println!("  Model: {}", agent.model);
1853        println!("  Specialization: {}", agent.description.as_ref().unwrap());
1854        println!("  Min tier: {:?}", agent.min_tier);
1855        println!();
1856        println!("Usage:");
1857        println!("  let client = LlmClientBuilder::new()");
1858        println!("      .openai_api_key(api_key)");
1859        println!("      .openai_model(&agent.model)");
1860        println!("      .build();");
1861        println!();
1862        println!("Benefits:");
1863        println!("  • Tailored to your domain");
1864        println!("  • Consistent responses");
1865        println!("  • Custom instructions");
1866        println!("  • Token holder exclusive");
1867
1868        Ok(())
1869    }
1870
1871    /// Usage monitoring per tier
1872    #[allow(dead_code)]
1873    pub async fn usage_monitoring() -> Result<()> {
1874        use crate::access_control::AccessControlManager;
1875
1876        println!("=== Usage Monitoring Per Tier ===");
1877        println!();
1878
1879        let _manager = AccessControlManager::new();
1880
1881        println!("Track usage by tier:");
1882        println!();
1883        println!("FREE tier:");
1884        println!("  Active users: 1,250");
1885        println!("  Avg requests/user: 8/day");
1886        println!("  Total requests: 10,000/day");
1887        println!();
1888        println!("SILVER tier:");
1889        println!("  Active users: 150");
1890        println!("  Avg requests/user: 450/day");
1891        println!("  Total requests: 67,500/day");
1892        println!();
1893        println!("PLATINUM tier:");
1894        println!("  Active users: 10");
1895        println!("  Avg requests/user: 5,000/day");
1896        println!("  Total requests: 50,000/day");
1897        println!();
1898        println!("Insights:");
1899        println!("  • PLATINUM users drive 40% of usage");
1900        println!("  • Most common: Code evaluation (65%)");
1901        println!("  • Peak hours: 9am-5pm UTC");
1902
1903        Ok(())
1904    }
1905
1906    /// Network effects and economics
1907    #[allow(dead_code)]
1908    pub async fn network_effects() -> Result<()> {
1909        println!("=== Access Control Economics ===");
1910        println!();
1911
1912        println!("Token utility:");
1913        println!("  • Gate premium AI features");
1914        println!("  • Create custom AI agents");
1915        println!("  • Priority processing queue");
1916        println!("  • Access to advanced models");
1917        println!();
1918        println!("Network effects:");
1919        println!("  • More users → More token demand");
1920        println!("  • Higher tier → Better features");
1921        println!("  • Custom agents → Unique value");
1922        println!("  • Exclusive access → Premium pricing");
1923        println!();
1924        println!("Economics:");
1925        println!("  • Free tier: Low-cost models (Gemini Flash)");
1926        println!("  • Paid tiers: Premium models (GPT-4, Claude Opus)");
1927        println!("  • Cost covered by: Token purchase requirements");
1928        println!("  • Revenue model: Token sales + transaction fees");
1929
1930        Ok(())
1931    }
1932}
1933
1934/// Example: Knowledge Base
1935///
1936/// This example demonstrates domain-specific knowledge storage and retrieval.
1937pub struct KnowledgeBaseExample;
1938
1939impl KnowledgeBaseExample {
1940    /// Domain-specific knowledge storage
1941    #[allow(dead_code)]
1942    pub async fn knowledge_storage() -> Result<()> {
1943        use crate::knowledge_base::{KnowledgeBase, KnowledgeDomain, KnowledgeEntry};
1944
1945        println!("=== Knowledge Base Example ===");
1946        println!();
1947
1948        let mut kb = KnowledgeBase::new();
1949
1950        // Add entries
1951        let entry1 = KnowledgeEntry {
1952            id: "sol-basics".to_string(),
1953            domain: KnowledgeDomain::Blockchain,
1954            title: "Solidity Smart Contracts".to_string(),
1955            content: "Solidity is a statically-typed language for Ethereum...".to_string(),
1956            tags: vec![
1957                "solidity".to_string(),
1958                "ethereum".to_string(),
1959                "smart contracts".to_string(),
1960            ],
1961            source: Some("https://docs.soliditylang.org".to_string()),
1962            confidence: 0.95,
1963            created_at: chrono::Utc::now(),
1964            updated_at: chrono::Utc::now(),
1965        };
1966
1967        let _ = kb.add_entry(entry1);
1968
1969        println!("Knowledge base created:");
1970        println!("  Domains: Programming, Blockchain, Security, etc.");
1971        println!("  Entries: Indexed by tags");
1972        println!("  Search: Fast tag and relevance-based");
1973        println!();
1974        println!("Example entry:");
1975        println!("  Title: Solidity Smart Contracts");
1976        println!("  Domain: Blockchain");
1977        println!("  Tags: solidity, ethereum, smart contracts");
1978
1979        Ok(())
1980    }
1981
1982    /// Keyword-based search
1983    #[allow(dead_code)]
1984    pub async fn keyword_search() -> Result<()> {
1985        use crate::knowledge_base::{KnowledgeBase, KnowledgeDomain, KnowledgeEntry};
1986
1987        println!("=== Keyword-Based Search ===");
1988        println!();
1989
1990        let mut kb = KnowledgeBase::new();
1991
1992        // Add sample entries
1993        let entry = KnowledgeEntry {
1994            id: "rust-ownership".to_string(),
1995            domain: KnowledgeDomain::Programming,
1996            title: "Rust Ownership System".to_string(),
1997            content: "Rust's ownership system ensures memory safety...".to_string(),
1998            tags: vec![
1999                "rust".to_string(),
2000                "ownership".to_string(),
2001                "memory safety".to_string(),
2002            ],
2003            source: Some("https://doc.rust-lang.org/book/".to_string()),
2004            confidence: 0.98,
2005            created_at: chrono::Utc::now(),
2006            updated_at: chrono::Utc::now(),
2007        };
2008
2009        let _ = kb.add_entry(entry);
2010
2011        // Search
2012        let results = kb.search("rust ownership");
2013
2014        println!("Search: 'rust ownership'");
2015        println!("Found {} results", results.len());
2016        for entry in results {
2017            println!("  • {}", entry.title);
2018        }
2019
2020        Ok(())
2021    }
2022
2023    /// AI evaluator integration
2024    #[allow(dead_code)]
2025    pub async fn ai_integration(api_key: &str) -> Result<()> {
2026        use crate::ai_evaluator::AiEvaluator;
2027        use crate::knowledge_base::KnowledgeBase;
2028        use crate::llm::LlmClientBuilder;
2029
2030        println!("=== AI Evaluator + Knowledge Base ===");
2031        println!();
2032
2033        let _kb = KnowledgeBase::new();
2034        let llm = LlmClientBuilder::new()
2035            .openai_api_key(api_key)
2036            .build()
2037            .expect("Failed to build LLM client");
2038
2039        let _evaluator = AiEvaluator::new(llm);
2040
2041        println!("Integration workflow:");
2042        println!("  1. Search knowledge base for relevant info");
2043        println!("  2. Include in evaluation context");
2044        println!("  3. AI provides domain-aware feedback");
2045        println!();
2046        println!("Example:");
2047        println!("  Code: Solidity smart contract");
2048        println!("  KB: Retrieve Solidity best practices");
2049        println!("  AI: Evaluate against known patterns");
2050        println!("  Result: More accurate, context-aware review");
2051
2052        Ok(())
2053    }
2054
2055    /// Persistence (save/load)
2056    #[allow(dead_code)]
2057    pub async fn persistence() -> Result<()> {
2058        use crate::knowledge_base::KnowledgeBase;
2059
2060        println!("=== Knowledge Base Persistence ===");
2061        println!();
2062
2063        let kb = KnowledgeBase::new();
2064
2065        // Save to file
2066        kb.save_to_file("/tmp/knowledge_base.json")?;
2067        println!("✓ Saved to /tmp/knowledge_base.json");
2068
2069        // Load from file
2070        let _loaded_kb = KnowledgeBase::load_from_file("/tmp/knowledge_base.json")?;
2071        println!("✓ Loaded from /tmp/knowledge_base.json");
2072        println!();
2073        println!("Benefits:");
2074        println!("  • Preserve domain knowledge");
2075        println!("  • Share across team");
2076        println!("  • Version control");
2077        println!("  • Backup and restore");
2078
2079        Ok(())
2080    }
2081
2082    /// Multi-domain categorization
2083    #[allow(dead_code)]
2084    pub async fn multi_domain() -> Result<()> {
2085        use crate::knowledge_base::KnowledgeBase;
2086
2087        println!("=== Multi-Domain Knowledge Base ===");
2088        println!();
2089
2090        let _kb = KnowledgeBase::new();
2091
2092        println!("Supported domains:");
2093        println!("  • Programming (Rust, Python, JavaScript, etc.)");
2094        println!("  • Blockchain (Ethereum, Bitcoin, Solana, etc.)");
2095        println!("  • Security (OWASP, CVEs, best practices)");
2096        println!("  • MachineLearning (models, algorithms, frameworks)");
2097        println!("  • DevOps (CI/CD, containers, cloud)");
2098        println!("  • General (miscellaneous knowledge)");
2099        println!();
2100        println!("Query by domain:");
2101        println!("  let blockchain = kb.get_by_domain(KnowledgeDomain::Blockchain);");
2102        println!("  let security = kb.get_by_domain(KnowledgeDomain::Security);");
2103
2104        Ok(())
2105    }
2106}
2107
2108/// Performance profiling examples
2109///
2110/// This example demonstrates how to use the performance profiling utilities
2111/// to track, measure, and optimize AI service performance.
2112pub struct PerformanceProfilingExample;
2113
2114impl PerformanceProfilingExample {
2115    /// Basic operation profiling
2116    pub async fn basic_profiling() -> Result<()> {
2117        use crate::profiling::{OperationMetrics, PerformanceProfiler};
2118        use std::time::Duration;
2119
2120        println!("=== Basic Performance Profiling ===");
2121        println!();
2122
2123        let profiler = PerformanceProfiler::new();
2124
2125        // Record some operations
2126        println!("Recording operations...");
2127        for i in 0..5 {
2128            let metrics = OperationMetrics::new(
2129                "code_evaluation".to_string(),
2130                Duration::from_millis(100 + i * 50),
2131                true,
2132            )
2133            .with_tokens(1500)
2134            .with_cost(0.002);
2135
2136            profiler.record(metrics);
2137        }
2138
2139        // Get statistics
2140        if let Some(stats) = profiler.get_stats("code_evaluation") {
2141            println!("Code Evaluation Statistics:");
2142            println!("  Total operations: {}", stats.total_count);
2143            println!("  Success rate: {:.1}%", stats.success_rate * 100.0);
2144            println!("  Avg duration: {:.2}ms", stats.avg_duration.as_millis());
2145            println!("  Min duration: {:.2}ms", stats.min_duration.as_millis());
2146            println!("  Max duration: {:.2}ms", stats.max_duration.as_millis());
2147            println!("  Total tokens: {}", stats.total_tokens);
2148            println!("  Total cost: ${:.4}", stats.total_cost);
2149        }
2150
2151        Ok(())
2152    }
2153
2154    /// Scoped profiling with automatic timing
2155    pub async fn scoped_profiling() -> Result<()> {
2156        use crate::profiling::{PerformanceProfiler, ScopedProfiler};
2157        use std::sync::Arc;
2158
2159        println!("=== Scoped Profiling ===");
2160        println!();
2161
2162        let profiler = Arc::new(PerformanceProfiler::new());
2163
2164        println!("Profiling code evaluation...");
2165        {
2166            let _scope = ScopedProfiler::new("code_evaluation".to_string(), profiler.clone());
2167            // Simulate work
2168            tokio::time::sleep(std::time::Duration::from_millis(150)).await;
2169            // _scope automatically records when dropped
2170        }
2171
2172        println!("Profiling fraud detection...");
2173        {
2174            let _scope = ScopedProfiler::new("fraud_detection".to_string(), profiler.clone());
2175            tokio::time::sleep(std::time::Duration::from_millis(200)).await;
2176        }
2177
2178        // Generate report
2179        let report = profiler.generate_report();
2180        println!();
2181        println!("Performance Report:");
2182        println!("  Total operations: {}", report.total_operations);
2183        println!("  Total cost: ${:.4}", report.total_cost);
2184        println!("  Total tokens: {}", report.total_tokens);
2185        println!("  Operations by type: {}", report.operation_stats.len());
2186
2187        Ok(())
2188    }
2189
2190    /// Comparative performance analysis
2191    pub async fn comparative_analysis() -> Result<()> {
2192        use crate::profiling::{OperationMetrics, PerformanceProfiler};
2193        use std::time::Duration;
2194
2195        println!("=== Comparative Performance Analysis ===");
2196        println!();
2197
2198        let profiler = PerformanceProfiler::new();
2199
2200        // Simulate GPT-4 operations
2201        println!("Recording GPT-4 operations...");
2202        for _ in 0..10 {
2203            let metrics =
2204                OperationMetrics::new("gpt-4".to_string(), Duration::from_millis(200), true)
2205                    .with_tokens(2000)
2206                    .with_cost(0.04);
2207            profiler.record(metrics);
2208        }
2209
2210        // Simulate Claude operations
2211        println!("Recording Claude operations...");
2212        for _ in 0..10 {
2213            let metrics =
2214                OperationMetrics::new("claude".to_string(), Duration::from_millis(180), true)
2215                    .with_tokens(1800)
2216                    .with_cost(0.036);
2217            profiler.record(metrics);
2218        }
2219
2220        // Simulate Gemini operations
2221        println!("Recording Gemini operations...");
2222        for _ in 0..10 {
2223            let metrics =
2224                OperationMetrics::new("gemini".to_string(), Duration::from_millis(150), true)
2225                    .with_tokens(1900)
2226                    .with_cost(0.019);
2227            profiler.record(metrics);
2228        }
2229
2230        println!();
2231        println!("Performance Comparison:");
2232        println!("┌─────────┬──────────┬──────────┬────────────┬──────────┐");
2233        println!("│ Model   │ Avg Time │ Tokens   │ Total Cost │ Success  │");
2234        println!("├─────────┼──────────┼──────────┼────────────┼──────────┤");
2235
2236        for model in ["gpt-4", "claude", "gemini"] {
2237            if let Some(stats) = profiler.get_stats(model) {
2238                println!(
2239                    "│ {:<7} │ {:>6}ms │ {:>8} │ ${:>9.4} │ {:>7.1}% │",
2240                    model,
2241                    stats.avg_duration.as_millis(),
2242                    stats.total_tokens,
2243                    stats.total_cost,
2244                    stats.success_rate * 100.0
2245                );
2246            }
2247        }
2248        println!("└─────────┴──────────┴──────────┴────────────┴──────────┘");
2249
2250        println!();
2251        println!("Winner: Gemini (fastest + cheapest)");
2252
2253        Ok(())
2254    }
2255
2256    /// Cost tracking and optimization
2257    pub async fn cost_optimization() -> Result<()> {
2258        use crate::profiling::{OperationMetrics, PerformanceProfiler};
2259        use std::time::Duration;
2260
2261        println!("=== Cost Optimization Tracking ===");
2262        println!();
2263
2264        let profiler = PerformanceProfiler::new();
2265
2266        // Track operations over time
2267        println!("Tracking costs over 24 hours simulation...");
2268
2269        // Hour 1-8: Expensive operations
2270        for _ in 0..100 {
2271            let metrics =
2272                OperationMetrics::new("evaluation".to_string(), Duration::from_millis(200), true)
2273                    .with_tokens(2000)
2274                    .with_cost(0.04);
2275            profiler.record(metrics);
2276        }
2277
2278        let report_before = profiler.generate_report();
2279        println!("Before optimization:");
2280        println!("  Total cost: ${:.2}", report_before.total_cost);
2281        println!(
2282            "  Avg cost/op: ${:.4}",
2283            report_before.total_cost / report_before.total_operations as f64
2284        );
2285
2286        // Hour 9-24: Optimized operations (cheaper model, caching)
2287        for _ in 0..100 {
2288            let metrics = OperationMetrics::new(
2289                "evaluation_optimized".to_string(),
2290                Duration::from_millis(150),
2291                true,
2292            )
2293            .with_tokens(1800)
2294            .with_cost(0.018); // 55% cost reduction
2295            profiler.record(metrics);
2296        }
2297
2298        println!();
2299        println!("After optimization:");
2300        if let Some(stats) = profiler.get_stats("evaluation_optimized") {
2301            let avg_cost = if stats.total_count > 0 {
2302                stats.total_cost / stats.total_count as f64
2303            } else {
2304                0.0
2305            };
2306            println!("  Total cost: ${:.2}", stats.total_cost);
2307            println!("  Avg cost/op: ${avg_cost:.4}");
2308            println!("  Cost savings: 55%");
2309            println!("  Monthly projection: ${:.2}", stats.total_cost * 15.0); // 30 days worth
2310        }
2311
2312        println!();
2313        println!("Optimization strategies applied:");
2314        println!("  ✓ Switched to cheaper model for simple tasks");
2315        println!("  ✓ Implemented response caching");
2316        println!("  ✓ Batched similar requests");
2317
2318        Ok(())
2319    }
2320
2321    /// Real-time performance monitoring
2322    pub async fn realtime_monitoring() -> Result<()> {
2323        use crate::profiling::{OperationMetrics, PerformanceProfiler};
2324        use std::time::Duration;
2325
2326        println!("=== Real-time Performance Monitoring ===");
2327        println!();
2328
2329        let mut profiler = PerformanceProfiler::new();
2330        profiler.enable();
2331
2332        println!("Processing operations...");
2333        for i in 0..5 {
2334            let success = i < 4; // Last one fails
2335            let metrics = OperationMetrics::new(
2336                "api_call".to_string(),
2337                Duration::from_millis(100 + i * 30),
2338                success,
2339            )
2340            .with_tokens((1000 + i * 200) as u32)
2341            .with_cost(0.02 + i as f64 * 0.005);
2342
2343            if success {
2344                profiler.record(metrics);
2345            } else {
2346                let metrics = metrics.with_error("Rate limit exceeded".to_string());
2347                profiler.record(metrics);
2348            }
2349
2350            // Show live stats
2351            if let Some(stats) = profiler.get_stats("api_call") {
2352                println!(
2353                    "[{}] Requests: {} | Success: {:.1}% | Avg latency: {}ms | Cost: ${:.4}",
2354                    i + 1,
2355                    stats.total_count,
2356                    stats.success_rate * 100.0,
2357                    stats.avg_duration.as_millis(),
2358                    stats.total_cost
2359                );
2360            }
2361        }
2362
2363        println!();
2364        println!("Monitoring alerts:");
2365        if let Some(stats) = profiler.get_stats("api_call") {
2366            if stats.success_rate < 0.95 {
2367                println!("  ⚠ Warning: Success rate below 95%");
2368            }
2369            if stats.avg_duration.as_millis() > 150 {
2370                println!("  ⚠ Warning: Average latency above threshold");
2371            }
2372        }
2373
2374        Ok(())
2375    }
2376}
2377
2378/// Model version management examples
2379///
2380/// This example demonstrates how to track, compare, and manage
2381/// different AI model versions for optimal performance.
2382pub struct ModelVersionManagementExample;
2383
2384impl ModelVersionManagementExample {
2385    /// Basic model version tracking
2386    pub async fn basic_version_tracking() -> Result<()> {
2387        use crate::model_version::{ModelMetrics, ModelRegistry, ModelVersion};
2388
2389        println!("=== Basic Model Version Tracking ===");
2390        println!();
2391
2392        let mut registry = ModelRegistry::new();
2393
2394        // Register multiple model versions
2395        println!("Registering model versions...");
2396
2397        let gpt4_v1 = ModelVersion::new("gpt-4-turbo", "20240301", "GPT-4 Turbo March 2024");
2398        registry.register_version(gpt4_v1)?;
2399
2400        let gpt4_v2 = ModelVersion::new(
2401            "gpt-4-turbo",
2402            "20240601",
2403            "GPT-4 Turbo June 2024 - Improved reasoning",
2404        );
2405        registry.register_version(gpt4_v2)?;
2406
2407        println!("Registered versions:");
2408        for version in registry.get_model_versions("gpt-4-turbo") {
2409            println!("  • {} ({})", version.version, version.description);
2410        }
2411
2412        // Update metrics
2413        println!();
2414        println!("Recording performance metrics...");
2415        let metrics_v1 = ModelMetrics::new(94.5, 1500, 0.04);
2416        registry.update_metrics("gpt-4-turbo", "20240301", metrics_v1)?;
2417
2418        let metrics_v2 = ModelMetrics::new(96.2, 1400, 0.042);
2419        registry.update_metrics("gpt-4-turbo", "20240601", metrics_v2)?;
2420
2421        println!("Metrics updated successfully");
2422
2423        Ok(())
2424    }
2425
2426    /// Version comparison and recommendation
2427    pub async fn version_comparison() -> Result<()> {
2428        use crate::model_version::{ModelMetrics, ModelRegistry, ModelVersion};
2429
2430        println!("=== Model Version Comparison ===");
2431        println!();
2432
2433        let mut registry = ModelRegistry::new();
2434
2435        // Register Claude versions
2436        let claude_opus =
2437            ModelVersion::new("claude-3-opus", "20240229", "Claude 3 Opus - Most capable");
2438        registry.register_version(claude_opus)?;
2439
2440        let claude_sonnet = ModelVersion::new(
2441            "claude-3-5-sonnet",
2442            "20241022",
2443            "Claude 3.5 Sonnet - Balanced",
2444        );
2445        registry.register_version(claude_sonnet)?;
2446
2447        let claude_haiku =
2448            ModelVersion::new("claude-3-haiku", "20240307", "Claude 3 Haiku - Fastest");
2449        registry.register_version(claude_haiku)?;
2450
2451        // Add realistic metrics
2452        registry.update_metrics(
2453            "claude-3-opus",
2454            "20240229",
2455            ModelMetrics::new(97.8, 2000, 0.075),
2456        )?;
2457
2458        registry.update_metrics(
2459            "claude-3-5-sonnet",
2460            "20241022",
2461            ModelMetrics::new(96.5, 1800, 0.045),
2462        )?;
2463
2464        registry.update_metrics(
2465            "claude-3-haiku",
2466            "20240307",
2467            ModelMetrics::new(93.2, 1200, 0.0125),
2468        )?;
2469
2470        // Compare versions
2471        println!("Claude Model Comparison:");
2472        println!("┌──────────────────┬──────────┬────────┬──────────┬─────────────┐");
2473        println!("│ Model            │ Accuracy │ Tokens │ Avg Cost │ Cost/Acc    │");
2474        println!("├──────────────────┼──────────┼────────┼──────────┼─────────────┤");
2475
2476        for (model, version) in [
2477            ("claude-3-opus", "20240229"),
2478            ("claude-3-5-sonnet", "20241022"),
2479            ("claude-3-haiku", "20240307"),
2480        ] {
2481            if let Some(v) = registry.get_version(model, version) {
2482                println!(
2483                    "│ {:<16} │ {:>7.1}% │ {:>6} │ ${:>7.4} │ {:>11.2} │",
2484                    model,
2485                    v.metrics.accuracy,
2486                    v.metrics.avg_tokens,
2487                    v.metrics.avg_cost,
2488                    v.metrics.cost_effectiveness()
2489                );
2490            }
2491        }
2492        println!("└──────────────────┴──────────┴────────┴──────────┴─────────────┘");
2493
2494        println!();
2495        println!("Recommendations:");
2496        println!("  • For maximum accuracy: claude-3-opus (97.8%)");
2497        println!("  • For best value: claude-3-5-sonnet (2144 cost-effectiveness)");
2498        println!("  • For lowest cost: claude-3-haiku ($0.0125/request)");
2499
2500        Ok(())
2501    }
2502
2503    /// Model performance over time
2504    pub async fn performance_tracking() -> Result<()> {
2505        use crate::model_version::{ModelMetrics, ModelRegistry, ModelVersion};
2506
2507        println!("=== Model Performance Over Time ===");
2508        println!();
2509
2510        let mut registry = ModelRegistry::new();
2511
2512        let gpt4 = ModelVersion::new("gpt-4-turbo", "20240801", "GPT-4 Turbo August 2024");
2513        registry.register_version(gpt4)?;
2514
2515        println!("Tracking performance over time...");
2516        println!();
2517
2518        // Simulate performance data collection
2519        let mut metrics = ModelMetrics::new(95.0, 1500, 0.04);
2520
2521        // Week 1
2522        for _ in 0..100 {
2523            metrics.record_request(1500, 0.04, 200.0, true);
2524        }
2525        println!(
2526            "Week 1: Accuracy={:.1}%, Requests={}, Cost=${:.2}",
2527            metrics.accuracy,
2528            metrics.total_requests,
2529            metrics.avg_cost * metrics.total_requests as f64
2530        );
2531
2532        // Week 2 - some errors
2533        for i in 0..100 {
2534            let success = i % 10 != 0; // 10% error rate
2535            metrics.record_request(1500, 0.04, 220.0, success);
2536        }
2537        println!(
2538            "Week 2: Accuracy={:.1}%, Requests={}, Errors={}",
2539            metrics.accuracy, metrics.total_requests, metrics.total_errors
2540        );
2541
2542        // Week 3 - optimized
2543        for _ in 0..100 {
2544            metrics.record_request(1400, 0.038, 180.0, true);
2545        }
2546        println!(
2547            "Week 3: Accuracy={:.1}%, Avg Latency={:.1}ms, Cost=${:.4}",
2548            metrics.accuracy, metrics.avg_latency_ms, metrics.avg_cost
2549        );
2550
2551        registry.update_metrics("gpt-4-turbo", "20240801", metrics)?;
2552
2553        println!();
2554        println!("Analysis:");
2555        if let Some(v) = registry.get_version("gpt-4-turbo", "20240801") {
2556            println!("  Total requests: {}", v.metrics.total_requests);
2557            println!("  Error rate: {:.2}%", v.metrics.error_rate());
2558            println!("  Average latency: {:.1}ms", v.metrics.avg_latency_ms);
2559            println!(
2560                "  Cost effectiveness: {:.2}",
2561                v.metrics.cost_effectiveness()
2562            );
2563        }
2564
2565        Ok(())
2566    }
2567
2568    /// A/B testing model versions
2569    pub async fn ab_testing() -> Result<()> {
2570        use crate::model_version::{ModelMetrics, ModelRegistry, ModelVersion};
2571
2572        println!("=== A/B Testing Model Versions ===");
2573        println!();
2574
2575        let mut registry = ModelRegistry::new();
2576
2577        // Register two versions for testing
2578        let version_a = ModelVersion::new("custom-model", "v1.0", "Baseline model");
2579        registry.register_version(version_a)?;
2580
2581        let version_b = ModelVersion::new(
2582            "custom-model",
2583            "v1.1",
2584            "Optimized model with new training data",
2585        );
2586        registry.register_version(version_b)?;
2587
2588        println!("A/B Test: custom-model v1.0 vs v1.1");
2589        println!();
2590
2591        // Simulate A/B test traffic split (50/50)
2592        let mut metrics_a = ModelMetrics::new(94.0, 1600, 0.035);
2593        let mut metrics_b = ModelMetrics::new(95.5, 1550, 0.034);
2594
2595        println!("Running A/B test with 1000 requests...");
2596        for _ in 0..500 {
2597            metrics_a.record_request(1600, 0.035, 195.0, true);
2598            metrics_b.record_request(1550, 0.034, 185.0, true);
2599        }
2600
2601        registry.update_metrics("custom-model", "v1.0", metrics_a)?;
2602        registry.update_metrics("custom-model", "v1.1", metrics_b)?;
2603
2604        println!();
2605        println!("A/B Test Results:");
2606        println!("┌───────────┬──────────┬──────────┬──────────┬────────────┐");
2607        println!("│ Version   │ Accuracy │ Latency  │ Avg Cost │ Recommend  │");
2608        println!("├───────────┼──────────┼──────────┼──────────┼────────────┤");
2609
2610        if let Some(v1) = registry.get_version("custom-model", "v1.0") {
2611            println!(
2612                "│ v1.0 (A)  │ {:>7.1}% │ {:>6.0}ms │ ${:>6.4} │            │",
2613                v1.metrics.accuracy, v1.metrics.avg_latency_ms, v1.metrics.avg_cost
2614            );
2615        }
2616        if let Some(v2) = registry.get_version("custom-model", "v1.1") {
2617            println!(
2618                "│ v1.1 (B)  │ {:>7.1}% │ {:>6.0}ms │ ${:>6.4} │ ✓ Winner   │",
2619                v2.metrics.accuracy, v2.metrics.avg_latency_ms, v2.metrics.avg_cost
2620            );
2621        }
2622        println!("└───────────┴──────────┴──────────┴──────────┴────────────┘");
2623
2624        println!();
2625        println!("Decision: Promote v1.1 to production");
2626        println!("Improvements:");
2627        println!("  • +1.5% accuracy improvement");
2628        println!("  • -10ms latency reduction");
2629        println!("  • -2.9% cost reduction");
2630
2631        // Set active version
2632        registry.set_active_version("custom-model", "v1.1")?;
2633        println!();
2634        println!("✓ v1.1 is now the active version");
2635
2636        Ok(())
2637    }
2638
2639    /// Model deprecation workflow
2640    pub async fn deprecation_workflow() -> Result<()> {
2641        use crate::model_version::{ModelRegistry, ModelVersion};
2642
2643        println!("=== Model Deprecation Workflow ===");
2644        println!();
2645
2646        let mut registry = ModelRegistry::new();
2647
2648        // Register old and new versions
2649        let old_version = ModelVersion::new("gpt-3.5-turbo", "0301", "GPT-3.5 Turbo - March 2023");
2650        registry.register_version(old_version)?;
2651
2652        let new_version = ModelVersion::new("gpt-4-turbo", "20240401", "GPT-4 Turbo - April 2024");
2653        registry.register_version(new_version)?;
2654
2655        println!("Models registered:");
2656        println!("  • gpt-3.5-turbo (0301) - Old version");
2657        println!("  • gpt-4-turbo (20240401) - New version");
2658
2659        // Mark old version as deprecated
2660        println!();
2661        println!("Deprecating gpt-3.5-turbo...");
2662        registry.deprecate_version("gpt-3.5-turbo", "0301")?;
2663
2664        println!("✓ Model marked as deprecated");
2665        println!();
2666        println!("Migration path:");
2667        println!("  1. Update applications to use gpt-4-turbo");
2668        println!("  2. Monitor performance metrics");
2669        println!("  3. Gradual traffic shift (0% → 100%)");
2670        println!("  4. Remove deprecated model after 90 days");
2671
2672        // Set new version as active
2673        registry.set_active_version("gpt-4-turbo", "20240401")?;
2674
2675        println!();
2676        println!("Active models:");
2677        if let Some(active) = registry.get_active_version("gpt-4-turbo") {
2678            println!("  ✓ {} - {}", active.version, active.description);
2679        }
2680
2681        println!();
2682        println!("Deprecated models:");
2683        println!("  ⚠ gpt-3.5-turbo (0301) - Deprecated");
2684
2685        Ok(())
2686    }
2687}
2688
2689/// Example: Professional report generation
2690///
2691/// Demonstrates how to:
2692/// - Generate cost analysis reports
2693/// - Create performance benchmark reports
2694/// - Produce fraud detection summaries
2695/// - Export reports in multiple formats (Markdown, JSON, CSV)
2696pub struct ReportGenerationExample;
2697
2698impl ReportGenerationExample {
2699    /// Generate a comprehensive cost analysis report
2700    pub async fn cost_analysis_report() -> Result<()> {
2701        use crate::reports::{CostAnalysisReport, ReportFormat, ReportGenerator, ReportType};
2702
2703        println!("=== Cost Analysis Report Generation ===");
2704        println!();
2705
2706        // Create a cost analysis report
2707        let mut report = CostAnalysisReport::new(
2708            "AI Operations Cost Analysis - Q1 2026".to_string(),
2709            "January - March 2026".to_string(),
2710        );
2711
2712        // Add provider costs
2713        report.add_provider_cost("OpenAI".to_string(), 1_850.50);
2714        report.add_provider_cost("Anthropic".to_string(), 1_420.75);
2715        report.add_provider_cost("Gemini".to_string(), 345.00);
2716        report.add_provider_cost("DeepSeek".to_string(), 125.25);
2717        report.add_provider_cost("Ollama".to_string(), 0.0);
2718
2719        // Add operation costs
2720        report.add_operation_cost("Code Evaluation".to_string(), 1_680.00);
2721        report.add_operation_cost("Commitment Verification".to_string(), 895.75);
2722        report.add_operation_cost("Fraud Detection".to_string(), 540.50);
2723        report.add_operation_cost("Plagiarism Detection".to_string(), 425.25);
2724        report.add_operation_cost("Document Analysis".to_string(), 200.00);
2725
2726        // Set request metrics
2727        report.set_total_requests(15_450);
2728        report.cost_trend = Some(-12.5); // 12.5% cost reduction
2729
2730        // Add recommendations
2731        report.add_recommendation(
2732            "Switch 40% of simple tasks to Gemini Flash to reduce costs by ~$600/month".to_string(),
2733        );
2734        report.add_recommendation(
2735            "Use Ollama for development and testing to eliminate dev environment costs".to_string(),
2736        );
2737        report.add_recommendation(
2738            "Implement request batching to reduce API calls by 25%".to_string(),
2739        );
2740        report.add_recommendation(
2741            "Enable caching for repetitive operations (estimated 15% cost savings)".to_string(),
2742        );
2743
2744        println!("Report Summary:");
2745        println!("  • Total Cost: ${:.2}", report.total_cost);
2746        println!("  • Total Requests: {}", report.total_requests);
2747        println!("  • Avg Cost/Request: ${:.4}", report.avg_cost_per_request);
2748        println!("  • Cost Trend: ↓ 12.5%");
2749        println!();
2750
2751        // Generate Markdown report
2752        println!("=== Markdown Report ===");
2753        let markdown = ReportGenerator::generate(
2754            ReportType::CostAnalysis(report.clone()),
2755            ReportFormat::Markdown,
2756        )?;
2757        println!("{markdown}");
2758
2759        // Generate JSON report
2760        println!("=== JSON Report (Preview) ===");
2761        let json = ReportGenerator::generate(
2762            ReportType::CostAnalysis(report.clone()),
2763            ReportFormat::Json,
2764        )?;
2765        println!("{}...{}", &json[..200], &json[json.len() - 50..]);
2766        println!();
2767
2768        // Generate CSV report
2769        println!("=== CSV Report ===");
2770        let csv = ReportGenerator::generate(ReportType::CostAnalysis(report), ReportFormat::Csv)?;
2771        let lines: Vec<&str> = csv.lines().take(8).collect();
2772        println!("{}", lines.join("\n"));
2773        println!("...");
2774        println!();
2775
2776        println!("✓ Cost analysis report generated in 3 formats");
2777
2778        Ok(())
2779    }
2780
2781    /// Generate a performance benchmark report
2782    pub async fn performance_benchmark_report() -> Result<()> {
2783        use crate::reports::{
2784            OperationBenchmark, PerformanceBenchmarkReport, ReportFormat, ReportGenerator,
2785            ReportType,
2786        };
2787
2788        println!("=== Performance Benchmark Report Generation ===");
2789        println!();
2790
2791        let mut report = PerformanceBenchmarkReport::new(
2792            "AI Services Performance Benchmarks".to_string(),
2793            "2026-01-09".to_string(),
2794        );
2795
2796        // Add operation benchmarks
2797        report.add_operation(
2798            "code_evaluation".to_string(),
2799            OperationBenchmark {
2800                name: "Code Evaluation".to_string(),
2801                avg_latency_ms: 285.5,
2802                median_latency_ms: 265.0,
2803                p95_latency_ms: 420.0,
2804                p99_latency_ms: 580.0,
2805                total_ops: 5_420,
2806                success_rate: 99.2,
2807            },
2808        );
2809
2810        report.add_operation(
2811            "commitment_verification".to_string(),
2812            OperationBenchmark {
2813                name: "Commitment Verification".to_string(),
2814                avg_latency_ms: 320.2,
2815                median_latency_ms: 295.0,
2816                p95_latency_ms: 495.0,
2817                p99_latency_ms: 650.0,
2818                total_ops: 3_850,
2819                success_rate: 98.7,
2820            },
2821        );
2822
2823        report.add_operation(
2824            "fraud_detection".to_string(),
2825            OperationBenchmark {
2826                name: "Fraud Detection".to_string(),
2827                avg_latency_ms: 195.8,
2828                median_latency_ms: 180.0,
2829                p95_latency_ms: 280.0,
2830                p99_latency_ms: 385.0,
2831                total_ops: 2_240,
2832                success_rate: 99.5,
2833            },
2834        );
2835
2836        report.add_operation(
2837            "plagiarism_detection".to_string(),
2838            OperationBenchmark {
2839                name: "Plagiarism Detection".to_string(),
2840                avg_latency_ms: 425.3,
2841                median_latency_ms: 390.0,
2842                p95_latency_ms: 620.0,
2843                p99_latency_ms: 820.0,
2844                total_ops: 1_650,
2845                success_rate: 97.8,
2846            },
2847        );
2848
2849        report.add_operation(
2850            "document_analysis".to_string(),
2851            OperationBenchmark {
2852                name: "Document Analysis".to_string(),
2853                avg_latency_ms: 520.7,
2854                median_latency_ms: 475.0,
2855                p95_latency_ms: 780.0,
2856                p99_latency_ms: 1050.0,
2857                total_ops: 890,
2858                success_rate: 98.3,
2859            },
2860        );
2861
2862        // Calculate summary statistics
2863        report.calculate_summary();
2864
2865        println!("Performance Summary:");
2866        println!("  • Total Operations: {}", report.summary.total_operations);
2867        println!(
2868            "  • Overall Avg Latency: {:.2}ms",
2869            report.summary.overall_avg_latency_ms
2870        );
2871        println!(
2872            "  • Overall Success Rate: {:.1}%",
2873            report.summary.overall_success_rate
2874        );
2875        if let Some(ref fastest) = report.summary.fastest_operation {
2876            println!("  • Fastest Operation: {fastest}");
2877        }
2878        if let Some(ref slowest) = report.summary.slowest_operation {
2879            println!("  • Slowest Operation: {slowest}");
2880        }
2881        println!();
2882
2883        // Generate Markdown report
2884        println!("=== Markdown Report ===");
2885        let markdown = ReportGenerator::generate(
2886            ReportType::PerformanceBenchmark(report.clone()),
2887            ReportFormat::Markdown,
2888        )?;
2889        println!("{markdown}");
2890
2891        // Generate JSON report
2892        println!("=== JSON Report (Preview) ===");
2893        let json = ReportGenerator::generate(
2894            ReportType::PerformanceBenchmark(report),
2895            ReportFormat::Json,
2896        )?;
2897        let lines: Vec<&str> = json.lines().take(10).collect();
2898        println!("{}", lines.join("\n"));
2899        println!("...");
2900        println!();
2901
2902        println!("✓ Performance benchmark report generated");
2903
2904        Ok(())
2905    }
2906
2907    /// Generate a fraud detection summary report
2908    pub async fn fraud_summary_report() -> Result<()> {
2909        use crate::fraud::RiskLevel;
2910        use crate::reports::{FraudSummaryReport, ReportFormat, ReportGenerator, ReportType};
2911
2912        println!("=== Fraud Detection Summary Report Generation ===");
2913        println!();
2914
2915        let mut report = FraudSummaryReport::new(
2916            "Fraud Detection Analysis - Q1 2026".to_string(),
2917            "January - March 2026".to_string(),
2918        );
2919
2920        // Add fraud cases
2921        for _ in 0..125 {
2922            report.add_case(RiskLevel::Low);
2923        }
2924        for _ in 0..48 {
2925            report.add_case(RiskLevel::Medium);
2926        }
2927        for _ in 0..23 {
2928            report.add_case(RiskLevel::High);
2929        }
2930        for _ in 0..7 {
2931            report.add_case(RiskLevel::Critical);
2932        }
2933
2934        // Set common fraud types
2935        report.set_common_fraud_types(vec![
2936            ("Sybil Attack".to_string(), 45),
2937            ("Wash Trading".to_string(), 28),
2938            ("Reputation Gaming".to_string(), 22),
2939            ("Image Manipulation".to_string(), 18),
2940            ("Content Plagiarism".to_string(), 15),
2941        ]);
2942
2943        // Set detection accuracy
2944        report.accuracy = Some(96.8);
2945
2946        // Add insights
2947        report.add_insight(
2948            "Sybil attacks increased by 32% compared to Q4 2026, primarily from newly registered accounts".to_string(),
2949        );
2950        report.add_insight(
2951            "Average detection time improved by 45% due to enhanced ML models".to_string(),
2952        );
2953        report.add_insight(
2954            "Image manipulation attempts decreased by 18% after implementing perceptual hashing"
2955                .to_string(),
2956        );
2957        report.add_insight(
2958            "Critical risk cases require average 15 minutes for manual review and final decision"
2959                .to_string(),
2960        );
2961
2962        println!("Fraud Detection Summary:");
2963        println!("  • Total Cases Analyzed: {}", report.total_cases);
2964        println!("  • Detection Accuracy: {:.1}%", report.accuracy.unwrap());
2965        println!("  • Most Common: Sybil Attack (45 cases)");
2966        println!("  • Critical Cases: 7");
2967        println!();
2968
2969        // Generate Markdown report
2970        println!("=== Markdown Report ===");
2971        let markdown = ReportGenerator::generate(
2972            ReportType::FraudSummary(report.clone()),
2973            ReportFormat::Markdown,
2974        )?;
2975        println!("{markdown}");
2976
2977        // Generate JSON report
2978        println!("=== JSON Report (Preview) ===");
2979        let json = ReportGenerator::generate(ReportType::FraudSummary(report), ReportFormat::Json)?;
2980        let lines: Vec<&str> = json.lines().take(15).collect();
2981        println!("{}", lines.join("\n"));
2982        println!("...");
2983        println!();
2984
2985        println!("✓ Fraud detection summary report generated");
2986
2987        Ok(())
2988    }
2989
2990    /// Demonstrate multi-format report export
2991    pub async fn multi_format_export() -> Result<()> {
2992        use crate::reports::{CostAnalysisReport, ReportFormat, ReportGenerator, ReportType};
2993
2994        println!("=== Multi-Format Report Export ===");
2995        println!();
2996
2997        let mut report = CostAnalysisReport::new(
2998            "Weekly Cost Report".to_string(),
2999            "Week of Jan 9, 2026".to_string(),
3000        );
3001
3002        report.add_provider_cost("OpenAI".to_string(), 425.00);
3003        report.add_provider_cost("Anthropic".to_string(), 325.00);
3004        report.add_provider_cost("Gemini".to_string(), 85.00);
3005        report.set_total_requests(3_500);
3006
3007        println!("Exporting report in 3 formats...");
3008        println!();
3009
3010        // Export as Markdown
3011        let markdown = ReportGenerator::generate(
3012            ReportType::CostAnalysis(report.clone()),
3013            ReportFormat::Markdown,
3014        )?;
3015        println!("✓ Markdown export: {} bytes", markdown.len());
3016        println!("  Use case: Documentation, GitHub issues, Slack messages");
3017        println!();
3018
3019        // Export as JSON
3020        let json = ReportGenerator::generate(
3021            ReportType::CostAnalysis(report.clone()),
3022            ReportFormat::Json,
3023        )?;
3024        println!("✓ JSON export: {} bytes", json.len());
3025        println!("  Use case: API responses, data storage, dashboard integration");
3026        println!();
3027
3028        // Export as CSV
3029        let csv = ReportGenerator::generate(ReportType::CostAnalysis(report), ReportFormat::Csv)?;
3030        println!("✓ CSV export: {} bytes", csv.len());
3031        println!("  Use case: Excel analysis, data science, financial reporting");
3032        println!();
3033
3034        println!("All reports generated successfully!");
3035        println!();
3036        println!("Integration tips:");
3037        println!("  • Save to files: write reports to disk for archival");
3038        println!("  • Send via email: attach reports to automated email notifications");
3039        println!("  • Post to Slack: use Markdown format for rich formatting");
3040        println!("  • Store in DB: save JSON format for queryable storage");
3041        println!("  • Generate dashboards: parse JSON data for real-time visualizations");
3042
3043        Ok(())
3044    }
3045
3046    /// Demonstrate automated report scheduling
3047    pub async fn automated_reporting() -> Result<()> {
3048        use crate::reports::{CostAnalysisReport, ReportFormat, ReportGenerator, ReportType};
3049
3050        println!("=== Automated Report Scheduling ===");
3051        println!();
3052
3053        println!("Example: Daily cost monitoring workflow");
3054        println!();
3055
3056        // Simulate daily report generation
3057        let mut report = CostAnalysisReport::new(
3058            "Daily Cost Report".to_string(),
3059            format!("Date: {}", chrono::Local::now().format("%Y-%m-%d")),
3060        );
3061
3062        report.add_provider_cost("OpenAI".to_string(), 125.50);
3063        report.add_provider_cost("Anthropic".to_string(), 95.25);
3064        report.set_total_requests(1_250);
3065        report.cost_trend = Some(5.2); // 5.2% increase
3066
3067        // Generate alert if cost exceeds threshold
3068        if report.total_cost > 200.0 {
3069            println!("⚠ ALERT: Daily cost exceeds $200 threshold!");
3070            println!("  Current: ${:.2}", report.total_cost);
3071            println!("  Trend: +{:.1}%", report.cost_trend.unwrap());
3072            println!();
3073        }
3074
3075        // Add automated recommendations
3076        if let Some(trend) = report.cost_trend {
3077            if trend > 10.0 {
3078                report.add_recommendation(
3079                    "Cost increased significantly. Review recent usage patterns".to_string(),
3080                );
3081            }
3082        }
3083
3084        let _markdown =
3085            ReportGenerator::generate(ReportType::CostAnalysis(report), ReportFormat::Markdown)?;
3086
3087        println!("Report generated and ready for distribution:");
3088        println!("  ✓ Post to Slack #finance channel");
3089        println!("  ✓ Email to finance@company.com");
3090        println!("  ✓ Archive in S3 bucket");
3091        println!("  ✓ Update monitoring dashboard");
3092        println!();
3093
3094        println!("Scheduling options:");
3095        println!("  • Use cron: Schedule daily/weekly/monthly reports");
3096        println!("  • Use tokio interval: In-app scheduled reporting");
3097        println!("  • Use GitHub Actions: Automated CI/CD reports");
3098        println!("  • Use AWS Lambda: Serverless scheduled reports");
3099
3100        Ok(())
3101    }
3102}
3103
3104/// Example: Dashboard integration and monitoring
3105///
3106/// Demonstrates how to:
3107/// - Export metrics to Prometheus
3108/// - Integrate with Grafana
3109/// - Generate health check endpoints
3110/// - Monitor AI operations in real-time
3111pub struct DashboardIntegrationExample;
3112
3113impl DashboardIntegrationExample {
3114    /// Export metrics to Prometheus format
3115    pub async fn prometheus_export() -> Result<()> {
3116        use crate::dashboard::DashboardMetrics;
3117
3118        println!("=== Prometheus Metrics Export ===");
3119        println!();
3120
3121        // Create dashboard metrics
3122        let mut metrics = DashboardMetrics::new();
3123        metrics.request_count = 15_450;
3124        metrics.total_cost = 3_741.50;
3125        metrics.avg_latency_ms = 285.5;
3126        metrics.error_count = 127;
3127        metrics.success_rate = 99.18;
3128        metrics.cache_hit_rate = 42.5;
3129        metrics.circuit_breaker_open = 3;
3130        metrics.budget_utilization = 67.8;
3131        metrics.active_providers = vec![
3132            "openai".to_string(),
3133            "anthropic".to_string(),
3134            "gemini".to_string(),
3135        ];
3136
3137        // Add custom metrics
3138        metrics.add_custom_metric("fraud_detection_accuracy".to_string(), 96.8);
3139        metrics.add_custom_metric("plagiarism_checks_total".to_string(), 1_250.0);
3140
3141        println!("Current Metrics:");
3142        println!("  • Total Requests: {}", metrics.request_count);
3143        println!("  • Total Cost: ${:.2}", metrics.total_cost);
3144        println!("  • Avg Latency: {:.2}ms", metrics.avg_latency_ms);
3145        println!("  • Success Rate: {:.2}%", metrics.success_rate);
3146        println!("  • Cache Hit Rate: {:.2}%", metrics.cache_hit_rate);
3147        println!();
3148
3149        // Export to Prometheus format
3150        let prometheus_metrics = metrics.to_prometheus();
3151
3152        println!("=== Prometheus Format Output ===");
3153        println!();
3154        for metric in prometheus_metrics.iter().take(3) {
3155            println!("{}", metric.to_prometheus_format());
3156        }
3157        println!("... {} more metrics", prometheus_metrics.len() - 3);
3158        println!();
3159
3160        println!("Integration:");
3161        println!("  • Expose this endpoint at /metrics");
3162        println!("  • Configure Prometheus to scrape this endpoint");
3163        println!("  • Default scrape interval: 15s");
3164        println!();
3165
3166        println!("Example Prometheus Configuration:");
3167        println!("```yaml");
3168        println!("scrape_configs:");
3169        println!("  - job_name: 'kaccy-ai'");
3170        println!("    static_configs:");
3171        println!("      - targets: ['localhost:8080']");
3172        println!("    metrics_path: '/metrics'");
3173        println!("    scrape_interval: 15s");
3174        println!("```");
3175
3176        Ok(())
3177    }
3178
3179    /// Integrate with Grafana dashboard
3180    pub async fn grafana_integration() -> Result<()> {
3181        use crate::dashboard::{DashboardMetrics, to_grafana_format};
3182
3183        println!("=== Grafana Dashboard Integration ===");
3184        println!();
3185
3186        let mut metrics = DashboardMetrics::new();
3187        metrics.request_count = 8_500;
3188        metrics.total_cost = 1_847.25;
3189        metrics.avg_latency_ms = 320.5;
3190        metrics.success_rate = 98.9;
3191        metrics.cache_hit_rate = 38.2;
3192        metrics.budget_utilization = 52.1;
3193
3194        // Convert to Grafana format
3195        let datapoints = to_grafana_format(&metrics);
3196
3197        println!("Grafana Data Points (JSON Simple Format):");
3198        println!();
3199        for dp in datapoints.iter().take(3) {
3200            let json = dp.to_json()?;
3201            let lines: Vec<&str> = json.lines().take(5).collect();
3202            println!("{}", lines.join("\n"));
3203            println!("  ...");
3204            println!();
3205        }
3206
3207        println!("Dashboard Setup:");
3208        println!("  1. Add JSON data source in Grafana");
3209        println!("  2. Configure endpoint: http://your-api/grafana-metrics");
3210        println!("  3. Set refresh interval: 10s");
3211        println!("  4. Create panels for each metric");
3212        println!();
3213
3214        println!("Recommended Grafana Panels:");
3215        println!("  • Request Rate (Time Series)");
3216        println!("  • Cost Tracking (Stat + Time Series)");
3217        println!("  • Latency Distribution (Histogram)");
3218        println!("  • Success Rate (Gauge)");
3219        println!("  • Cache Hit Rate (Bar Gauge)");
3220        println!("  • Budget Utilization (Gauge with thresholds)");
3221
3222        Ok(())
3223    }
3224
3225    /// Health check endpoint for monitoring
3226    pub async fn health_check_endpoint() -> Result<()> {
3227        use crate::dashboard::{ComponentHealth, HealthCheckStatus};
3228        use std::time::{SystemTime, UNIX_EPOCH};
3229
3230        println!("=== Health Check Endpoint ===");
3231        println!();
3232
3233        let mut status = HealthCheckStatus::new();
3234
3235        // Check LLM client health
3236        let llm_health = ComponentHealth {
3237            healthy: true,
3238            last_check: SystemTime::now()
3239                .duration_since(UNIX_EPOCH)
3240                .unwrap()
3241                .as_secs(),
3242            response_time_ms: Some(45.2),
3243            error: None,
3244        };
3245        status.add_component("llm_client".to_string(), llm_health);
3246
3247        // Check cache health
3248        let cache_health = ComponentHealth {
3249            healthy: true,
3250            last_check: SystemTime::now()
3251                .duration_since(UNIX_EPOCH)
3252                .unwrap()
3253                .as_secs(),
3254            response_time_ms: Some(2.1),
3255            error: None,
3256        };
3257        status.add_component("cache".to_string(), cache_health);
3258
3259        // Check database health (simulating degraded state)
3260        let db_health = ComponentHealth {
3261            healthy: true,
3262            last_check: SystemTime::now()
3263                .duration_since(UNIX_EPOCH)
3264                .unwrap()
3265                .as_secs(),
3266            response_time_ms: Some(125.8),
3267            error: None,
3268        };
3269        status.add_component("database".to_string(), db_health);
3270
3271        println!("Health Check Status:");
3272        println!(
3273            "  • Overall: {}",
3274            if status.healthy {
3275                "✓ HEALTHY"
3276            } else {
3277                "✗ UNHEALTHY"
3278            }
3279        );
3280        println!("  • Components: {}", status.components.len());
3281        println!();
3282
3283        let json = status.to_json()?;
3284        println!("=== JSON Response ===");
3285        let lines: Vec<&str> = json.lines().take(15).collect();
3286        println!("{}", lines.join("\n"));
3287        println!("  ...");
3288        println!();
3289
3290        println!("Integration:");
3291        println!("  • Expose at /health or /healthz");
3292        println!("  • Used by Kubernetes liveness/readiness probes");
3293        println!("  • Monitored by uptime services (StatusPage, Pingdom)");
3294        println!("  • Return HTTP 200 if healthy, 503 if unhealthy");
3295
3296        Ok(())
3297    }
3298
3299    /// Real-time metrics monitoring
3300    pub async fn realtime_monitoring() -> Result<()> {
3301        use crate::dashboard::DashboardMetrics;
3302
3303        println!("=== Real-Time Metrics Monitoring ===");
3304        println!();
3305
3306        println!("Simulating real-time metric updates...");
3307        println!();
3308
3309        // Simulate collecting metrics over time
3310        let mut metrics_history: Vec<DashboardMetrics> = Vec::new();
3311
3312        for i in 0..5 {
3313            let mut metrics = DashboardMetrics::new();
3314            metrics.request_count = 1000 + i * 250;
3315            metrics.total_cost = 250.0 + (i as f64 * 62.5);
3316            metrics.avg_latency_ms = 280.0 + (i as f64 * 10.0);
3317            metrics.success_rate = 99.5 - (i as f64 * 0.1);
3318            metrics.cache_hit_rate = 40.0 + (i as f64 * 2.5);
3319
3320            metrics_history.push(metrics);
3321        }
3322
3323        println!("┌─────────┬───────────┬────────────┬────────────┬─────────────┐");
3324        println!("│ Time    │ Requests  │ Cost       │ Latency    │ Success %   │");
3325        println!("├─────────┼───────────┼────────────┼────────────┼─────────────┤");
3326
3327        for (i, m) in metrics_history.iter().enumerate() {
3328            println!(
3329                "│ T+{}min  │ {:>9} │ ${:>9.2} │ {:>8.1}ms │ {:>10.2}% │",
3330                i * 5,
3331                m.request_count,
3332                m.total_cost,
3333                m.avg_latency_ms,
3334                m.success_rate
3335            );
3336        }
3337        println!("└─────────┴───────────┴────────────┴────────────┴─────────────┘");
3338        println!();
3339
3340        println!("Monitoring Strategy:");
3341        println!("  • Update metrics every 10-30 seconds");
3342        println!("  • Store historical data for trend analysis");
3343        println!("  • Alert on threshold violations:");
3344        println!("    - Success rate < 95%");
3345        println!("    - Latency > 500ms (P95)");
3346        println!("    - Cost growth > 20% per hour");
3347        println!("    - Error rate > 5%");
3348        println!();
3349
3350        println!("Alert Channels:");
3351        println!("  • PagerDuty for critical alerts");
3352        println!("  • Slack for warnings");
3353        println!("  • Email for daily summaries");
3354        println!("  • Webhook for custom integrations");
3355
3356        Ok(())
3357    }
3358
3359    /// Custom metrics and instrumentation
3360    pub async fn custom_instrumentation() -> Result<()> {
3361        use crate::dashboard::{DashboardMetrics, MetricType, PrometheusMetric, TimeSeriesPoint};
3362
3363        println!("=== Custom Metrics and Instrumentation ===");
3364        println!();
3365
3366        let mut metrics = DashboardMetrics::new();
3367
3368        // Add domain-specific custom metrics
3369        metrics.add_custom_metric("code_evaluations_completed".to_string(), 5_420.0);
3370        metrics.add_custom_metric("fraud_cases_detected".to_string(), 127.0);
3371        metrics.add_custom_metric("plagiarism_similarity_avg".to_string(), 15.3);
3372        metrics.add_custom_metric("github_verifications".to_string(), 3_850.0);
3373        metrics.add_custom_metric("document_pages_analyzed".to_string(), 12_450.0);
3374
3375        println!("Custom Metrics Registered:");
3376        for (name, value) in &metrics.custom_metrics {
3377            println!("  • {name}: {value}");
3378        }
3379        println!();
3380
3381        // Create labeled metrics for detailed tracking
3382        let mut provider_latency = PrometheusMetric::new(
3383            "kaccy_ai_provider_latency_ms".to_string(),
3384            MetricType::Histogram,
3385            "LLM provider latency in milliseconds".to_string(),
3386        );
3387
3388        provider_latency.add_point(
3389            TimeSeriesPoint::now(285.5)
3390                .with_label("provider".to_string(), "openai".to_string())
3391                .with_label("model".to_string(), "gpt-4-turbo".to_string()),
3392        );
3393
3394        provider_latency.add_point(
3395            TimeSeriesPoint::now(320.2)
3396                .with_label("provider".to_string(), "anthropic".to_string())
3397                .with_label("model".to_string(), "claude-3-opus".to_string()),
3398        );
3399
3400        provider_latency.add_point(
3401            TimeSeriesPoint::now(195.8)
3402                .with_label("provider".to_string(), "gemini".to_string())
3403                .with_label("model".to_string(), "gemini-1.5-flash".to_string()),
3404        );
3405
3406        println!("=== Labeled Metrics (Prometheus Format) ===");
3407        println!("{}", provider_latency.to_prometheus_format());
3408
3409        println!("Benefits of Custom Metrics:");
3410        println!("  • Track domain-specific KPIs");
3411        println!("  • Monitor business logic performance");
3412        println!("  • Identify bottlenecks and optimization opportunities");
3413        println!("  • Support data-driven decision making");
3414        println!("  • Enable advanced alerting rules");
3415        println!();
3416
3417        println!("Best Practices:");
3418        println!("  • Use meaningful metric names (snake_case)");
3419        println!("  • Add appropriate labels for filtering");
3420        println!("  • Choose correct metric type (Counter/Gauge/Histogram)");
3421        println!("  • Document metrics in code comments");
3422        println!("  • Set up retention policies for historical data");
3423
3424        Ok(())
3425    }
3426}
3427
3428#[cfg(test)]
3429mod tests {
3430    use super::*;
3431
3432    #[test]
3433    fn test_examples_compile() {
3434        // This test just ensures all examples compile
3435        // Actual execution requires API keys
3436        let _ = BasicCodeEvaluationExample;
3437        let _ = BatchProcessingExample;
3438        let _ = OracleConsensusExample;
3439        let _ = CompleteServiceExample;
3440        let _ = FraudDetectionExample;
3441        let _ = IntegrationExample;
3442        let _ = GeminiIntegrationExample;
3443        let _ = DeepSeekIntegrationExample;
3444        let _ = OllamaIntegrationExample;
3445        let _ = PlagiarismDetectionExample;
3446        let _ = ImageSimilarityExample;
3447        let _ = CostOptimizationExample;
3448        let _ = ResilienceExample;
3449        let _ = BudgetManagementExample;
3450        let _ = AccessControlExample;
3451        let _ = KnowledgeBaseExample;
3452        let _ = PerformanceProfilingExample;
3453        let _ = ModelVersionManagementExample;
3454        let _ = ReportGenerationExample;
3455        let _ = DashboardIntegrationExample;
3456    }
3457}