LecJackS commited on
Commit
fc48ec4
1 Parent(s): 1621cf9

Upload tool

Browse files
Files changed (2) hide show
  1. requirements.txt +2 -1
  2. wolfram_alpha_tool.py +32 -7
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
- requests
2
  transformers
 
 
1
+ os
2
  transformers
3
+ requests
wolfram_alpha_tool.py CHANGED
@@ -1,5 +1,6 @@
1
- from transformers import Tool
2
  import requests
 
3
 
4
  class WolframAlpha(Tool):
5
  name = "wolfram_alpha"
@@ -8,9 +9,27 @@ class WolframAlpha(Tool):
8
  inputs = ["query"]
9
  outputs = ["result"]
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def __call__(self, query, output='xml', pod_format='plaintext'):
12
- CFG_appid="YOUR_WOLFRAM_API_KEY_HERE"
13
- base_url = 'http://api.wolframalpha.com/v2/query'
14
 
15
  output_opts = ['xml','json']
16
  format_opts = ['plaintext', 'image', 'image,imagemap', 'mathml', 'sound', 'wav']
@@ -24,13 +43,19 @@ class WolframAlpha(Tool):
24
  params = {
25
  'input': query,
26
  'output': output,
27
- 'appid': CFG_appid,
28
  }
29
- response = requests.get(base_url, params=params)
 
 
 
 
 
 
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:
 
1
+ import os
2
  import requests
3
+ from transformers import Tool
4
 
5
  class WolframAlpha(Tool):
6
  name = "wolfram_alpha"
 
9
  inputs = ["query"]
10
  outputs = ["result"]
11
 
12
+ def __init__(self):
13
+ self.base_url = 'http://api.wolframalpha.com/v2/query'
14
+
15
+ self.app_id = os.environ.get('WOLFRAM_APP_ID')
16
+ if self.app_id is None:
17
+ raise ValueError("Please set the `WOLFRAM_APP_ID` as an environment variable in order to instantiate the Wolfram tool.\nTo do so, before instantiating the class, run:\nos.environ['WOLFRAM_APP_ID'] = 'YOUR_WOLFRAM_APP_ID'")
18
+
19
+ print("Making sure APP_ID is valid... ", end="")
20
+ dummy_params = {
21
+ 'input': '1+1',
22
+ 'output': 'xml',
23
+ 'appid': self.app_id,
24
+ }
25
+ response = self.make_request(params=dummy_params)
26
+ if "Invalid appid" in response:
27
+ appid_url = 'https://developer.wolframalpha.com/portal/myapps/index.html'
28
+ raise ValueError(f"Please set a valid `WOLFRAM_APP_ID` as an environment variable.\nWolframAlpha is not validating APP_ID: {self.app_id}\nTo get an APP_ID, go to:\n{appid_url}\nand click on [Get an AppID]")
29
+ print("APP_ID validated! Tool ready to use.")
30
+
31
  def __call__(self, query, output='xml', pod_format='plaintext'):
32
+
 
33
 
34
  output_opts = ['xml','json']
35
  format_opts = ['plaintext', 'image', 'image,imagemap', 'mathml', 'sound', 'wav']
 
43
  params = {
44
  'input': query,
45
  'output': output,
46
+ 'appid': self.app_id,
47
  }
48
+
49
+ response = self.make_request(params)
50
+
51
+ return response
52
+
53
+ def make_request(self, params):
54
+ response = requests.get(self.base_url, params=params)
55
  if response.status_code == 200:
56
+ if params['output'] == 'xml':
57
  return response.text
58
+ elif params['output'] == 'json':
59
  # Remove unnecessary empty spaces
60
  return str(response.json())
61
  else: