rexis-llm 0.1.0

Rexis LLM - Multi-provider LLM client with streaming, tool calling, and JSON schema support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//! Complete Tool Calling Application Example
//!
//! This example demonstrates ALL tool calling features in a real-world scenario:
//! - Multiple tools using different approaches
//! - Tool registry management
//! - Batch execution
//! - Error handling
//! - Validation
//! - Type safety
//! - Automatic schema generation
//!
//! Scenario: A productivity assistant with various tools
//!
//! Run with: cargo run -p rsllm --example complete_tool_application --features ollama

use rsllm::simple_tool;
use rsllm::tool;
use rsllm::tools::{SchemaBasedTool, Tool, ToolCall as ToolCallExec, ToolRegistry, ToolResult};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::error::Error;

// ============================================================================
// TOOL 1: Calculator (Using #[tool] Macro - EASIEST!)
// ============================================================================

#[derive(JsonSchema, Serialize, Deserialize, Debug)]
pub struct CalculatorParams {
    /// The arithmetic operation to perform
    operation: Operation,
    /// First number
    a: f64,
    /// Second number
    b: f64,
}

#[derive(JsonSchema, Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum Operation {
    Add,
    Subtract,
    Multiply,
    Divide,
}

#[derive(JsonSchema, Serialize, Deserialize)]
pub struct CalculatorResult {
    /// The result of the calculation
    pub result: f64,
    /// The operation performed
    pub operation: String,
}

#[tool(description = "Performs basic arithmetic operations")]
fn calculator(params: CalculatorParams) -> Result<CalculatorResult, Box<dyn Error + Send + Sync>> {
    let result = match params.operation {
        Operation::Add => params.a + params.b,
        Operation::Subtract => params.a - params.b,
        Operation::Multiply => params.a * params.b,
        Operation::Divide => {
            if params.b == 0.0 {
                return Err("Cannot divide by zero".into());
            }
            params.a / params.b
        }
    };

    Ok(CalculatorResult {
        result,
        operation: format!("{:?}", params.operation),
    })
}

// ============================================================================
// TOOL 2: Task Manager (Using SchemaBasedTool - More Control)
// ============================================================================

#[derive(JsonSchema, Serialize, Deserialize, Debug)]
pub struct TaskParams {
    /// Action to perform on tasks
    action: TaskAction,
    /// Task description (for create/update)
    #[serde(default)]
    description: Option<String>,
    /// Task ID (for update/complete/delete)
    #[serde(default)]
    task_id: Option<u32>,
    /// Priority level
    #[serde(default = "default_priority")]
    priority: Priority,
}

fn default_priority() -> Priority {
    Priority::Medium
}

#[derive(JsonSchema, Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum TaskAction {
    Create,
    List,
    Complete,
    Delete,
}

#[derive(JsonSchema, Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "lowercase")]
pub enum Priority {
    Low,
    Medium,
    High,
}

pub struct TaskManager {
    tasks: std::sync::Mutex<Vec<Task>>,
}

#[derive(Debug, Clone, Serialize)]
struct Task {
    id: u32,
    description: String,
    priority: Priority,
    completed: bool,
}

impl TaskManager {
    fn new() -> Self {
        Self {
            tasks: std::sync::Mutex::new(Vec::new()),
        }
    }
}

impl SchemaBasedTool for TaskManager {
    type Params = TaskParams;

    fn name(&self) -> &str {
        "task_manager"
    }

    fn description(&self) -> &str {
        "Manage tasks: create, list, complete, or delete tasks"
    }

    fn validate_typed(&self, params: &Self::Params) -> Result<(), Box<dyn Error + Send + Sync>> {
        match params.action {
            TaskAction::Create => {
                if params.description.is_none() {
                    return Err("description is required for create action".into());
                }
            }
            TaskAction::Complete | TaskAction::Delete => {
                if params.task_id.is_none() {
                    return Err("task_id is required for this action".into());
                }
            }
            TaskAction::List => {}
        }
        Ok(())
    }

    fn execute_typed(
        &self,
        params: Self::Params,
    ) -> Result<serde_json::Value, Box<dyn Error + Send + Sync>> {
        let mut tasks = self.tasks.lock().unwrap();

        match params.action {
            TaskAction::Create => {
                let id = tasks.len() as u32 + 1;
                let task = Task {
                    id,
                    description: params.description.unwrap(),
                    priority: params.priority,
                    completed: false,
                };
                tasks.push(task.clone());
                Ok(json!({
                    "success": true,
                    "message": "Task created",
                    "task": task
                }))
            }
            TaskAction::List => Ok(json!({
                "tasks": tasks.clone(),
                "count": tasks.len()
            })),
            TaskAction::Complete => {
                let task_id = params.task_id.unwrap();
                if let Some(task) = tasks.iter_mut().find(|t| t.id == task_id) {
                    task.completed = true;
                    Ok(json!({"success": true, "message": "Task completed"}))
                } else {
                    Err(format!("Task {} not found", task_id).into())
                }
            }
            TaskAction::Delete => {
                let task_id = params.task_id.unwrap();
                if let Some(pos) = tasks.iter().position(|t| t.id == task_id) {
                    tasks.remove(pos);
                    Ok(json!({"success": true, "message": "Task deleted"}))
                } else {
                    Err(format!("Task {} not found", task_id).into())
                }
            }
        }
    }
}

