|
from langchain.tools import BaseTool |
|
import math |
|
|
|
class CalculatorTool(BaseTool): |
|
"""A tool for performing basic arithmetic operations.""" |
|
|
|
name = "calculator" |
|
description = "Performs arithmetic calculations given a mathematical expression." |
|
|
|
def _run(self, expression: str): |
|
try: |
|
result = eval(expression, {"__builtins__": None}, math.__dict__) |
|
return f"Calculation result: {result}" |
|
except Exception as e: |
|
return f"Error in calculation: {str(e)}" |
|
|
|
async def _arun(self, expression: str): |
|
return self._run(expression) |
|
|
|
# Add the calculator to your list of tools |
|
tools.append(CalculatorTool()) |