lucasnseq commited on
Commit
bbca027
·
verified ·
1 Parent(s): ae7a494

Add solve_equation tool

Browse files
Files changed (1) hide show
  1. app.py +56 -7
app.py CHANGED
@@ -4,19 +4,68 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
 
 
 
7
 
8
  from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
 
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -55,7 +104,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ import re
8
+ from typing import List, Union
9
+ import sympy as sp
10
+ from sympy.core.expr import Expr
11
 
12
  from Gradio_UI import GradioUI
13
 
14
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
15
  @tool
16
+ def solve_equation(equation_str: str) -> List[Union[Expr, float]]:
17
+ """
18
+ A tool for solving single-variable equation provided as a string.
19
+
20
  Args:
21
+ equation_str: A string representing the equation in the format
22
+ "<expression> = <expression>". Expressions can include numbers,
23
+ variables (e.g., 'x'), and arithmetic operators (+, -, *, /).
24
+ Exponentiation can be specified using Python's '**' operator or the
25
+ LaTeX-style '^{...}' (e.g., "x^{1/11}" is interpreted as x**(1/11)).
26
+
27
+ Returns:
28
+ A list of solutions (each a sympy.Expr or float) for the variable found in the equation.
29
+
30
+ Raises:
31
+ ValueError: If the equation does not contain an '=' sign or if no variable is found.
32
+
33
+ Example:
34
+ >>> eq_str = "x^{1/11} + (x^{10/11} / x) = 4.25"
35
+ >>> solutions = solve_equation(eq_str)
36
+ >>> for sol in solutions:
37
+ ... print(sol)
38
  """
39
+ # Convert LaTeX-style exponentiation (e.g., x^{1/11}) to Python-style (x**(1/11))
40
+ equation_str = re.sub(r'\^\{\s*([^}]*)\s*\}', r'**(\1)', equation_str)
41
+ # Replace any remaining '^' with '**'
42
+ equation_str = equation_str.replace('^', '**')
43
+
44
+ # Split the string into left-hand side and right-hand side
45
+ if '=' not in equation_str:
46
+ raise ValueError("The equation must contain an '=' sign.")
47
+ lhs_str, rhs_str = equation_str.split('=')
48
+
49
+ # Convert the string expressions to sympy expressions
50
+ lhs: Expr = sp.sympify(lhs_str)
51
+ rhs: Expr = sp.sympify(rhs_str)
52
+
53
+ # Form the equation lhs == rhs
54
+ eq = sp.Eq(lhs, rhs)
55
+
56
+ # Determine the free symbols (variables) in the equation
57
+ variables = list(eq.free_symbols)
58
+ if not variables:
59
+ raise ValueError("No variable found in the equation.")
60
+ elif len(variables) > 1:
61
+ # For multiple variables, the first one is chosen for solving.
62
+ var = variables[0]
63
+ else:
64
+ var = variables[0]
65
+
66
+ # Solve the equation for the chosen variable
67
+ solutions: List[Union[Expr, float]] = sp.solve(eq, var)
68
+ return solutions
69
 
70
  @tool
71
  def get_current_time_in_timezone(timezone: str) -> str:
 
104
 
105
  agent = CodeAgent(
106
  model=model,
107
+ tools=[final_answer, solve_equation], ## add your tools here (don't remove final answer)
108
  max_steps=6,
109
  verbosity_level=1,
110
  grammar=None,