So guys now let's continue and let's start building our LLM application okay.
So uh see if I want to probably interact see whenever you user gives any kind of query, building an
LLM application basically means let's say if I'm giving any query, uh, to my model, my model should
be able to give some response.
Okay.
But you should understand some very key libraries that are specifically used in long chain, which we
also see it as Runnables.
Okay.
so we'll be discussing more about it as we go ahead.
But just to give you an idea like which are the common libraries that we specifically use, like let's
say if the LM model is getting some kind of message, uh, if I want to tell the LM model to behave
in a certain way, how do I probably mention that all those things also will go ahead and discuss?
So first of all, what I will do is that quickly I will go ahead and install one more library which
is called as lang chain underscore core.
Okay.
Now this particular library is important because I will be discussing about some of the libraries that
I'm actually going to use from this particular library itself.
Right.
Some of the important components.
So once I install this you can also see that the requirement is already satisfied.
I will go ahead and update in the requirement dot txt.
Now I will go ahead and write from lang chain okay.
Underscore core underscore core okay.
Dot messages I'm going to import one is called as human message.
And the other one is something called as system message.
Okay.
Now why this human message and system message is required.
See whenever we chat or whenever we provide any messages to our LM model, we should specify our LM
model, which message is basically provided by the human being, and which message is a kind of instruction
to the LM model.
So the system message that you have, this is just like an instruction that how the LM model should
basically work like okay, so here what I will do, I will just go ahead and create a list of messages
okay.
And this list of messages I'm going to pass to my LM model okay.
Now inside this list of messages I have to first of all say okay, how the LM model should behave like.
So here I'm just going to use my system message.
And I'm going to say that, hey, let's create this particular content.
The content will be I will say hey translate the following following from English to French.
Okay, French.
So this is what I'm actually telling my LM model to basically do.
The second message will be that whatever human is basically asking what sentence it really needs to
convert, we can go ahead and write human message is equal to.
And here let's say I will go ahead and write content as.
Hello.
How are you?
Okay, so this is the content that I really want to send to the system that is my LLM and get the response.
Okay.
So we have created this list of messages in the form of system message and human message.
Again, repeating what the system message basically does is that it instructs the LLM model like how
it really needs to behave or work like right.
In this particular case, it needs to work like a translator which will be converting from English to
French.
Then I'm also giving the human message like what sentence I really need to convert.
Now if I get this list of messages now, I can use my model, I can invoke I can invoke this specific
messages.
So let me just go ahead and invoke this.
And once I get and once I execute this here, you'll be able to see some output.
And this output starts with something called as a message.
And the output over here is Bonjour.
Whenever I get an, uh, output which says of I message, this basically means it is a response from
my LM model.
Okay, so here it shows.
Hey, content is bonjour.
And that is what we basically say.
Hello, how are you in French?
If we try to convert this we basically say Bonjour, comment allez vous.
Okay, I don't know this one, but at least I know bonjour and I think it is correct itself.
It cannot be wrong because we are working with powerful LM models.
Then, uh, along with that you'll also be able to see some response metadata, like how much tokens
it has basically created, how much prompt tokens are there total tokens.
Completion time.
Prompt time.
Right.
Uh, total time.
Along with that, which model name we have basically used.
And these are my metadata information that you'll be able to see at the last.
You'll also be able to see that how many were input tokens.
So input tokens were 26 output tokens I got as 11.
So total tokens is 27.
By seeing all this particular information, you get to know that how much cost is basically involved,
uh, with respect to each request.
Mm.
Now when I'm getting the output as I message okay.
That basically means we are getting the response from the, uh, LM model.
Okay.
Now, there may be scenarios that I just want I'm just interested with this particular content.
Now for that, what do I do?
Along with this invoke function, you know, whatever response I specifically get, whatever results
I actually get, let me just go ahead and save this in one kind of, uh, result or response.
So here I will say, hey, this will basically be my result.
Okay.
Now what I will do there is something called as string output parser because see here we know that this
result is nothing, but it is a I message.
Okay.
But I want to only retrieve this particular output.
Right.
So for that in uh lang chain, if I go ahead and say in lang chain underscore core okay.
Underscore core, we have something called as output parser.
This output parser will be responsible to basically display the message that is coming out side as the
response from the LM model.
Okay.
So here we will go ahead and import string output parser.
And I will just go ahead and create my parser.
So let's go ahead and initialize this string output parser.
What this string output parser is going to do is that it is just going to display my content message
out, right?
So this is the default.
You can also go ahead and create your own custom string output parser if you want.
So uh from this particular result.
So this is the result that I have.
If I say parser dot invoke okay.
And I invoke this or I give this particular result value from this.
So what it is going to do is that this string output parser is going to take the content, uh, from
that particular message, from that I message, and it is just going to display it.
So here you can see Bonjour comment always was okay.
So here you'll be able to see the entire output okay.
Now there is I already told you something called as LCL okay.
In the initial stages.
Right?
I told you about language, expression, uh, language.
Right.
Sorry.
Language and expression.
Language to chain components together.
Now, you know that I have two components over here.
One is my model.
Okay.
Model dot invoke, which is specifically doing.
And the second is my string output parser.
So here using LCL, using LCL, we can chain the components okay.
So we can chain the components.
Chaining basically means one after the other.
So for this I'll create my variable I'll say hey model slash parser okay so here what I'm saying.
So I'm creating a chain of events right.
First of all I'm adding model.
Then I'm adding parser.
Now whenever I invoke I say hey dot invoke and I give my messages parameter.
Over here in this messages we have that list of message of human message and uh, the system message
Once I do this invoke, what it is going to do is that first of all, it is going to go to this particular
model.
It is going to send both those messages to the model.
The model is going to give you the output as a message over here, like how it is given.
Then this particular output is going to go to the parser.
And finally I'll be able to get this particular output okay.
And that is what you'll be able to see over here.
See finally we get this Bonjour command.
Otherwise right.
So this is one way of for creating, uh, agents with LM or.
Sorry.
Uh, not agents.
Sorry.
Uh, creating our, uh, or using our LCL to chain the components.
Okay.
Now, uh, we have discussed about output parser over here.
We have discussed about, uh, the prompt, uh, the LCL chaining the components right now instead of
given.
See, C instead of giving like this.
Right?
Right now we are passing every time list of messages, right?
Instead of this, we can use one more efficient technique and that is nothing.
But it is called as prompt templates.
Okay.
Now with respect to the prompt templates what I will do is that again let's go ahead and do this.
So I'll say instead of just giving the list of message, I will try to, uh, you know, take a combination
of user input and some application logic, you know, uh, where I am also able to give the instruction,
I'm able to take the user input over there.
Okay.
And, uh, this application logic, which I'm actually talking about, is usually takes the raw user
input and transforms into a list of message ready to be passed to the language models.
Now how actually it is done.
I have already told many things over here, so let me just go ahead and write.
Langston underscore so code or prompts and I will go ahead and import chat prompt template okay.
So here I'm going to use this chat prompt template.
Uh again it is available in action underscore core okay.
So first of all I will define my generic underscore template okay I will say hey uh translate the following
into this particular language and this language, I will give it as a parameter okay.
This particular language okay.
And I will give this entire into like this language will be passed as an parameter.
Okay.
Now once I take this particular generic template I will create my prompt okay.
And this prompt will be assigned to this particular prompt template that is chat prompt template.
And here I'm just going to say dot from underscore messages.
Okay.
And here we are just going to give this prompts right.
Like how we give the list of system of messages.
List of messages.
Right.
The first prompt will be in the form of tuple.
Here I'm going to add system comma.
Let's say I'm going to say system comma system template or system comma.
Uh like what is the instruction that you really want to give to the system.
So it will be like a generic template over here okay.
And the next one will be basically with respect to the users.
Right.
So here I instead of just giving the message I'm giving in this way.
So I'll say hey user.
And this will basically be my text okay.
What text I'm actually giving.
So this text we have to give it in the run time okay.
Same thing.
See before we use messages how we are giving this in this particular way here, we are going to give
it in the form of uh chat prompt template where I also have to information system and user.
Okay.
Now this basically becomes my prompt okay.
Now let me quickly go ahead and write something like this prompt.
Or let me just go ahead and execute this.
First of all, now if I go ahead and write prompt dot invoke.
Okay.
If I invoke this okay.
Uh, over here, let's say two parameters I want to give.
Right.
So I'll give it in the form of key value pairs.
One is language will be my input and then it will be text okay.
So first of all we will go ahead and set language.
Let's say language I'm going to set it as French okay.
So it will be French.
And the second parameter that I'm actually going to set it's called as text.
Okay.
So text will be let's say hello I will just say hello okay.
And once I execute this here you'll be able to see that this is what is my entire chat prompt value
and see how beautiful it is converting this into a list of messages.
Right.
So this list of messages that we were getting right.
The same list of messages you will be able to see over here.
Right.
So what this is basically doing this chat prompt template is doing it is converting that into a list
of messages.
So here I have system message translate the following into French.
And then I have my human message content as hello okay.
And this is the power of this prompt right.
This prompt uh whenever we use this chat prompt template okay.
Now this is fine if I just want to get the information like what all messages it is able to convert
to.
So let me just keep this in a variable.
I'll say result is equal to prompt.
And let me just go ahead and write result dot two underscore message okay.
So two underscore message.
Once I do it here you'll be able to see these are my messages that is visible.
And that is the same list of messages that we used to pass earlier.
Now again I will go ahead and try to create a chain.
Right.
So now in this chain, first of all I will go ahead and add my entire prompt.
So so let me just go ahead and add my prompt.
So this will basically be my prompt.
Now after this prompt what will happen is that I will go ahead and give it to my model.
So this will basically be my model.
And finally you'll be able to see that I'll finally get my output.
And this output should also be passed to the parser.
Okay.
So this time using LCL, I've combined three things prompt, model and parser.
Now once I execute it.
So I will go ahead and write chain dot invoke.
And here what are the key parameters that I really want to pass.
One is language.
So let me just go ahead and write.
Language is equal to French.
Okay.
And then text is equal to hello.
Okay.
So once I execute this you'll be able to see that I'm able to get the output as Bonjour.
Okay, so this is how two things we have learned.
One is how to create chain and how to create chain with prompt templates.
So here I'll give this particular comments over here.
Chaining together corporates with LCL.
And here we have combined prompt model and parser.
So I hope you are able to understand this.
Now in the next video, uh what we will do is that we'll try to create, uh, or, you know, uh, let's
say, like, I want to create an end to end project.
I want to make sure that I want to create all these things, all this chain in the form of APIs.
Right?
How do I basically use it?
And for that, we'll be using a new component which is called as Lang sir.
So in the next video we'll be discussing more about the Lang server itself.
So here, uh, this was it from my side.
I hope you liked this particular video.
I will see you all in the next video.
Thank you.