File size: 1,470 Bytes
43ae560
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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}"