From 8ef7274ee6ff289505d00a5b17999cc0c4412c7d Mon Sep 17 00:00:00 2001 From: whysage <67018871+whysage@users.noreply.github.com> Date: Wed, 7 Jun 2023 19:39:02 +0300 Subject: [PATCH] feat: issue-5712 add sleep tool (#5715) Fixes # 5712 added sleep tool --- docs/modules/agents/tools/getting_started.md | 6 +++ langchain/agents/load_tools.py | 6 +++ langchain/tools/sleep/__init__.py | 1 + langchain/tools/sleep/tool.py | 44 ++++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 langchain/tools/sleep/__init__.py create mode 100644 langchain/tools/sleep/tool.py diff --git a/docs/modules/agents/tools/getting_started.md b/docs/modules/agents/tools/getting_started.md index e6f5832f..879112a7 100644 --- a/docs/modules/agents/tools/getting_started.md +++ b/docs/modules/agents/tools/getting_started.md @@ -160,3 +160,9 @@ Below is a list of all supported tools and relevant information: - Notes: A connection to the OpenWeatherMap API (https://api.openweathermap.org), specifically the `/data/2.5/weather` endpoint. - Requires LLM: No - Extra Parameters: `openweathermap_api_key` (your API key to access this endpoint) + +**sleep** + +- Tool Name: Sleep +- Tool Description: Make agent sleep for some time. +- Requires LLM: No diff --git a/langchain/agents/load_tools.py b/langchain/agents/load_tools.py index 969b4838..20750fd8 100644 --- a/langchain/agents/load_tools.py +++ b/langchain/agents/load_tools.py @@ -34,6 +34,7 @@ from langchain.tools.requests.tool import ( from langchain.tools.scenexplain.tool import SceneXplainTool from langchain.tools.searx_search.tool import SearxSearchResults, SearxSearchRun from langchain.tools.shell.tool import ShellTool +from langchain.tools.sleep.tool import SleepTool from langchain.tools.wikipedia.tool import WikipediaQueryRun from langchain.tools.wolfram_alpha.tool import WolframAlphaQueryRun from langchain.tools.openweathermap.tool import OpenWeatherMapQueryRun @@ -82,6 +83,10 @@ def _get_terminal() -> BaseTool: return ShellTool() +def _get_sleep() -> BaseTool: + return SleepTool() + + _BASE_TOOLS: Dict[str, Callable[[], BaseTool]] = { "python_repl": _get_python_repl, "requests": _get_tools_requests_get, # preserved for backwards compatability @@ -91,6 +96,7 @@ _BASE_TOOLS: Dict[str, Callable[[], BaseTool]] = { "requests_put": _get_tools_requests_put, "requests_delete": _get_tools_requests_delete, "terminal": _get_terminal, + "sleep": _get_sleep, } diff --git a/langchain/tools/sleep/__init__.py b/langchain/tools/sleep/__init__.py new file mode 100644 index 00000000..4d6319e2 --- /dev/null +++ b/langchain/tools/sleep/__init__.py @@ -0,0 +1 @@ +"""Sleep tool.""" diff --git a/langchain/tools/sleep/tool.py b/langchain/tools/sleep/tool.py new file mode 100644 index 00000000..707cc329 --- /dev/null +++ b/langchain/tools/sleep/tool.py @@ -0,0 +1,44 @@ +"""Tool for agent to sleep.""" +from asyncio import sleep as asleep +from time import sleep +from typing import Optional, Type + +from pydantic import BaseModel, Field + +from langchain.callbacks.manager import ( + AsyncCallbackManagerForToolRun, + CallbackManagerForToolRun, +) +from langchain.tools.base import BaseTool + + +class SleepInput(BaseModel): + """Input for CopyFileTool.""" + + sleep_time: int = Field(..., description="Time to sleep in seconds") + + +class SleepTool(BaseTool): + """Tool that adds the capability to sleep.""" + + name = "sleep" + args_schema: Type[BaseModel] = SleepInput + description = "Make agent sleep for a specified number of seconds." + + def _run( + self, + sleep_time: int, + run_manager: Optional[CallbackManagerForToolRun] = None, + ) -> str: + """Use the Sleep tool.""" + sleep(sleep_time) + return f"Agent slept for {sleep_time} seconds." + + async def _arun( + self, + sleep_time: int, + run_manager: Optional[AsyncCallbackManagerForToolRun] = None, + ) -> str: + """Use the sleep tool asynchronously.""" + await asleep(sleep_time) + return f"Agent slept for {sleep_time} seconds."