langchainrust 0.2.15

A LangChain-inspired framework for building LLM applications in Rust. Supports OpenAI, Agents, Tools, Memory, Chains, RAG, BM25, Hybrid Retrieval, LangGraph, HyDE, Reranking, MultiQuery, and native Function Calling.
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
// src/tools/math.rs
//! Advanced math tool for agents.
//!
//! Provides complex mathematical operations including exponent, logarithm, trigonometry, etc.

use async_trait::async_trait;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::core::tools::{BaseTool, Tool, ToolError};

/// Math tool input parameters.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct MathInput {
    /// Operation type: "power", "sqrt", "log", "ln", "sin", "cos", "tan", "abs", "factorial", "mod", "gcd", "lcm".
    pub operation: String,
    
    /// First value for calculation.
    pub value: Option<f64>,
    
    /// Second value for operations requiring two parameters (power, mod, gcd, lcm).
    pub value2: Option<f64>,
    
    /// Logarithm base for log operation (default: 10).
    pub base: Option<f64>,
}

/// Math tool output result.
#[derive(Debug, Serialize)]
pub struct MathOutput {
    /// Calculation result.
    pub result: f64,
    
    /// Operation type.
    pub operation: String,
    
    /// Additional details.
    pub details: Option<String>,
}

/// Advanced math tool for agents.
pub struct SimpleMathTool;

impl SimpleMathTool {
    /// Creates a new SimpleMathTool instance.
    pub fn new() -> Self {
        Self
    }
    
    /// 幂运算
    fn power(&self, base: f64, exponent: f64) -> Result<MathOutput, ToolError> {
        let result = base.powf(exponent);
        Ok(MathOutput {
            result,
            operation: "power".to_string(),
            details: Some(format!("{}^{} = {}", base, exponent, result)),
        })
    }
    
    /// 平方根
    fn sqrt(&self, value: f64) -> Result<MathOutput, ToolError> {
        if value < 0.0 {
            return Err(ToolError::InvalidInput(
                "平方根操作要求非负数值".to_string()
            ));
        }
        let result = value.sqrt();
        Ok(MathOutput {
            result,
            operation: "sqrt".to_string(),
            details: Some(format!("{} = {}", value, result)),
        })
    }
    
    /// 对数(指定底数)
    fn log(&self, value: f64, base: f64) -> Result<MathOutput, ToolError> {
        if value <= 0.0 || base <= 0.0 || base == 1.0 {
            return Err(ToolError::InvalidInput(
                "对数操作要求正数值且底数不为1".to_string()
            ));
        }
        let result = value.log(base);
        Ok(MathOutput {
            result,
            operation: "log".to_string(),
            details: Some(format!("log_{}({}) = {}", base, value, result)),
        })
    }
    
    /// 自然对数
    fn ln(&self, value: f64) -> Result<MathOutput, ToolError> {
        if value <= 0.0 {
            return Err(ToolError::InvalidInput(
                "自然对数操作要求正数值".to_string()
            ));
        }
        let result = value.ln();
        Ok(MathOutput {
            result,
            operation: "ln".to_string(),
            details: Some(format!("ln({}) = {}", value, result)),
        })
    }
    
    /// 正弦函数
    fn sin(&self, value: f64) -> Result<MathOutput, ToolError> {
        let result = value.sin();
        Ok(MathOutput {
            result,
            operation: "sin".to_string(),
            details: Some(format!("sin({}弧度) = {}", value, result)),
        })
    }
    
    /// 余弦函数
    fn cos(&self, value: f64) -> Result<MathOutput, ToolError> {
        let result = value.cos();
        Ok(MathOutput {
            result,
            operation: "cos".to_string(),
            details: Some(format!("cos({}弧度) = {}", value, result)),
        })
    }
    
    /// 正切函数
    fn tan(&self, value: f64) -> Result<MathOutput, ToolError> {
        let result = value.tan();
        Ok(MathOutput {
            result,
            operation: "tan".to_string(),
            details: Some(format!("tan({}弧度) = {}", value, result)),
        })
    }
    
    /// 绝对值
    fn abs(&self, value: f64) -> Result<MathOutput, ToolError> {
        let result = value.abs();
        Ok(MathOutput {
            result,
            operation: "abs".to_string(),
            details: Some(format!("|{}| = {}", value, result)),
        })
    }
    
