1
00:00:03,000 --> 00:00:08,680
Welcome to this video on Build a Custom Math Toolkit Agent with LangChain.

2
00:00:08,680 --> 00:00:13,279
In this video, you'll learn how to create flexible agents using LangChain and LangGraph.

3
00:00:13,279 --> 00:00:18,959
You'll build a custom math toolkit with tools for addition, subtraction, multiplication,

4
00:00:18,959 --> 00:00:19,959
and division.

5
00:00:19,959 --> 00:00:25,559
You'll guide the agent with a custom prompt and run complex queries using the invoke method.

6
00:00:25,719 --> 00:00:30,600
Finally, you'll learn how to integrate tools and build agents that both calculate and retrieve

7
00:00:30,600 --> 00:00:32,720
information.

8
00:00:32,720 --> 00:00:37,279
So far, you've used Initialize Agent to set up agents with predefined strategies such

9
00:00:37,279 --> 00:00:39,560
as React or Structured Chat.

10
00:00:39,560 --> 00:00:44,959
Now, let's look at another way to build agents with even more flexibility, LangGraph,

11
00:00:44,959 --> 00:00:49,279
which is quickly becoming the preferred approach over Initialize_Agent for building

12
00:00:49,279 --> 00:00:53,040
robust, multi-step agent workflows.

13
00:00:53,040 --> 00:00:58,040
Using "create react agent", you can work with just one agent type, but you can fully customize

14
00:00:58,040 --> 00:01:02,479
the prompt template, giving you more control over the agent's behavior and reasoning

15
00:01:02,479 --> 00:01:04,239
style.

16
00:01:04,239 --> 00:01:08,080
Before creating the ReAct agent, you'll define a structured tool that handles numeric

17
00:01:08,080 --> 00:01:13,080
extraction and summation from input text.

18
00:01:13,080 --> 00:01:17,199
Just like LangChain, LangGraph helps you build intelligent agents.

19
00:01:17,199 --> 00:01:22,000
To create a React-style agent in LangGraph, use the "create react agent" function from the

20
00:01:22,000 --> 00:01:25,040
langgraph.prebuilt module.

21
00:01:25,040 --> 00:01:30,879
Create the agent using the "create react agent" and pass in the LLM and a list of tools, for

22
00:01:30,879 --> 00:01:34,639
example, sum numbers with complex output.

23
00:01:34,639 --> 00:01:40,319
Optionally, include a custom prompt to guide the agent's behavior.

24
00:01:40,319 --> 00:01:44,519
To interact with the agent, use the invoke method and pass a dictionary with a messages

25
00:01:44,519 --> 00:01:45,720
key.

26
00:01:45,720 --> 00:01:51,400
This key contains a list of role message pairs, such as human, and add the numbers "-10",

27
00:01:51,800 --> 00:01:56,400
"-20", "-30", which simulates a chat interaction.

28
00:01:56,400 --> 00:02:01,919
The agent processes the input, reasons through the problem, and uses tools as needed.

29
00:02:01,919 --> 00:02:07,019
It then appends its final response to the message list, extract the final answer.

30
00:02:07,019 --> 00:02:11,720
The full response object also includes other useful information, such as tool call traces

31
00:02:11,720 --> 00:02:17,960
and intermediate steps, which can help with debugging or tracing the agent's reasoning.

32
00:02:17,960 --> 00:02:23,839
In real-world use, a single tool often isn't enough to handle everything a user might ask.

33
00:02:23,839 --> 00:02:26,199
Some tasks might require different tools.

34
00:02:26,199 --> 00:02:30,479
For example, if you're building an agent for banking transactions, you might need one

35
00:02:30,479 --> 00:02:34,839
tool for deposits, another for withdrawals, and a third for transfers.

36
00:02:34,839 --> 00:02:39,580
To demonstrate this idea, you'll build a simple math toolkit.

37
00:02:39,580 --> 00:02:44,080
To begin with, the basic addition tool, while functional, might not be sufficient for all

38
00:02:44,080 --> 00:02:46,839
mathematical tasks a user might ask.

39
00:02:46,839 --> 00:02:52,720
Alongside the addition tool, you'll add a multiplication tool, a division tool, and

40
00:02:52,720 --> 00:02:55,080
a subtraction tool.

41
00:02:55,080 --> 00:02:57,639
You will now create a ReAct agent.

42
00:02:57,639 --> 00:03:01,080
Start by adding all the tools to a list called tools.

43
00:03:01,080 --> 00:03:06,520
Then create the ReAct agent using the "create react agent" function, passing in the LLM and the tools

44
00:03:06,520 --> 00:03:08,339
list as parameters.

45
00:03:08,339 --> 00:03:13,220
Also include an optional prompt or system message that helps guide the agent's behavior.

46
00:03:13,220 --> 00:03:18,779
In this case, define the agent as a helpful mathematical assistant.

