lysandre HF staff commited on
Commit
43ae560
1 Parent(s): 13758b1

Support load_tool

Browse files
Files changed (3) hide show
  1. requirements.txt +1 -0
  2. tool_config.json +5 -0
  3. wolfram_alpha_tool.py +37 -0
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ transformers
tool_config.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "description": "This is a tool that uses WolframAlpha to compute any mathematical question. It takes one input query, and returns a verbose result in xml format, which includes the solution.",
3
+ "name": "wolfram_alpha",
4
+ "tool_class": "wolfram_alpha_tool.WolframAlpha"
5
+ }
wolfram_alpha_tool.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import Tool
2
+
3
+ class WolframAlpha(Tool):
4
+ name = "wolfram_alpha"
5
+ description = ("This is a tool that uses WolframAlpha to compute any mathematical question. It takes one input query, and returns a verbose result in xml format, which includes the solution.")
6
+
7
+ inputs = ["query"]
8
+ outputs = ["result"]
9
+
10
+ def __call__(self, query, output='xml', pod_format='plaintext'):
11
+ base_url = 'http://api.wolframalpha.com/v2/query'
12
+
13
+ output_opts = ['xml','json']
14
+ format_opts = ['plaintext', 'image', 'image,imagemap', 'mathml', 'sound', 'wav']
15
+
16
+ if output not in output_opts:
17
+ return f"{output} is not a correct output parameter! It must be one of these: {output_opts}"
18
+
19
+ if pod_format not in format_opts:
20
+ return f"{pod_format} is not a correct pod_format parameter! It must be one of these: {format_opts}"
21
+
22
+ params = {
23
+ 'input': query,
24
+ 'output': output,
25
+ 'appid': CFG_appid,
26
+ }
27
+ print("About to response")
28
+ response = requests.get(base_url, params=params)
29
+ print("response:",response)
30
+ if response.status_code == 200:
31
+ if output == 'xml':
32
+ return response.text
33
+ elif output == 'json':
34
+ # Remove unnecessary empty spaces
35
+ return str(response.json())
36
+ else:
37
+ return f"There was an error with the request, with response: {response}"