siumai 0.10.3

A unified LLM interface library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! 🔍 Capability Detection - Feature detection and graceful degradation
//!
//! This example demonstrates how to detect and adapt to provider capabilities:
//! - Runtime capability detection
//! - Feature availability checking
//! - Graceful degradation when features aren't available
//! - Provider metadata and limitations
//!
//! Before running, set your API keys:
//! ```bash
//! export OPENAI_API_KEY="your-key"
//! export ANTHROPIC_API_KEY="your-key"
//! ```
//!
//! Run with:
//! ```bash
//! cargo run --example capability_detection
//! ```

use siumai::models;
use siumai::prelude::*;
use siumai::traits::ChatCapability;
use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("🔍 Capability Detection - Feature detection and graceful degradation\n");

    // Demonstrate different aspects of capability detection
    demonstrate_basic_capability_detection().await;
    demonstrate_feature_availability().await;
    demonstrate_graceful_feature_degradation().await;
    demonstrate_provider_metadata().await;
    demonstrate_adaptive_behavior().await;

    println!("\n✅ Capability detection examples completed!");
    Ok(())
}

/// Demonstrate basic capability detection
async fn demonstrate_basic_capability_detection() {
    println!("🔍 Basic Capability Detection:\n");

    let providers = create_test_providers().await;

    for (name, client) in providers {
        println!("   Provider: {name}");

        let capabilities = detect_capabilities(client.as_ref(), &name).await;

        println!("      📋 Detected Capabilities:");
        for (feature, supported) in capabilities {
            let status = if supported { "" } else { "" };
            println!("         {status} {feature}");
        }
        println!();
    }
}

/// Demonstrate feature availability checking
async fn demonstrate_feature_availability() {
    println!("🎯 Feature Availability Checking:\n");

    let providers = create_test_providers().await;

    let features_to_test = vec![
        "streaming",
        "vision",
        "audio",
        "tools",
        "json_mode",
        "thinking",
    ];

    for feature in features_to_test {
        println!("   Feature: {feature}");

        for (name, client) in &providers {
            let available = check_feature_availability(client.as_ref(), name, feature).await;
            let status = if available { "" } else { "" };
            println!("      {status} {name}");
        }
        println!();
    }
}

/// Demonstrate graceful feature degradation
async fn demonstrate_graceful_feature_degradation() {
    println!("🎭 Graceful Feature Degradation:\n");

    let providers = create_test_providers().await;

    if let Some((name, client)) = providers.into_iter().next() {
        println!("   Using provider: {name}");

        // Test streaming with fallback
        println!("   Testing streaming with fallback:");
        match try_streaming_with_fallback(client.as_ref()).await {
            Ok(response) => {
                println!(
                    "      ✅ Got response: {}",
                    &response[..response.len().min(100)]
                );
            }
            Err(e) => {
                println!("      ❌ Failed: {e}");
            }
        }

        // Test vision with fallback
        println!("\n   Testing vision with fallback:");
        match try_vision_with_fallback(client.as_ref()).await {
            Ok(response) => {
                println!(
                    "      ✅ Got response: {}",
                    &response[..response.len().min(100)]
                );
            }
            Err(e) => {
                println!("      ❌ Failed: {e}");
            }
        }
    } else {
        println!("   ⚠️  No providers available for testing");
    }

    println!();
}

/// Demonstrate provider metadata detection
async fn demonstrate_provider_metadata() {
    println!("📊 Provider Metadata:\n");

    let providers = create_test_providers().await;

    for (name, client) in providers {
        println!("   Provider: {name}");

        let metadata = get_provider_metadata(client.as_ref(), &name).await;

        println!("      📋 Metadata:");
        println!("         Type: {}", metadata.provider_type);
        println!("         Model: {}", metadata.model);
        println!(
            "         Max tokens: {}",
            metadata.max_tokens.unwrap_or_default()
        );
        println!("         Context window: {}", metadata.context_window);
        println!(
            "         Supports streaming: {}",
            metadata.supports_streaming
        );
        println!("         Supports vision: {}", metadata.supports_vision);
        println!(
            "         Cost per 1K tokens: ${:.4}",
            metadata.cost_per_1k_tokens
        );
        println!();
    }
}

/// Demonstrate adaptive behavior based on capabilities
async fn demonstrate_adaptive_behavior() {
    println!("🤖 Adaptive Behavior:\n");

    let providers = create_test_providers().await;

    if let Some((name, client)) = providers.into_iter().next() {
        println!("   Using provider: {name}");

        // Adapt behavior based on capabilities
        let message = "Explain machine learning in simple terms";

        match adaptive_chat(client.as_ref(), &name, message).await {
            Ok(response) => {
                println!("   ✅ Adaptive response received");
                println!("   Response: {}", &response[..response.len().min(150)]);
            }
            Err(e) => {
                println!("   ❌ Adaptive chat failed: {e}");
            }
        }
    } else {
        println!("   ⚠️  No providers available for testing");
    }

    println!();
}

