1
00:00:00,000 --> 00:00:03,440
[MUSIC]

2
00:00:03,440 --> 00:00:09,475
Welcome to this video on Build Intelligent Agents for Dynamic LLM Tool Use.

3
00:00:09,475 --> 00:00:14,079
In this video, you'll explore how to design smart agents that use tools, reason through

4
00:00:14,079 --> 00:00:19,579
multi-step tasks, and respond with precision. You'll learn how to define structured tools

5
00:00:19,579 --> 00:00:25,159
with typed inputs and outputs, and combine tools with different LLMs. You'll also learn

6
00:00:25,159 --> 00:00:29,260
how to debug and fine-tune multi-step agent workflows.

7
00:00:29,617 --> 00:00:35,200
In LangChain, an agent combines an LLM and one or more tools, allowing you to build intelligent

8
00:00:35,200 --> 00:00:40,840
applications that can reason, act, and interact with real-world data. Agents don't just

9
00:00:40,840 --> 00:00:46,779
generate responses. They make decisions, call tools, and guide the flow of logic. This makes

10
00:00:46,779 --> 00:00:51,253
them ideal for complex workflows where static prompts alone aren't enough.

11
00:00:51,697 --> 00:00:56,680
When building an agent in LangChain, consider three key factors. Not all LLMs support tool

12
00:00:56,720 --> 00:01:02,373
use or complex reasoning. Your choice of model directly affects what the agent can do.

13
00:01:02,373 --> 00:01:08,515
Newer tools must have JSON-serializable inputs and outputs. Structured tools are preferred,

14
00:01:08,515 --> 00:01:12,800
as their schema makes it easier for agents and LLMs to use them correctly.

15
00:01:12,800 --> 00:01:16,800
The agent strategy: Some agents are best for simple tasks.

16
00:01:16,800 --> 00:01:21,217
Others, like ReAct agents, handle multi-step, tool-driven workflows.

17
00:01:21,217 --> 00:01:25,283
LangChain and LLMs evolve quickly, so while documentation helps,

18
00:01:25,283 --> 00:01:29,217
effective agent design often requires hands-on experimentation.

19
00:01:30,230 --> 00:01:36,204
Let's see how to load and configure an LLM, one of the foundational elements of an agent.

20
00:01:36,204 --> 00:01:43,650
First, initialize the LLM with IBM watsonx.ai language model Granite using the langchain_ibm integration,

21
00:01:43,650 --> 00:01:46,950
allowing it to be used as an LLM within LangChain workflows.

22
00:01:46,950 --> 00:01:50,026
You can ask the LLM a question and get a response.

23
00:01:50,026 --> 00:01:55,137
Once your LLM is ready, you can pair it with tools and define the agent's behavior strategy.

24
00:01:55,137 --> 00:01:57,742
That's where the real intelligence begins.

25
00:01:58,350 --> 00:02:03,350
"initialize_agent" is a convenient starting point for building agents in LangChain.

26
00:02:03,350 --> 00:02:07,817
It lets you choose from multiple predefined agent types using a single constructor,

27
00:02:07,817 --> 00:02:12,317
making it easy to combine an LLM with tools and get up and running quickly.

28
00:02:12,317 --> 00:02:16,917
In general, agents follow a standard reasoning loop based on the prompt.

29
00:02:16,917 --> 00:02:19,417
The agent begins by taking the user's query.

30
00:02:19,417 --> 00:02:25,022
It reasons about what to do, often deciding which tool to call. It then calls the selected tool.

31
00:02:25,022 --> 00:02:29,017
After observing the tool's output, the agent decides what to do next.

32
00:02:29,017 --> 00:02:34,835
This step can be complex and often involves feeding the result back into itself for further reasoning.

33
00:02:34,835 --> 00:02:37,317
Finally, the agent generates the output,

34
00:02:37,317 --> 00:02:41,244
typically based on the tool's result and its internal reasoning trace.

