WEBVTT

00:00.000 --> 00:02.490
-: In this video we're gonna have a look at a variety

00:02.490 --> 00:04.380
of different approaches to summarizing,

00:04.380 --> 00:07.170
specifically looking at summarizing chains.

00:07.170 --> 00:08.940
Now we'll do our regular form of import,

00:08.940 --> 00:11.580
so Chat OpenAI, LLM chains.

00:11.580 --> 00:12.690
There's some new ones here.

00:12.690 --> 00:15.150
So for example, we can use a document loader

00:15.150 --> 00:18.630
to specifically load a URL or a list of URLs.

00:18.630 --> 00:20.460
You're also gonna need to import something called

00:20.460 --> 00:22.200
a load summarize chain

00:22.200 --> 00:25.770
and so when we run this, you'll see what happens is

00:25.770 --> 00:27.870
basically we load in some documents

00:27.870 --> 00:30.750
and these get assigned to LangChain documents.

00:30.750 --> 00:33.480
We then load up a chat open AI model,

00:33.480 --> 00:37.920
and then we use a load summarize chain called a stuff chain,

00:37.920 --> 00:40.830
which basically means that we stuff all of the documents

00:40.830 --> 00:43.080
into a single LLM call.

00:43.080 --> 00:44.130
That's a really important point.

00:44.130 --> 00:48.120
So again, stuffing is putting all of your LangChain

00:48.120 --> 00:50.910
documents into a single call,

00:50.910 --> 00:53.970
which does mean that if you have more documents than you can

00:53.970 --> 00:57.030
fit into a model, it will return an error.

00:57.030 --> 00:58.350
And you can see after we've done that,

00:58.350 --> 01:00.180
we've got a nice little summary explaining

01:00.180 --> 01:03.101
that this article is about building autonomous agents,

01:03.101 --> 01:05.640
which are powered by these large language models.

01:05.640 --> 01:08.400
We have then done a different approach,

01:08.400 --> 01:11.700
which is using something called the map reduce pattern.

01:11.700 --> 01:14.398
So you import a couple of different variables

01:14.398 --> 01:16.680
and things like the reduced documents,

01:16.680 --> 01:20.070
a map reduced documents chain and a stuff documents chain.

01:20.070 --> 01:21.870
You first have to set up your map,

01:21.870 --> 01:25.913
which is basically mapping over a set of documents

01:25.913 --> 01:29.730
and basically on that, identifying the main themes.

01:29.730 --> 01:34.320
And then you create a map chain whilst the reduce takes all

01:34.320 --> 01:37.530
of the summaries that are created inside of your map.

01:37.530 --> 01:40.260
And what it will do is it will then reduce those summaries

01:40.260 --> 01:42.030
down into a single summary.

01:42.030 --> 01:44.430
So map reduce runs really, really quickly.

01:44.430 --> 01:46.860
It's one of the faster ways of doing summaries.

01:46.860 --> 01:48.720
The problem with the map reduce chain

01:48.720 --> 01:51.000
is the pages don't remember each other.

01:51.000 --> 01:55.440
So, it can cause an issue where if page three kind of needs

01:55.440 --> 01:56.580
to know about page five,

01:56.580 --> 01:58.560
then the summaries are all done independently.

01:58.560 --> 02:01.740
So you can get some different forms of summarization

02:01.740 --> 02:02.820
which will handle for that.

02:02.820 --> 02:05.310
Moving back up, we use this LLM chain

02:05.310 --> 02:08.490
and we run something called a stuff documents chain,

02:08.490 --> 02:10.400
which takes in the reduced chain.

02:10.400 --> 02:13.770
And it's basically what it does is it combines

02:13.770 --> 02:16.770
these documents into a single string, right?

02:16.770 --> 02:18.420
So it takes a list of documents

02:18.420 --> 02:20.343
and combines 'em into a single string.

02:21.810 --> 02:24.120
And then what we do is we build something called

02:24.120 --> 02:26.880
a reduced documents chain, which combines

02:26.880 --> 02:29.520
and iteratively reduces the mapped documents.

02:29.520 --> 02:33.180
So the reduced chain here is this one,

02:33.180 --> 02:35.280
which is your stuffed documents chain.

02:35.280 --> 02:38.400
And then we have a collapsed documents chain, which is set

02:38.400 --> 02:40.080
to equal to the combined documents chain.

02:40.080 --> 02:42.093
So we're just using the same chain here.

