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.
74 lines
2.5 KiB
74 lines
2.5 KiB
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")
|
|
# 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 = 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]):
|
|
"""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 = 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
|