Agents
Agents allow using a language model to dynamically determine the sequence of actions to take to accomplish a task. This is in contrast to chains, where the sequence of actions is hardcoded.
Key Components
Agent
The agent is the class responsible for deciding the next action to take based on the prompt, tools available, and previous actions taken. The prompt provides context like the agent's personality and background. Popular agent types include ReAct and OpenAI Functions.
Tools
Tools are functions the agent can call as actions. Key considerations:
- Providing the right tools for the task
- Describing tools in a way the agent understands
LangChain provides many prebuilt tools like search and web. It's also easy to define custom tools.
Toolkits
Toolkits are groups of 3-5 related tools the agent needs to accomplish a specific task. For example, the search toolkit contains tools for web search and scraping. LangChain provides many prebuilt toolkits.
AgentExecutor
The AgentExecutor runs the loop that executes the agent's chosen actions. It:
- Calls the agent to get the next action
- Executes the action tool
- Feeds the observation back to the agent
- Continues until the agent returns
AgentFinish
It also handles errors and logging.
Getting Started
To build a custom agent:
- Choose an agent type like ReAct
- Define relevant tools
- Create a prompt with tool info
- Instantiate the agent class
- Run it with an AgentExecutor
See the custom agent example for sample code.
Additional Resources
- Agent types - Details on different agent algorithms
- Tools - Available tools
- Toolkits - Prebuilt tool groups
- AgentExecutor - Runtime class
- Examples - Code samples for custom agents