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
//! Tool Capability Integration Tests
//!
//! These tests verify tool calling functionality across all supported providers.
//! They are ignored by default to prevent accidental API usage during normal testing.
//!
//! ## Running Tests
//!
//! ```bash
//! # Test specific provider tool capabilities
//! export OPENAI_API_KEY="your-key"
//! cargo test test_openai_tools -- --ignored
//!
//! export ANTHROPIC_API_KEY="your-key"
//! cargo test test_anthropic_tools -- --ignored
//!
//! # Test all available providers
//! cargo test test_all_provider_tools -- --ignored
//! ```

use futures::StreamExt;
use serde_json::json;
use siumai::prelude::*;
use siumai::stream::ChatStreamEvent;
use std::env;

/// Create a simple calculator tool for testing
fn create_calculator_tool() -> Tool {
    Tool {
        r#type: "function".to_string(),
        function: ToolFunction {
            name: "calculate".to_string(),
            description: "Perform basic mathematical calculations".to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "expression": {
                        "type": "string",
                        "description": "Mathematical expression to evaluate (e.g., '2 + 3', '10 * 5')"
                    }
                },
                "required": ["expression"]
            }),
        },
    }
}

/// Create a weather tool for testing
fn create_weather_tool() -> Tool {
    Tool {
        r#type: "function".to_string(),
        function: ToolFunction {
            name: "get_weather".to_string(),
            description: "Get current weather information for a location".to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "City name or location"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "Temperature unit"
                    }
                },
                "required": ["location"]
            }),
        },
    }
}

/// Test basic tool calling functionality
async fn test_basic_tool_calling<T: ChatCapability>(client: &T, provider_name: &str) {
    println!("  🔧 Testing basic tool calling for {}...", provider_name);

    let tools = vec![create_calculator_tool()];
    let messages = vec![
        system!(
            "You are a helpful assistant. When asked to calculate something, use the calculate tool."
        ),
        user!("What is 15 + 27? Please use the calculator tool to compute this."),
    ];

    match client.chat_with_tools(messages, Some(tools)).await {
        Ok(response) => {
            println!("    ✅ Tool calling successful");

            // Check if tool calls were made
            if let Some(tool_calls) = &response.tool_calls {
                println!("    🔧 Tool calls made: {}", tool_calls.len());
                for (i, tool_call) in tool_calls.iter().enumerate() {
                    println!(
                        "      Tool {}: {} ({})",
                        i + 1,
                        tool_call
                            .function
                            .as_ref()
                            .map(|f| f.name.as_str())
                            .unwrap_or("unknown"),
                        tool_call.id
                    );
                }
            } else {
                println!("    ⚠️ No tool calls in response (model may have answered directly)");
            }

            let content = response.content_text().unwrap_or_default();
            if !content.is_empty() {
                println!("    📝 Response: {}", content.trim());
            }

            if let Some(usage) = response.usage {
                println!(
                    "    📊 Usage: {} prompt + {} completion = {} total tokens",
                    usage.prompt_tokens, usage.completion_tokens, usage.total_tokens
                );
            }
        }
        Err(e) => {
            println!("    ⚠️ Tool calling failed: {}", e);
            println!(
                "    💡 Note: Some models may not support tool calling or may need specific configuration"
            );
        }
    }
}