35
00:02:41,617 --> 00:02:44,783
The ReAct agent framework essentially works as follows.

36
00:02:44,783 --> 00:02:48,617
After receiving the user input, the agent performs a reasoning step,

37
00:02:48,617 --> 00:02:51,183
thinking through the problem step-by-step.

38
00:02:51,183 --> 00:02:55,517
It then takes action, using tools to gather information or perform operations.

39
00:02:55,517 --> 00:02:58,826
It observes and processes the results returned by the tools.

40
00:02:58,826 --> 00:03:02,217
It plans the next steps based on those observations,

41
00:03:02,217 --> 00:03:05,751
either continuing the loop or generating the final answer.

42
00:03:06,450 --> 00:03:09,857
A Zero-Shot ReAct Agent uses zero-shot reasoning,

43
00:03:09,857 --> 00:03:14,951
the ability to solve tasks it hasn't seen before by thinking through the problem step-by-step.

44
00:03:14,951 --> 00:03:19,288
This approach is especially useful for simple or well-structured tasks.

45
00:03:21,537 --> 00:03:25,883
Here, the add_numbers function is used as the first tool for the zero-shot ReAct agent

46
00:03:25,883 --> 00:03:30,213
to demonstrate how it processes in response to a simple query.

47
00:03:34,071 --> 00:03:37,617
Next, wrap the add_numbers function into a LangChain-compatible tool

48
00:03:37,617 --> 00:03:41,483
by casting it to a tool object, making it usable by the agent.

49
00:03:42,266 --> 00:03:47,583
You can create a zero-shot ReAct agent using LangChain's initialize_agent function.

50
00:03:47,583 --> 00:03:51,717
Start by importing initialize_agent from langchain.agents.

51
00:03:51,717 --> 00:03:56,410
Pass in a list of tools (in this case, [add_tool]) and the LLM instance.

52
00:03:56,419 --> 00:04:01,940
The parameter agent="zero-shot-react-description" tells LangChain to use the ReAct

53
00:04:01,940 --> 00:04:07,119
strategy where the LLM observes the question, reasons about the next step, and selects a

54
00:04:07,119 --> 00:04:12,097
tool to call, all without being shown any specific examples, hence zero-shot.

55
00:04:12,097 --> 00:04:16,488
Set verbose=True to print the agent's reasoning process step-by-step,

56
00:04:16,488 --> 00:04:24,524
and handle_parsing_errors=True to allow the agent to recover if the LLM's tool output is slightly malformed.

57
00:04:25,128 --> 00:04:29,386
You can call the agent using the run method by passing in a natural language question.

58
00:04:29,386 --> 00:04:37,591
For example: "In 2023, the U.S. GDP was approximately $27.72 trillion, while Canada's was around

59
00:04:37,591 --> 00:04:44,791
$2.14 trillion, and Mexico's was about $1.79 trillion. What is the total?"

60
00:04:45,528 --> 00:04:49,450
The agent will follow a reasoning and action loop powered by the LLM.

61
00:04:49,450 --> 00:04:55,117
The LLM begins by understanding the question and recognizing that it needs to sum the GDP values.

62
00:04:55,117 --> 00:05:00,380
It then determines an Action and an Action Input. The action corresponds to a specific tool

63
00:05:00,380 --> 00:05:05,283
(in this case, AddTool) and the input includes a list of numbers to be added.

64
00:05:05,283 --> 00:05:10,850
The observation represents the output from the tool, the computed sum of the GDPs.

65
00:05:10,850 --> 00:05:15,650
Finally, the agent returns the final answer, formatting the result in natural language.

66
00:05:15,650 --> 00:05:19,883
In this case, it converts the large number into trillions and replies:

67
00:05:19,883 --> 00:05:27,582
"The total GDP of the U.S., Canada, and Mexico in 2023 was approximately $31.55 trillion."

