import asyncio

from langchain.tools import BaseTool
from langchain.utilities.wolfram_alpha import WolframAlphaAPIWrapper


class WolframAlpha(BaseTool):
    name = "Wolfram Alpha"
    description = "useful for calculation and computation"
    return_direct = False

    def _run(self, input: str) -> str:
        app_id = self.metadata["appId"]
        wolfram = WolframAlphaAPIWrapper(wolfram_alpha_appid=app_id)
        return wolfram.run(input)

    async def _arun(self, input: str) -> str:
        app_id = self.metadata["appId"]
        wolfram = WolframAlphaAPIWrapper(wolfram_alpha_appid=app_id)
        loop = asyncio.get_event_loop()
        output = await loop.run_in_executor(None, wolfram.run, input)
        return output