    /// 阶乘
    fn factorial(&self, value: f64) -> Result<MathOutput, ToolError> {
        if value < 0.0 {
            return Err(ToolError::InvalidInput(
                "阶乘操作要求非负整数".to_string()
            ));
        }
        let n = value as u64;
        if n > 20 {
            // 防止溢出,限制最大值为20
            return Err(ToolError::InvalidInput(
                "阶乘值过大,最大支持20".to_string()
            ));
        }
        let result = self.compute_factorial(n);
        Ok(MathOutput {
            result: result as f64,
            operation: "factorial".to_string(),
            details: Some(format!("{}! = {}", n, result)),
        })
    }
    
    /// 计算阶乘
    fn compute_factorial(&self, n: u64) -> u64 {
        if n == 0 || n == 1 {
            1
        } else {
            n * self.compute_factorial(n - 1)
        }
    }
    
    /// 取模运算
    fn mod_op(&self, a: f64, b: f64) -> Result<MathOutput, ToolError> {
        if b == 0.0 {
            return Err(ToolError::InvalidInput(
                "取模运算的除数不能为零".to_string()
            ));
        }
        let result = a % b;
        Ok(MathOutput {
            result,
            operation: "mod".to_string(),
            details: Some(format!("{} mod {} = {}", a, b, result)),
        })
    }
    
    /// 最大公约数(GCD)
    fn gcd(&self, a: f64, b: f64) -> Result<MathOutput, ToolError> {
        let a_int = a as i64;
        let b_int = b as i64;
        
        if a_int < 0 || b_int < 0 {
            return Err(ToolError::InvalidInput(
                "GCD 操作要求正整数".to_string()
            ));
        }
        
        let result = self.compute_gcd(a_int.abs(), b_int.abs());
        Ok(MathOutput {
            result: result as f64,
            operation: "gcd".to_string(),
            details: Some(format!("gcd({}, {}) = {}", a_int, b_int, result)),
        })
    }
    
    /// 计算 GCD(欧几里得算法)
    fn compute_gcd(&self, a: i64, b: i64) -> i64 {
        if b == 0 {
            a
        } else {
            self.compute_gcd(b, a % b)
        }
    }
    
    /// 最小公倍数(LCM)
    fn lcm(&self, a: f64, b: f64) -> Result<MathOutput, ToolError> {
        let a_int = a as i64;
        let b_int = b as i64;
        
        if a_int <= 0 || b_int <= 0 {
            return Err(ToolError::InvalidInput(
                "LCM 操作要求正整数".to_string()
            ));
        }
        
        let gcd = self.compute_gcd(a_int, b_int);
        let result = (a_int * b_int) / gcd;
        Ok(MathOutput {
            result: result as f64,
            operation: "lcm".to_string(),
            details: Some(format!("lcm({}, {}) = {}", a_int, b_int, result)),
        })
    }
    
    /// 圆周率
    fn pi(&self) -> MathOutput {
        MathOutput {
            result: std::f64::consts::PI,
            operation: "pi".to_string(),
            details: Some("π ≈ 3.141592653589793".to_string()),
        }
    }
    
    /// 自然常数 e
    fn e(&self) -> MathOutput {
        MathOutput {
            result: std::f64::consts::E,
            operation: "e".to_string(),
            details: Some("e ≈ 2.718281828459045".to_string()),
        }
    }
}

impl Default for SimpleMathTool {
    fn default() -> Self {
        Self::new()
    }
}

/// 实现 Tool trait
#[async_trait]
impl Tool for SimpleMathTool {
    type Input = MathInput;
    type Output = MathOutput;
    
