AgentExecutor helps you run tasks by connecting tools and decision logic. It makes your program smart and flexible.
0
0
AgentExecutor setup and configuration in LangChain
Introduction
When you want a program to choose the right tool automatically.
When you need to run multiple steps based on user input.
When you want to build a chatbot that can answer questions using different helpers.
When you want to automate tasks that require thinking and tool use.
When you want to combine language models with external APIs or functions.
Syntax
LangChain
from langchain.agents import AgentExecutor, initialize_agent agent_executor = initialize_agent( tools, # list of tools your agent can use llm, # language model instance agent=agent_type, # type of agent logic verbose=True # show detailed logs )
tools is a list of helpers your agent can call.
llm is the language model that understands and generates text.
Examples
This example sets up an agent that can use a search tool and a language model to answer questions.
LangChain
from langchain.agents import initialize_agent, Tool from langchain.llms import OpenAI llm = OpenAI(temperature=0) tools = [Tool(name="Search", func=search_function)] agent_executor = initialize_agent( tools, llm, agent="zero-shot-react-description", verbose=True )
Here, the agent uses a chat-based logic and runs quietly without printing logs.
LangChain
agent_executor = initialize_agent(
tools=tools_list,
llm=llm_instance,
agent="chat-zero-shot-react-description",
verbose=False
)Sample Program
This program creates a greeting tool, sets up an agent with a language model, and runs it to greet Alice.
LangChain
from langchain.agents import initialize_agent, Tool from langchain.llms import OpenAI # Define a simple tool function def greet(name: str) -> str: return f"Hello, {name}!" # Create a Tool object n_greet_tool = Tool(name="Greet", func=greet, description="Greets a person by name") # Initialize the language model llm = OpenAI(temperature=0) # Setup the agent executor with the tool and llm agent_executor = initialize_agent( tools=[greet_tool], llm=llm, agent="zero-shot-react-description", verbose=False ) # Run the agent with an input result = agent_executor.run("Greet Alice") print(result)
OutputSuccess
Important Notes
Always provide clear descriptions for your tools to help the agent understand when to use them.
Set verbose=True during development to see how the agent thinks and acts.
Make sure your language model and tools are compatible and properly initialized before creating the agent.
Summary
AgentExecutor connects tools and language models to automate smart tasks.
Use initialize_agent to set up your agent with tools and logic.
Test with simple inputs and watch verbose logs to understand agent behavior.