How to Create an Agent in LangChain: Simple Guide
To create an agent in
LangChain, you first define a language model and tools, then use initialize_agent to combine them into an agent. This agent can then process inputs and decide which tool to use to answer queries.Syntax
Creating an agent in LangChain involves these parts:
- Language Model: The AI model that understands and generates text.
- Tools: Functions or APIs the agent can call to get information or perform actions.
- initialize_agent: A function that combines the model and tools into an agent.
The agent uses the model to decide which tool to use based on the input.
python
from langchain.agents import initialize_agent, Tool from langchain.llms import OpenAI # Define the language model llm = OpenAI(temperature=0) # Define a simple tool def greet(name: str) -> str: return f"Hello, {name}!" greet_tool = Tool( name="Greet", func=greet, description="Greets a person by name" ) # Create the agent agent = initialize_agent( tools=[greet_tool], llm=llm, agent="zero-shot-react-description", verbose=True )
Example
This example shows how to create a LangChain agent that can greet someone by name when asked.
python
from langchain.agents import initialize_agent, Tool from langchain.llms import OpenAI # Language model with no randomness llm = OpenAI(temperature=0) # Tool function to greet def greet(name: str) -> str: return f"Hello, {name}!" # Define the tool greet_tool = Tool( name="Greet", func=greet, description="Greets a person by name" ) # Initialize the agent with the tool and model agent = initialize_agent( tools=[greet_tool], llm=llm, agent="zero-shot-react-description", verbose=False ) # Use the agent response = agent.run("Greet Alice") print(response)
Output
Hello, Alice!
Common Pitfalls
Common mistakes when creating LangChain agents include:
- Not defining tools with clear
nameanddescription, which confuses the agent. - Using a language model without setting
temperature=0for predictable outputs during testing. - Forgetting to include all tools the agent might need in the
toolslist. - Using an unsupported
agenttype ininitialize_agent.
python
from langchain.agents import initialize_agent, Tool from langchain.llms import OpenAI llm = OpenAI(temperature=0) # Wrong: Tool missing description wrong_tool = Tool( name="Greet", func=lambda x: f"Hello, {x}!" # Missing description causes issues ) # Right: Tool with description right_tool = Tool( name="Greet", func=lambda x: f"Hello, {x}!", description="Greets a person by name" ) # Wrong: Unsupported agent type # agent = initialize_agent(tools=[right_tool], llm=llm, agent="unknown-agent") # This will error # Right: Supported agent type agent = initialize_agent(tools=[right_tool], llm=llm, agent="zero-shot-react-description")
Quick Reference
Here is a quick summary of key steps to create a LangChain agent:
| Step | Description |
|---|---|
| Define Language Model | Create an LLM instance like OpenAI with desired settings. |
| Create Tools | Wrap functions or APIs with Tool(name, func, description). |
| Initialize Agent | Use initialize_agent(tools, llm, agent) to combine them. |
| Run Agent | Call agent.run(input) to get responses. |
Key Takeaways
Create an agent by combining a language model and tools using initialize_agent.
Always define tools with clear names and descriptions for the agent to understand them.
Set temperature=0 in the language model for consistent outputs during testing.
Use supported agent types like 'zero-shot-react-description' in initialize_agent.
Test your agent with simple inputs to verify it calls the right tools.