// ============================================================================
// TOOL 3: Text Analyzer (Using simple_tool! Macro - Quick Inline)
// ============================================================================

fn create_text_analyzer() -> Box<dyn Tool> {
    simple_tool!(
        name: "analyze_text",
        description: "Analyzes text and returns statistics",
        parameters: json!({
            "type": "object",
            "properties": {
                "text": {
                    "type": "string",
                    "description": "The text to analyze"
                }
            },
            "required": ["text"]
        }),
        execute: |args| {
            let text = args["text"].as_str().unwrap_or("");
            json!({
                "length": text.len(),
                "words": text.split_whitespace().count(),
                "lines": text.lines().count(),
                "chars": text.chars().count(),
                "uppercase": text.chars().filter(|c| c.is_uppercase()).count(),
                "lowercase": text.chars().filter(|c| c.is_lowercase()).count(),
                "digits": text.chars().filter(|c| c.is_numeric()).count()
            })
        }
    )
}

// ============================================================================
// TOOL 4: Data Converter (Manual JSON - Full Control)
// ============================================================================

struct DataConverter;

impl Tool for DataConverter {
    fn name(&self) -> &str {
        "convert_data"
    }

    fn description(&self) -> &str {
        "Converts data between different formats"
    }

    fn parameters_schema(&self) -> serde_json::Value {
        json!({
            "type": "object",
            "properties": {
                "data": {
                    "type": "string",
                    "description": "The data to convert"
                },
                "from_format": {
                    "type": "string",
                    "enum": ["json", "csv", "text"],
                    "description": "Source format"
                },
                "to_format": {
                    "type": "string",
                    "enum": ["json", "csv", "text"],
                    "description": "Target format"
                }
            },
            "required": ["data", "from_format", "to_format"]
        })
    }

    fn execute(
        &self,
        args: serde_json::Value,
    ) -> Result<serde_json::Value, Box<dyn Error + Send + Sync>> {
        let data = args["data"].as_str().ok_or("Missing data")?;
        let from_format = args["from_format"].as_str().ok_or("Missing from_format")?;
        let to_format = args["to_format"].as_str().ok_or("Missing to_format")?;

        // Simple mock conversion
        Ok(json!({
            "converted": format!("Converted from {} to {}: {}", from_format, to_format, data),
            "from": from_format,
            "to": to_format,
            "length": data.len()
        }))
    }
}

// ============================================================================
// MAIN APPLICATION
// ============================================================================

