Added Tools for PowerShell and WSL (#947)

I added utilities for PowerShell and WSL. Both tools are based on the
bash utility. I also updated 'load_tools.py' with the new tools.
This commit is contained in:
dkowitz 2023-02-10 02:49:59 -05:00 committed by GitHub
parent ba54d36787
commit 46354d4f64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 10 deletions

View File

@ -12,6 +12,8 @@ from langchain.python import PythonREPL
from langchain.requests import RequestsWrapper from langchain.requests import RequestsWrapper
from langchain.serpapi import SerpAPIWrapper from langchain.serpapi import SerpAPIWrapper
from langchain.utilities.bash import BashProcess 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.google_search import GoogleSearchAPIWrapper
from langchain.utilities.wolfram_alpha import WolframAlphaAPIWrapper from langchain.utilities.wolfram_alpha import WolframAlphaAPIWrapper
@ -39,11 +41,26 @@ def _get_terminal() -> Tool:
"Executes commands in a terminal. Input should be valid commands, and the output will be any output from running that command.", "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 = { _BASE_TOOLS = {
"python_repl": _get_python_repl, "python_repl": _get_python_repl,
"requests": _get_requests, "requests": _get_requests,
"terminal": _get_terminal, "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: def _get_llm_math(llm: BaseLLM) -> Tool:
return Tool( return Tool(
name="Calculator", "Calculator",
description="Useful for when you need to answer questions about math.", LLMMathChain(llm=llm).run,
func=LLMMathChain(llm=llm, callback_manager=llm.callback_manager).run, "Useful for when you need to answer questions about math.",
coroutine=LLMMathChain(llm=llm, callback_manager=llm.callback_manager).arun,
) )
@ -133,10 +149,9 @@ def _get_google_search(**kwargs: Any) -> Tool:
def _get_serpapi(**kwargs: Any) -> Tool: def _get_serpapi(**kwargs: Any) -> Tool:
return Tool( return Tool(
name="Search", "Search",
description="A search engine. Useful for when you need to answer questions about current events. Input should be a search query.", SerpAPIWrapper(**kwargs).run,
func=SerpAPIWrapper(**kwargs).run, "A search engine. Useful for when you need to answer questions about current events. Input should be a search query.",
coroutine=SerpAPIWrapper(**kwargs).arun,
) )
@ -147,7 +162,7 @@ _EXTRA_LLM_TOOLS = {
_EXTRA_OPTIONAL_TOOLS = { _EXTRA_OPTIONAL_TOOLS = {
"wolfram-alpha": (_get_wolfram_alpha, ["wolfram_alpha_appid"]), "wolfram-alpha": (_get_wolfram_alpha, ["wolfram_alpha_appid"]),
"google-search": (_get_google_search, ["google_api_key", "google_cse_id"]), "google-search": (_get_google_search, ["google_api_key", "google_cse_id"]),
"serpapi": (_get_serpapi, ["serpapi_api_key", "aiosession"]), "serpapi": (_get_serpapi, ["serpapi_api_key"]),
} }

View File

@ -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

View File

@ -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