You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

73 lines
2.0 KiB

import random
import datetime
from tool_helper import tool
import math_lexer
import math_ast
import math_interpreter
@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.
"""
tokens = math_lexer.tokenize(expression)
parser = math_ast.Parser()
ast = parser.parse(tokens)
return math_interpreter.interpret(ast)
@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.
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)
def register_dummy():
"""dummy function to run and be sure the decorators have been initialized"""
pass