fn main() -> Result<(), Box<dyn Error>> {
    tracing::debug!("🚀 Complete Tool Calling Application");
    tracing::debug!("=====================================\n");
    tracing::debug!("Scenario: Productivity Assistant with Multiple Tools\n");

    // ========================================================================
    // STEP 1: Initialize Tool Registry
    // ========================================================================

    tracing::debug!("📦 STEP 1: Initialize Tool Registry");
    tracing::debug!("────────────────────────────────────\n");

    let mut registry = ToolRegistry::new();

    // Register all tools using different approaches
    registry.register(Box::new(CalculatorTool))?;
    tracing::debug!("   ✅ Calculator (macro)");

    registry.register(Box::new(TaskManager::new()))?;
    tracing::debug!("   ✅ Task Manager (SchemaBasedTool)");

    registry.register(create_text_analyzer())?;
    tracing::debug!("   ✅ Text Analyzer (simple_tool!)");

    registry.register(Box::new(DataConverter))?;
    tracing::debug!("   ✅ Data Converter (manual JSON)");

    tracing::debug!("\n   📊 Total tools: {}\n", registry.len());

    // ========================================================================
    // STEP 2: Show Tool Definitions (What gets sent to LLM)
    // ========================================================================

    tracing::debug!("📋 STEP 2: Tool Definitions for LLM");
    tracing::debug!("────────────────────────────────────\n");

    for def in registry.tool_definitions() {
        tracing::debug!("   📝 {}", def.name);
        tracing::debug!("      Description: {}", def.description);
        tracing::debug!("      Type: {}", def.tool_type);
    }

    // ========================================================================
    // STEP 3: Execute Individual Tools
    // ========================================================================

    tracing::debug!("🔧 STEP 3: Execute Individual Tools");
    tracing::debug!("────────────────────────────────────\n");

    // 3.1: Calculator
    tracing::debug!("   3.1: Calculator - Multiply 15 * 8");
    let calc_result = registry.execute(&ToolCallExec::new(
        "calc-1",
        "calculator",
        json!({"operation": "multiply", "a": 15, "b": 8}),
    ));
    print_tool_result(&calc_result);

    // 3.2: Task Manager - Create task
    tracing::debug!("\n   3.2: Task Manager - Create new task");
    let create_task = registry.execute(&ToolCallExec::new(
        "task-1",
        "task_manager",
        json!({
            "action": "create",
            "description": "Review pull requests",
            "priority": "high"
        }),
    ));
    print_tool_result(&create_task);

    // 3.3: Task Manager - List tasks
    tracing::debug!("\n   3.3: Task Manager - List all tasks");
    let list_tasks = registry.execute(&ToolCallExec::new(
        "task-2",
        "task_manager",
        json!({"action": "list"}),
    ));
    print_tool_result(&list_tasks);

    // 3.4: Text Analyzer
    tracing::debug!("\n   3.4: Text Analyzer - Analyze sample text");
    let analyze_result = registry.execute(&ToolCallExec::new(
        "text-1",
        "analyze_text",
        json!({"text": "Hello World! This is RSLLM tool calling. Very COOL!"}),
    ));
    print_tool_result(&analyze_result);

    // 3.5: Data Converter
    tracing::debug!("\n   3.5: Data Converter - Convert JSON to CSV");
    let convert_result = registry.execute(&ToolCallExec::new(
        "convert-1",
        "convert_data",
        json!({
            "data": "{\"name\": \"John\", \"age\": 30}",
            "from_format": "json",
            "to_format": "csv"
        }),
    ));
    print_tool_result(&convert_result);

    // ========================================================================
    // STEP 4: Batch Execution
    // ========================================================================

    tracing::debug!("\n\n🔄 STEP 4: Batch Tool Execution");
    tracing::debug!("────────────────────────────────────\n");

    let batch_calls = vec![
        ToolCallExec::new(
            "batch-1",
            "calculator",
            json!({"operation": "add", "a": 100, "b": 50}),
        ),
        ToolCallExec::new(
            "batch-2",
            "calculator",
            json!({"operation": "divide", "a": 144, "b": 12}),
        ),
        ToolCallExec::new(
            "batch-3",
            "task_manager",
            json!({"action": "create", "description": "Test batch execution"}),
        ),
    ];

    tracing::debug!("   Executing {} tools in batch...", batch_calls.len());
    let batch_results = registry.execute_batch(&batch_calls);

    for (i, result) in batch_results.iter().enumerate() {
        tracing::debug!("\n   Batch #{}: {}", i + 1, result.tool_name);
        if result.success {
            tracing::debug!(
                "      ✅ Success: {}",
                serde_json::to_string(&result.content)?
            );
        } else {
            tracing::debug!("      ❌ Error: {}", result.error.as_ref().unwrap());
        }
    }

    // ========================================================================
    // STEP 5: Error Handling
    // ========================================================================

    tracing::debug!("\n\n⚠️  STEP 5: Error Handling");
    tracing::debug!("────────────────────────────────────\n");

    // 5.1: Division by zero
    tracing::debug!("   5.1: Testing division by zero");
    let error_result = registry.execute(&ToolCallExec::new(
        "error-1",
        "calculator",
        json!({"operation": "divide", "a": 10, "b": 0}),
    ));
    print_tool_result(&error_result);

    // 5.2: Invalid tool name
    tracing::debug!("\n   5.2: Testing invalid tool name");
    let invalid_tool =
        registry.execute(&ToolCallExec::new("error-2", "nonexistent_tool", json!({})));
    print_tool_result(&invalid_tool);

    // 5.3: Missing required parameter
    tracing::debug!("\n   5.3: Testing missing required parameter");
    let missing_param = registry.execute(&ToolCallExec::new(
        "error-3",
        "task_manager",
        json!({"action": "create"}), // Missing description
    ));
    print_tool_result(&missing_param);

    // ========================================================================
    // STEP 6: Tool Discovery & Introspection
    // ========================================================================

    tracing::debug!("\n\n🔍 STEP 6: Tool Discovery");
    tracing::debug!("────────────────────────────────────\n");

    tracing::debug!("   Available tools:");
    for tool_name in registry.tool_names() {
        tracing::debug!("      - {}", tool_name);
    }

    tracing::debug!("\n   Registry status:");
    tracing::debug!("      Total tools: {}", registry.len());
    tracing::debug!("      Is empty: {}", registry.is_empty());

    // Check if specific tools exist
    tracing::debug!("\n   Tool availability checks:");
    tracing::debug!(
        "      calculator exists: {}",
        registry.contains("calculator")
    );
    tracing::debug!(
        "      task_manager exists: {}",
        registry.contains("task_manager")
    );
    tracing::debug!(
        "      nonexistent exists: {}",
        registry.contains("nonexistent")
    );

    // ========================================================================
    // STEP 7: Complete Workflow Example
    // ========================================================================

    tracing::debug!("\n\n🎬 STEP 7: Complete Workflow");
    tracing::debug!("────────────────────────────────────\n");
    tracing::debug!("   Simulating: User asks 'Calculate 25% of my 5 tasks'\n");

    // Step 1: List tasks
    tracing::debug!("   → Calling task_manager to count tasks");
    let count_tasks = registry.execute(&ToolCallExec::new(
        "workflow-1",
        "task_manager",
        json!({"action": "list"}),
    ));

    let task_count = if count_tasks.success {
        count_tasks.content["count"].as_u64().unwrap_or(0)
    } else {
        0
    };
    tracing::debug!("      Found {} tasks", task_count);

    // Step 2: Calculate 25%
    tracing::debug!("\n   → Calling calculator to compute 25% of {}", task_count);
    let calc_25_percent = registry.execute(&ToolCallExec::new(
        "workflow-2",
        "calculator",
        json!({
            "operation": "multiply",
            "a": task_count,
            "b": 0.25
        }),
    ));

    if calc_25_percent.success {
        let result = calc_25_percent.content["result"].as_f64().unwrap_or(0.0);
        tracing::debug!("      25% of {} tasks = {} tasks", task_count, result);
    }

    // Step 3: Create analysis report
    tracing::debug!("\n   → Creating analysis report");
    let report = format!(
        "Task Analysis: You have {} tasks. 25% completion would be {:.1} tasks.",
        task_count,
        task_count as f64 * 0.25
    );

    let analyze_report = registry.execute(&ToolCallExec::new(
        "workflow-3",
        "analyze_text",
        json!({"text": report}),
    ));

    if analyze_report.success {
        tracing::debug!(
            "      Report stats: {} words, {} chars",
            analyze_report.content["words"],
            analyze_report.content["chars"]
        );
    }

    // ========================================================================
    // SUMMARY
    // ========================================================================

    tracing::debug!("\n\n📊 APPLICATION SUMMARY");
    tracing::debug!("══════════════════════════════════════════════════\n");

    tracing::debug!("✅ Features Demonstrated:");
    tracing::debug!("   1. ✅ Multiple tools with different approaches");
    tracing::debug!("   2. ✅ #[tool] macro (easiest - auto-generates everything)");
    tracing::debug!("   3. ✅ SchemaBasedTool (automatic schema, custom logic)");
    tracing::debug!("   4. ✅ simple_tool! macro (quick inline tools)");
    tracing::debug!("   5. ✅ Manual JSON (full control)");
    tracing::debug!("   6. ✅ Type-safe parameter handling");
    tracing::debug!("   7. ✅ Automatic JSON schema generation");
    tracing::debug!("   8. ✅ Custom validation logic");
    tracing::debug!("   9. ✅ Batch execution");
    tracing::debug!("   10. ✅ Error handling");
    tracing::debug!("   11. ✅ Tool discovery & introspection");
    tracing::debug!("   12. ✅ Multi-step workflows");

    tracing::debug!("\n💡 Key Takeaways:");
    tracing::debug!("   • Use #[tool] macro for new tools (15 lines per tool)");
    tracing::debug!("   • Use SchemaBasedTool for stateful/complex tools");
    tracing::debug!("   • Use simple_tool! for quick prototypes");
    tracing::debug!("   • All approaches work together seamlessly");
    tracing::debug!("   • Zero manual JSON schema writing needed");
    tracing::debug!("   • Full type safety throughout");

    tracing::debug!("\n🎉 Complete tool calling system ready for production!");

    Ok(())
}

// ============================================================================
// HELPER FUNCTIONS
// ============================================================================

fn print_tool_result(result: &ToolResult) {
    if result.success {
        match serde_json::to_string_pretty(&result.content) {
            Ok(json_str) => tracing::debug!("      ✅ Success:\n{}", indent(&json_str, 9)),
            Err(_) => tracing::debug!("      ✅ Success: {:?}", result.content),
        }
    } else {
        tracing::debug!("      ❌ Error: {}", result.error.as_ref().unwrap());
    }
}

fn indent(text: &str, spaces: usize) -> String {
    let prefix = " ".repeat(spaces);
    text.lines()
        .map(|line| format!("{}{}", prefix, line))
        .collect::<Vec<_>>()
        .join("\n")
}