File size: 2,588 Bytes
fc48ec4
bedaad1
fc48ec4
d3d11f8
4bf4406
 
bedaad1
4bf4406
 
 
 
3183b9a
fc48ec4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bf4406
fc48ec4
4bf4406
 
 
 
 
 
 
 
 
 
 
 
 
fc48ec4
4bf4406
fc48ec4
 
 
 
 
 
 
4bf4406
fc48ec4
4bf4406
fc48ec4
4bf4406
 
 
bedaad1
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import requests
from transformers import Tool

class WolframAlpha(Tool):
    name = "wolfram_alpha"
    description = ("This is a tool that uses WolframAlpha to compute any mathematical query. It takes one input query, and returns a verbose result in xml format, which includes the solution.")

    inputs = ["query"]
    outputs = ["result"]

    def __init__(self, *args, **kwargs):
        self.base_url = 'http://api.wolframalpha.com/v2/query'

        self.app_id = os.environ.get('WOLFRAM_APP_ID')
        if self.app_id is None:
            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'")

        print("Making sure APP_ID is valid... ", end="")
        dummy_params = {
            'input': '1+1',
            'output': 'xml',
            'appid': self.app_id,
        }
        response = self.make_request(params=dummy_params)
        if "Invalid appid" in response:
            appid_url = 'https://developer.wolframalpha.com/portal/myapps/index.html'
            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]")
        print("APP_ID validated! Tool ready to use.")

    def __call__(self, query, output='xml', pod_format='plaintext'):
        

        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': self.app_id,
        }
        
        response = self.make_request(params)

        return response
    
    def make_request(self, params):
        response = requests.get(self.base_url, params=params)
        if response.status_code == 200:
            if params['output'] == 'xml':
                return response.text
            elif params['output'] == 'json':
                # Remove unnecessary empty spaces
                return str(response.json())
        else:
            return f"There was an error with the request, with response: {response}"