Welcome to this video on Build a Custom Math Toolkit Agent with LangChain.
In this video, you'll learn how to create flexible agents using LangChain and LangGraph.
You'll build a custom math toolkit with tools for addition, subtraction, multiplication,
and division.
You'll guide the agent with a custom prompt and run complex queries using the invoke method.
Finally, you'll learn how to integrate tools and build agents that both calculate and retrieve
information.
So far, you've used Initialize Agent to set up agents with predefined strategies such
as React or Structured Chat.
Now, let's look at another way to build agents with even more flexibility, LangGraph,
which is quickly becoming the preferred approach over Initialize_Agent for building
robust, multi-step agent workflows.
Using "create react agent", you can work with just one agent type, but you can fully customize
the prompt template, giving you more control over the agent's behavior and reasoning
style.
Before creating the ReAct agent, you'll define a structured tool that handles numeric
extraction and summation from input text.
Just like LangChain, LangGraph helps you build intelligent agents.
To create a React-style agent in LangGraph, use the "create react agent" function from the
langgraph.prebuilt module.
Create the agent using the "create react agent" and pass in the LLM and a list of tools, for
example, sum numbers with complex output.
Optionally, include a custom prompt to guide the agent's behavior.
To interact with the agent, use the invoke method and pass a dictionary with a messages
key.
This key contains a list of role message pairs, such as human, and add the numbers "-10",
"-20", "-30", which simulates a chat interaction.
The agent processes the input, reasons through the problem, and uses tools as needed.
It then appends its final response to the message list, extract the final answer.
The full response object also includes other useful information, such as tool call traces
and intermediate steps, which can help with debugging or tracing the agent's reasoning.
In real-world use, a single tool often isn't enough to handle everything a user might ask.
Some tasks might require different tools.
For example, if you're building an agent for banking transactions, you might need one
tool for deposits, another for withdrawals, and a third for transfers.
To demonstrate this idea, you'll build a simple math toolkit.
To begin with, the basic addition tool, while functional, might not be sufficient for all
mathematical tasks a user might ask.
Alongside the addition tool, you'll add a multiplication tool, a division tool, and
a subtraction tool.
You will now create a ReAct agent.
Start by adding all the tools to a list called tools.
Then create the ReAct agent using the "create react agent" function, passing in the LLM and the tools
list as parameters.
Also include an optional prompt or system message that helps guide the agent's behavior.
In this case, define the agent as a helpful mathematical assistant.
Here's a basic idea of how the agent works.
The agent receives an input query, such as what is 7x3.
The LLM analyzes the query and decides which tools to call.
In this case, call the multiplication tool.
The input parameters are passed to the multiplication tool.
The tool returns the result, and the LLM checks that the output makes sense.
The LLM then formats and returns the final answer to the user.
Call the agent using invoke with a user message, multiply 2, 3, and 4.
The agent selects the right tool and processes the request.
The answer is received from response messages.
The full response is shown.
LangChain provides a rich ecosystem of pre-built tools that solve common tasks out of the box.
Here are some examples.
WikipediaQueryRun searches Wikipedia for factual information.
GoogleSearchRun performs web searches using Google's API.
PythonREPLTool runs Python code safely, useful for calculations or logic.
OpenWeatherMapQueryRun fetches real-time weather data from the OpenWeatherMap API.
YouTubeSearchTool searches for videos on YouTube.
Now you'll create a custom Wikipedia search tool using the Tool Decorator and the LangChain
Community's Wikipedia API wrapper.
First, define clear input and output types.
Include a detailed docstring describing the tool's purpose, extended parameters, and
return value.
The tool simply wraps the WikipediaQueryRun object, passing the input query to its
run method.
Using the invoke method, the tool searches Wikipedia for the query, what is tool calling,
and returns a summary from Wikipedia as the output.
Next, create a new agent that combines the math tools with the Search_Wikipedia
tool.
For this, create an updated tools list that includes the Wikipedia tool alongside the
math tools.
Then use "create react agent" to build a new agent that can both perform calculations and
fetch information from Wikipedia.
Now test the updated agent with a query that combines information retrieval and math operation.
Provide the input, what is the population of Canada, multiply it by 0.75, to the agent
using invoke.
The agent first uses Search_Wikipedia to find Canada's population.
Then it applies the multiplication tool to calculate 75% of that number.
Finally, it returns a natural language response with a result.
In this video, you learned to
Build React-style agents using the Create_React_Agent method for
greater customization and control.
Construct a multi-tool math assistant by combining tools for addition, subtraction, multiplication,
and division.
Guide agent behavior using custom prompts and structure tool inputs.
Orchestrate multiple tools within a single agent to handle real-world multi-step queries.
Extend agent functionality by integrating external tools, such as Wikipedia Search for
dynamic, hybrid responses.