import random import datetime from tool_helper import tool import math_lexer import math_ast import math_interpreter import utils @tool def current_time(): """Get the current local date and time as a string.""" return datetime.datetime.now().strftime("%Y-%m-%d %H:%M") @tool def random_float(): """Generate a random float from 0..1.""" return str(random.random()) # @tool # def random_float(a: float=0.0, b: float=1.0): # """Generate a random float in range [a, b], including both end points. Optional pass no parameter and range 0..1 will be used. # Args: # a: minimum possible value # b: maximum possible value""" # return str(random.randint(a, b)) @tool def random_int(a: int, b: int): """Generate a random integer in range [a, b], including both end points. Args: a: minimum possible value b: maximum possible value""" return str(random.randint(a, b)) @tool def math_evaluate(expression: str): """evaluate and reduce a mathematical expression. Args: expression: Reduce mathematic expression (without '=') algebraically. """ 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. 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. """ 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." def register_dummy(): """dummy function to run and be sure the decorators have been initialized""" pass