/// Create test providers for capability detection
async fn create_test_providers() -> Vec<(String, Box<dyn ChatCapability + Send + Sync>)> {
    let mut providers = Vec::new();

    // Try OpenAI
    if let Ok(api_key) = std::env::var("OPENAI_API_KEY")
        && let Ok(client) = LlmBuilder::new()
            .openai()
            .api_key(&api_key)
            .model(models::openai::GPT_4O_MINI)
            .build()
            .await
    {
        providers.push((
            "OpenAI".to_string(),
            Box::new(client) as Box<dyn ChatCapability + Send + Sync>,
        ));
    }

    // Try Anthropic
    if let Ok(api_key) = std::env::var("ANTHROPIC_API_KEY")
        && let Ok(client) = LlmBuilder::new()
            .anthropic()
            .api_key(&api_key)
            .model(models::anthropic::CLAUDE_HAIKU_3_5)
            .build()
            .await
    {
        providers.push((
            "Anthropic".to_string(),
            Box::new(client) as Box<dyn ChatCapability + Send + Sync>,
        ));
    }

    // Try Ollama
    if let Ok(client) = LlmBuilder::new()
        .ollama()
        .base_url("http://localhost:11434")
        .model("llama3.2")
        .build()
        .await
    {
        // Test if Ollama is actually available
        let test_messages = vec![user!("Hi")];
        if client.chat(test_messages).await.is_ok() {
            providers.push((
                "Ollama".to_string(),
                Box::new(client) as Box<dyn ChatCapability + Send + Sync>,
            ));
        }
    }

    providers
}

/// Detect capabilities of a provider
async fn detect_capabilities(
    client: &dyn ChatCapability,
    provider_name: &str,
) -> HashMap<String, bool> {
    let mut capabilities = HashMap::new();

    // Basic chat (all providers should support this)
    capabilities.insert("Basic Chat".to_string(), true);

    // Streaming support
    capabilities.insert(
        "Streaming".to_string(),
        test_streaming_support(client).await,
    );

    // Vision support (simplified detection)
    capabilities.insert(
        "Vision".to_string(),
        provider_supports_vision(provider_name),
    );

    // Audio support
    capabilities.insert("Audio".to_string(), provider_supports_audio(provider_name));

    // Tool calling
    capabilities.insert("Tools".to_string(), provider_supports_tools(provider_name));

    // JSON mode
    capabilities.insert(
        "JSON Mode".to_string(),
        provider_supports_json_mode(provider_name),
    );

    // Thinking process
    capabilities.insert(
        "Thinking".to_string(),
        provider_supports_thinking(provider_name),
    );

    capabilities
}

/// Check if a specific feature is available
async fn check_feature_availability(
    client: &dyn ChatCapability,
    provider_name: &str,
    feature: &str,
) -> bool {
    match feature {
        "streaming" => test_streaming_support(client).await,
        "vision" => provider_supports_vision(provider_name),
        "audio" => provider_supports_audio(provider_name),
        "tools" => provider_supports_tools(provider_name),
        "json_mode" => provider_supports_json_mode(provider_name),
        "thinking" => provider_supports_thinking(provider_name),
        _ => false,
    }
}

/// Test streaming support
async fn test_streaming_support(_client: &dyn ChatCapability) -> bool {
    // In a real implementation, you would try to create a stream
    // For now, we'll assume all providers support streaming
    true
}

/// Check if provider supports vision
fn provider_supports_vision(provider_name: &str) -> bool {
    matches!(provider_name, "OpenAI" | "Anthropic")
}

/// Check if provider supports audio
fn provider_supports_audio(provider_name: &str) -> bool {
    matches!(provider_name, "OpenAI")
}

/// Check if provider supports tools
fn provider_supports_tools(provider_name: &str) -> bool {
    matches!(provider_name, "OpenAI" | "Anthropic")
}

/// Check if provider supports JSON mode
fn provider_supports_json_mode(provider_name: &str) -> bool {
    matches!(provider_name, "OpenAI" | "Anthropic")
}

/// Check if provider supports thinking process
fn provider_supports_thinking(provider_name: &str) -> bool {
    matches!(provider_name, "Anthropic")
}

/// Try streaming with fallback to regular chat
async fn try_streaming_with_fallback(client: &dyn ChatCapability) -> Result<String, LlmError> {
    let messages = vec![user!("Count from 1 to 5")];

    // Try streaming first
    if let Ok(mut stream) = client.chat_stream(messages.clone(), None).await {
        use futures_util::StreamExt;
        let mut result = String::new();

        while let Some(event) = stream.next().await {
            match event? {
                ChatStreamEvent::ContentDelta { delta, .. } => {
                    result.push_str(&delta);
                }
                ChatStreamEvent::StreamEnd { .. } => break,
                _ => {}
            }
        }

        Ok(format!("Streaming: {result}"))
    } else {
        // Fallback to regular chat
        let response = client.chat(messages).await?;
        Ok(format!(
            "Fallback: {}",
            response.content_text().unwrap_or_default()
        ))
    }
}

