Hello guys.
So we are going to continue the discussion with respect to Lang Chen.
And now I'm going to show you how we can go ahead and build a chat bot.
Already in our previous video we have seen that how to build a simple LM application with LCL.
And we have discussed about Lang Lang chain expression, uh, language where when we were able to,
you know, uh, may, uh, combine the chains together, all the chain components and we were able to
do the prediction.
Right.
So let's go ahead and let's start working with chat bots.
Now in this video we will go over an example of how to design and implement an LLM powered chat bot.
This chat bot will be able to have a conversation and remember previous interaction.
Note that this chatbot that we build will only use the language model to have a conversation.
Okay, there are several other related concepts that you may be looking for, like conversational rag,
like how to enable a chat bot experience over an external source of data.
We will probably discuss this in the upcoming videos.
Then we will also be discussing about agents.
We will be developing multiple projects.
Okay, but in this video we will try to focus on LLM powered chatbots.
Okay.
So first of all, as usual again over here I am going to use the open source uh uh models from grok
API instead of using OpenAI.
Okay.
So first of all what I will say I will go ahead and say import OS.
Uh, along with this, I will go ahead and write from uh dot env import load underscore dot env.
And then we have this load underscore dot env.
Here we are just uh loading all the environment variables.
All the environment variables okay.
And then after this, uh, what do we exactly do.
We basically get our grok underscore API underscore key.
And you know, that we have in our environment variable which we had updated last time.
So this is my grok API key.
So I will just go ahead and call it right.
And in order to call it I will just go ahead and write OS dot get env.
And here I'm just going to call this right.
Perfect.
So if I go ahead and print my grok API key this is the output.
Uh, please do not use this.
Anyhow, uh, after I upload all these videos, uh, in the course, uh, there's all keys will be deleted.
Okay, so now I have my grok API key.
Now the next thing is that we will go ahead and see how to call our LLM model.
So that already we have seen over here.
So here we will be importing chat grok.
Right.
So here you can see lang chain underscore grok import chat grok.
And here we are going to use chat grok with model gamma two nine bit.
This is the most newest model that we are going to use over here.
Let me just go ahead and execute this.
And here I have actually given the grok API key.
Okay.
So this is how you specifically load a model okay.
Um, now after loading a model, what I am actually going to do is that we are going to, first of all,
start with basic things.
Okay.
So let me just go ahead and write from long chain underscore code I am going to go ahead and import
this messages.
And from this messages I'll import the first type of message which is called as human message.
Okay.
Whenever I have a human message that basically whenever I'm using this particular class, uh, it indicates
that a user is giving a message itself.
Right.
So let me just go ahead and write model dot invoke over here.
And uh, with respect to this, let me go ahead and create my human message, uh, class.
Let me initialize it.
And here we are going to talk about content is equal to.
And here I'm going to say hey hi my name is Chris and I am, uh, chief AI engineer.
Okay.
So I'm just giving this kind of message over here and let me go ahead and invoke this.
So here you'll be able to see as soon as I give the message over here, this we are invoking with respect
to this particular model, that basically means this information is going to my, uh, model that is
gamma2 over here.
And I'm getting the response.
Hello, Chris.
It's nice to meet you as a chief engineer.
What kind of projects are you working on these days?
I'm always eager to learn about the exciting work being done in the field of AI, and these are my all
the metadata info Information like how much is the input tokens, what is the output tokens, and what
is the total number of tokens.
So here, uh, just a simple conversation by using a human message, uh, class, I've actually done
it right.
Still, I have not initialized anything from the system message itself.
I have not told, uh, like, what kind of behavior the LM model should probably go ahead with right
now.
Let me do one thing.
Let me.
And you know this, right?
Whenever we get this AI message, that basically means we are getting the response from our model.
Now let me go ahead and write from Long Chain.
Oops.
Uh, over here I'll go ahead and write from Long Chain.
Okay.
Now, I think I was searching it over here from long chain underscore core dot messages.
I'm also going to import I message okay.
Now again here I'll go ahead and write model dot invoke.
Uh, and let me put some combination of AI and human message.
Now understand, one thing is that, uh, we can give a list of messages like how we basically developed
a simple LM application.
Right?
So let's say first message is my human message.
And here I will go ahead and write content with respect to this entire information.
Hi.
My name is so and so and I'm a chief engineer.
I'll put it over here itself.
Okay?
Uh, and I will close this quickly, and then I will go ahead and take my next message.
The next message will be basically, uh, AI message.
And here in the AI message, I'm just going to go ahead and write this particular content and let me
just go ahead and copy this entire thing, whatever content is over here.
Right.
Uh, in the field of AI.
Right.
So I'm just going to go ahead and write this particular content over here.
Right.
So this is the I, uh, message that I've actually got a response.
So now here I'm actually going to create a sequence of messages from this particular text.
I got the I'm just hard coding.
Okay.
I got this particular message from this particular model.
And now I will go ahead and just trace again a human message.
And now I will go and ask, hey, hey, Hey, what's my name and what do I do?
Okay, so this is the question that I'm going to ask over here.
Now, this is amazing.
Okay, let's see the output.
What I'm actually going to do because I'm, I'm first of all feeding this particular information with
respect to the human message.
Hey, my name is Krish and I am a chief AI engineer.
And from this AI message I got this entire value.
So I am probably putting it up.
And I'm asking it whether it is able to remember my no.
It is whether it is able to remember the information that I have actually given since I'm passing it
in the form of list of messages here.
Right.
I think it should be able to remember it.
Okay.
So now once I go ahead and execute it.
So here you can say you said your name is Krish and you are a chief AI engineer, right?
Is there anything else you would like me to remember about you?
Okay, so all this information is basically coming.
That basically means, uh, one important thing is that whatever conversation I'm giving inside this
list of messages with the human message, I message human message.
It is also able to remember the previous context.
Okay.
Now that is where we are going to discuss about message history okay.
And this message history will probably help you to understand the all the context that it will help
the LM to remember all the context that we are probably discussing about.
This is just an idea that whether the I, the LM model is able to remember things or not.
But at the end of the day, whenever we develop real world application, there will be different, different
sessions that will be happening.
Right?
Let's say I'm using ChatGPT.
Someone is using ChatGPT, some other people are using some other LLM models and they are chatting.
Right.
How that particular uh, models are able to remember their context, that is with respect to session.
So in order to understand that how these sessions are managed, we will be discussing about a very important
property which is called as message history.
Okay.
Now let's discuss about this message history.
And this is a very important component altogether in the, uh, long chain itself.
Right.
Which will actually help you to work with the LM model.
Okay.
Now, what we really need to use or understand how message history works.
So here I'm going to put a message saying that we can use message history class to wrap our model and
make it stateful.
We have to make it stateful so that it remembers all the context with respect to any kind of person
who are actually interacting with that model.
This will keep track of our inputs and output of the model and store them in some data store.
Further interaction will then load these messages and pass them into the chain as part of the input.
Okay, so let's see how this can be actually done.
So first of all for this we require another library which is called as long chain underscore community.
So please make sure that you install this.
And once you install this I have already done this particular installation so that it does not take
much time.
You have to go ahead and update in the requirement dot txt okay.
So first of all go ahead and update this in the requirement dot txt and make sure that whatever installation
that you have done, you are doing it in your, uh, virtual environment.
Okay.
Perfect.
So from here, uh, what I will do is that I, uh, Langton community is basically used because, uh,
uh, any integration related to measure history will be available in this Langton underscore community.
Okay.
So, uh, let me go ahead and import some very important libraries, let's say from long chain underscore
community.
Uh, I will be using something called as chat message histories.
Okay.
Inside this chat message history we will be importing chat chat message history okay I will talk about
this.
What exactly is this chat message history?
Just for now, let me just import some more libraries.
So I will go ahead and write lecture underscore code dot chat history okay I'm going to import bass
chat history okay.
Bass chat message history.
And then finally I will go ahead and import from luncheon underscore core dot runnables okay.
Since this is for the chain and for the rain chain, also, we basically go ahead and uh, import this
particular history and I will go ahead and import runnable with message history.
Now this is something really important okay.
Here you'll be able to see that we can import the relevant class over here.
Set up our chain uh which wraps the model and add in the message history.
That is what we really need to do, right?
Every message that we are probably putting up, it needs to be, uh, added in the message history itself,
how it is done.
I'll just talk about it sometime.
Okay.
But the thing that you really need to understand is that whenever different, different users are chatting
with the LM model, how we are going to make sure that one session is completely different from the
other session.
So for that reason, what we will do is that we will create one amazing function, okay.
And this particular function will be definition get underscore session underscore history.
Now what this function is going to do.
Let me talk about it okay.
Here this function will be creating a session ID okay this session ID and this session id will be a
string okay.
Right I've written a session ID and this will be a string.
And this will get inherited to your base I mean the return type.
Sorry.
Not inherited.
The return type of this particular function will be base chat message history.
So whatever.
Chat history is basically getting created right that is imported from this particular library.
So what I'm actually going to do is that this get session history right with respect to a session ID
will be created.
Right.
And this session ID will be used to distinguish one chat session with the other.
Okay.
So here, uh, the return type is based chat history.
I will say hey if session ID not in store.
So let me just go ahead and create one variable called as store which will be a dictionary over here.
So here I will just go ahead and write store okay.
Okay.
Let me talk more about it.
And the explanation will be very good.
And I will show you the practical example.
I will go ahead and write store of session underscore ID is equal to chat message history okay.
So now see this everything.
Please try to understand it right.
First of all, for this particular message history.
So you can see that we have a class which is called as chat message history.
Okay.
Now this chat message history, whenever we are creating a session ID right, we are storing the session
ID in this particular dictionary.
Right.
With respect to this particular value, whatever session ID I'm actually creating, I am I'm initializing
a chat message history.
Right.
So that basically means this is an object of a chat message history.
And whatever chat is specifically happening automatically, it should be going inside this session ID
okay.
Next thing is that after this I will just go ahead and return store of session underscore ID.
Now see you will.
You know I know there is some confusion with respect to the definition, but let's understand this definition.
So what is the definition over here?
This is a function which is called as get session history.
Whenever I give a session ID, it should be able to check whether the session ID is present in this
particular dictionary or not.
If it is present, we are going to get the entire chat message history from that and we will return
it.
Okay.
And that is how we are going to distinguish it.
Two things this get session history will give you a response type of base chat history.
Uh, it is an abstract class for storing the chat message history.
If I talk about chat message history it is an in-memory implementation of chat message history stores.
Messages are in an memory list.
So it is very much simple.
Whenever I give a session ID it should.
First of all go ahead and check in this particular dictionary whether it is available or not.
If it is available, it will go ahead and pick up the entire chat message that we have discussed with
respect to, or whatever questions we have asked with respect to the LM model for that session ID,
and it is just going to return it now, see how it is going to work.
Okay.
So I'll execute this.
Uh, I will talk about this runnable with message history.
Uh, soon it will be used as we go ahead.
Okay.
Now the best thing over here will be that we will be seeing how we will be using this.
Okay?
So first of all, we will go ahead and create our config okay.
So I'll write hey this is my config over here.
And this config will be nothing but it will called as configurable.
Okay I will just give this key value pairs configurable.
And here I'm just going to give my session ID.
So let's say my session ID is something like this.
Right now I'm hard coding it.
Let's say my session ID is something like um chat one okay I'm just going to write it as chat one.
Now let's go ahead and execute this okay.
Let's go ahead and execute.
So this basically becomes my config okay.
My session ID is chat one.
Now let me use this particular session ID and let me chat with the LM model.
So here you'll be able to see that I will write response is equal to width.
Uh, so before this let me do one more thing okay.
Uh, see, I have used the Get session history right now.
In order to execute this, I really need to use this runnable.
Right.
Unless and until we don't have this runnable, uh, it is not going to probably execute this entire
chain.
So here you say see runnable that manage chat message history for other runnables.
Okay.
The chat message history is a sequence of messages that represent a conversation.
So here what I'm actually going to do I'll go ahead and write width underscore message underscore history.
And here I will go ahead and write a runnable with message history.
And here the first parameter will get is model whatever model we are going to use.
And the second is get session history Okay.
So this two parameters will be specifically required, uh, for this right.
And this width session with message history.
We will try to invoke this as soon as we invoke this.
This is going to take this particular model.
This is going to take the entire chat history.
What we are getting based on the session ID again how to pass the session ID I'll let you know.
Okay.
And then it will be able to give you the entire information.
So here, let me just go ahead and write double s, uh, because I don't want to write triple s.
Okay.
So here so many S's are here okay.
Perfect.
Right.
So again let me repeat it.
What we have exactly done.
First of all we have created a dictionary over here.
Then we have created a function which will be able to retrieve whatever uh, chat history is there for
a specific session ID.
Then we are converting, or we are using this runnable with message history so that we can, um, you
know, interact with our LM model based on the chat history.
So for this we give two parameters that is model and get session history.
Now we have created a configuration called as session ID as chat one.
And now what we will do is that we will call this with message history.
And here I'm just going to go ahead and invoke invoke over here.
First of all I will give a human message.
Let's say this is my human message over here.
Now see see the magic okay.
This will be amazing.
I will go ahead and write.
This is my content is equal to.
And let me say hi.
Uh, or I'll give the same message over here.
Right.
So hi, my name is Krish and I am an AI engineer.
Okay.
Now see, I am trying to interact with based on the session ID.
Okay, now see this all information is over here.
But along with this we can go ahead and give our configuration.
The configuration will be given inside this invoke.
And here I will go ahead and write.
This is my config which will be equal to config.
Now when we are giving the config that basically means for this session id we are interacting okay.
So based on the session id it will be able to remember all this context.
So if I go ahead and execute this okay.
So with message history is not defined because I did not execute this.
Let me execute it.
Now let's go ahead and do this.
So here you can see I message hi crush.
It's great to meet you.
Being a chief engineer is an exciting role.
All this information is basically coming Okay, now if you want to get this particular content, I will
also save this in my response variable.
Okay.
And let me just go ahead and write response dot content okay.
I can also go ahead and write like this.
So this is what is the message that I will be getting.
Now let me do one thing okay.
Let me take another response or let me go ahead and again use this with message history.
And this time I'm sending a message of human message like what's my name?
And I'll write config is equal to config.
Now in this case if I see the output here you'll it will say that hey your name is Chris okay.
White is able to remember it because we are giving the same config.
So let's say that now if I change the config, will it be able to remember this particular context?
Okay, now here I'm just going to change the config When I say change the config I'm changing the session
ID right.
So I'm changing the session id okay.
Now if I give the session ID some some different session ID.
So for giving the session id I will again use this config over here.
The first session id was chat one.
Now this will be my config one.
Okay let me just go ahead and give it.
So this will basically be my config one okay.
Now with respect to this particular config I will again go ahead and use my response and I'll say,
hey, uh, with this particular message history right over here, I will go ahead and I dot invoke and
here I will go ahead and give my message.
The first message is nothing but my human message.
The human message will be nothing but content is equal to I'll say, what's my name?
Okay.
Now see, before also I asked what's my name?
But I give the previous session ID with respect to this particular configuration.
But here I'm going to give my new configuration.
So I'll go ahead and write config is equal to I will go ahead and write config one okay.
Now do you think it will be able to remember or not.
Just try to guess, take a guess and make sure that you write down in the comment section the Q&A section
that we have.
Okay, so here you will be able to see that if I go ahead and execute this response dot text.
Uh sorry, it should be response dot content.
The reason it is able to remember because I have given the same session ID.
Now let me change this particular session ID I'll say chat two.
Okay.
Now if I go ahead and execute it.
Hey, as an AI, I have no memory of past conversation and don't know your name.
Now what exactly I've actually done over here.
I've just changed the session ID now, whatever conversation I have right now, uh, it will entirely
be saved in the session ID chat two okay, so that in the later stages, whenever we use this with message
history, it will just give the session ID of chat two and it will retrieve the information.
Now let me do one thing.
Let me quickly go ahead and copy this entire thing.
Okay.
Let me go ahead and write and say, uh, I'll say, hey, my name is John.
Okay.
Hey, my name is John.
Okay.
And now I will go ahead and execute with this config okay.
So if I go ahead and say, hey John, it's nice to meet you.
Now if I go ahead and execute the same code with this particular config okay.
Now you'll be able to see that it will understand.
Your name is John, I remember.
Is there anything I, uh, else I can help you with?
Okay, so with the help of this session ID, you'll be able to see that how easily we are able to switch
with respect to the context of conversation that we are having.
Okay.
So I hope you got an idea.
I hope you got an idea with respect to this particular message history, why we are specifically using
it, but just a easy way to understand if just highlight it over that particular class, let's say over
here, base chat message history is there.
This is nothing, but this is an abstract base class for storing chat message history and chat.
Message history is basically present over here.
This is an in memory implementation of chat message history.
As soon as I give my session ID whenever I call this.
So what is with underscore message underscore history.
It is a runnable.
It is a chain between model and get session history.
Whenever I am going to interact with the model, it is first of all going to, uh, you know, probably
go ahead and give your get session history.
That basically means based on the session ID, it is going to pick up your entire chat session.
Okay.
And based on that it is going to remember the context.
So this was just an idea with respect to implementing uh, you know, or how you can basically work
with chat history, uh, whenever you're developing a chat bot.
Now we will try to implement all this chat history, uh, in our, uh, prompt template.
And that is what we are going to see in the next video.
So here I hope you are able to understand this.
This was it from my side.
I'll see you all in the next video where I will be discussing about Prompt template.
So thank you.
I'll see you all in the next video.