add mathematical solver engine based on sympy
This commit is contained in:
@@ -1,24 +1,29 @@
|
||||
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))
|
||||
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):
|
||||
@@ -31,5 +36,37 @@ Args:
|
||||
|
||||
|
||||
|
||||
@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():
|
||||
pass # dummy function to run and be sure the decorators have run
|
Reference in New Issue
Block a user