/// Test streaming tool calling functionality
async fn test_streaming_tool_calling<T: ChatCapability>(client: &T, provider_name: &str) {
    println!(
        "  🌊 Testing streaming tool calling for {}...",
        provider_name
    );

    let tools = vec![create_weather_tool()];
    let messages = vec![
        system!("You are a helpful assistant. When asked about weather, use the get_weather tool."),
        user!("What's the weather like in Tokyo? Please use the weather tool."),
    ];

    match client.chat_stream(messages, Some(tools)).await {
        Ok(mut stream) => {
            let mut tool_calls_received = 0;
            let mut content_chunks = Vec::new();

            while let Some(event_result) = stream.next().await {
                match event_result {
                    Ok(event) => match event {
                        ChatStreamEvent::ContentDelta { delta, .. } => {
                            content_chunks.push(delta);
                        }
                        ChatStreamEvent::ToolCallDelta {
                            id,
                            function_name,
                            arguments_delta,
                            ..
                        } => {
                            tool_calls_received += 1;
                            if let Some(name) = &function_name {
                                println!("    🔧 Tool call: {} (ID: {})", name, id);
                            }
                            if let Some(args) = &arguments_delta {
                                println!("    📝 Arguments delta: {}", args);
                            }
                        }
                        ChatStreamEvent::StreamEnd { response } => {
                            println!("    ✅ Streaming tool calling successful");

                            if tool_calls_received > 0 {
                                println!(
                                    "    🔧 Tool call deltas received: {}",
                                    tool_calls_received
                                );
                            }

                            if let Some(tool_calls) = &response.tool_calls {
                                println!("    🔧 Final tool calls: {}", tool_calls.len());
                            }

                            let final_content = response.content_text().unwrap_or_default();
                            if !final_content.is_empty() {
                                println!("    📝 Final response: {}", final_content.trim());
                            }

                            if let Some(usage) = response.usage {
                                println!(
                                    "    📊 Usage: {} prompt + {} completion = {} total tokens",
                                    usage.prompt_tokens,
                                    usage.completion_tokens,
                                    usage.total_tokens
                                );
                            }
                            break;
                        }
                        ChatStreamEvent::Error { error } => {
                            println!("    ❌ Stream error: {}", error);
                            return;
                        }
                        _ => {
                            // Handle other events
                        }
                    },
                    Err(e) => {
                        println!("    ❌ Stream error: {}", e);
                        return;
                    }
                }
            }
        }
        Err(e) => {
            println!("    ⚠️ Streaming tool calling failed: {}", e);
            println!("    💡 Note: Some providers may not support streaming tool calls");
        }
    }
}

/// Test multiple tools functionality
async fn test_multiple_tools<T: ChatCapability>(client: &T, provider_name: &str) {
    println!("  🔧 Testing multiple tools for {}...", provider_name);

    let tools = vec![create_calculator_tool(), create_weather_tool()];
    let messages = vec![
        system!("You are a helpful assistant. Use the appropriate tools when needed."),
        user!("Calculate 8 * 7 and also tell me about the weather in London."),
    ];

    match client.chat_with_tools(messages, Some(tools)).await {
        Ok(response) => {
            println!("    ✅ Multiple tools test successful");

            if let Some(tool_calls) = &response.tool_calls {
                println!("    🔧 Tool calls made: {}", tool_calls.len());
                for tool_call in tool_calls {
                    if let Some(function) = &tool_call.function {
                        println!("      - {}: {}", function.name, function.arguments);
                    }
                }
            }

            let content = response.content_text().unwrap_or_default();
            if !content.is_empty() {
                println!("    📝 Response: {}", content.trim());
            }
        }
        Err(e) => {
            println!("    ⚠️ Multiple tools test failed: {}", e);
        }
    }
}

