Hello guys.
So we are going to continue the discussion with respect to text summarization.
Already in our previous video, we have seen the theoretical intuition behind stuff document Chain and
map reduce text summarization technique.
In this video, I will show you the practical implementation how you can specifically implement it.
So first we will go with the stuff document chain text summarization technique.
So here I have written a code of pi load pi pdf loader, where we will be loading a specific PDF and
this PDF that I've actually taken over here you can see it is something called as FPGA speech dot PDF.
Now inside this particular PDF there is an amazing speech that was given by and probably the last speech
that was given by our former president, you know, uh, doctor APJ Abdul Kalam, who is a famous he
who was a famous personality.
And uh, this specific pdf, I will try to read it.
Okay.
So now let's go ahead and do this and we'll try to summarize this entire PDF, okay.
In entire text inside this particular PDF.
So now if I go ahead and just execute this.
So here I'm actually going to get this one.
So as soon as I read this PDF here you can see I'm getting so many number of documents.
Right.
And uh in order to just write pages over here, I'll just go ahead and write documents.
So here are all the documents that I have got from reading from this particular PDF file.
Now I will take up all this particular documents and then uh, we will try to summarize it first of
all by using the stuff document chain.
Now in order to summarize it, uh, with respect to the stuff document chain, first of all, you know
that we need to go ahead and create our prompt template.
So for that we will create our template first of all.
So let's go ahead and write it down over here.
So I'll go ahead and write hey this is my template.
And I will make a, uh, I'll just make a triple quotes for multi-line, uh, multi-line comments or
sentences that I really want to use.
Okay, so inside this template I will go ahead and say, hey, uh, write, uh, concise.
Write a concise and short summary of the following speech.
Okay.
If you remember, uh, in our previous video.
Right.
Uh, what did we basically say?
If I want to use a stuff document chain?
First of all, if this document fits in the LM context window, I am going to, first of all, create
this, uh, prompt, uh, prompt template itself.
I'll say extract a final summary from the documents, and then I'll pass it to the LLM.
And finally I'll get the final summary.
So this is the same, uh, fundamental thing that I'm going to apply, uh, the same chain I'm going
to specifically apply over here.
Okay.
So quickly let's go ahead and continue it.
And here I'm going to basically say write a course concise and short summary of the following speech.
And here I will just go ahead and give my speech itself.
The speech will be nothing but with this.
And I'll use an input variable where I'll say it is a text.
Okay.
Uh, after creating this particular template.
Now let me just go ahead and define my prompt.
So prompt will be nothing, but I will use the same prompt template.
And inside this I will go ahead and use my input variables.
And inside this input variables I will just go ahead and write my input variable as text.
Because this is the information.
This will be the uh text that I am going to pick it from here right from this entire documents.
So here I'll actually go went ahead and probably created my prompt template.
So, uh, if I just go ahead and execute this.
So one error you can probably see speech is equal to text okay I think so.
I have not given my okay.
After this.
The second parameter that we really need to give is our template.
Right.
Because it is expecting template over here.
So I'll just go ahead and write it okay.
So once this is getting executed this has basically created my entire prompt template.
Now the most important thing that we are going to discuss how do we apply this stuff.
Document chain text summarization on this entire text by using this particular prompt template.
So in long chain we have something called as from lang chain.
Let me just go ahead and import this.
And then I'll explain you.
So in long chain you have something called as chains.
And inside this you have a module which is called as summarize.
And whenever you want to create this entire text summarize chain itself.
You can use this import library called as load summarize chain okay.
Load summarize chain is just going to combine this entire thing uh, with respect to the implementation
that we are doing for the text summarization.
Okay.
So I will just go ahead and execute this.
Now, what I will do, I will just go ahead and create my chain.
In this chain I will just use load summarization.
Now understand what we are basically using.
First of all we need our LM model.
The LM model is used as Chad Grok that same LM model that we are going to use.
And then we have our chain type okay.
Now inside this chain type these are the three chain types that I'm going to use.
One is stuff uh one is map reduce and the other one is refine.
Okay.
We have already discussed the theoretical intuition between, uh, you know, we have understood the
theoretical intuition, uh, with respect to stuff document chain and map reduce.
So let me just go ahead and give the parameter as stuff.
Okay.
Then the third parameter that we have to give over here is nothing but my prompt, which will be my
prompt over here.
And then finally I will go ahead and write verbose is equal to true to see all the information whenever
I probably run this particular chain okay.
Now the most important thing what exactly is happening when I keep this particular chain type as stuff?
It is just going to combine all these documents and it is going to pass it to the prompt template along
with the prompt template.
It is going to pass it to the LLM module.
Right now this is the chain that is specifically created.
So let me go ahead and explain what this load underscore summarize chain will basically do.
So here you will be able to see that I have a list of documents.
So whenever we use this chain type is equal to stuff.
What it is going to do is that it is going to combine all these documents.
Once it is going to combine all the documents, then it is going to create a chain wherein this documents
will be passed to the prompt template.
From this prompt template, it will be passed to the LLM model.
And finally we get the entire text summarization of all that particular documents.
Okay.
And this is this will happen when when you go ahead and execute this chain.
Right.
So for that you just need to write chain dot run.
And inside this I will go ahead and pass my documents okay.
Now with respect to this documents I will be getting my output summary okay.
And I will just go ahead and execute it.
Now see the magic okay I'm just going to display the output summary.
So docs was written over here.
Let's see I have not executed this.
Oh now I have executed this docs.
Now it's fine.
We should be able to get the output now.
See, it is entering into the new stuff document chain.
It's entering into the LLM chain that is basically being created with the help of prompt and LLM model.
And here is my entire prompt.
Write a concise and short summary of the following speech.
And this is the speech where you will be able to see APJ Abdul Kalam.
This is the entire speech.
And finally you can see that there are some more content which you are not able to see.
But we get the final summary over here.
Right?
So it shows APJ Abdul Kalam departing speech.
In this departing speech, former uh president APJ Abdul Kalam highlights the remarkable progress made
by uh India and various sectors and emphasizes the potential.
Right.
So this entire information is basically my summary.
So how easy it was basically to do with the help of this stuff.
Document chain type for text summarization okay.
Now similarly what we will do is that we will also go ahead and I'll show you one example for your MapReduce.
Okay.
So MapReduce or to summarize summarize or large documents.
Okay, now here with respect to MapReduce, I have already shown told you like how it is going to work.
First of all, all the document chunks will be made right.
All the document chunks will be named.
Then for each chunk we will try to create a prompt template.
That prompt template can be same and it is going to summarize for that particular chunk.
And finally we will be able to get the output okay.
Once we combine all the chunks and then we send it with another prompt template finally to get the output.
So just to show you what all things we are basically going to do over here, uh, I will also go ahead
and import from Lang chain dot text splitter.
I'm going to import recursive character text splitter because we need to divide that into chunks.
Right.
So we are going to import this okay.
Now you know that uh, this is my documents right.
Or my documents.
I have already read it.
So this is my documents now for this particular documents, what I am actually going to specifically
do is that, uh, I will go ahead and probably create a chat prompt template.
But before that, uh, what I will do is that I will go ahead and use this text splitter.
I will say, hey, create an object of text splitter where I'll say recursive character text splitter
and I'll say, okay, let's go and make this chunk size of 500 or let's say 2000 okay.
And here I'm also going to make a chunk overlap.
So first of all I'm going to divide this documents based on this chunk size and chunk overlap okay.
And I'm considering that uh, the document that is basically present with me, it fits, uh, it is
more than the context size or context window size of the LM models.
Right.
So here I'm going to use a chunk overlap of 100 okay.
So this is what is my text splitter.
And uh here what I will specifically do is that I will take this entire text splitter.
Okay.
I will say from documents okay.
So it should be from underscore documents I hope so it is right.
Um text splitter dot okay.
Or instead of this, you know, if I just use split documents.
Let's see.
So if I just use this function of split documents I will be able to get my final documents.
I can also do this okay.
So I don't want to write much line of code line by line Okay.
Then finally I can see my final documents.
So documents.
Okay.
I have to provide my documents over here.
So let's go ahead and give my docs.
And here you have this.
So here I you have seen that already.
I had this kind of chunks of documents but I still divided this into many more chunks.
Okay.
Now what is going to happen if I use MapReduce?
Okay.
it will first of all take this chunk.
It will pass to the prompt template and then it will summarize this with the help of LM model.
Similarly, it will send the next sentence or next document to the prompt template along with the LM
model in a chain.
And it will summarize this.
So all these documents will get summarized in a separate way.
And finally, whatever summary we get, we can combine them with another prompt template and get the
final summary.
That is what I am planning to do it over here.
Okay, so here you can actually see.
And if I also go ahead and see my length of the final documents.
So this many are like 13 documents are basically there okay.
Now quickly let's go ahead and create our uh prompt template.
And you know that our prompt template, uh, how it is basically created.
Um, over there, we have specifically used something like this.
Right.
So this is my prompt, right, with respect to this particular prompt.
Right.
Um, so what I will do, I will just go ahead and say, okay.
And uh, let's go ahead and create that all prompt over here.
So this will be my first prompt.
Let's say for this I will create another prompt.
Okay.
So here is my chunk underscore prompt.
I'll say please summarize the below.
Speech and speech is equal to text summary.
So much information.
And here I'll get the summary right.
And uh this is my prompt template here.
My input variable is text.
And template is nothing but this chunk prompt okay.
Finally I'll go ahead and execute it.
This is my uh, map prompt okay.
Now understand for all the chunks I'm going to apply this particular prompt okay.
So in order to execute it what I will do I will go ahead and create my summary chain again.
So this will basically be my summary chain.
Okay I'll create a variable and I'll say hey I'll call that load summarize chain again.
Again if you remember the first parameter was nothing but LM.
LM is equal to lm.
The second parameter was something called as chain type, right?
Previously we gave the chain type as stuff.
But in this case we will give it as Map-reduce okay.
And the third one what we will do is that we will give the map prompt template sorry map underscore
prompt okay.
Map underscore prompt is equal to.
And here I'm actually going to give my chunks chunk prompt okay.
So this will specifically be my chunk prompt.
Or instead of giving chunk prompt I'll give this map prompt template because I need to give a prompt
template over here.
Now understand one thing, okay.
This prompt, uh, this prompt template that we have created, this is for the chunks, right.
Finally, when I get the summary of all these things right, separate, separate summary for all these
documents, I also need to create another prompt template.
Right.
And that will basically be my final prompt.
So I'll say, hey, this is my final prompt and this will be for my all the summarized text with respect
to the chunks.
Right.
So here I will just go ahead and write this quickly and let's write it over here.
So what I will say over here is that provide the final summary of the entire speech with these important
points.
I'll just go ahead and write like this.
Okay, first of all, I'll say, hey, go ahead and add a generic or I'll say, hey, go ahead and add
a motivational title.
Okay.
Then start the precise summary.
Summary with an introduction.
I'm just writing my own prompt.
Okay.
And provide the summary in number.
In in number points for the speech.
Okay.
So I basically want the entire summary in the form of numbers.
So that is the reason I'm writing this.
Okay.
Finally I'll go ahead and write my speech.
And this speech will basically be having my text.
So here you can see that this ad has got added.
So let me just go ahead and remove it okay.
So this basically becomes my final prompt.
Now I'm going to combine this uh as my final prompt template okay.
Prompt template.
This will be my final prompt.
Remember when this will get applied, when the entire summary of, uh, the chunks have actually got
right.
So in order to show you the diagram over here.
Right.
Let me just show you the diagram.
Okay.
Quickly.
And then here you will be able to understand it.
So here you can see I'm implementing this.
The first prompt has been created for the chunks right.
I will get all the summarize.
So second will be my final prompt which I am actually implementing.
Okay so let's go back to uh my code okay.
So final prompt template you can see over here I will go ahead and say hey this will basically be my
prompt template.
And this my input variables will be nothing but it will be text okay.
This is how we specifically write.
And here you will be able to see template is equal to final uh final prompt.
Or I can go ahead and write this final prompt.
Okay.
I have to close this.
Right.
So this will basically be my input template okay.
Done.
So if I go ahead and execute this final prompt template this is what is the template I'm going to use
over here in the summarize chain.
Now this became my first template.
The second template parameter that we have in load summarize chain is something called as combine prompt
okay.
So if I go ahead and write combine prompt.
Now what does combine prompt basically says that this map prompt is the first prompt template which
we are going to use for.
And this will give the summary.
This will basically combine with the LM and summary and will give the summary of the smaller chunks.
Right.
Uh, once we get all the summaries, then we can combine them with this specific prompt template.
That basically means all the summary will be combined and sent to this prompt template.
Right.
So here I will go ahead and write this.
And finally I will go ahead and write.
Verbose is equal to true so that you will be able to see the details.
Now quickly let's go ahead and execute this here I'm going to write.
Output is nothing but I'll write summary whatever summary chain.
We are basically going to create our summary chain dot run.
And here what I will be giving I will be giving my docs write whatever docs I have.
Not docs.
Sorry.
Uh, I should be giving this final documents.
Okay?
Not docs, but final documents.
Quickly.
Let's go ahead and execute this.
I'll go ahead and execute this over here.
So this will basically be my output.
Now let's execute this.
See the magic now.
So entering a MapReduce document chain it has entered the LM chain.
First of all it took this prompt template I am getting the summary right.
Then it went into a new chain with the final prompt.
So here you can say find.
Please provide the final summary of the entire speech.
Add a motivational title.
So this is the output that you are getting right and this is how you are displaying your output.
Introduction.
This this this.
So so based on this when I did this verbose is equal to true here you could see that initially we when
we applied this MapReduce documentation, the first prompt template that got applied was this right
for every chunks.
Then we got all the summaries.
And finally we combined all the summaries and passed into a another prompt template, which is nothing
but your final prompt template.
And here you get the complete final response.
So this was the two types of uh text summarization techniques, which we specifically use in load summarization.
Okay.
I'll just make one more change over here.
I'll just make the spelling.
Right.
Okay.
Now in our next video we are going to see about refine.
Right.
And uh, the differences between refine and all.
We will try to discuss, write, refine text summarization technique and all.
There is just a minor changes which we are going to discuss in the next video.
So yes, this was it from my side.
I'll see you all in the next video.
Thank you.