-: In this video we're gonna have a look at a variety
of different approaches to summarizing,
specifically looking at summarizing chains.
Now we'll do our regular form of import,
so Chat OpenAI, LLM chains.
There's some new ones here.
So for example, we can use a document loader
to specifically load a URL or a list of URLs.
You're also gonna need to import something called
a load summarize chain
and so when we run this, you'll see what happens is
basically we load in some documents
and these get assigned to LangChain documents.
We then load up a chat open AI model,
and then we use a load summarize chain called a stuff chain,
which basically means that we stuff all of the documents
into a single LLM call.
That's a really important point.
So again, stuffing is putting all of your LangChain
documents into a single call,
which does mean that if you have more documents than you can
fit into a model, it will return an error.
And you can see after we've done that,
we've got a nice little summary explaining
that this article is about building autonomous agents,
which are powered by these large language models.
We have then done a different approach,
which is using something called the map reduce pattern.
So you import a couple of different variables
and things like the reduced documents,
a map reduced documents chain and a stuff documents chain.
You first have to set up your map,
which is basically mapping over a set of documents
and basically on that, identifying the main themes.
And then you create a map chain whilst the reduce takes all
of the summaries that are created inside of your map.
And what it will do is it will then reduce those summaries
down into a single summary.
So map reduce runs really, really quickly.
It's one of the faster ways of doing summaries.
The problem with the map reduce chain
is the pages don't remember each other.
So, it can cause an issue where if page three kind of needs
to know about page five,
then the summaries are all done independently.
So you can get some different forms of summarization
which will handle for that.
Moving back up, we use this LLM chain
and we run something called a stuff documents chain,
which takes in the reduced chain.
And it's basically what it does is it combines
these documents into a single string, right?
So it takes a list of documents
and combines 'em into a single string.
And then what we do is we build something called
a reduced documents chain, which combines
and iteratively reduces the mapped documents.
So the reduced chain here is this one,
which is your stuffed documents chain.
And then we have a collapsed documents chain, which is set
to equal to the combined documents chain.
So we're just using the same chain here.
And this reduced documents chain,
what this basically does is
it's gonna reduce your documents down using map reduce.
All right?
And the map prompt comes from up here, right?
So this is where the map chain is used.
And then we have this map reduce documents chain,
which is the final bit.
So this is the signing, the reduce chain, right,
which uses this reduce template.
And then after that then we've got the reduce chain set up.
We then make the map reduce chain.
So the LLM chain that we're gonna use for our map chain
is this one here.
The reduce chain is this one which reduces the documents.
And this is the variable name in the LLM chain,
which we call here in the map, which is called docs.
And we do also return immediate steps.
And so when we run that,
it gives us a summary using map reduce, all right?
So you can see here we've got a couple
of different summaries that have been therefore reduced.
So there you go.
So that's map reduce.
The final summary type that we have is something called
a refine template, which is basically like where you say,
right, a concise summary of the following
and you ask it to have a summary.
And then we have a something called a refined template.
And this refined template
is to basically take the existing summary
that get generated from these, provide it new texts
and say, given the new context,
could you do something to that?
So could you refine the original summary,
so we could do this, refine the original summary,
if the content isn't useful, return the original summary.
And then when we do our load summarization chain,
what we're doing is we're passing in the question prompt,
which comes from this prompt template here.
And then we also have our refined prompt,
which comes from here.
And then we put our input key, which is our input documents
and our output key, which is our output text.
And then we'll end up with a result.
And what's good about the refined way is if you want
to essentially read the document, one page,
then the second page, and then page three and page four.
That's what the refined summarization technique is doing.
It's essentially saying, let's start with page one.
We'll make a summary.
We'll go onto page two, refine,
and then we'll go onto page three and then we'll refine.
So the danger of using refine is that it's gonna take you
a lot longer than something like MapReduce,
because MapReduce runs each individual summarization
independently whilst refine has to be a sequential series
of steps, right?
And those steps will lead to a greater summary.
But the benefit of refine is that obviously
it can improve the summary over time.
So here's three different summarization techniques
that you can use inside LangChain to get better results.