diff --git a/src/backend/config/tools.py b/src/backend/config/tools.py index 4541d77e39..01247222e3 100644 --- a/src/backend/config/tools.py +++ b/src/backend/config/tools.py @@ -87,7 +87,7 @@ class ToolName(StrEnum): implementation=Calculator, parameter_definitions={ "code": { - "description": "Arithmetic expression to evaluate", + "description": "The expression for the calculator to evaluate, it should be a valid mathematical expression.", "type": "str", "required": True, } @@ -96,7 +96,7 @@ class ToolName(StrEnum): is_available=Calculator.is_available(), error_message="Calculator tool not available.", category=Category.Function, - description="Evaluate arithmetic expressions.", + description="This is a powerful multi-purpose calculator. It is capable of a wide array of math calculation and a range of other useful features. Features include a large library of customizable functions, unit calculations and conversion, currency conversion, symbolic calculations (including integrals and equations) and interval arithmetic.", ), ToolName.Tavily_Internet_Search: ManagedTool( name=ToolName.Tavily_Internet_Search, diff --git a/src/backend/tests/tools/test_calculator.py b/src/backend/tests/tools/test_calculator.py index 8fdc706e13..af387344ac 100644 --- a/src/backend/tests/tools/test_calculator.py +++ b/src/backend/tests/tools/test_calculator.py @@ -4,10 +4,10 @@ def test_calculator() -> None: calculator = Calculator() result = calculator.call({"code": "2+2"}) - assert result == {"result": 4} + assert result == {"text": 4} def test_calculator_invalid_syntax() -> None: calculator = Calculator() result = calculator.call({"code": "2+"}) - assert result == {"result": "Parsing error - syntax not allowed."} + assert result == {"text": "Parsing error - syntax not allowed."} diff --git a/src/backend/tools/calculator.py b/src/backend/tools/calculator.py index b006091be0..1a76ca255f 100644 --- a/src/backend/tools/calculator.py +++ b/src/backend/tools/calculator.py @@ -1,3 +1,4 @@ +import logging from typing import Any, Dict, List from py_expression_eval import Parser @@ -16,11 +17,20 @@ def is_available(cls) -> bool: def call(self, parameters: dict, **kwargs: Any) -> List[Dict[str, Any]]: math_parser = Parser() - to_evaluate = parameters.get("code", "").replace("pi", "PI").replace("e", "E") + expression = parameters.get("code", "") + + # remove lines that start with # and empty lines + expression = "\n".join( + [line for line in expression.split("\n") if not line.startswith("#")] + ) + + to_evaluate = expression.replace("pi", "PI").replace("e", "E") result = [] try: - result = {"result": math_parser.parse(to_evaluate).evaluate({})} - except Exception: - result = {"result": "Parsing error - syntax not allowed."} + result = {"text": math_parser.parse(to_evaluate).evaluate({})} + except Exception as e: + logging.error(f"Error parsing expression: {e}") + result = {"text": f"Parsing error - syntax not allowed."} + return result