LecJackS commited on
Commit
4bf4406
1 Parent(s): 503290b

Create wolfram-alpha-tool

Browse files

# Usage:

CFG_appid=<WOLFRAM API KEY>
# https://products.wolframalpha.com/simple-api/documentation

# Test it:
query = "Integrate [ log(x)^2 + e^(x^2) dx ]"
wolframalpha_tool = WolframAlpha()
print(wolframalpha_tool(query))

# Add tool
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder", additional_tools=[wolframalpha_tool])

# Ask the agent to solve some math
res = agent.run("Solve the following equation: Area of circle of radius 2")
print(res)

res = agent.run("Integrate log(x)^2 + e^(x^2) dx")
print(res)

Files changed (1) hide show
  1. wolfram-alpha-tool +35 -0
wolfram-alpha-tool ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class WolframAlpha(Tool):
2
+ name = "wolfram_alpha"
3
+ 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.")
4
+
5
+ inputs = ["query"]
6
+ outputs = ["result"]
7
+
8
+ def __call__(self, query, output='xml', pod_format='plaintext'):
9
+ base_url = 'http://api.wolframalpha.com/v2/query'
10
+
11
+ output_opts = ['xml','json']
12
+ format_opts = ['plaintext', 'image', 'image,imagemap', 'mathml', 'sound', 'wav']
13
+
14
+ if output not in output_opts:
15
+ return f"{output} is not a correct output parameter! It must be one of these: {output_opts}"
16
+
17
+ if pod_format not in format_opts:
18
+ return f"{pod_format} is not a correct pod_format parameter! It must be one of these: {format_opts}"
19
+
20
+ params = {
21
+ 'input': query,
22
+ 'output': output,
23
+ 'appid': CFG_appid,
24
+ }
25
+ print("About to response")
26
+ response = requests.get(base_url, params=params)
27
+ print("response:",response)
28
+ if response.status_code == 200:
29
+ if output == 'xml':
30
+ return response.text
31
+ elif output == 'json':
32
+ # Remove unnecessary empty spaces
33
+ return str(response.json())
34
+ else:
35
+ return f"There was an error with the request, with response: {response}"