Hello guys.
So we are going to continue the discussion with respect to creating this chat bot.
So already we have seen about the message history and how we can remember the context.
Now let's go ahead and use this prompt templates okay.
Now prompt templates helps to turn raw user information into a format that NLM can work with in case
the raw user input is just a message, which we are passing to the LM.
Let's now make that a bit more complicated.
First, let's add in the system message and add some custom instruction.
Till now we were just passing the list of messages, right?
Now let's go ahead and work with this particular prompt template.
Now for working with Prompt template I will go ahead and write from long chain.
Uh from long chain underscore core dot prompts.
Okay I'm going to import my chat prompt template.
Okay.
Chat Chart prompt or prompts.
It should be prompts okay.
So chart prompt template okay.
Along with this I will also use a message placeholder before we used to give the messages right directly.
If you see over here like human message and all, we are just giving it right in the form of list of
messages.
But here, uh, we will just go ahead and use this.
Now quickly, let's go ahead and create my prompt.
Uh, here I'm just going to write chat prompt template is equal to.
And uh with respect to this here I'm going to give a list of information.
The first information that we are interested in in providing system information.
So I will go ahead and write system I'll say hey you are uh, helpful assistant okay.
And I will just go ahead and say, hey, answer all the questions to the best, to the best of your
your ability.
Right?
Something like this.
So once I do this, I will go ahead and create, uh, my next field.
Now in this case, uh, the next field that I have to probably go ahead and create it is nothing but
message folder.
Okay.
So message placeholder.
So message placeholder here I will just go ahead and create a variable name called as messages.
And this messages will specifically be used.
Uh I'll talk about this why we are creating this particular variable name just in some time okay.
Now once I go ahead and execute this, uh, let me go ahead and first of all, create my prompt, uh,
chain.
So I will write prompt off model.
And this should be my chain right.
So and it takes exactly how many parameters one positional argument two are given.
Let's see why two are actually given over here.
Uh this is my prompt template.
And here I have used the message player.
This should be the system over here or system comma.
This right with respect to the key value pairs like this.
And uh one more thing that we can just do is remove this.
Just a second.
Okay?
Now I think it should be right.
Okay.
Let's execute this and let's remove this okay.
So it says uh, and it takes exactly one positional argument.
Why.
Let's see chat prompt template okay.
That was not the issue okay.
The issue was something else.
I will try to fix this issue.
We have to use a function called as from underscore messages.
Okay.
I'm not going to editing this part because you have to definitely see all the errors and then try to
fix it okay.
So here you have this.
And this is my chain.
Now let me quickly go ahead and uh, you know uh provide this entire or invoke this chain.
Now Understand one thing, guys, whatever human message we specifically give, it needs to be given
in a key value pair where the key names should be messages.
And that is the reason we are giving a message placeholder over here.
Right before we were not using any message placeholder.
So over there we were giving in the form of list of messages like human message, AI message system
message like this.
Right.
But here I've told that, hey, we are going to use a message placeholder called as variable name as
messages.
And inside this, whatever information you definitely require from the human side, it will be given
in that.
So how do I call it.
So here I have created my chain.
So let me quickly go ahead and write chain dot invoke okay.
And inside this I'm going to pass my messages okay.
Messages.
This will be my key right.
This will basically be my key.
And here I can give.
Hey, whether I want to give a human message over here and let's say I will go ahead and write content.
Hi, my name is Krish.
Okay.
So this is my message that I'm specifically giving over here, right?
So once I execute it automatically will be able to see that.
Now the message placeholder is messages.
So in inside this it will try to understand okay Okay, here is what I will get my entire messages or
all the list of messages that I'm actually going to get.
And this will basically go ahead and interact with the chain.
Um, so whatever information I'm giving it over here, it will be going and it will get displayed over
here.
Right.
So this entirely basically talks about, uh, how you can actually include the along with the prompt
template itself.
And here I can get the answer that.
Hi Chris, it is nice to meet you.
And all the information is specifically over here, but how to invoke with respect to the chat message
history.
Right with the chat message history.
Now let me just go ahead and create a variable over here.
So I will say hey with message history let me create a new one.
And here I will say hey runnable with message history.
Now two important information needs to be given.
One is chain and one is the get session history.
If you remember the get session history that I've actually created, right.
So once I give this two information now see how I will be able to invoke this okay.
So this is why with message industry here I'm just giving the chain.
Chain is nothing but this entire prompt and model.
And this is my get session history over here.
Now I will go ahead and create my config.
So let me go ahead and create my config.
And this is my chat three okay I'm just going to write a session ID over here.
Chat three.
And I'm going to probably go ahead and create my response.
And again we will go ahead and use this with messages three dot invoke dot invoke.
Now when I am specifically invoking um, when I'm actually taking this entire chain and all, I can
give the same information like how I gave it over here, right?
Something like this.
So I will say, hey, this is my human message.
I will go and paste it over here.
Okay?
Along with this, the next thing that I am going to give is my config.
Okay?
So once I get the response I should be able to display the response.
Hello Chris, it's nice to meet to you right now.
This is fine.
See this message history is almost same like how we used to use it before.
Right?
But, uh, let me add some more complexity instead of just one input variable.
Now let me go ahead and add multiple input variable okay.
So I will be taking one type of prompt over here.
So this is my prompt.
See over here I'm using chat prompt template dot underscore from messages.
So I'm going to add some more complexity okay.
Add more complexity okay.
So here you can see I'm having chat prompt template dot from underscore message.
Here is my system.
You are a helpful assistant.
Answer all the questions to your ability in this language whatever language I have right.
So this basically becomes my new input that I'm actually going to give okay.
So I'm saying hey system you are such a helpful assistant.
And you are have to probably answer all the questions based on your ability in this particular language.
The language will be passed as a keyword.
Okay then this is my message folder.
Um, sorry message placeholder where I am giving the variable as message and this is my chin.
Okay.
So this is fine.
Now over here, you'll be able to see that whenever I need to invoke this chain.
So if I go ahead and write response is equal to chain dot invoke.
Here there are multiple parameters that I really need to give.
One is messages and this will be nothing but human message.
And here I'm just going to write my content where I'll say, hey, hi my name is Krish okay.
And along with this, the second parameter that I need to give see very important thing in this messages.
This will be a key value pair right.
So one parameter I'm actually giving it over here.
The second parameter I will again go ahead and add a key which is called as language okay.
And let's say language over here I'm just going to write it as Hindi okay.
So this is the second parameter I need to give.
So first I'm passing it as a human message content.
Uh this is basically there in the key called as message.
And the second key that I'm actually passing is for this particular input that is this particular language.
Okay.
Now once I could probably get the response I can go ahead and display the response dot content okay.
So once I go ahead and see Namaste to Krishna Nagar.
So here you can see that I am able to probably give this uh again in this message key and in this language
key I'm actually giving.
Okay.
Now there are two keys that are going over here.
So with respect to the chat message history, how it is going to change okay.
So now I will just go ahead and create a markdown and add some information.
Let's now wrap this more complicated chain in a message history class.
This time because there are multiple keys in the input, we need to specify the correct key used to
save the chat history.
Okay.
So let's do this okay.
This time what I will do while I'm creating my with message history okay.
I will use this runnable message history.
Now inside this runnable message history, first parameter is nothing but my chain.
Whatever chain I'm specifically giving.
The second parameter is nothing but my sketch session history function.
Based on the session id it is going to pick up all the information.
Now is the most important one, which is called my input key message input message key.
And here I'm going to give my input as messages.
Okay.
So these are my parameters that I really need to give.
Now see the magic okay.
Based on this particular input message keys.
Now it knows where uh like uh uh, since there are multiple keys, it knows uh, which one to basically
specify to uh, we need to specify C over here.
As soon as we give this particular key, it will be able to understand when we are giving messages and
when we are giving the language input over here Okay.
So let's go ahead and execute it now.
This time I will go ahead and create my next config over here.
So let's take a simple config.
So this time I will go ahead and write chat for message okay.
And here will be my chat for okay.
And along with this we will go ahead and write.
Response is equal to width message history dot invoke.
Now see how I'm going to pass the parameter?
The parameter will be with respect to the messages or messages, which will be my key.
Here I'm going to pass my uh here I'm going to basically pass my human message, human message.
And inside this human message, again, this will be in the form of list, okay, human message.
And inside this human message, I'll say, hey, content will be nothing.
But.
Hi, I am Krish.
Okay?
Now this is fine till here.
We have given this particular information.
Remember, this will get closed over here, okay?
Or it should get closed over here.
So this will basically be my list of messages.
Now in the next parameter what I give I give my language key.
So this will be nothing but language is equal to.
Hindi.
Okay so these are my information.
Now let's go ahead and probably give my configuration.
And this is how we basically give the entire response.
So finally I can go ahead and print the response dot content.
So once I execute it Namaste.
Crushed uh to make a system to make a whole car madam.
Car okay.
It's somewhat right.
Again, there are some grammatical issues.
Uh, but again, it is an open source model, so we should be happy that we are able to use it completely
for free.
Okay, so here are all my information.
Then again, if I really want to go ahead and ask, hey, what's my name?
And I can go ahead and ask over here and here also I will go ahead and uh, press Hindi.
So here you will be able to see my response dot content.
It shows up here right.
So it is able to understand all this specific things okay.
Uh so this is how you can actually work with chat history where you really need to worry about the message
history class, how you can go ahead and create it, what all input message keys needs to be used.
What is this particular function based on a session ID?
How do you pick up information?
So I hope you are able to understand this particular video.
Now in my next video, I will be showing you how you can manage this entire conversation history.
Because managing this conversation history is very much important.
Yes, you can uh, whenever you are communicating with the LM model here, what we are specifically
doing is that we are giving the entire history itself.
But sometimes this history can become very, very long, you know, and it remains completely unmanaged.
So how we can probably and see there is a limitation with respect to the context window of every LM.
So managing the conversation history is very very much important.
So in my next video what we are going to do is that we are going to see how we can manage the conversation
history.
Okay.
That is what we are going to discuss in the next video.
So yes, uh, this was it for my side.
I will see you all in the next video.
Thank you.
Take care.