diff --git a/langchain/agents/load_tools.py b/langchain/agents/load_tools.py index 2c520973277..28e037636b5 100644 --- a/langchain/agents/load_tools.py +++ b/langchain/agents/load_tools.py @@ -12,6 +12,8 @@ from langchain.python import PythonREPL from langchain.requests import RequestsWrapper from langchain.serpapi import SerpAPIWrapper from langchain.utilities.bash import BashProcess +from langchain.utilities.wsl import WSLProcess +from langchain.utilities.powershell import PowerShellProcess from langchain.utilities.google_search import GoogleSearchAPIWrapper from langchain.utilities.wolfram_alpha import WolframAlphaAPIWrapper @@ -38,12 +40,27 @@ def _get_terminal() -> Tool: BashProcess().run, "Executes commands in a terminal. Input should be valid commands, and the output will be any output from running that command.", ) - + +def _get_wsl() -> Tool: + return Tool( + "WSL", + WSLProcess().run, + "Executes bash commands in a linux system running on Windows Subsystem for Linux (WSL). Input should be valid bash commands, and the output will be any output from running that command.", + ) + +def _get_powershell() -> Tool: + return Tool( + "PowerShell", + PowerShellProcess().run, + "Executes commands in PowerShell. Input should be valid commands, and the output will be any output from running that command.", + ) _BASE_TOOLS = { "python_repl": _get_python_repl, "requests": _get_requests, "terminal": _get_terminal, + "wsl": _get_wsl, + "powershell": _get_powershell, } @@ -65,10 +82,9 @@ def _get_pal_colored_objects(llm: BaseLLM) -> Tool: def _get_llm_math(llm: BaseLLM) -> Tool: return Tool( - name="Calculator", - description="Useful for when you need to answer questions about math.", - func=LLMMathChain(llm=llm, callback_manager=llm.callback_manager).run, - coroutine=LLMMathChain(llm=llm, callback_manager=llm.callback_manager).arun, + "Calculator", + LLMMathChain(llm=llm).run, + "Useful for when you need to answer questions about math.", ) @@ -133,10 +149,9 @@ def _get_google_search(**kwargs: Any) -> Tool: def _get_serpapi(**kwargs: Any) -> Tool: return Tool( - name="Search", - description="A search engine. Useful for when you need to answer questions about current events. Input should be a search query.", - func=SerpAPIWrapper(**kwargs).run, - coroutine=SerpAPIWrapper(**kwargs).arun, + "Search", + SerpAPIWrapper(**kwargs).run, + "A search engine. Useful for when you need to answer questions about current events. Input should be a search query.", ) @@ -147,7 +162,7 @@ _EXTRA_LLM_TOOLS = { _EXTRA_OPTIONAL_TOOLS = { "wolfram-alpha": (_get_wolfram_alpha, ["wolfram_alpha_appid"]), "google-search": (_get_google_search, ["google_api_key", "google_cse_id"]), - "serpapi": (_get_serpapi, ["serpapi_api_key", "aiosession"]), + "serpapi": (_get_serpapi, ["serpapi_api_key"]), } diff --git a/langchain/utilities/powershell.py b/langchain/utilities/powershell.py new file mode 100644 index 00000000000..e54b8fc2821 --- /dev/null +++ b/langchain/utilities/powershell.py @@ -0,0 +1,26 @@ +"""Wrapper around subprocess to run commands.""" +import subprocess +from typing import List, Union + + +class PowerShellProcess: + """Executes PowerShell commands and returns the output.""" + + def __init__(self, strip_newlines: bool = False): + """Initialize with stripping newlines.""" + self.strip_newlines = strip_newlines + + def run(self, commands: Union[str, List[str]]) -> str: + """Run commands and return final output.""" + if isinstance(commands, str): + commands = [commands] + commands = ";".join(commands) + + try: + output = subprocess.check_output(["powershell.exe", "-Command", commands]).decode() + + except subprocess.CalledProcessError as error: + return str(error) + if self.strip_newlines: + output = output.strip() + return output diff --git a/langchain/utilities/wsl.py b/langchain/utilities/wsl.py new file mode 100644 index 00000000000..660a29116d8 --- /dev/null +++ b/langchain/utilities/wsl.py @@ -0,0 +1,26 @@ +"""Wrapper around subprocess to run commands.""" +import subprocess +from typing import List, Union + + +class WSLProcess: + """Executes bash commands on Windows Subsystem for Linux (WSL) and returns the output.""" + + def __init__(self, strip_newlines: bool = False): + """Initialize with stripping newlines.""" + self.strip_newlines = strip_newlines + + def run(self, commands: Union[str, List[str]]) -> str: + """Run commands and return final output.""" + if isinstance(commands, str): + commands = [commands] + commands = ";".join(commands) + commands = 'wsl.exe ' + commands + try: + output = subprocess.check_output(commands, shell=True).decode() + + except subprocess.CalledProcessError as error: + return str(error) + if self.strip_newlines: + output = output.strip() + return output