WEBVTT

00:00.240 --> 00:00.720
Cool.

00:00.720 --> 00:02.480
So let me go to Main.py.

00:02.640 --> 00:06.280
And here we're going to implement our Landgraaf react graph.

00:06.640 --> 00:08.640
And we'll start from the imports.

00:09.000 --> 00:13.200
Let's start by first importing from LinkedIn core.

00:13.240 --> 00:15.320
Let's import the human message.

00:15.440 --> 00:17.720
And this is the input.

00:17.760 --> 00:20.120
We're going to start our graph to run with.

00:20.760 --> 00:23.960
Now I want to import from Landgraaf.

00:24.000 --> 00:31.040
I want to import the messages date object like we had from before, which is going to hold all of our

00:31.040 --> 00:31.800
messages.

00:32.040 --> 00:36.840
And I want to also import something which is called a state graph.

00:37.080 --> 00:40.320
And this is the most generic graph we can have.

00:40.360 --> 00:44.200
And it can receive any state object that we want to give it.

00:44.400 --> 00:47.280
And this is how we're going to initialize our graph.

00:47.960 --> 00:52.400
And when we'll initialize our graph it will be much clearer later in this video.

00:53.040 --> 00:54.680
All right let's go and continue.

00:54.680 --> 00:56.880
And let's go and import our nodes.

00:57.000 --> 01:00.560
I want the run agent reasoning node and the tool node.

01:01.000 --> 01:03.280
And now we are done with the imports.

01:03.280 --> 01:03.410
Ports.

01:03.690 --> 01:07.370
So I want to start by defining some constants.

01:07.930 --> 01:11.970
And let's go and define the constant of agent reasoning.

01:13.010 --> 01:14.650
We'll call it agent reason.

01:15.130 --> 01:18.770
And let's define act as the string act.

01:18.770 --> 01:21.050
And this is simply to have cleaner code.

01:21.050 --> 01:25.250
So we won't need to rewrite those strings every time that we want to use them.

01:25.930 --> 01:31.490
And lastly let's create a new const which is called last which is going to be minus one.

01:31.490 --> 01:39.210
And this is for future when we're going to reference the last message between the user and the agent.

01:39.450 --> 01:43.130
And the last message is going to be at the minus one index.

01:43.130 --> 01:45.450
So this is some Python notation.

01:45.450 --> 01:49.610
And again this is simply for more readability when we write the code.

01:49.610 --> 01:51.130
So the code is a bit cleaner.

01:51.930 --> 01:52.490
Cool.

01:52.490 --> 01:55.490
So now it's time to finally implement our graph.

01:55.970 --> 01:59.410
Now let's define a new state graph object.

01:59.410 --> 02:01.250
Like we are auto populated here.

02:01.410 --> 02:04.010
And we want to give it the message graph.

02:04.770 --> 02:08.070
So we're going to call it flow instead of graph.

02:08.550 --> 02:10.670
And let's go and do that.

02:11.430 --> 02:15.710
And now we want to add the nodes like we're autocompleted.

02:15.710 --> 02:20.990
So we want to add the node of agent reason and the node of act.

02:21.910 --> 02:24.630
And notice how we're using those constants here.

02:25.270 --> 02:31.270
And by the way right over here cursor auto completed me this edge from start to agent reason which is

02:31.270 --> 02:32.110
correct.

02:32.110 --> 02:35.150
But I want to define it in another way.

02:35.470 --> 02:40.910
So let me go to the line where we define the agent reason node and one line below it.

02:40.910 --> 02:43.910
Let me write flow dot set entry point.

02:44.190 --> 02:46.430
And I'm going to set it to Agent Reason.

02:46.630 --> 02:50.550
And this is how I personally like to define this edge.

02:50.990 --> 02:56.270
It defines an edge from the starting node and going to the agent reason node.

02:56.710 --> 02:58.590
Anyways let me go and remove this.

02:59.350 --> 03:01.790
And now let's go and add some edges.

03:02.110 --> 03:11.080
Let's add the conditional edge from the agent reason node to the node and to the unknown depending of

03:11.120 --> 03:13.000
a certain functionality that we write.

03:13.360 --> 03:18.920
So I'm going to decide this functionality should continue function which will implement later.

03:19.280 --> 03:27.200
And the third argument is going to be a dictionary of mapping between the string end to end from the

03:27.200 --> 03:28.800
string act to act.

03:29.480 --> 03:38.280
And this mapping over here really tells Landgraf that whatever comes from the should continue function,