/// Try vision with fallback to text-only
async fn try_vision_with_fallback(client: &dyn ChatCapability) -> Result<String, LlmError> {
    // Try vision request (simplified)
    let messages = vec![user!(
        "Describe what you see in this image: [image would be here]"
    )];

    if let Ok(response) = client.chat(messages.clone()).await {
        Ok(format!(
            "Vision: {}",
            response.content_text().unwrap_or_default()
        ))
    } else {
        // Fallback to text-only
        let fallback_messages = vec![user!("Explain how image analysis works")];
        let response = client.chat(fallback_messages).await?;
        Ok(format!(
            "Text fallback: {}",
            response.content_text().unwrap_or_default()
        ))
    }
}

/// Provider metadata structure
#[derive(Debug)]
struct ProviderMetadata {
    provider_type: String,
    model: String,
    max_tokens: Option<u32>,
    context_window: u32,
    supports_streaming: bool,
    supports_vision: bool,
    cost_per_1k_tokens: f64,
}

/// Get provider metadata
async fn get_provider_metadata(
    _client: &dyn ChatCapability,
    provider_name: &str,
) -> ProviderMetadata {
    match provider_name {
        "OpenAI" => ProviderMetadata {
            provider_type: "Cloud API".to_string(),
            model: models::openai::GPT_4O_MINI.to_string(),
            max_tokens: Some(4096),
            context_window: 128_000,
            supports_streaming: true,
            supports_vision: true,
            cost_per_1k_tokens: 0.15,
        },
        "Anthropic" => ProviderMetadata {
            provider_type: "Cloud API".to_string(),
            model: models::anthropic::CLAUDE_HAIKU_3_5.to_string(),
            max_tokens: Some(4096),
            context_window: 200_000,
            supports_streaming: true,
            supports_vision: true,
            cost_per_1k_tokens: 0.25,
        },
        "Ollama" => ProviderMetadata {
            provider_type: "Local".to_string(),
            model: "llama3.2".to_string(),
            max_tokens: Some(2048),
            context_window: 8192,
            supports_streaming: true,
            supports_vision: false,
            cost_per_1k_tokens: 0.0,
        },
        _ => ProviderMetadata {
            provider_type: "Unknown".to_string(),
            model: "unknown".to_string(),
            max_tokens: None,
            context_window: 4096,
            supports_streaming: false,
            supports_vision: false,
            cost_per_1k_tokens: 0.0,
        },
    }
}

/// Adaptive chat that adjusts behavior based on capabilities
async fn adaptive_chat(
    client: &dyn ChatCapability,
    provider_name: &str,
    message: &str,
) -> Result<String, LlmError> {
    let metadata = get_provider_metadata(client, provider_name).await;

    // Adjust message based on capabilities
    let adjusted_message = if metadata.supports_vision {
        format!("{message} (Note: This provider supports vision)")
    } else {
        message.to_string()
    };

    let messages = vec![user!(&adjusted_message)];

    // Use streaming if supported, otherwise regular chat
    if metadata.supports_streaming {
        // Try streaming
        if let Ok(mut stream) = client.chat_stream(messages.clone(), None).await {
            use futures_util::StreamExt;
            let mut result = String::new();

            while let Some(event) = stream.next().await {
                match event? {
                    ChatStreamEvent::ContentDelta { delta, .. } => {
                        result.push_str(&delta);
                    }
                    ChatStreamEvent::StreamEnd { .. } => break,
                    _ => {}
                }
            }

            Ok(result)
        } else {
            // Fallback to regular chat
            let response = client.chat(messages).await?;
            Ok(response.content_text().unwrap_or_default().to_string())
        }
    } else {
        // Use regular chat
        let response = client.chat(messages).await?;
        Ok(response.content_text().unwrap_or_default().to_string())
    }
}

/*
🎯 Key Capability Detection Concepts:

Detection Methods:
- Runtime testing: Try features and handle failures
- Provider metadata: Known capabilities per provider
- API introspection: Query provider capabilities
- Feature flags: Configuration-based feature control

Graceful Degradation:
- Fallback to simpler features when advanced ones fail
- Progressive enhancement based on capabilities
- User notification of limited functionality
- Alternative workflows for missing features

Best Practices:
1. Cache capability detection results
2. Provide clear fallback behaviors
3. Test capabilities at startup
4. Monitor feature usage and failures
5. Document capability requirements
6. Handle capability changes gracefully

Production Considerations:
- Performance impact of capability detection
- Caching strategies for capability results
- Monitoring capability availability
- User experience with degraded features
- Cost implications of different capabilities

Next Steps:
- parameter_mapping.rs: Parameter handling across providers
- ../03_advanced_features/: Advanced capability patterns
- ../04_providers/: Provider-specific capabilities
*/