WEBVTT

00:00.320 --> 00:01.160
All right.

00:01.160 --> 00:04.720
So finally in this section we're going to build our graph.

00:04.720 --> 00:08.040
And we're going to connect all the nodes and edges together.

00:08.400 --> 00:10.560
So let's go to the code.

00:11.200 --> 00:15.920
We first want to go to the init file under the nodes module.

00:16.240 --> 00:20.000
So here we want to import all of the nodes we created.

00:20.000 --> 00:25.080
So it's going to be the generate node the create documents node the retrieve node the the web search

00:25.080 --> 00:25.520
node.

00:25.720 --> 00:29.360
And we want to be able to import them from outside the package.

00:29.480 --> 00:32.480
So that's why we're going to use the underscore all.

00:32.800 --> 00:36.760
And here we're going to put the string names of those nodes.

00:36.800 --> 00:41.880
So this will make all of those nodes importable from outside packages.

00:42.280 --> 00:43.000
Alrighty.

00:43.040 --> 00:45.120
Now I want to go to const py file.

00:45.120 --> 00:47.160
And let's define a bunch of constants.

00:47.320 --> 00:52.400
And all of those cons are going to be the node names that we're going to refer to in the graph.

00:52.480 --> 00:56.040
And the reason we do this is to avoid code duplication.

00:56.040 --> 01:00.160
So every time we'll reference the nodes we'll reference the const.

01:00.600 --> 01:06.040
And if we want to change the name from some reason we will only need to change it in one place.

01:06.280 --> 01:08.640
So that's why I put it in the const file.

01:08.680 --> 01:12.200
So we defined the retrieve const with capital letters.

01:12.200 --> 01:17.480
And the value is going to be lowercase retrieve which is going to be the name of the retrieve node.

01:17.760 --> 01:21.440
Similarly to what we see on the graph of our right.

01:23.400 --> 01:32.440
Alrighty, now it's time to go to the graph.py file, and let's finally build an orchestrator graph.

01:33.080 --> 01:35.680
So we first want to start with the imports.

01:36.040 --> 01:39.880
Let's start by importing load dot env to load the environment variables.

01:40.320 --> 01:45.040
We also want to import from graph the add node and the state graph.

01:45.520 --> 01:52.920
And we want to import all of our costs that we defined earlier which are going to be the nodes names.

01:53.600 --> 01:58.520
And now we want to import all of the nodes we created in the earlier sections.

01:58.880 --> 02:05.440
So we'll import the generate node, the greet documents, the retrieve node and finally the web search

02:05.440 --> 02:05.920
node.

02:06.480 --> 02:11.970
And finally we want to import the graph state which we created in the states py paid for.

02:13.730 --> 02:20.130
All right, so if we take a look at the graph from our right, then we can see that after the great

02:20.130 --> 02:22.330
documents node we have two edges.

02:22.330 --> 02:23.850
One is going to the web search.

02:23.850 --> 02:28.690
And this is if we found at least one document that is not relevant to the question.

02:29.050 --> 02:31.730
And we have an edge to the generate node.

02:31.770 --> 02:37.210
So this is if all documents are valid and are related to the question.

02:37.410 --> 02:41.010
And this is going to be our graph conditional edge.

02:41.370 --> 02:47.730
So let's go and define the function that will decide which node are we going next.

02:47.850 --> 02:53.930
So it's going to output either the web search node or the generate node according to the conditions

02:53.930 --> 02:55.250
we described just now.

02:55.850 --> 03:01.450
So we'll define the function we'll call it decide to generate which will accept the graph state.

03:01.890 --> 03:06.890
And if the web search flag in our state is going to be true.

03:07.210 --> 03:12.330
And this means that we found a document that is not relevant to the user's query.

03:12.530 --> 03:16.570
So this is the heuristic that in this case we want to search online.

03:16.770 --> 03:19.810
So here we would like to return the web search node.

03:19.850 --> 03:21.810
Now notice I'm returning the const.

03:21.810 --> 03:22.930
And this is the name.

03:23.570 --> 03:29.490
And if not and this means that all of the documents we retrieved were relevant to the query.

03:29.490 --> 03:31.330
So here we want to return generate.

03:31.330 --> 03:34.930
So the graph execution will move to the generate node.

03:35.330 --> 03:40.010
And now let's do the easiest part which is connecting everything together.

03:40.290 --> 03:42.210
So we'll create our flow.

03:42.250 --> 03:43.490
We'll call it workflow.

03:43.490 --> 03:49.290
And it's going to be the graph state graph where the state is the state we defined.

