52 lines
1.6 KiB
JavaScript
52 lines
1.6 KiB
JavaScript
|
"use strict";
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.Calculator = void 0;
|
||
|
const expr_eval_1 = require("expr-eval");
|
||
|
const tools_1 = require("@langchain/core/tools");
|
||
|
/**
|
||
|
* The Calculator class is a tool used to evaluate mathematical
|
||
|
* expressions. It extends the base Tool class.
|
||
|
* @example
|
||
|
* ```typescript
|
||
|
* import { Calculator } from "@langchain/community/tools/calculator";
|
||
|
*
|
||
|
* const calculator = new Calculator();
|
||
|
* const sum = await calculator.invoke("99 + 99");
|
||
|
* console.log("The sum of 99 and 99 is:", sum);
|
||
|
* // The sum of 99 and 99 is: 198
|
||
|
* ```
|
||
|
*/
|
||
|
class Calculator extends tools_1.Tool {
|
||
|
constructor() {
|
||
|
super(...arguments);
|
||
|
Object.defineProperty(this, "name", {
|
||
|
enumerable: true,
|
||
|
configurable: true,
|
||
|
writable: true,
|
||
|
value: "calculator"
|
||
|
});
|
||
|
Object.defineProperty(this, "description", {
|
||
|
enumerable: true,
|
||
|
configurable: true,
|
||
|
writable: true,
|
||
|
value: `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.`
|
||
|
});
|
||
|
}
|
||
|
static lc_name() {
|
||
|
return "Calculator";
|
||
|
}
|
||
|
get lc_namespace() {
|
||
|
return [...super.lc_namespace, "calculator"];
|
||
|
}
|
||
|
/** @ignore */
|
||
|
async _call(input) {
|
||
|
try {
|
||
|
return expr_eval_1.Parser.evaluate(input).toString();
|
||
|
}
|
||
|
catch (error) {
|
||
|
return "I don't know how to do that.";
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
exports.Calculator = Calculator;
|