-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
calculator.ts
36 lines (30 loc) · 929 Bytes
/
calculator.ts
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
import { Parser } from "expr-eval";
import { Tool } from "@langchain/core/tools";
/**
* The Calculator class is a tool used to evaluate mathematical
* expressions. It extends the base Tool class.
* @example
* ```typescript
* const calculator = new Calculator();
* const sum = calculator.add(99, 99);
* console.log("The sum of 99 and 99 is:", sum);
* ```
*/
export class Calculator extends Tool {
static lc_name() {
return "Calculator";
}
get lc_namespace() {
return [...super.lc_namespace, "calculator"];
}
name = "calculator";
/** @ignore */
async _call(input: string) {
try {
return Parser.evaluate(input).toString();
} catch (error) {
return "I don't know how to do that.";
}
}
description = `Useful for getting the result of a math expression. The input to this tool should be a valid mathematical expression that could be executed by a simple calculator.`;
}