WEBVTT

00:00.360 --> 00:00.840
All right.

00:00.840 --> 00:08.560
Let's go to notes and let's go and import from env the load env function.

00:08.800 --> 00:12.760
And now let's go and import the primitives from graph.

00:13.240 --> 00:16.440
So we want to import from graph.

00:16.760 --> 00:17.400
Graph.

00:17.680 --> 00:20.800
We want to import the messages state.

00:21.160 --> 00:27.760
And this is a simple object which has the dictionary of the key messages.

00:28.000 --> 00:32.000
And that messages is going to be a list of messages.

00:32.280 --> 00:37.520
So this is going to keep track on the state on all of the messages back in between our agent.

00:37.520 --> 00:40.120
So the human message and the AI message.

00:40.480 --> 00:40.920
All right.

00:40.920 --> 00:47.200
So now we want to import from graph prebuilt a prebuilt node which is called tool node.

00:47.400 --> 00:49.040
And this is a node.

00:49.040 --> 00:54.320
And I elaborate on this in the graph course which is going to execute tools.

00:54.320 --> 00:58.560
So it's going to check the last message between the agent and human.

00:58.840 --> 01:05.690
And if that last message is an I message that has a valid tool call, it's going to go and execute that

01:05.690 --> 01:10.210
tool code, assuming that tools is initiated with the tool node object.

01:11.850 --> 01:21.210
So if the agent decides to execute a search or decides to run the triple function, then those are going

01:21.210 --> 01:23.290
to run inside this tool node.

01:23.290 --> 01:26.010
And it will all be clear when we'll use it in action.

01:26.490 --> 01:26.970
All right.

01:26.970 --> 01:34.450
So we want to import from react the LM with the tool calls that we binded to and the list of tools that

01:34.450 --> 01:36.850
we defined in the react py file.

01:37.690 --> 01:38.130
All right.

01:38.130 --> 01:40.210
Let's load the environment variables.

01:40.450 --> 01:45.530
And let's implement now the reasoning engine of our agent.

01:46.250 --> 01:51.410
So basically it's going to be a node where the agent is going to receive our message.

01:51.530 --> 01:54.130
And it's going to decide whether it can answer it.

01:54.130 --> 01:56.010
Or it needs to invoke a tool.

01:56.010 --> 01:59.210
And it's going to arrange all the arguments for that too.

01:59.650 --> 02:02.050
So for that, we'll define a system message.

02:02.370 --> 02:09.330
And here we'll give the very generic system message of you are a helpful assistant with access to tools

02:09.650 --> 02:12.010
and that you can use to answer the question.

02:12.250 --> 02:17.930
And now let's go and define the first node which is going to be the agent reasoning node.

02:18.250 --> 02:23.650
So it's going to receive the state which is a message state which is a dictionary that has the key of

02:23.650 --> 02:24.330
messages.

02:24.850 --> 02:31.970
And here all that we're going to do is simply make an LM call with the user input.

02:32.170 --> 02:38.290
And the LM call is going to do all of the heavy lifting because the LM is binded with the tools.

02:38.290 --> 02:40.570
And we're going to leverage function calling here.

02:40.890 --> 02:45.090
So we're going to invoke the LM and the invocation.

02:45.090 --> 02:49.810
We're simply going to put here the role of a system.

02:49.810 --> 02:51.690
We're going to put the system message.

02:51.850 --> 02:55.210
And then we're going to put all of the messages that we have.

02:55.450 --> 02:59.090
So in the first iteration, we're going to have only the human message.

02:59.330 --> 03:05.850
In the second iteration, we're going to have also the human message and the response and so on and

03:05.850 --> 03:06.370
so on.

03:06.490 --> 03:10.170
So all the information is going to be in the messages state over here.

03:10.890 --> 03:14.690
So I'm going to access the messages key in the state.

03:14.930 --> 03:18.610
And right now I'm accessing it as a Python property.

03:18.810 --> 03:24.930
But let me go and change it soon to access a dictionary key instead just to be on the safe side.

03:25.530 --> 03:28.410
And this is going to send the human message.

03:28.410 --> 03:30.810
It's going to send it back to the LM.

03:30.810 --> 03:37.930
And then the LM decides whether we need to use a function call and execute a function, or it decides

03:37.930 --> 03:39.570
that it can output the answer.

03:40.290 --> 03:43.490
So after the node runs, we need to update our state.

03:43.690 --> 03:48.930
And if we'll return a dictionary with the key of messages and return here a list.

03:48.930 --> 03:52.130
And that list is going to have the response from the LM.

03:52.370 --> 03:59.620
So this is going to be the AI message here then is going to append this value to our state in the messages

03:59.660 --> 03:59.980
key.

04:00.340 --> 04:03.300
So the key has all the messages so far.

04:03.500 --> 04:05.260
And this node is going to update it.

04:05.260 --> 04:07.300
And it's going to add another message here.

04:07.580 --> 04:10.060
So we're done with the reasoning node.

04:10.060 --> 04:14.980
And we also need to define the node with the relevant tools.

04:15.180 --> 04:18.500
So this is going to help Landgraf execute the tools that we need.

04:19.020 --> 04:22.060
So let me define a variable called tool node.

04:22.260 --> 04:26.780
And it's going to be object of the class tool node from Landgraf prebuilt.

04:26.780 --> 04:32.900
And we're going to provide it with the tools that we defined the search and the multiply tools which

04:32.900 --> 04:35.060
are both link chain tools.

04:35.500 --> 04:40.220
So this node over here does a lot of heavy lifting.

04:40.420 --> 04:44.460
It can run tools in parallel, can run them in streaming mode.

04:44.500 --> 04:46.860
And it has a lot of cool functionality.

04:47.020 --> 04:49.820
But right now we're going to use the vanilla version.

04:49.820 --> 04:54.460
And by the way, I go deep into the implementations of this in my course.

04:54.660 --> 05:01.300
And before we had this object, it was actually a lot of boilerplate code to write just to run the tools

05:01.300 --> 05:02.140
in our graph.

05:02.740 --> 05:06.260
All right, let me go and commit now all the changes.

05:06.420 --> 05:11.700
And let me add this notepad file and I'll call this commit notes.

05:12.340 --> 05:14.100
Let me push it to the repo.

05:14.700 --> 05:17.380
And if you want you can go to the repository.

05:17.420 --> 05:18.980
Go to the notes commit.

05:19.220 --> 05:23.820
And here you have the code that we just wrote right.

05:23.860 --> 05:27.260
So we finished implementing the graph nodes.

05:27.540 --> 05:33.060
And we didn't implement a lot because we used a lot of language primitives, which saves us a lot of

05:33.100 --> 05:34.700
boilerplate code to run.

05:34.940 --> 05:39.180
And we are using the function calling features of LM methods.

05:39.180 --> 05:41.900
So the LM is going to do the reasoning for us.

05:41.900 --> 05:43.740
We don't really need to do anything here.

05:43.740 --> 05:45.380
We don't need to write a special prompt.

05:45.380 --> 05:47.300
We don't need to do any of this.

05:47.340 --> 05:51.780
And in the next video we're going to stitch everything together and implement our graph.
