diff --git a/tool_functions.py b/tool_functions.py index cac6c54..2f9ecc3 100644 --- a/tool_functions.py +++ b/tool_functions.py @@ -4,6 +4,7 @@ from tool_helper import tool import math_lexer import math_ast import math_interpreter +import utils @tool @@ -42,28 +43,34 @@ def math_evaluate(expression: str): Args: expression: Reduce mathematic expression (without '=') algebraically. """ - - tokens = math_lexer.tokenize(expression) - parser = math_ast.Parser() - ast = parser.parse(tokens) - return math_interpreter.interpret(ast) + try: + tokens = math_lexer.tokenize(expression) + parser = math_ast.Parser() + ast = parser.parse(tokens) + return math_interpreter.interpret(ast) + except Exception as e: + utils.print_error("Tool call evaluation failed. - " + str(e)) + return "Tool call evaluation failed." @tool def math_solve(equations: list[str], variables: list[str]): - """evaluate a mathematical equation system and solve equation systems. Can be used to solve (x + 1)*(x - 1) = 1 for x as an example. + """evaluate a mathematical equation system and solve equation systems. Args: equations: list of mathematical equations containing a '='. variables: list of variables to solve for. Must be lower or equal the number of given equations. """ - - expression = "solve " + " and ".join(equations) + " for " + " and ".join(variables) - print(expression) - - tokens = math_lexer.tokenize(expression) - parser = math_ast.Parser() - ast = parser.parse(tokens) - return math_interpreter.interpret(ast) + try: + expression = "solve " + " and ".join(equations) + " for " + " and ".join(variables) + print(expression) + + tokens = math_lexer.tokenize(expression) + parser = math_ast.Parser() + ast = parser.parse(tokens) + return math_interpreter.interpret(ast) + except Exception as e: + utils.print_error("Tool call evaluation failed. - " + str(e)) + return "Tool call evaluation failed."