03:38.280 --> 03:40.560
which is going to be some kind of string.

03:40.760 --> 03:45.160
And we're going to return from that function the answering or the x ring.

03:45.520 --> 03:52.200
Then we want to map it to go to the end node or to the node with the name of act.

03:52.720 --> 03:59.800
So this is going to help us draw those dotted lines over here in the graph itself.

04:00.520 --> 04:05.280
And if we didn't write this mapping the graph would still run as we want to.

04:05.400 --> 04:08.240
But the drawing wouldn't be that explicit.

04:08.240 --> 04:11.280
So this really helps with drawing what we see over here.

04:11.760 --> 04:19.290
It defines which nodes can come after the agent reason node, and this is the node and the end node.

04:20.290 --> 04:22.370
Alrighty, let's go and implement.

04:22.370 --> 04:24.050
This should continue function.

04:24.290 --> 04:26.890
So let me go up a bit in the code.

04:27.970 --> 04:31.250
And let's go and put it right above here.

04:31.730 --> 04:36.970
And this function should continue is going to receive the state of the graph which is going to be a

04:36.970 --> 04:37.810
message state.

04:38.010 --> 04:45.090
And it's going to return us the string, which is going to annotate the node that should run after the

04:45.090 --> 04:46.170
agent reason node.

04:46.650 --> 04:50.250
So how we're going to decide we're going to check the messages.

04:50.530 --> 04:55.370
And if the last message and this is the usage of the constant here.

04:55.370 --> 05:02.250
So if the last message is going to be a tool call then we want to go to the node.

05:02.570 --> 05:06.170
And if not we want to go to end.

05:06.650 --> 05:09.370
Because if the last message is a tool call.

05:09.370 --> 05:15.620
So this means that the LM, the reasoning engine, decided that we need to invoke a tool and it has

05:15.620 --> 05:17.260
all the information of the arguments.

05:17.260 --> 05:21.180
Then we want to go and execute the tool node which will execute that tool.

05:21.580 --> 05:26.020
And if there isn't a tool call, then we simply want to end everything.

05:26.020 --> 05:32.980
Because this is a heuristic that the LM was able to answer the question with or without a tool invocation.

05:33.340 --> 05:33.780
All right.

05:33.780 --> 05:37.500
So we finished implementing those dotted lines over here in the graph.

05:37.700 --> 05:42.140
Now let's go and define the edge from act to agent reason.

05:42.140 --> 05:48.100
Because after we invoke the tool we want the agent to reason and to figure out whether it needs to return

05:48.100 --> 05:50.740
the answer or to run another tool call.

05:51.340 --> 05:53.660
And that's pretty much it for the graph.

05:53.660 --> 05:58.380
So we defined the nodes, the entry point, the edges between the nodes.

05:58.660 --> 06:00.980
And now we want to compile the graph.

06:01.300 --> 06:06.540
And let's go and print it and see that we get the same drawing that I'm showing you in the video.

06:06.820 --> 06:14.900
And for that we'll use the get graph and then draw Mermod pg function, which will print it and display

06:14.900 --> 06:15.220
it.

06:15.220 --> 06:19.320
And in the output file here which we're going to call flow PNG.

06:20.240 --> 06:24.120
Let's wait for it to run and we can see we get here an error.

06:24.560 --> 06:27.640
It seems that we cannot import messages.

06:28.120 --> 06:29.560
State message states here.

06:29.720 --> 06:31.680
And this is because I'm missing here an S.

06:31.720 --> 06:34.280
This is something a cursor made a mistake.

06:35.240 --> 06:39.920
So let me go and fix it and let's go and rerun it.

06:42.120 --> 06:47.400
And we can see we forgot to update the rest of the code to use messages state instead of message state.

06:47.400 --> 06:50.960
So let me copy that and let me put it over here.

06:51.320 --> 06:55.720
And we should also do it in the should continue function.

06:55.720 --> 06:57.520
So let me go and fix it as well.

06:58.480 --> 06:58.880
Yeah.

07:01.800 --> 07:02.840
Let me run it again.

07:02.840 --> 07:04.800
Now boom.

07:05.040 --> 07:07.840
So let's check out now the drawing of the graph.

07:08.040 --> 07:11.880
And we can see this is the graph that I showed you during this video.

07:12.440 --> 07:13.120
Awesome.

07:13.120 --> 07:16.640
So in the next video we're going to test everything.

07:16.640 --> 07:18.560
And we're going to review the traces.

07:18.560 --> 07:21.280
And we're going to see exactly how everything is working.