    async fn invoke(&self, input: Self::Input) -> Result<Self::Output, ToolError> {
        match input.operation.as_str() {
            "power" => {
                let base = input.value.ok_or_else(|| 
                    ToolError::InvalidInput("power 操作需要 value 参数作为底数".to_string()))?;
                let exp = input.value2.ok_or_else(|| 
                    ToolError::InvalidInput("power 操作需要 value2 参数作为指数".to_string()))?;
                self.power(base, exp)
            }
            "sqrt" => {
                let value = input.value.ok_or_else(|| 
                    ToolError::InvalidInput("sqrt 操作需要 value 参数".to_string()))?;
                self.sqrt(value)
            }
            "log" => {
                let value = input.value.ok_or_else(|| 
                    ToolError::InvalidInput("log 操作需要 value 参数".to_string()))?;
                let base = input.base.unwrap_or(10.0); // 默认以10为底
                self.log(value, base)
            }
            "ln" => {
                let value = input.value.ok_or_else(|| 
                    ToolError::InvalidInput("ln 操作需要 value 参数".to_string()))?;
                self.ln(value)
            }
            "sin" => {
                let value = input.value.ok_or_else(|| 
                    ToolError::InvalidInput("sin 操作需要 value 参数(弧度)".to_string()))?;
                self.sin(value)
            }
            "cos" => {
                let value = input.value.ok_or_else(|| 
                    ToolError::InvalidInput("cos 操作需要 value 参数(弧度)".to_string()))?;
                self.cos(value)
            }
            "tan" => {
                let value = input.value.ok_or_else(|| 
                    ToolError::InvalidInput("tan 操作需要 value 参数(弧度)".to_string()))?;
                self.tan(value)
            }
            "abs" => {
                let value = input.value.ok_or_else(|| 
                    ToolError::InvalidInput("abs 操作需要 value 参数".to_string()))?;
                self.abs(value)
            }
            "factorial" => {
                let value = input.value.ok_or_else(|| 
                    ToolError::InvalidInput("factorial 操作需要 value 参数".to_string()))?;
                self.factorial(value)
            }
            "mod" => {
                let a = input.value.ok_or_else(|| 
                    ToolError::InvalidInput("mod 操作需要 value 参数".to_string()))?;
                let b = input.value2.ok_or_else(|| 
                    ToolError::InvalidInput("mod 操作需要 value2 参数".to_string()))?;
                self.mod_op(a, b)
            }
            "gcd" => {
                let a = input.value.ok_or_else(|| 
                    ToolError::InvalidInput("gcd 操作需要 value 参数".to_string()))?;
                let b = input.value2.ok_or_else(|| 
                    ToolError::InvalidInput("gcd 操作需要 value2 参数".to_string()))?;
                self.gcd(a, b)
            }
            "lcm" => {
                let a = input.value.ok_or_else(|| 
                    ToolError::InvalidInput("lcm 操作需要 value 参数".to_string()))?;
                let b = input.value2.ok_or_else(|| 
                    ToolError::InvalidInput("lcm 操作需要 value2 参数".to_string()))?;
                self.lcm(a, b)
            }
            "pi" => Ok(self.pi()),
            "e" => Ok(self.e()),
            _ => Err(ToolError::InvalidInput(
                format!("不支持的操作: {},请使用: power, sqrt, log, ln, sin, cos, tan, abs, factorial, mod, gcd, lcm, pi, e", input.operation)
            )),
        }
    }
}

/// 实现 BaseTool trait
#[async_trait]
impl BaseTool for SimpleMathTool {
    fn name(&self) -> &str {
        "math"
    }
    