02:42.093 --> 02:44.333
And this reduced documents chain,

02:44.333 --> 02:46.630
what this basically does is

02:47.520 --> 02:51.420
it's gonna reduce your documents down using map reduce.

02:51.420 --> 02:52.253
All right?

02:52.253 --> 02:55.290
And the map prompt comes from up here, right?

02:55.290 --> 02:57.270
So this is where the map chain is used.

02:57.270 --> 02:59.760
And then we have this map reduce documents chain,

02:59.760 --> 03:00.593
which is the final bit.

03:00.593 --> 03:03.630
So this is the signing, the reduce chain, right,

03:03.630 --> 03:06.690
which uses this reduce template.

03:06.690 --> 03:09.900
And then after that then we've got the reduce chain set up.

03:09.900 --> 03:11.490
We then make the map reduce chain.

03:11.490 --> 03:14.430
So the LLM chain that we're gonna use for our map chain

03:14.430 --> 03:15.840
is this one here.

03:15.840 --> 03:20.100
The reduce chain is this one which reduces the documents.

03:20.100 --> 03:23.100
And this is the variable name in the LLM chain,

03:23.100 --> 03:26.820
which we call here in the map, which is called docs.

03:26.820 --> 03:29.400
And we do also return immediate steps.

03:29.400 --> 03:31.380
And so when we run that,

03:31.380 --> 03:35.670
it gives us a summary using map reduce, all right?

03:35.670 --> 03:37.140
So you can see here we've got a couple

03:37.140 --> 03:39.570
of different summaries that have been therefore reduced.

03:39.570 --> 03:40.403
So there you go.

03:40.403 --> 03:41.910
So that's map reduce.

03:41.910 --> 03:44.610
The final summary type that we have is something called

03:44.610 --> 03:48.120
a refine template, which is basically like where you say,

03:48.120 --> 03:49.705
right, a concise summary of the following

03:49.705 --> 03:52.440
and you ask it to have a summary.

03:52.440 --> 03:55.380
And then we have a something called a refined template.

03:55.380 --> 03:56.490
And this refined template

03:56.490 --> 03:59.220
is to basically take the existing summary

03:59.220 --> 04:02.130
that get generated from these, provide it new texts

04:02.130 --> 04:04.170
and say, given the new context,

04:04.170 --> 04:05.400
could you do something to that?

04:05.400 --> 04:07.680
So could you refine the original summary,

04:07.680 --> 04:10.350
so we could do this, refine the original summary,

04:10.350 --> 04:13.050
if the content isn't useful, return the original summary.

04:13.050 --> 04:16.080
And then when we do our load summarization chain,

04:16.080 --> 04:18.810
what we're doing is we're passing in the question prompt,

04:18.810 --> 04:21.300
which comes from this prompt template here.

04:21.300 --> 04:24.570
And then we also have our refined prompt,

04:24.570 --> 04:26.100
which comes from here.

04:26.100 --> 04:30.060
And then we put our input key, which is our input documents

04:30.060 --> 04:33.930
and our output key, which is our output text.

04:33.930 --> 04:36.450
And then we'll end up with a result.

04:36.450 --> 04:39.840
And what's good about the refined way is if you want

04:39.840 --> 04:43.110
to essentially read the document, one page,

04:43.110 --> 04:45.900
then the second page, and then page three and page four.

04:45.900 --> 04:49.410
That's what the refined summarization technique is doing.

04:49.410 --> 04:52.950
It's essentially saying, let's start with page one.

04:52.950 --> 04:54.030
We'll make a summary.

04:54.030 --> 04:56.670
We'll go onto page two, refine,

04:56.670 --> 04:59.460
and then we'll go onto page three and then we'll refine.

04:59.460 --> 05:02.910
So the danger of using refine is that it's gonna take you

05:02.910 --> 05:05.545
a lot longer than something like MapReduce,

05:05.545 --> 05:09.060
because MapReduce runs each individual summarization

05:09.060 --> 05:13.718
independently whilst refine has to be a sequential series

05:13.718 --> 05:15.540
of steps, right?

05:15.540 --> 05:18.669
And those steps will lead to a greater summary.

05:18.669 --> 05:21.060
But the benefit of refine is that obviously

05:21.060 --> 05:23.190
it can improve the summary over time.

05:23.190 --> 05:25.650
So here's three different summarization techniques

05:25.650 --> 05:28.953
that you can use inside LangChain to get better results.
