-: So in this video we're gonna cover
how you can summarize large documents using lang chain.
So let's start by installing lang chain
and Py PDF if you don't already have it.
After installing those packages,
what we're then gonna do is import a range
of different packages from lang chain and requests.
So we're gonna import the chat models, open AI.
We're gonna also import a document loader called
Py PDF loader,
that's going to allow us to load PDF documents
directly into lang chain documents.
We'll also use one of lang chain's
most common text splitters called the
recursive text splitter,
and we'll load in something called a
load summarized chain function.
As well as that we're going to import requests
and we'll be going and getting a marketing book
called Principles of Marketing PDF.
We're gonna just download that
and store it on the local file system
with the requests.
After we have downloaded the PDF,
what you're then going to do is you're going
to use a recursive character text splitter,
which will recursively split across a range
of different separators and it will try
and keep within a specific size.
So there are a couple of different parameters
that you can choose with the recursive text splitter.
One that's quite common is using
something called the chunk size.
And the chunk size is where you're defining
how large your chunks are
and you can basically determine
what size the chunks that you would like to have.
And then by default, all of the documents
that you create will have that size
in terms of character length.
You'll see here that we're loading in the PDF
and we're then splitting it.
So we're doing loader, load and split.
And then if we have just a brief look here at the pages,
you will see that we now have these pages
which are basically lang chain documents
and it's got some metadata about what the source of it is
and the page of it is.
Also, notice how we've got some of the page content here.
So you can see the first page doesn't have much content.
And then if we start to look at some of the other ones,
we can see that this book is produced
by the University of Minnesota.
What we want to do is we want to load in our chat model
as an LLM, and this is important
because we're going to use this in the various functions.
So what you're gonna do is you're going to import
that chat, open AI.
Remember that if you do need to set an API key,
you will have to set that here.
So the way that you can do that,
and I'll leave this in the code,
would be to use the OS package
and then you could do OS environ,
and then we'll put the open AI API key
and you add your API key here.
And then that would basically mean
that this model is then authenticated
so that you can easily call open AI.
You'll see that the chat model by default uses
GPT 3.5 turbo
and this will be, you know, upgraded as more
and more models roll out.
We're gonna use a function today called
load summarize chain.
And essentially what this does is it takes in an LLM,
a chain type, so that could be stuff, map, MapReduce,
there's other ones that are quite important.
I personally find that MapReduce is very good
for large documents.
And essentially what happens in MapReduce is
you can think of MapReduce being,
you will do an operation on each individual document
and then once you've got all those documents,
you will then use a reduce pattern
on all of those documents.
So you could have a hundred documents,
you create a hundred summaries,
and then you would have a reducing pattern
that would then recursively reduce those
hundred documents into a final summary.
The way that we can load this up is
by using load summarize chain,
the LLM, and the chain type.
And in this case we're passing in map underscore reduce.
This one in particular to do the whole 400 pages of a book.
And that took about 30 minutes.
So it does take a lot of time
to run a large summarization chain.
However, this is kind of the summary that you can see.
And it says this passage covers a range of topics
related to marketing, including defining marketing,
strategic planning, consumer behavior.
So we've got a really nice high level summary
after running that summarization chain on the
entire PDF of the book.
Now I think it's worthwhile just seeing it in action.
So if we just have a look at like, you know,
the 10 pages and running a summarization chain,
you'll see it's taking a bit of time
and what's going on underneath the hood
is it's running that MapReduce pattern
across a range of different documents for you
and essentially getting summaries of each
of the individual pieces
and then reducing that back into a single LLM response,
which you'll then get back as a summarized output.
So we're just gonna see how long this takes here.
We're currently at about half a minute,
so let's actually just see what's come back now.
So we've got various marketing concepts
as marketing segmentation, targeting, planning,
advertising, it's designed to help individuals
effectively promote products and services.
Cool, so it's very easy for you to, you know,
do a summarization chain on any type of document
and so essentially what we've got here is
we've got this LLM, we have the chain type,
and then we are also passing in these pages,
which is a list of documents.
So now that you can sort of see that running in action,
I think it's important for us to take a step back
and say, well, maybe you want
to do a summary in a different language
or you know, maybe you've got something that you need
to really specifically do at every stage of
that map or reduce pattern.
This is where you can create a MapReduce chain,
which is a custom chain.
And so what you'll see here is we've got this
map pattern here that says,
given the following pages of a marketing book,
generate a summary in Spanish
and we've got the pages here.
And then you say, given the following summaries of pages
of a marketing book, generate a high level
description of the book in Spanish.
We've set up a map prompts
that will map over the pages one at a time.
We also set up a reduce prompt
that will go over the summaries
and then we'll reduce those.
We set up two LLM chains.
And then as well as that we have a list of documents
and combining that into a single string.
We also have the MapReduce documents chain,
which takes in the map
and then associates all of the documents,
the individual documents with pages.
And we also have the combined document chain here.
So just in case we need to combine these,
that's kind of how that's working underneath the hood.
And then, so that's part
of the reduce section when we're doing that combine.
So that's a chain to use to combine results
of applying LLM chains to documents.
We then set that up with a MapReduce chain,
and we feed it in the MapReduce documents chain.
And as well as that you can also attach a text splitter
and then essentially you just pass in your input text.
So in this scenario here you can see,
we call our MapReduce chain, which is called MapReduce
and then we pass in our input text.
And what we've done is I've looked at the top
hundred documents to start with
and then what we've done is extracted the page content,
join those altogether and run that
through a MapReduce pattern.
But notice now that the actual text
that you're getting back is
not necessarily an English
summarization across all of these
first hundred documents.
It's actually a Spanish translation.
So you know, being able to specifically change
what happens at the map section
of your summarization chains,
or even just what's happening when all the documents
are getting stuffed in to get reduced,
can give you the flexibility to really
optimize your summarization techniques.