ABVM commited on
Commit
73e4c8e
·
verified ·
1 Parent(s): ce2b3fa

Create calculator_tool.py

Browse files
Files changed (1) hide show
  1. calculator_tool.py +37 -0
calculator_tool.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #from https://github.com/aymeric-roucher/GAIA/blob/main/scripts/experiments/calculator_tool.py
2
+ import math
3
+ import numexpr
4
+ from typing import Dict
5
+ from transformers.agents import Tool
6
+
7
+ class CalculatorTool(Tool):
8
+ name = "calculator"
9
+ description = "This is a tool that performs simple arithmetic operations."
10
+
11
+ inputs = {
12
+ "expression": {
13
+ "type": "text",
14
+ "description": "The expression to be evaluated.The variables used CANNOT be placeholders like 'x' or 'mike's age', they must be numbers",
15
+ },
16
+ "useless_expression": {
17
+ "type": "text",
18
+ "description": "The expression to not be evaluated.The variables used CANNOT be placeholders like 'x' or 'mike's age', they must be numbers",
19
+ }
20
+ }
21
+ output_type = "text"
22
+
23
+ def __init__(self, *args, **kwargs):
24
+ super().__init__(*args, **kwargs)
25
+
26
+ def __call__(self, expression, useless_expression):
27
+ if isinstance(expression, Dict):
28
+ expression = expression["expression"]
29
+ local_dict = {"pi": math.pi, "e": math.e}
30
+ output = str(
31
+ numexpr.evaluate(
32
+ expression.strip().replace("^", "**"),
33
+ global_dict={}, # restrict access to globals
34
+ local_dict=local_dict, # add common mathematical functions
35
+ )
36
+ )
37
+ return output