LangChain agents help programs think and act by using tools and language models together. They make AI smarter by letting it decide what to do next.
0
0
LangChain agents overview in Agentic AI
Introduction
When you want an AI to answer questions by using external tools like calculators or databases.
When you need an AI to perform multiple steps to solve a problem, like booking a flight and checking the weather.
When you want to build a chatbot that can search the internet or access APIs to give better answers.
When you want AI to decide which tool to use based on the question it receives.
When you want to combine language understanding with actions in a flexible way.
Syntax
Agentic AI
from langchain.agents import initialize_agent from langchain.llms import OpenAI from langchain.tools import Tool # Define tools tools = [Tool(name="Calculator", func=calculator_function, description="Performs math calculations")] # Initialize language model llm = OpenAI(temperature=0) # Create agent agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True) # Run agent agent.run("What is 12 times 15?")
The initialize_agent function sets up the agent with tools and a language model.
Agents can use different strategies like 'zero-shot-react-description' to decide actions.
Examples
This example creates an agent that uses a calculator tool to answer math questions.
Agentic AI
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True) agent.run("Calculate 5 plus 7")
This agent can handle conversations and decide which tool to use based on the question.
Agentic AI
agent = initialize_agent(tools, llm, agent="conversational-react-description", verbose=True) agent.run("What is the weather in New York?")
Sample Model
This program creates a LangChain agent that uses a calculator tool to answer a math question. It runs the agent to calculate 12 times 15 and prints the answer.
Agentic AI
from langchain.agents import initialize_agent from langchain.llms import OpenAI from langchain.tools import Tool import math # Define a simple calculator function def calculator_function(query: str) -> str: try: # Evaluate math expression safely result = str(eval(query, {"__builtins__": None}, {})) except Exception: result = "Error in calculation" return result # Create a calculator tool calculator_tool = Tool(name="Calculator", func=calculator_function, description="Performs math calculations") # Initialize language model llm = OpenAI(temperature=0) # Initialize agent with the calculator tool agent = initialize_agent([calculator_tool], llm, agent="zero-shot-react-description", verbose=False) # Run agent to calculate 12 times 15 output = agent.run("What is 12 times 15?") print(output)
OutputSuccess
Important Notes
Agents combine language models and tools to solve tasks step-by-step.
Choosing the right agent type affects how the AI decides what to do next.
Always test tools separately to ensure they work before adding to agents.
Summary
LangChain agents let AI use tools and language models together.
They help AI decide actions based on questions or tasks.
Agents make AI more flexible and powerful for real-world problems.