/// Generic provider tool testing
async fn test_provider_tools(provider_name: &str, api_key_env: &str, model: &str) {
    if env::var(api_key_env).is_err() {
        println!(
            "⏭️ Skipping {} tool tests: {} not set",
            provider_name, api_key_env
        );
        return;
    }

    println!("🔧 Testing {} tool capabilities...", provider_name);

    match provider_name {
        "OpenAI" => {
            let api_key = env::var(api_key_env).unwrap();
            let mut builder = LlmBuilder::new().openai().api_key(api_key).model(model);

            if let Ok(base_url) = env::var("OPENAI_BASE_URL") {
                builder = builder.base_url(base_url);
            }

            match builder.build().await {
                Ok(client) => {
                    test_basic_tool_calling(&client, provider_name).await;
                    test_streaming_tool_calling(&client, provider_name).await;
                    test_multiple_tools(&client, provider_name).await;
                }
                Err(e) => {
                    println!("❌ Failed to build OpenAI client: {}", e);
                    return;
                }
            }
        }
        "Anthropic" => {
            let api_key = env::var(api_key_env).unwrap();
            let mut builder = LlmBuilder::new().anthropic().api_key(api_key).model(model);

            if let Ok(base_url) = env::var("ANTHROPIC_BASE_URL") {
                builder = builder.base_url(base_url);
            }

            match builder.build().await {
                Ok(client) => {
                    test_basic_tool_calling(&client, provider_name).await;
                    test_streaming_tool_calling(&client, provider_name).await;
                    test_multiple_tools(&client, provider_name).await;
                }
                Err(e) => {
                    println!("❌ Failed to build Anthropic client: {}", e);
                    return;
                }
            }
        }
        "Gemini" => {
            let api_key = env::var(api_key_env).unwrap();
            match LlmBuilder::new()
                .gemini()
                .api_key(api_key)
                .model(model)
                .build()
                .await
            {
                Ok(client) => {
                    test_basic_tool_calling(&client, provider_name).await;
                    test_streaming_tool_calling(&client, provider_name).await;
                    test_multiple_tools(&client, provider_name).await;
                }
                Err(e) => {
                    println!("❌ Failed to build Gemini client: {}", e);
                    return;
                }
            }
        }
        "xAI" => {
            let api_key = env::var(api_key_env).unwrap();
            match LlmBuilder::new()
                .xai()
                .api_key(api_key)
                .model(model)
                .build()
                .await
            {
                Ok(client) => {
                    test_basic_tool_calling(&client, provider_name).await;
                    test_streaming_tool_calling(&client, provider_name).await;
                    test_multiple_tools(&client, provider_name).await;
                }
                Err(e) => {
                    println!("❌ Failed to build xAI client: {}", e);
                    return;
                }
            }
        }
        "Ollama" => {
            let base_url = env::var("OLLAMA_BASE_URL")
                .unwrap_or_else(|_| "http://localhost:11434".to_string());
            match LlmBuilder::new()
                .ollama()
                .base_url(&base_url)
                .model(model)
                .build()
                .await
            {
                Ok(client) => {
                    test_basic_tool_calling(&client, provider_name).await;
                    test_streaming_tool_calling(&client, provider_name).await;
                    test_multiple_tools(&client, provider_name).await;
                }
                Err(e) => {
                    println!("❌ Failed to build Ollama client: {}", e);
                    return;
                }
            }
        }
        _ => {
            println!("❌ Unknown provider: {}", provider_name);
            return;
        }
    }

    println!("{} tool testing completed\n", provider_name);
}

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

    #[tokio::test]
    #[ignore]
    async fn test_openai_tools() {
        test_provider_tools("OpenAI", "OPENAI_API_KEY", "gpt-4o-mini").await;
    }

    #[tokio::test]
    #[ignore]
    async fn test_anthropic_tools() {
        test_provider_tools(
            "Anthropic",
            "ANTHROPIC_API_KEY",
            "claude-3-5-haiku-20241022",
        )
        .await;
    }

    #[tokio::test]
    #[ignore]
    async fn test_gemini_tools() {
        test_provider_tools("Gemini", "GEMINI_API_KEY", "gemini-2.5-flash").await;
    }

    #[tokio::test]
    #[ignore]
    async fn test_xai_tools() {
        test_provider_tools("xAI", "XAI_API_KEY", "grok-4-0709").await;
    }

    #[tokio::test]
    #[ignore]
    async fn test_ollama_tools() {
        // Check if Ollama is available
        let base_url =
            env::var("OLLAMA_BASE_URL").unwrap_or_else(|_| "http://localhost:11434".to_string());
        let test_client = reqwest::Client::new();

        match test_client
            .get(format!("{}/api/tags", base_url))
            .send()
            .await
        {
            Ok(response) if response.status().is_success() => {
                test_provider_tools("Ollama", "OLLAMA_BASE_URL", "qwen3").await;
            }
            _ => {
                println!(
                    "⏭️ Skipping Ollama tool tests: Ollama not available at {}",
                    base_url
                );
            }
        }
    }

    #[tokio::test]
    #[ignore]
    async fn test_all_provider_tools() {
        println!("🚀 Running tool capability tests for all available providers...\n");

        let providers = vec![
            ("OpenAI", "OPENAI_API_KEY", "gpt-4o-mini"),
            (
                "Anthropic",
                "ANTHROPIC_API_KEY",
                "claude-3-5-haiku-20241022",
            ),
            ("Gemini", "GEMINI_API_KEY", "gemini-2.5-flash"),
            ("xAI", "XAI_API_KEY", "grok-4-0709"),
        ];

        for (provider_name, api_key_env, model) in providers {
            test_provider_tools(provider_name, api_key_env, model).await;
        }

        // Test Ollama separately due to different setup
        test_provider_tools("Ollama", "OLLAMA_BASE_URL", "qwen3").await;

        println!("🎉 All provider tool testing completed!");
    }
}