03:49.730 --> 03:55.370
And we'll use the add node functions to add all of the nodes currently not connected.

03:55.570 --> 04:00.570
So we added four nodes and they retrieve node is going to run the retrieve function.

04:00.610 --> 04:04.490
Now notice that the retrieve name is lowercase like we see in the picture.

04:04.490 --> 04:05.410
From our right.

04:05.770 --> 04:11.570
We have the Great Documents node which is going to run the Great Documents function, the generate node,

04:11.570 --> 04:12.770
and the web search node.

04:13.330 --> 04:16.690
Let's now add the entry point to be the retrieve node.

04:16.690 --> 04:20.610
So this is the first function which is going to run to retrieve the documents.

04:20.810 --> 04:24.450
We then want to move into the Great Documents node.

04:24.450 --> 04:30.010
So we'll add an edge between the retrieve node and between the Great Documents node.

04:30.410 --> 04:37.210
And now we want to add the conditional edge from the great nodes to either web search or the generate

04:37.250 --> 04:37.770
node.

04:38.170 --> 04:43.450
So we're going to use the add conditional edge function from the great documents.

04:43.450 --> 04:44.770
This is the source node.

04:45.050 --> 04:48.650
The function which will decide which node is going to be executed.

04:48.690 --> 04:52.170
Next is going to be decide to generate which we implemented.

04:52.450 --> 04:55.410
And the third argument which is going to be the path map.

04:55.410 --> 05:00.450
I'm going to put here a dictionary from web search to web search and from generate to generate.

05:00.930 --> 05:06.850
And the reason I put it here is simply to show you that there is this option, and what this will do

05:06.850 --> 05:09.370
if we put here other names in the values.

05:09.370 --> 05:16.970
For example, if instead of web search as the value would put end, then this would be a simply different

05:16.970 --> 05:18.330
mapping to go to.

05:18.770 --> 05:24.490
So it's useful when the function you implement that decides which node to go to is not returning the

05:24.490 --> 05:25.250
node names.

05:25.810 --> 05:28.330
And again in this function we we implement it?

05:28.370 --> 05:29.530
We don't really need them.

05:29.530 --> 05:32.490
I simply wanted to show you that this option exists.

05:33.850 --> 05:41.290
Alright, so after we execute the web search node, we have the results and we're ready to go to the

05:41.290 --> 05:42.210
generate node.

05:42.210 --> 05:45.570
So let's add the edge from web search to generate.

05:46.330 --> 05:50.650
And now we want to add the last edge from generate to end.

05:51.210 --> 05:52.010
And that's it.

05:52.050 --> 05:53.930
We finished building our graph.

05:54.330 --> 05:55.650
Let's compile it.

05:56.010 --> 05:58.010
And we also want to print it.

05:58.050 --> 05:59.970
We'll print it into graph PNG.

06:01.250 --> 06:05.090
And finally we are done with our graph.

06:05.090 --> 06:08.890
So now let's go to the main.py file.

06:08.890 --> 06:11.410
And let's import the graph.

06:11.410 --> 06:15.490
And let's invoke it with the questions of what is agent memory.

06:16.490 --> 06:19.250
Alrighty, let's go and run this baby.

06:21.450 --> 06:25.290
And from our prints we should see the graph structure.

06:27.210 --> 06:29.250
So we can see we achieved the documents.

06:29.250 --> 06:31.010
We checked relevance.

06:31.050 --> 06:33.330
We saw which documents are relevant or not.

06:33.370 --> 06:39.740
We saw that one document was not relevant, so we executed the external search.

06:40.180 --> 06:44.260
We then went to the generate node and finally we had the response.

06:44.500 --> 06:46.700
And the response we got was pretty good.

06:46.700 --> 06:48.540
Let's go and check out the graph png.

06:48.580 --> 06:51.580
So this is the exact graph we see from our right.

06:52.260 --> 06:57.540
And let's go to lengths to trace our graph execution.

06:58.260 --> 07:00.980
And let's take the last trace over here.

07:01.460 --> 07:06.540
And we can see we have all the nodes that we executed from their order.

07:07.100 --> 07:07.620
We can see.

07:07.660 --> 07:11.300
For example let's take a look at the web search.

07:11.300 --> 07:17.860
And we can see that we executed here with the search item of what is agent memory.

07:20.220 --> 07:21.060
Alrighty.

07:21.100 --> 07:27.060
And last thing, if you want to check out the code, please go and visit the GitHub repository in the

07:27.060 --> 07:32.420
branch line graph and you should see the entire implementation you just saw there.

07:32.740 --> 07:37.380
Updated with the latest line chain and line graph versions.
