wolfram-alpha-query / wolfram_alpha_tool.py
LecJackS's picture
Support `load_tool` (#1)
d3d11f8
raw
history blame
1.47 kB
from transformers import Tool
class WolframAlpha(Tool):
name = "wolfram_alpha"
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.")
inputs = ["query"]
outputs = ["result"]
def __call__(self, query, output='xml', pod_format='plaintext'):
base_url = 'http://api.wolframalpha.com/v2/query'
output_opts = ['xml','json']
format_opts = ['plaintext', 'image', 'image,imagemap', 'mathml', 'sound', 'wav']
if output not in output_opts:
return f"{output} is not a correct output parameter! It must be one of these: {output_opts}"
if pod_format not in format_opts:
return f"{pod_format} is not a correct pod_format parameter! It must be one of these: {format_opts}"
params = {
'input': query,
'output': output,
'appid': CFG_appid,
}
print("About to response")
response = requests.get(base_url, params=params)
print("response:",response)
if response.status_code == 200:
if output == 'xml':
return response.text
elif output == 'json':
# Remove unnecessary empty spaces
return str(response.json())
else:
return f"There was an error with the request, with response: {response}"