antoncio commited on
Commit
b412ed7
·
verified ·
1 Parent(s): 9f39c86

added calculator tool in app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -0
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
@@ -18,6 +20,31 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
18
  """
19
  return "What magic will you build ?"
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
 
1
+ from math import *
2
+
3
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
4
  import datetime
5
  import requests
 
20
  """
21
  return "What magic will you build ?"
22
 
23
+ @tool
24
+ def calculator(math_expr: str) -> tuple[float | None, str]:
25
+ """Calculates any math expression, that is passed as a string
26
+ Args:
27
+ math_expr (str): math expression to be evaluated.
28
+ Expression can contain any operations and constants from math standart python library and basic math operations,
29
+ like: +,-,/,*,** and so on.
30
+ Returns:
31
+ - float result of math expression evaluation (is expression was evaluated correctly, else: None)
32
+ - status_code: 'ok' or 'failed'
33
+ Examples:
34
+ 1)
35
+ >>> calculator(math_expr='3 + 5')[0]
36
+ >>> 8
37
+
38
+ 2)
39
+ >>> calculator(math_expr='(exp(-3.5)**2 - sin(pi/2)) / log2(3)')[0]
40
+ >>> -0.630354420107644
41
+ """
42
+ try:
43
+ return float(eval(math_expr)), 'ok' # status_code
44
+ except Exception as e:
45
+ print(f"Exception raised when trying to compute expression {math_expr}")
46
+ return None, 'failed'
47
+
48
  @tool
49
  def get_current_time_in_timezone(timezone: str) -> str:
50
  """A tool that fetches the current local time in a specified timezone.