47
00:03:18,779 --> 00:03:21,380
Here's a basic idea of how the agent works.

48
00:03:21,380 --> 00:03:26,100
The agent receives an input query, such as what is 7x3.

49
00:03:26,100 --> 00:03:30,100
The LLM analyzes the query and decides which tools to call.

50
00:03:30,100 --> 00:03:32,899
In this case, call the multiplication tool.

51
00:03:32,899 --> 00:03:36,300
The input parameters are passed to the multiplication tool.

52
00:03:36,300 --> 00:03:40,820
The tool returns the result, and the LLM checks that the output makes sense.

53
00:03:40,820 --> 00:03:46,100
The LLM then formats and returns the final answer to the user.

54
00:03:46,100 --> 00:03:51,179
Call the agent using invoke with a user message, multiply 2, 3, and 4.

55
00:03:51,179 --> 00:03:54,580
The agent selects the right tool and processes the request.

56
00:03:54,580 --> 00:03:57,139
The answer is received from response messages.

57
00:03:57,139 --> 00:03:59,820
The full response is shown.

58
00:03:59,820 --> 00:04:04,860
LangChain provides a rich ecosystem of pre-built tools that solve common tasks out of the box.

59
00:04:04,860 --> 00:04:06,339
Here are some examples.

60
00:04:06,339 --> 00:04:10,460
WikipediaQueryRun searches Wikipedia for factual information.

61
00:04:10,619 --> 00:04:14,539
GoogleSearchRun performs web searches using Google's API.

62
00:04:14,539 --> 00:04:20,700
PythonREPLTool runs Python code safely, useful for calculations or logic.

63
00:04:20,700 --> 00:04:26,179
OpenWeatherMapQueryRun fetches real-time weather data from the OpenWeatherMap API.

64
00:04:26,179 --> 00:04:29,820
YouTubeSearchTool searches for videos on YouTube.

65
00:04:29,820 --> 00:04:33,940
Now you'll create a custom Wikipedia search tool using the Tool Decorator and the LangChain

66
00:04:33,940 --> 00:04:36,380
Community's Wikipedia API wrapper.

67
00:04:36,380 --> 00:04:39,660
First, define clear input and output types.

68
00:04:39,660 --> 00:04:43,739
Include a detailed docstring describing the tool's purpose, extended parameters, and

69
00:04:43,739 --> 00:04:45,019
return value.

70
00:04:45,019 --> 00:04:49,500
The tool simply wraps the WikipediaQueryRun object, passing the input query to its

71
00:04:49,500 --> 00:04:51,500
run method.

72
00:04:51,500 --> 00:04:56,019
Using the invoke method, the tool searches Wikipedia for the query, what is tool calling,

73
00:04:56,019 --> 00:04:59,059
and returns a summary from Wikipedia as the output.

74
00:04:59,059 --> 00:05:03,700
Next, create a new agent that combines the math tools with the Search_Wikipedia

75
00:05:03,700 --> 00:05:04,700
tool.

76
00:05:04,700 --> 00:05:08,820
For this, create an updated tools list that includes the Wikipedia tool alongside the

77
00:05:08,980 --> 00:05:10,420
math tools.

78
00:05:10,420 --> 00:05:14,660
Then use "create react agent" to build a new agent that can both perform calculations and

79
00:05:14,660 --> 00:05:18,739
fetch information from Wikipedia.

80
00:05:18,739 --> 00:05:24,059
Now test the updated agent with a query that combines information retrieval and math operation.

81
00:05:24,059 --> 00:05:29,480
Provide the input, what is the population of Canada, multiply it by 0.75, to the agent

82
00:05:29,480 --> 00:05:30,980
using invoke.

83
00:05:30,980 --> 00:05:35,640
The agent first uses Search_Wikipedia to find Canada's population.

84
00:05:35,640 --> 00:05:39,600
Then it applies the multiplication tool to calculate 75% of that number.

85
00:05:39,600 --> 00:05:44,200
Finally, it returns a natural language response with a result.

86
00:05:44,200 --> 00:05:46,079
In this video, you learned to

87
00:05:46,079 --> 00:05:50,820
Build React-style agents using the Create_React_Agent method for

88
00:05:50,820 --> 00:05:53,519
greater customization and control.

89
00:05:53,519 --> 00:05:59,160
Construct a multi-tool math assistant by combining tools for addition, subtraction, multiplication,

90
00:05:59,160 --> 00:06:00,160
and division.

91
00:06:00,160 --> 00:06:04,440
Guide agent behavior using custom prompts and structure tool inputs.

92
00:06:04,440 --> 00:06:09,440
Orchestrate multiple tools within a single agent to handle real-world multi-step queries.

93
00:06:09,440 --> 00:06:13,399
Extend agent functionality by integrating external tools, such as Wikipedia Search for

94
00:06:13,399 --> 00:06:15,119
dynamic, hybrid responses.