.ipynb
.pdf
How to use the async API for Agents
 Contents 
Serial vs. Concurrent Execution
How to use the async API for Agents#
LangChain provides async support for Agents by leveraging the asyncio library.
Async methods are currently supported for the following Tools: GoogleSerperAPIWrapper, SerpAPIWrapper and LLMMathChain. Async support for other agent tools are on the roadmap.
For Tools that have a coroutine implemented (the three mentioned above), the AgentExecutor will await them directly. Otherwise, the AgentExecutor will call the Tool’s func via asyncio.get_event_loop().run_in_executor to avoid blocking the main runloop.
You can use arun to call an AgentExecutor asynchronously.
Serial vs. Concurrent Execution#
In this example, we kick off agents to answer some questions serially vs. concurrently. You can see that concurrent execution significantly speeds this up.
import asyncio
import time
from langchain.agents import initialize_agent, load_tools
from langchain.agents import AgentType
from langchain.llms import OpenAI
from langchain.callbacks.stdout import StdOutCallbackHandler
from langchain.callbacks.tracers import LangChainTracer
from aiohttp import ClientSession
questions = [
    "Who won the US Open men's final in 2019? What is his age raised to the 0.334 power?",
    "Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?",
    "Who won the most recent formula 1 grand prix? What is their age raised to the 0.23 power?",
    "Who won the US Open women's final in 2019? What is her age raised to the 0.34 power?",
    "Who is Beyonce's husband? What is his age raised to the 0.19 power?"
]
llm = OpenAI(temperature=0)
rtdocs_stable/api.python.langchain.com/en/stable/modules/agents/agent_executors/examples/async_agent.html