68
00:05:28,550 --> 00:05:33,120
The run method doesn't work with some agents, so you use the invoke method instead.

69
00:05:33,120 --> 00:05:38,080
It's especially useful for debugging, particularly when working with more complex agents.

70
00:05:38,791 --> 00:05:45,417
When selecting an agent in LangChain, the tool format, as well as the tool's input and return types, matters.

71
00:05:45,417 --> 00:05:51,483
For example, the zero-shot-react-description agent expects tools to accept and return plain strings.

72
00:05:51,483 --> 00:05:57,050
In contrast, the structured-chat-zero-shot-react-description agent supports StructuredTools,

73
00:05:57,050 --> 00:06:01,342
which allows for typed inputs and structured outputs (for example, JSON).

74
00:06:02,151 --> 00:06:07,610
Here is a demonstration of the add_numbers_with_options tool. This function is used because it's

75
00:06:07,619 --> 00:06:12,179
designed to handle multiple inputs, including an optional argument for performing calculations

76
00:06:12,179 --> 00:06:17,059
with absolute values. This structured approach ensures flexibility and clarity in how the

77
00:06:17,059 --> 00:06:23,208
tool interacts with the LLM. To handle structured inputs, use an initialize_agent,

78
00:06:23,208 --> 00:06:27,031
passing in the tool "add_numbers_with_options" and the LLM.

79
00:06:28,620 --> 00:06:33,950
Setting agent as "structured-chat-zero-shot-react-description" enables ReAct-style reasoning

80
00:06:33,950 --> 00:06:36,826
with support for typed inputs and structured outputs.

81
00:06:37,208 --> 00:06:43,377
Call the agent using invoke; the input prompt appears under "input" and the result under "output".

82
00:06:43,377 --> 00:06:48,035
This structured format simplifies debugging and tracking in complex workflows.

83
00:06:48,755 --> 00:06:50,850
You can try different LLMs with your agent.

84
00:06:50,850 --> 00:06:54,204
For example, different tools work better with different agents.

85
00:06:54,204 --> 00:06:58,700
Here, sum_numbers_with_complex_output() may cause issues with certain

86
00:06:58,700 --> 00:07:02,750
LLM-agent combinations as it returns a dictionary type.

87
00:07:02,750 --> 00:07:06,373
To handle this, use an agent that supports structured outputs.

88
00:07:06,980 --> 00:07:14,080
First, import and create a gpt-4.1-nano instance. This is capable of handling complex outputs.

89
00:07:14,380 --> 00:07:18,257
Then create an openai-functions agent using initialize_agent,

90
00:07:18,257 --> 00:07:24,119
passing in the tool (add_numbers_with_options) and the LLM. Setting the agent as openai-functions

91
00:07:24,119 --> 00:07:28,640
enables structured tool support. You can call the agent just like before.

92
00:07:28,640 --> 00:07:30,950
If you want to use Granite for this use case,

93
00:07:30,950 --> 00:07:35,079
you'll have to change the agent to "structured-chat-zero-shot-react-description",

94
00:07:35,079 --> 00:07:39,004
because this specific agent can invoke tools that accept multiple inputs.

95
00:07:40,017 --> 00:07:41,830
In this video, you learned to

96
00:07:41,839 --> 00:07:46,920
Distinguish LLMs from agents by exploring the critical role of tools in enabling real-world

97
00:07:46,920 --> 00:07:53,959
interactions. Extend LLM capabilities by integrating tools for data access, precise calculations,

98
00:07:53,959 --> 00:08:00,079
and multi-step reasoning. Design effective tool interfaces with clear inputs, outputs,

99
00:08:00,079 --> 00:08:05,440
and descriptions to enhance agent responses. Build structured tools for flexible, context-aware

100
00:08:05,440 --> 00:08:09,520
interactions supporting complex inputs and robust data handling.

101
00:08:10,364 --> 00:08:13,928
[MUSIC]