Hello guys.
So we are going to continue the discussion with respect to Lang Chain.
In this video and in the upcoming series of video, we are going to discuss about this module, which
is called as Summarize text.
Let's say if you have any kind of data source, it can be a unstructured file, it can be a structured
file.
It can be different different types of file.
What we will be doing is that we will be reading the specific files, and then we will summarize the
entire information that is present in that.
Whenever we talk with respect to summarization, um, you will be seeing that, uh, you know, there
are three ways specifically in long chain.
Okay.
And those three ways also will be using like stuff is one of the way MapReduce is one of the way and
refining is one of the way.
Right?
So first of all, we'll just get to know about this basic implementation, how to properly use stuff
MapReduce and refine.
And as we go ahead, we will be creating an end to end project.
And in that end to end project, we will try to summarize text from both structured URL or let's say
from a website content, or from an unstructured, uh, content.
Also.
Right?
Unstructured content can be YouTube videos.
It can be, uh, Twitters, it can be Wikipedia page, you know, and structured content basically means
like how you have a data in a tabular format.
Right.
So we will be using different different data sets.
And we will try to perform this end to end project.
So that will be coming in the upcoming, uh videos.
But in this video let's go ahead and start with this.
You know, uh, we will I will show you all the three different types of techniques, specifically in
text summarization that we use.
Okay.
So first of all, what I'm actually going to do over here, you can see that I have imported the OS.
Then from dot env we have imported load underscore dot env.
This is just to import the environment variables.
And I'm also going to use the grok API because grok actually helps you to connect with the open source
LM models.
I don't even have to use OpenAI over there.
Okay.
Now, uh, quickly, what I will do is that, uh, I will make some cells.
Okay.
Now, first of all, what I will do, I will just go ahead and write from Lang chain.
Okay.
Dot schema I'm just going to import three important things okay.
So here I'm going to import I message okay I message is specifically with respect to the response that
we get from the LM.
Then you have this human message and then you have the system message.
So till now I have not even discussed about this.
So that is the reason in every module you will be seeing, I'll be adding some of the new things right.
And Lang Chin is completely vast.
Like I don't know, like what all developments will be happening in the future.
Right.
So here what we are doing is that we are using this kind of schema which will have I message.
I message specifically refers to all the response that is coming from the LM model.
Human message is the kind of queries that we are specifically asking.
And system message is the, uh, is the instruction given to the system, how they really need to behave?
Okay.
Uh, here text summarization will be showing.
Uh, I will be showing you with the help of prompt template with the different types of techniques also.
Okay.
So first of all let's go ahead and import all these things.
Now I'm going to take a specific speech okay.
It is a long speech from our um honorable Prime Minister Narendra modi.
Okay.
So this particular uh, speech specifically in India, you have this famous speech over here and I'm
taking this, uh, it is spoken like, uh, mostly the things that are spoken over here are related
to, uh, Indian government and what all developments are specifically happening.
So I will just go ahead and execute this.
So what I'm actually going to do, I'll take the speech.
Okay.
And we will probably go ahead and summarize this entire speech.
Right.
That is what is our task.
So what I will do over here, I will just go ahead and give some basic prompts.
I'll say, hey, let's go ahead and create this chat message.
It will be a kind of list.
Okay.
The first default, uh, the first default instruction that I'm giving is with respect to system message,
I'll say, here is my content and you need to behave in this way.
So I'll say, hey, you are an expert with expertise in summarizing summarizing the speeches.
Okay, so there is your first one.
Then let's go to the next one.
Here.
I'm going to probably go ahead and write human message.
And again with respect to the content over here, I will go ahead and use an F string and I'll say hey
please provide a short and concise summary of the following speech.
Okay?
And here I will just go ahead and give my text placeholder okay.
And this text placeholder will be assigned with a variable which looks like this speech okay.
And this speech will be is given over here.
Right.
So here, uh, we are just setting up this chat underscore message over here with respect to system
message and human message.
In the human message, we are just telling from the human side what information is basically going and
what we really need to what the LM model needs to do.
And the system message is a kind of instruction like what that LM model has to do with respect to its
functionality.
Okay.
So all the things is specifically mentioned over here.
Now quickly, what I'll do is that I've already initialized my LM model.
So I will just go ahead and execute this.
Okay.
First of all now I will go ahead and write LM dot get underscore tokens okay.
So I think there should be get underscore num underscore token.
So this will first of all determine how many number of tokens are basically present in this speech.
Right.
So if I execute this it will take some amount of time based on the kind of LLM models that you are specifically
using.
And then, uh, we should be able to see over here.
Right.
Uh, let's see why it is basically okay.
It is showing 909 as your, um, you know, the tokens, number of tokens over here.
Right.
So 909 is specifically the number of tokens in your speech.
Okay.
Now if you specifically want to see the, uh, content from this particular thing, or let's say I'm
just going to go ahead.
And here you can see in this particular model I have created this chat message, okay.
This chat message is also acting like a basic prompt.
Like what I'm saying that what the, LM model needs to do, and what is the human message that is probably
going over here based on the chat message.
I can also go ahead and pass this particular chat message over here.
And if I just go ahead and explore the content, this will be my summarized text.
So here you can see.
Summary.
The member of the Parliament emphasizes the importance of ensuring the government schemes reach the
standard beneficiary.
He highlights the visit Bharath Sankalp Yatra as an initiative to assess the effectiveness of government.
So this is the entire summary and this is one way of creating the summary, where I probably go ahead
and create a list of chat message where what is my system message that I'll mention, and what is the
human message that I'm going to mention over here?
I'm going to provide it as a parameter, which with respect to the entire speech and with respect to
the chat message, if I call with respect to LM, I will be able to get the entire summary.
Okay, if I just remove this dot content.
Also, you will be able to see that I will be able to get the entire response I message.
Now this is very much important.
As I said, I message is the response from the LM model.
And this is one way how you can specifically go ahead and summarize the entire content.
Okay.
Here you can see the output tokens here.
Output token.
See it Input tokens were 895 right.
And now you have this output tokens to 108.
So this entire 895 tokens has been summarized to 108 tokens okay.
So this is one way of getting the summary okay.
Now similarly I will show you multiple ways right.
This is one way right.
But this do you think it will work if your if your message is or if your entire speech is very huge,
if it is of 100 200 pages, then at that point of time what you will do.
Now the second technique that I am specifically going to use is nothing with.
I will create a prompt template first of all, and I'll say with this prompt template I'll try to perform
my text summarization.
So this is my second step that I am going to show you a second way.
okay?
And remember, all these ways will be very important when we are actually creating our end to end project.
Okay.
So here first of all, I will go ahead and write from long chain okay.
Or from long chain dot chains.
I'm going to import LM chain.
Now let me just go ahead and talk about this LM chain.
What exactly is LM chain.
Whenever we combine right.
Whenever we combine a prompt template with an LLM right, we basically say that as an LLM chain.
Okay.
You have actually seen in the initial stages of our, uh, of our course right there, we had specifically
used like we used to create an LLM along with the LLM.
We used to also create prompt along with the prompt.
We used to also create our string output parser.
So all these things we used to create.
Now in order to create a chain, we used to combine something like this prompt LM and your string output
parser.
Right.
Something like this.
But in the case of lm chain we just use this two.
We don't use a string output parser because whatever will be the default string output parser that will
get the response with okay.
Now in this case what I'm actually going to do, I'm going to use this LM chain.
Along with this I will go ahead and define my prompt template.
So I'm just going to from Lang Chain I'm going to import and I'm going to import the prompt template.
Now prompt template is specifically with respect to the kind of prompt that you really want to create.
Okay.
So here let me just go ahead and create my generic prompt template.
So here I'll go ahead and write Generate Top Prompt template.
And let's go ahead and write my prompt okay.
So now with respect to this particular prompt you need to understand one very important thing.
How do we write a prompt over here.
Right.
So I will say hey write a summary of the following speech okay.
And I'll say, hey, this is what I'm actually giving as a prompt okay.
Now I need to give my speech.
The speech will be given in the form of a input.
So this will basically be my input over here.
Right.
Speech is equal to speech.
Now I will also add one more task.
Let's say I will go ahead and say, hey, translate the precise summary to this specific language,
whatever language I have okay.
Now I'm doing one more additional task.
I'm just not getting the summary.
I'm also converting that entire summary to a specific language okay.
Okay, so this actually becomes my generic template.
And this is what prompt engineering is all about here I'm actually giving two inputs okay.
Now in order to convert this into a prompt I will just go ahead and use the same prompt template.
Uh, inside this prompt template.
First of all, I need to go ahead and mention my input variables.
Okay.
So when I mention my input variables there are two input variables and this needs to be provided in
the form of list.
One is speech and the other one is something called as language.
Once I give the speech and language then I also need to give my template over here.
So the second parameter that is basically used is something called as template.
And this template will be assigned to my generic template okay.
Now if I just go ahead and execute this prompt you will be able to see that this is my prompt template
with input variables, language and speech.
This is my template, right?
A number of, uh, summary of the speech.
And here I've assigned this particular placeholder.
Now if you just pass this input variable you can also see the entire prompt.
So for that I will just go ahead and write prompt dot format.
And inside this I will give my two input variable.
One is speech okay.
One is speech over here, which will be assigned to our speech value that we have given uh as a parameter.
And then I will also go ahead and pass our language.
Language.
Let's say I want to convert this into French, this entire summary into French.
So once I execute it now you can see right a summary of the following speech.
Okay.
The entire speech is over here.
It is placed it completely over here.
And in the end you can see translate the precise summary to French.
Okay.
So this actually becomes my entire prompt.
Okay.
Now what I'm actually going to do over here is that, uh, once I get this specific prompt okay, I
will just save this in our prompt.
Uh, I'll write.
Okay.
This is my complete underscore prompt.
Okay.
And this is what I will go ahead and execute it okay.
I'm just saving it in a variable over here.
Now let's execute this specific prompt okay.
Now in order to execute this specific prompt you could see right.
Initially my number of tokens were 909.
And now if I go ahead and see with respect to my token it is nothing but LM dot get num tokens.
Okay.
And in order to just pass this complete prompt, I'll pass it over here.
And if I execute it here, you can see now it has increased to 931.
Why it has increased to 931.
Because along with this speech, I have also put some more additional information over here.
Write this all and this will also be added in the form of tokens.
Understand with respect to every LLM model there is a restriction with the number of tokens, right?
Right now it is fine 931 tokens.
Most of the advanced LLM models will just consider this many number of tokens in a very much easy way,
and it will be able to execute and give you the response.
But there are also some limitations with respect to other LLM models with respect to maximum number
of tokens.
Let's say if in this in our case, our speech was somewhere around 25 pages, at that point of time,
I have token what had increased more than 10,000.
If I see with respect to OpenAI GPT 3.5, there was a limitation of 4096 tokens with respect to the
input that we give.
right?
So it is necessary that whenever we try to give this kind of tokens, we also try to split it.
But till here it is.
Fine.
Let's run this entire thing now okay.
Based on this complete prompt template.
So I'll go ahead and write uh LM underscore chain.
Now I'll go ahead and create my LM chain.
As I said, if I have an LM model along with this, if I have the prompt, uh, prompt is equal to prompt.
Okay.
Only in this two case I will be using an LM chain.
And this chain will be responsible based on the prompt, how the LM needs to execute and provide the
response.
Okay.
That is the information that you will be able to see over here.
Then I will just go ahead and write my summary.
And here I will say LM underscore chain dot run.
Okay.
Now in order to run it what I will do is that quickly I will go ahead and write speech.
Uh, what will be my input that I need to give as my input variable speech, which will be colon equal
to speech.
And then you'll also be seeing I will go ahead and write something like my language.
Language and this language will be nothing.
But let's say I will go ahead and write French okay.
And then finally I'll go ahead and see my summary.
So this is going to take some amount of time I guess.
Okay.
Perfect.
See that is the power of uh, you can basically see the power of uh, grok, right.
How fast it was.
And here is your entire, uh, information in French.
Obviously, I cannot understand it.
So let me just go ahead and convert this into Hindi.
But I think Hindi will be able to understand.
So summary is that Vishay may uh, it is written.
I'll just read it in front of you.
I don't know how many of you know Hindi, but Vishay may look sarkar Rajnaitik or Samajik gati video
may occur.
So this is one way of doing another summary where you can actually use LM chain and you can take, you
can design your entire prompt with by using a generic prompt template.
Okay.
So all this information is specifically there.
Now this is fine.
See here, what is basically happening is that you have just a limited number of text, right?
When you have a huge PDFs, when you have bigger size PDFs, at that point of time, we definitely have
to use different, different summarization technique.
Here what we are doing, the entire document, whatever we are getting, entire speech, whatever we
are getting, we are just giving it to the LM model.
And LM model is able to handle it because the token size is less based on the context token size.
That is the limitation with respect to any LM model.
Now is the main types that I am actually going to discuss.
The first type is something called as stuff document chain.
Okay.
And here we are just going to go ahead and write text summarization technique okay.
Now let's uh do one thing.
Let's discuss this entire thing in the next video.
Okay.
And uh, here, uh, one by one we will go ahead with this.
There are three types.
One is the stuff documentation, uh, chain that we use.
And for this I will be using a bigger PDF.
When compared to the previous one, I'll be using at least 5 to 6 pages PDF.
Okay.
And uh, after completing uh, this that is using stuff document chain.
We will also see other types like refine.
There is there is something called as a refine document chain.
Um, you know and if I also see show you the documentation.
Right.
Lets me show you the documentation over here.
So here you will be able to see map reduce.
MapReduce.
So stuff document chain basically means it simply concatenates document into a prompt.
MapReduce basically means it split document into batches, summarize those and then summarize into summaries.
Refine basically means which updates a rolling summary by iterating over the documents in a sequence.
So I will just show you with this, and I will also try to draw a diagram to in order to make you understand.
This is what I'm going to discuss in the next video.
Thank you.
I will see you all in the next video.