From 45f0f9460a6f5a6491ecd91f160defc14e6edc14 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Thu, 10 Aug 2023 16:07:06 -0700 Subject: [PATCH] add async for python repl (#9080) --- libs/langchain/langchain/tools/python/tool.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libs/langchain/langchain/tools/python/tool.py b/libs/langchain/langchain/tools/python/tool.py index d5ae950927..a820ba6140 100644 --- a/libs/langchain/langchain/tools/python/tool.py +++ b/libs/langchain/langchain/tools/python/tool.py @@ -131,3 +131,15 @@ class PythonAstREPLTool(BaseTool): return io_buffer.getvalue() except Exception as e: return "{}: {}".format(type(e).__name__, str(e)) + + async def _arun( + self, + query: str, + run_manager: Optional[AsyncCallbackManagerForToolRun] = None, + ) -> Any: + """Use the tool asynchronously.""" + + loop = asyncio.get_running_loop() + result = await loop.run_in_executor(None, self._run, query) + + return result