import random import datetime from chatbug.tool_helper import tool import chatbug.matheval as matheval # from chatbug.matheval import interpreter, lexer # from chatbug.matheval.ast import Parser import chatbug.utils as 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") # return f"The current local date and time is {datetime.datetime.now().strftime('%Y-%m-%d %H:%M %p')}." # @tool # def random_float(): # """Generate a random float in range 0 to 1.""" # # return str(random.random()) # return f"The freshly generated a random number from 0..1 is: {random.random():.5f}." # @tool # def random_int(a: int, b: int): # """Generate a random integer in the range [a, b], including both end points. # Args: # a: minimum possible value (must be <= b) # b: maximum possible value (must be >= a)""" # # return str(random.randint(a, b)) # return f"A fresh generated random integer between {a} and {b} is {random.randint(a, b)}." @tool def math_evaluate(expression: str): """Evaluate and simplify a mathematical expression. Returns the evaluated result or a simplified version of the expression as a string. Args: expression: A valid arithmetic expression (e.g., '2 + 3 * 4'). The expression must not contain '='.""" try: tokens = matheval.lexer.tokenize(expression) parser = matheval.ast.Parser() ast = parser.parse(tokens) return matheval.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]): """Solve a system of linear or non-linear equation system. Returns the solutions as a string, or an error message if the input is invalid or unsolvable. Args: equations: A list of mathematical equations in the format 'x + y = 2'. variables: A list of variables to solve for. The number of variables must not exceed the number of equations.""" try: expression = "solve " + " and ".join(equations) + " for " + " and ".join(variables) print(expression) tokens = lexer.tokenize(expression) parser = ast.Parser() ast = parser.parse(tokens) return 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