Skip to content

Commit

Permalink
Tool: improve calculator (#178)
Browse files Browse the repository at this point in the history
* Tool: improve calculator

* remove error message

* lint

* comments
  • Loading branch information
lusmoura authored Jun 6, 2024
1 parent 2f420f3 commit 55190fa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/backend/config/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/backend/tests/tools/test_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."}
18 changes: 14 additions & 4 deletions src/backend/tools/calculator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from typing import Any, Dict, List

from py_expression_eval import Parser
Expand All @@ -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

0 comments on commit 55190fa

Please sign in to comment.