    fn description(&self) -> &str {
        "高级数学工具。支持多种数学运算:
        
操作类型:
- power: 幂运算 (value^value2)
- sqrt: 平方根
- log: 对数(可指定底数,默认为10)
- ln: 自然对数
- sin, cos, tan: 三角函数(参数为弧度)
- abs: 绝对值
- factorial: 阶乘(最大支持20)
- mod: 取模运算
- gcd: 最大公约数
- lcm: 最小公倍数
- pi: 圆周率
- e: 自然常数

示例:
- 幂运算: {\"operation\": \"power\", \"value\": 2, \"value2\": 10}
- 平方根: {\"operation\": \"sqrt\", \"value\": 16}
- 对数: {\"operation\": \"log\", \"value\": 100, \"base\": 10}
- 三角函数: {\"operation\": \"sin\", \"value\": 1.5708}
- GCD: {\"operation\": \"gcd\", \"value\": 12, \"value2\": 18}"
    }
    
    async fn run(&self, input: String) -> Result<String, ToolError> {
        let parsed: MathInput = serde_json::from_str(&input)
            .map_err(|e| ToolError::InvalidInput(format!("JSON 解析失败: {}", e)))?;
        
        let output = self.invoke(parsed).await?;
        
        Ok(format!(
            "结果: {}\n详细信息: {}",
            output.result,
            output.details.unwrap_or_default()
        ))
    }
    
    fn args_schema(&self) -> Option<serde_json::Value> {
        use schemars::schema_for;
        serde_json::to_value(schema_for!(MathInput)).ok()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[tokio::test]
    async fn test_math_power() {
        let tool = SimpleMathTool::new();
        
        let input = MathInput {
            operation: "power".to_string(),
            value: Some(2.0),
            value2: Some(10.0),
            base: None,
        };
        
        let result = tool.invoke(input).await.unwrap();
        assert_eq!(result.result, 1024.0);
    }
    
    #[tokio::test]
    async fn test_math_sqrt() {
        let tool = SimpleMathTool::new();
        
        let input = MathInput {
            operation: "sqrt".to_string(),
            value: Some(16.0),
            value2: None,
            base: None,
        };
        
        let result = tool.invoke(input).await.unwrap();
        assert_eq!(result.result, 4.0);
    }
    
    #[tokio::test]
    async fn test_math_log() {
        let tool = SimpleMathTool::new();
        
        let input = MathInput {
            operation: "log".to_string(),
            value: Some(100.0),
            value2: None,
            base: Some(10.0),
        };
        
        let result = tool.invoke(input).await.unwrap();
        assert_eq!(result.result, 2.0);
    }
    
    #[tokio::test]
    async fn test_math_ln() {
        let tool = SimpleMathTool::new();
        
        let input = MathInput {
            operation: "ln".to_string(),
            value: Some(std::f64::consts::E),
            value2: None,
            base: None,
        };
        
        let result = tool.invoke(input).await.unwrap();
        assert!((result.result - 1.0).abs() < 0.0001);
    }
    
    #[tokio::test]
    async fn test_math_sin() {
        let tool = SimpleMathTool::new();
        
        let input = MathInput {
            operation: "sin".to_string(),
            value: Some(std::f64::consts::PI / 2.0),
            value2: None,
            base: None,
        };
        
        let result = tool.invoke(input).await.unwrap();
        assert!((result.result - 1.0).abs() < 0.0001);
    }
    
    #[tokio::test]
    async fn test_math_factorial() {
        let tool = SimpleMathTool::new();
        
        let input = MathInput {
            operation: "factorial".to_string(),
            value: Some(5.0),
            value2: None,
            base: None,
        };
        
        let result = tool.invoke(input).await.unwrap();
        assert_eq!(result.result, 120.0);
    }
    
    #[tokio::test]
    async fn test_math_gcd() {
        let tool = SimpleMathTool::new();
        
        let input = MathInput {
            operation: "gcd".to_string(),
            value: Some(12.0),
            value2: Some(18.0),
            base: None,
        };
        
        let result = tool.invoke(input).await.unwrap();
        assert_eq!(result.result, 6.0);
    }
    
    #[tokio::test]
    async fn test_math_lcm() {
        let tool = SimpleMathTool::new();
        
        let input = MathInput {
            operation: "lcm".to_string(),
            value: Some(4.0),
            value2: Some(6.0),
            base: None,
        };
        
        let result = tool.invoke(input).await.unwrap();
        assert_eq!(result.result, 12.0);
    }
    
    #[tokio::test]
    async fn test_math_pi() {
        let tool = SimpleMathTool::new();
        
        let input = MathInput {
            operation: "pi".to_string(),
            value: None,
            value2: None,
            base: None,
        };
        
        let result = tool.invoke(input).await.unwrap();
        assert!((result.result - std::f64::consts::PI).abs() < 0.0001);
    }
    
    #[tokio::test]
    async fn test_math_abs() {
        let tool = SimpleMathTool::new();
        
        let input = MathInput {
            operation: "abs".to_string(),
            value: Some(-5.0),
            value2: None,
            base: None,
        };
        
        let result = tool.invoke(input).await.unwrap();
        assert_eq!(result.result, 5.0);
    }
    
    #[tokio::test]
    async fn test_math_sqrt_negative_error() {
        let tool = SimpleMathTool::new();
        
        let input = MathInput {
            operation: "sqrt".to_string(),
            value: Some(-4.0),
            value2: None,
            base: None,
        };
        
        let result = tool.invoke(input).await;
        assert!(result.is_err());
    }
    
    #[tokio::test]
    async fn test_math_factorial_overflow_error() {
        let tool = SimpleMathTool::new();
        
        let input = MathInput {
            operation: "factorial".to_string(),
            value: Some(25.0),
            value2: None,
            base: None,
        };
        
        let result = tool.invoke(input).await;
        assert!(result.is_err());
    }
    
    #[tokio::test]
    async fn test_math_base_tool_run() {
        let tool = SimpleMathTool::new();
        
        let input = "{\"operation\": \"power\", \"value\": 3, \"value2\": 4}".to_string();
        let result = tool.run(input).await.unwrap();
        
        assert!(result.contains("81"));
        assert!(result.contains("3^4"));
    }
}