1
00:00:00,000 --> 00:00:01,000
Hello guys.

2
00:00:01,000 --> 00:00:03,000
So we are going to continue the discussion with respect to Lang Chen.

3
00:00:03,000 --> 00:00:07,000
And now I'm going to show you how we can go ahead and build a chat bot.

4
00:00:07,000 --> 00:00:13,000
Already in our previous video we have seen that how to build a simple LM application with LCL.

5
00:00:13,000 --> 00:00:19,000
And we have discussed about Lang Lang chain expression, uh, language where when we were able to,

6
00:00:19,000 --> 00:00:25,000
you know, uh, may, uh, combine the chains together, all the chain components and we were able to

7
00:00:25,000 --> 00:00:25,000
do the prediction.

8
00:00:25,000 --> 00:00:26,000
Right.

9
00:00:26,000 --> 00:00:29,000
So let's go ahead and let's start working with chat bots.

10
00:00:29,000 --> 00:00:35,000
Now in this video we will go over an example of how to design and implement an LLM powered chat bot.

11
00:00:35,000 --> 00:00:39,000
This chat bot will be able to have a conversation and remember previous interaction.

12
00:00:39,000 --> 00:00:44,000
Note that this chatbot that we build will only use the language model to have a conversation.

13
00:00:44,000 --> 00:00:49,000
Okay, there are several other related concepts that you may be looking for, like conversational rag,

14
00:00:49,000 --> 00:00:53,000
like how to enable a chat bot experience over an external source of data.

15
00:00:53,000 --> 00:00:55,000
We will probably discuss this in the upcoming videos.

16
00:00:55,000 --> 00:00:57,000
Then we will also be discussing about agents.

17
00:00:57,000 --> 00:00:59,000
We will be developing multiple projects.

18
00:00:59,000 --> 00:01:03,000
Okay, but in this video we will try to focus on LLM powered chatbots.

19
00:01:03,000 --> 00:01:03,000
Okay.

20
00:01:03,000 --> 00:01:10,000
So first of all, as usual again over here I am going to use the open source uh uh models from grok

21
00:01:10,000 --> 00:01:11,000
API instead of using OpenAI.

22
00:01:12,000 --> 00:01:12,000
Okay.

23
00:01:12,000 --> 00:01:15,000
So first of all what I will say I will go ahead and say import OS.

24
00:01:15,000 --> 00:01:23,000
Uh, along with this, I will go ahead and write from uh dot env import load underscore dot env.

25
00:01:23,000 --> 00:01:26,000
And then we have this load underscore dot env.

26
00:01:26,000 --> 00:01:30,000
Here we are just uh loading all the environment variables.

27
00:01:30,000 --> 00:01:33,000
All the environment variables okay.

28
00:01:33,000 --> 00:01:37,000
And then after this, uh, what do we exactly do.

29
00:01:37,000 --> 00:01:41,000
We basically get our grok underscore API underscore key.

30
00:01:41,000 --> 00:01:46,000
And you know, that we have in our environment variable which we had updated last time.

31
00:01:46,000 --> 00:01:48,000
So this is my grok API key.

32
00:01:48,000 --> 00:01:50,000
So I will just go ahead and call it right.

33
00:01:50,000 --> 00:01:55,000
And in order to call it I will just go ahead and write OS dot get env.

34
00:01:55,000 --> 00:01:58,000
And here I'm just going to call this right.

35
00:01:59,000 --> 00:01:59,000
Perfect.

36
00:01:59,000 --> 00:02:03,000
So if I go ahead and print my grok API key this is the output.

37
00:02:03,000 --> 00:02:05,000
Uh, please do not use this.

38
00:02:05,000 --> 00:02:11,000
Anyhow, uh, after I upload all these videos, uh, in the course, uh, there's all keys will be deleted.

39
00:02:11,000 --> 00:02:14,000
Okay, so now I have my grok API key.

40
00:02:14,000 --> 00:02:17,000
Now the next thing is that we will go ahead and see how to call our LLM model.

41
00:02:17,000 --> 00:02:19,000
So that already we have seen over here.

42
00:02:19,000 --> 00:02:22,000
So here we will be importing chat grok.

43
00:02:22,000 --> 00:02:23,000
Right.

44
00:02:23,000 --> 00:02:26,000
So here you can see lang chain underscore grok import chat grok.

45
00:02:26,000 --> 00:02:30,000
And here we are going to use chat grok with model gamma two nine bit.

46
00:02:30,000 --> 00:02:33,000
This is the most newest model that we are going to use over here.

47
00:02:33,000 --> 00:02:35,000
Let me just go ahead and execute this.

48
00:02:35,000 --> 00:02:37,000
And here I have actually given the grok API key.

49
00:02:37,000 --> 00:02:38,000
Okay.

50
00:02:38,000 --> 00:02:41,000
So this is how you specifically load a model okay.

51
00:02:42,000 --> 00:02:46,000
Um, now after loading a model, what I am actually going to do is that we are going to, first of all,

52
00:02:46,000 --> 00:02:47,000
start with basic things.

53
00:02:47,000 --> 00:02:47,000
Okay.

54
00:02:47,000 --> 00:02:54,000
So let me just go ahead and write from long chain underscore code I am going to go ahead and import

55
00:02:54,000 --> 00:02:55,000
this messages.

56
00:02:55,000 --> 00:03:00,000
And from this messages I'll import the first type of message which is called as human message.

57
00:03:00,000 --> 00:03:00,000
Okay.

58
00:03:00,000 --> 00:03:06,000
Whenever I have a human message that basically whenever I'm using this particular class, uh, it indicates

59
00:03:06,000 --> 00:03:08,000
that a user is giving a message itself.

60
00:03:08,000 --> 00:03:08,000
Right.

61
00:03:08,000 --> 00:03:12,000
So let me just go ahead and write model dot invoke over here.

62
00:03:12,000 --> 00:03:16,000
And uh, with respect to this, let me go ahead and create my human message, uh, class.

63
00:03:16,000 --> 00:03:17,000
Let me initialize it.

64
00:03:17,000 --> 00:03:21,000
And here we are going to talk about content is equal to.

65
00:03:21,000 --> 00:03:35,000
And here I'm going to say hey hi my name is Chris and I am, uh, chief AI engineer.

66
00:03:35,000 --> 00:03:35,000
Okay.

67
00:03:35,000 --> 00:03:39,000
So I'm just giving this kind of message over here and let me go ahead and invoke this.

68
00:03:39,000 --> 00:03:44,000
So here you'll be able to see as soon as I give the message over here, this we are invoking with respect

69
00:03:44,000 --> 00:03:49,000
to this particular model, that basically means this information is going to my, uh, model that is

70
00:03:49,000 --> 00:03:50,000
gamma2 over here.

71
00:03:50,000 --> 00:03:51,000
And I'm getting the response.

72
00:03:51,000 --> 00:03:52,000
Hello, Chris.

73
00:03:52,000 --> 00:03:54,000
It's nice to meet you as a chief engineer.

74
00:03:54,000 --> 00:03:57,000
What kind of projects are you working on these days?

75
00:03:57,000 --> 00:04:02,000
I'm always eager to learn about the exciting work being done in the field of AI, and these are my all

76
00:04:02,000 --> 00:04:07,000
the metadata info Information like how much is the input tokens, what is the output tokens, and what

77
00:04:07,000 --> 00:04:08,000
is the total number of tokens.

78
00:04:08,000 --> 00:04:14,000
So here, uh, just a simple conversation by using a human message, uh, class, I've actually done

79
00:04:14,000 --> 00:04:14,000
it right.

80
00:04:14,000 --> 00:04:17,000
Still, I have not initialized anything from the system message itself.

81
00:04:17,000 --> 00:04:23,000
I have not told, uh, like, what kind of behavior the LM model should probably go ahead with right

82
00:04:23,000 --> 00:04:24,000
now.

83
00:04:24,000 --> 00:04:24,000
Let me do one thing.

84
00:04:24,000 --> 00:04:25,000
Let me.

85
00:04:25,000 --> 00:04:26,000
And you know this, right?

86
00:04:26,000 --> 00:04:30,000
Whenever we get this AI message, that basically means we are getting the response from our model.

87
00:04:30,000 --> 00:04:32,000
Now let me go ahead and write from Long Chain.

88
00:04:33,000 --> 00:04:33,000
Oops.

89
00:04:34,000 --> 00:04:38,000
Uh, over here I'll go ahead and write from Long Chain.

90
00:04:38,000 --> 00:04:38,000
Okay.

91
00:04:38,000 --> 00:04:44,000
Now, I think I was searching it over here from long chain underscore core dot messages.

92
00:04:44,000 --> 00:04:48,000
I'm also going to import I message okay.

93
00:04:48,000 --> 00:04:51,000
Now again here I'll go ahead and write model dot invoke.

94
00:04:51,000 --> 00:04:56,000
Uh, and let me put some combination of AI and human message.

95
00:04:56,000 --> 00:05:02,000
Now understand, one thing is that, uh, we can give a list of messages like how we basically developed

96
00:05:02,000 --> 00:05:03,000
a simple LM application.

97
00:05:03,000 --> 00:05:03,000
Right?

98
00:05:03,000 --> 00:05:05,000
So let's say first message is my human message.

99
00:05:05,000 --> 00:05:11,000
And here I will go ahead and write content with respect to this entire information.

100
00:05:11,000 --> 00:05:11,000
Hi.

101
00:05:11,000 --> 00:05:14,000
My name is so and so and I'm a chief engineer.

102
00:05:14,000 --> 00:05:15,000
I'll put it over here itself.

103
00:05:15,000 --> 00:05:16,000
Okay?

104
00:05:16,000 --> 00:05:21,000
Uh, and I will close this quickly, and then I will go ahead and take my next message.

105
00:05:21,000 --> 00:05:25,000
The next message will be basically, uh, AI message.

106
00:05:25,000 --> 00:05:29,000
And here in the AI message, I'm just going to go ahead and write this particular content and let me

107
00:05:29,000 --> 00:05:32,000
just go ahead and copy this entire thing, whatever content is over here.

108
00:05:32,000 --> 00:05:33,000
Right.

109
00:05:33,000 --> 00:05:35,000
Uh, in the field of AI.

110
00:05:35,000 --> 00:05:35,000
Right.

111
00:05:35,000 --> 00:05:39,000
So I'm just going to go ahead and write this particular content over here.

112
00:05:39,000 --> 00:05:39,000
Right.

113
00:05:39,000 --> 00:05:43,000
So this is the I, uh, message that I've actually got a response.

114
00:05:43,000 --> 00:05:47,000
So now here I'm actually going to create a sequence of messages from this particular text.

115
00:05:47,000 --> 00:05:49,000
I got the I'm just hard coding.

116
00:05:49,000 --> 00:05:49,000
Okay.

117
00:05:49,000 --> 00:05:52,000
I got this particular message from this particular model.

118
00:05:52,000 --> 00:05:56,000
And now I will go ahead and just trace again a human message.

119
00:05:56,000 --> 00:06:09,000
And now I will go and ask, hey, hey, Hey, what's my name and what do I do?

120
00:06:09,000 --> 00:06:12,000
Okay, so this is the question that I'm going to ask over here.

121
00:06:13,000 --> 00:06:14,000
Now, this is amazing.

122
00:06:14,000 --> 00:06:15,000
Okay, let's see the output.

123
00:06:15,000 --> 00:06:21,000
What I'm actually going to do because I'm, I'm first of all feeding this particular information with

124
00:06:21,000 --> 00:06:22,000
respect to the human message.

125
00:06:22,000 --> 00:06:25,000
Hey, my name is Krish and I am a chief AI engineer.

126
00:06:25,000 --> 00:06:28,000
And from this AI message I got this entire value.

127
00:06:28,000 --> 00:06:30,000
So I am probably putting it up.

128
00:06:30,000 --> 00:06:34,000
And I'm asking it whether it is able to remember my no.

129
00:06:34,000 --> 00:06:39,000
It is whether it is able to remember the information that I have actually given since I'm passing it

130
00:06:39,000 --> 00:06:42,000
in the form of list of messages here.

131
00:06:42,000 --> 00:06:42,000
Right.

132
00:06:43,000 --> 00:06:45,000
I think it should be able to remember it.

133
00:06:45,000 --> 00:06:45,000
Okay.

134
00:06:45,000 --> 00:06:46,000
So now once I go ahead and execute it.

135
00:06:46,000 --> 00:06:51,000
So here you can say you said your name is Krish and you are a chief AI engineer, right?

136
00:06:51,000 --> 00:06:54,000
Is there anything else you would like me to remember about you?

137
00:06:54,000 --> 00:06:57,000
Okay, so all this information is basically coming.

138
00:06:57,000 --> 00:07:02,000
That basically means, uh, one important thing is that whatever conversation I'm giving inside this

139
00:07:02,000 --> 00:07:05,000
list of messages with the human message, I message human message.

140
00:07:05,000 --> 00:07:08,000
It is also able to remember the previous context.

141
00:07:08,000 --> 00:07:08,000
Okay.

142
00:07:08,000 --> 00:07:14,000
Now that is where we are going to discuss about message history okay.

143
00:07:14,000 --> 00:07:19,000
And this message history will probably help you to understand the all the context that it will help

144
00:07:19,000 --> 00:07:22,000
the LM to remember all the context that we are probably discussing about.

145
00:07:22,000 --> 00:07:29,000
This is just an idea that whether the I, the LM model is able to remember things or not.

146
00:07:29,000 --> 00:07:34,000
But at the end of the day, whenever we develop real world application, there will be different, different

147
00:07:34,000 --> 00:07:35,000
sessions that will be happening.

148
00:07:35,000 --> 00:07:36,000
Right?

149
00:07:36,000 --> 00:07:37,000
Let's say I'm using ChatGPT.

150
00:07:37,000 --> 00:07:43,000
Someone is using ChatGPT, some other people are using some other LLM models and they are chatting.

151
00:07:43,000 --> 00:07:43,000
Right.

152
00:07:43,000 --> 00:07:49,000
How that particular uh, models are able to remember their context, that is with respect to session.

153
00:07:49,000 --> 00:07:55,000
So in order to understand that how these sessions are managed, we will be discussing about a very important

154
00:07:55,000 --> 00:07:57,000
property which is called as message history.

155
00:07:57,000 --> 00:07:57,000
Okay.

156
00:07:57,000 --> 00:08:01,000
Now let's discuss about this message history.

157
00:08:01,000 --> 00:08:06,000
And this is a very important component altogether in the, uh, long chain itself.

158
00:08:06,000 --> 00:08:07,000
Right.

159
00:08:07,000 --> 00:08:10,000
Which will actually help you to work with the LM model.

160
00:08:10,000 --> 00:08:10,000
Okay.

161
00:08:11,000 --> 00:08:17,000
Now, what we really need to use or understand how message history works.

162
00:08:17,000 --> 00:08:21,000
So here I'm going to put a message saying that we can use message history class to wrap our model and

163
00:08:21,000 --> 00:08:22,000
make it stateful.

164
00:08:22,000 --> 00:08:27,000
We have to make it stateful so that it remembers all the context with respect to any kind of person

165
00:08:27,000 --> 00:08:30,000
who are actually interacting with that model.

166
00:08:30,000 --> 00:08:34,000
This will keep track of our inputs and output of the model and store them in some data store.

167
00:08:34,000 --> 00:08:38,000
Further interaction will then load these messages and pass them into the chain as part of the input.

168
00:08:38,000 --> 00:08:42,000
Okay, so let's see how this can be actually done.

169
00:08:42,000 --> 00:08:49,000
So first of all for this we require another library which is called as long chain underscore community.

170
00:08:49,000 --> 00:08:51,000
So please make sure that you install this.

171
00:08:51,000 --> 00:08:55,000
And once you install this I have already done this particular installation so that it does not take

172
00:08:55,000 --> 00:08:56,000
much time.

173
00:08:56,000 --> 00:08:58,000
You have to go ahead and update in the requirement dot txt okay.

174
00:08:58,000 --> 00:09:03,000
So first of all go ahead and update this in the requirement dot txt and make sure that whatever installation

175
00:09:03,000 --> 00:09:06,000
that you have done, you are doing it in your, uh, virtual environment.

176
00:09:06,000 --> 00:09:07,000
Okay.

177
00:09:08,000 --> 00:09:08,000
Perfect.

178
00:09:08,000 --> 00:09:15,000
So from here, uh, what I will do is that I, uh, Langton community is basically used because, uh,

179
00:09:15,000 --> 00:09:20,000
uh, any integration related to measure history will be available in this Langton underscore community.

180
00:09:20,000 --> 00:09:20,000
Okay.

181
00:09:20,000 --> 00:09:27,000
So, uh, let me go ahead and import some very important libraries, let's say from long chain underscore

182
00:09:27,000 --> 00:09:28,000
community.

183
00:09:28,000 --> 00:09:31,000
Uh, I will be using something called as chat message histories.

184
00:09:31,000 --> 00:09:31,000
Okay.

185
00:09:31,000 --> 00:09:38,000
Inside this chat message history we will be importing chat chat message history okay I will talk about

186
00:09:38,000 --> 00:09:38,000
this.

187
00:09:38,000 --> 00:09:40,000
What exactly is this chat message history?

188
00:09:40,000 --> 00:09:43,000
Just for now, let me just import some more libraries.

189
00:09:43,000 --> 00:09:51,000
So I will go ahead and write lecture underscore code dot chat history okay I'm going to import bass

190
00:09:51,000 --> 00:09:53,000
chat history okay.

191
00:09:53,000 --> 00:09:54,000
Bass chat message history.

192
00:09:55,000 --> 00:10:00,000
And then finally I will go ahead and import from luncheon underscore core dot runnables okay.

193
00:10:00,000 --> 00:10:06,000
Since this is for the chain and for the rain chain, also, we basically go ahead and uh, import this

194
00:10:06,000 --> 00:10:10,000
particular history and I will go ahead and import runnable with message history.

195
00:10:11,000 --> 00:10:14,000
Now this is something really important okay.

196
00:10:14,000 --> 00:10:19,000
Here you'll be able to see that we can import the relevant class over here.

197
00:10:19,000 --> 00:10:23,000
Set up our chain uh which wraps the model and add in the message history.

198
00:10:23,000 --> 00:10:25,000
That is what we really need to do, right?

199
00:10:25,000 --> 00:10:32,000
Every message that we are probably putting up, it needs to be, uh, added in the message history itself,

200
00:10:32,000 --> 00:10:32,000
how it is done.

201
00:10:32,000 --> 00:10:34,000
I'll just talk about it sometime.

202
00:10:34,000 --> 00:10:35,000
Okay.

203
00:10:35,000 --> 00:10:41,000
But the thing that you really need to understand is that whenever different, different users are chatting

204
00:10:41,000 --> 00:10:47,000
with the LM model, how we are going to make sure that one session is completely different from the

205
00:10:47,000 --> 00:10:48,000
other session.

206
00:10:48,000 --> 00:10:53,000
So for that reason, what we will do is that we will create one amazing function, okay.

207
00:10:53,000 --> 00:10:59,000
And this particular function will be definition get underscore session underscore history.

208
00:10:59,000 --> 00:11:01,000
Now what this function is going to do.

209
00:11:01,000 --> 00:11:03,000
Let me talk about it okay.

210
00:11:03,000 --> 00:11:11,000
Here this function will be creating a session ID okay this session ID and this session id will be a

211
00:11:11,000 --> 00:11:12,000
string okay.

212
00:11:13,000 --> 00:11:16,000
Right I've written a session ID and this will be a string.

213
00:11:16,000 --> 00:11:21,000
And this will get inherited to your base I mean the return type.

214
00:11:21,000 --> 00:11:21,000
Sorry.

215
00:11:21,000 --> 00:11:22,000
Not inherited.

216
00:11:22,000 --> 00:11:26,000
The return type of this particular function will be base chat message history.

217
00:11:27,000 --> 00:11:27,000
So whatever.

218
00:11:27,000 --> 00:11:33,000
Chat history is basically getting created right that is imported from this particular library.

219
00:11:33,000 --> 00:11:38,000
So what I'm actually going to do is that this get session history right with respect to a session ID

220
00:11:38,000 --> 00:11:39,000
will be created.

221
00:11:39,000 --> 00:11:39,000
Right.

222
00:11:39,000 --> 00:11:44,000
And this session ID will be used to distinguish one chat session with the other.

223
00:11:44,000 --> 00:11:45,000
Okay.

224
00:11:45,000 --> 00:11:48,000
So here, uh, the return type is based chat history.

225
00:11:48,000 --> 00:11:53,000
I will say hey if session ID not in store.

226
00:11:53,000 --> 00:11:58,000
So let me just go ahead and create one variable called as store which will be a dictionary over here.

227
00:11:58,000 --> 00:12:01,000
So here I will just go ahead and write store okay.

228
00:12:01,000 --> 00:12:01,000
Okay.

229
00:12:02,000 --> 00:12:03,000
Let me talk more about it.

230
00:12:03,000 --> 00:12:05,000
And the explanation will be very good.

231
00:12:05,000 --> 00:12:08,000
And I will show you the practical example.

232
00:12:08,000 --> 00:12:19,000
I will go ahead and write store of session underscore ID is equal to chat message history okay.

233
00:12:20,000 --> 00:12:22,000
So now see this everything.

234
00:12:22,000 --> 00:12:24,000
Please try to understand it right.

235
00:12:24,000 --> 00:12:28,000
First of all, for this particular message history.

236
00:12:28,000 --> 00:12:34,000
So you can see that we have a class which is called as chat message history.

237
00:12:34,000 --> 00:12:34,000
Okay.

238
00:12:34,000 --> 00:12:41,000
Now this chat message history, whenever we are creating a session ID right, we are storing the session

239
00:12:41,000 --> 00:12:43,000
ID in this particular dictionary.

240
00:12:43,000 --> 00:12:43,000
Right.

241
00:12:43,000 --> 00:12:49,000
With respect to this particular value, whatever session ID I'm actually creating, I am I'm initializing

242
00:12:49,000 --> 00:12:51,000
a chat message history.

243
00:12:51,000 --> 00:12:51,000
Right.

244
00:12:51,000 --> 00:12:54,000
So that basically means this is an object of a chat message history.

245
00:12:54,000 --> 00:13:00,000
And whatever chat is specifically happening automatically, it should be going inside this session ID

246
00:13:00,000 --> 00:13:00,000
okay.

247
00:13:00,000 --> 00:13:08,000
Next thing is that after this I will just go ahead and return store of session underscore ID.

248
00:13:08,000 --> 00:13:10,000
Now see you will.

249
00:13:10,000 --> 00:13:15,000
You know I know there is some confusion with respect to the definition, but let's understand this definition.

250
00:13:15,000 --> 00:13:17,000
So what is the definition over here?

251
00:13:17,000 --> 00:13:20,000
This is a function which is called as get session history.

252
00:13:20,000 --> 00:13:26,000
Whenever I give a session ID, it should be able to check whether the session ID is present in this

253
00:13:26,000 --> 00:13:27,000
particular dictionary or not.

254
00:13:27,000 --> 00:13:33,000
If it is present, we are going to get the entire chat message history from that and we will return

255
00:13:33,000 --> 00:13:34,000
it.

256
00:13:34,000 --> 00:13:34,000
Okay.

257
00:13:34,000 --> 00:13:36,000
And that is how we are going to distinguish it.

258
00:13:37,000 --> 00:13:43,000
Two things this get session history will give you a response type of base chat history.

259
00:13:43,000 --> 00:13:46,000
Uh, it is an abstract class for storing the chat message history.

260
00:13:46,000 --> 00:13:52,000
If I talk about chat message history it is an in-memory implementation of chat message history stores.

261
00:13:52,000 --> 00:13:54,000
Messages are in an memory list.

262
00:13:54,000 --> 00:13:56,000
So it is very much simple.

263
00:13:56,000 --> 00:13:58,000
Whenever I give a session ID it should.

264
00:13:58,000 --> 00:14:02,000
First of all go ahead and check in this particular dictionary whether it is available or not.

265
00:14:02,000 --> 00:14:07,000
If it is available, it will go ahead and pick up the entire chat message that we have discussed with

266
00:14:07,000 --> 00:14:11,000
respect to, or whatever questions we have asked with respect to the LM model for that session ID,

267
00:14:11,000 --> 00:14:15,000
and it is just going to return it now, see how it is going to work.

268
00:14:15,000 --> 00:14:15,000
Okay.

269
00:14:15,000 --> 00:14:16,000
So I'll execute this.

270
00:14:17,000 --> 00:14:19,000
Uh, I will talk about this runnable with message history.

271
00:14:19,000 --> 00:14:21,000
Uh, soon it will be used as we go ahead.

272
00:14:21,000 --> 00:14:22,000
Okay.

273
00:14:22,000 --> 00:14:27,000
Now the best thing over here will be that we will be seeing how we will be using this.

274
00:14:27,000 --> 00:14:28,000
Okay?

275
00:14:28,000 --> 00:14:32,000
So first of all, we will go ahead and create our config okay.

276
00:14:33,000 --> 00:14:35,000
So I'll write hey this is my config over here.

277
00:14:35,000 --> 00:14:40,000
And this config will be nothing but it will called as configurable.

278
00:14:40,000 --> 00:14:44,000
Okay I will just give this key value pairs configurable.

279
00:14:44,000 --> 00:14:46,000
And here I'm just going to give my session ID.

280
00:14:46,000 --> 00:14:49,000
So let's say my session ID is something like this.

281
00:14:49,000 --> 00:14:51,000
Right now I'm hard coding it.

282
00:14:51,000 --> 00:14:59,000
Let's say my session ID is something like um chat one okay I'm just going to write it as chat one.

283
00:14:59,000 --> 00:15:01,000
Now let's go ahead and execute this okay.

284
00:15:02,000 --> 00:15:03,000
Let's go ahead and execute.

285
00:15:03,000 --> 00:15:05,000
So this basically becomes my config okay.

286
00:15:05,000 --> 00:15:07,000
My session ID is chat one.

287
00:15:07,000 --> 00:15:11,000
Now let me use this particular session ID and let me chat with the LM model.

288
00:15:11,000 --> 00:15:17,000
So here you'll be able to see that I will write response is equal to width.

289
00:15:17,000 --> 00:15:22,000
Uh, so before this let me do one more thing okay.

290
00:15:22,000 --> 00:15:26,000
Uh, see, I have used the Get session history right now.

291
00:15:26,000 --> 00:15:29,000
In order to execute this, I really need to use this runnable.

292
00:15:29,000 --> 00:15:30,000
Right.

293
00:15:30,000 --> 00:15:35,000
Unless and until we don't have this runnable, uh, it is not going to probably execute this entire

294
00:15:35,000 --> 00:15:35,000
chain.

295
00:15:35,000 --> 00:15:39,000
So here you say see runnable that manage chat message history for other runnables.

296
00:15:39,000 --> 00:15:39,000
Okay.

297
00:15:39,000 --> 00:15:44,000
The chat message history is a sequence of messages that represent a conversation.

298
00:15:44,000 --> 00:15:49,000
So here what I'm actually going to do I'll go ahead and write width underscore message underscore history.

299
00:15:49,000 --> 00:15:53,000
And here I will go ahead and write a runnable with message history.

300
00:15:53,000 --> 00:15:58,000
And here the first parameter will get is model whatever model we are going to use.

301
00:15:58,000 --> 00:16:01,000
And the second is get session history Okay.

302
00:16:01,000 --> 00:16:05,000
So this two parameters will be specifically required, uh, for this right.

303
00:16:05,000 --> 00:16:08,000
And this width session with message history.

304
00:16:08,000 --> 00:16:12,000
We will try to invoke this as soon as we invoke this.

305
00:16:12,000 --> 00:16:14,000
This is going to take this particular model.

306
00:16:14,000 --> 00:16:16,000
This is going to take the entire chat history.

307
00:16:16,000 --> 00:16:21,000
What we are getting based on the session ID again how to pass the session ID I'll let you know.

308
00:16:21,000 --> 00:16:21,000
Okay.

309
00:16:21,000 --> 00:16:25,000
And then it will be able to give you the entire information.

310
00:16:25,000 --> 00:16:31,000
So here, let me just go ahead and write double s, uh, because I don't want to write triple s.

311
00:16:31,000 --> 00:16:31,000
Okay.

312
00:16:31,000 --> 00:16:34,000
So here so many S's are here okay.

313
00:16:34,000 --> 00:16:35,000
Perfect.

314
00:16:36,000 --> 00:16:36,000
Right.

315
00:16:36,000 --> 00:16:38,000
So again let me repeat it.

316
00:16:38,000 --> 00:16:39,000
What we have exactly done.

317
00:16:39,000 --> 00:16:41,000
First of all we have created a dictionary over here.

318
00:16:41,000 --> 00:16:48,000
Then we have created a function which will be able to retrieve whatever uh, chat history is there for

319
00:16:48,000 --> 00:16:49,000
a specific session ID.

320
00:16:49,000 --> 00:16:56,000
Then we are converting, or we are using this runnable with message history so that we can, um, you

321
00:16:56,000 --> 00:16:58,000
know, interact with our LM model based on the chat history.

322
00:16:58,000 --> 00:17:01,000
So for this we give two parameters that is model and get session history.

323
00:17:01,000 --> 00:17:05,000
Now we have created a configuration called as session ID as chat one.

324
00:17:05,000 --> 00:17:10,000
And now what we will do is that we will call this with message history.

325
00:17:10,000 --> 00:17:16,000
And here I'm just going to go ahead and invoke invoke over here.

326
00:17:16,000 --> 00:17:18,000
First of all I will give a human message.

327
00:17:18,000 --> 00:17:21,000
Let's say this is my human message over here.

328
00:17:21,000 --> 00:17:22,000
Now see see the magic okay.

329
00:17:22,000 --> 00:17:23,000
This will be amazing.

330
00:17:23,000 --> 00:17:24,000
I will go ahead and write.

331
00:17:24,000 --> 00:17:27,000
This is my content is equal to.

332
00:17:27,000 --> 00:17:29,000
And let me say hi.

333
00:17:29,000 --> 00:17:32,000
Uh, or I'll give the same message over here.

334
00:17:32,000 --> 00:17:32,000
Right.

335
00:17:32,000 --> 00:17:36,000
So hi, my name is Krish and I am an AI engineer.

336
00:17:36,000 --> 00:17:37,000
Okay.

337
00:17:37,000 --> 00:17:41,000
Now see, I am trying to interact with based on the session ID.

338
00:17:41,000 --> 00:17:43,000
Okay, now see this all information is over here.

339
00:17:44,000 --> 00:17:48,000
But along with this we can go ahead and give our configuration.

340
00:17:48,000 --> 00:17:52,000
The configuration will be given inside this invoke.

341
00:17:52,000 --> 00:17:53,000
And here I will go ahead and write.

342
00:17:53,000 --> 00:17:56,000
This is my config which will be equal to config.

343
00:17:57,000 --> 00:18:03,000
Now when we are giving the config that basically means for this session id we are interacting okay.

344
00:18:03,000 --> 00:18:06,000
So based on the session id it will be able to remember all this context.

345
00:18:06,000 --> 00:18:09,000
So if I go ahead and execute this okay.

346
00:18:09,000 --> 00:18:12,000
So with message history is not defined because I did not execute this.

347
00:18:12,000 --> 00:18:13,000
Let me execute it.

348
00:18:13,000 --> 00:18:15,000
Now let's go ahead and do this.

349
00:18:15,000 --> 00:18:18,000
So here you can see I message hi crush.

350
00:18:18,000 --> 00:18:19,000
It's great to meet you.

351
00:18:19,000 --> 00:18:22,000
Being a chief engineer is an exciting role.

352
00:18:22,000 --> 00:18:28,000
All this information is basically coming Okay, now if you want to get this particular content, I will

353
00:18:28,000 --> 00:18:30,000
also save this in my response variable.

354
00:18:30,000 --> 00:18:30,000
Okay.

355
00:18:30,000 --> 00:18:34,000
And let me just go ahead and write response dot content okay.

356
00:18:34,000 --> 00:18:35,000
I can also go ahead and write like this.

357
00:18:35,000 --> 00:18:38,000
So this is what is the message that I will be getting.

358
00:18:38,000 --> 00:18:41,000
Now let me do one thing okay.

359
00:18:41,000 --> 00:18:47,000
Let me take another response or let me go ahead and again use this with message history.

360
00:18:47,000 --> 00:18:51,000
And this time I'm sending a message of human message like what's my name?

361
00:18:51,000 --> 00:18:53,000
And I'll write config is equal to config.

362
00:18:54,000 --> 00:18:59,000
Now in this case if I see the output here you'll it will say that hey your name is Chris okay.

363
00:18:59,000 --> 00:19:02,000
White is able to remember it because we are giving the same config.

364
00:19:02,000 --> 00:19:08,000
So let's say that now if I change the config, will it be able to remember this particular context?

365
00:19:08,000 --> 00:19:14,000
Okay, now here I'm just going to change the config When I say change the config I'm changing the session

366
00:19:14,000 --> 00:19:15,000
ID right.

367
00:19:15,000 --> 00:19:18,000
So I'm changing the session id okay.

368
00:19:18,000 --> 00:19:21,000
Now if I give the session ID some some different session ID.

369
00:19:21,000 --> 00:19:25,000
So for giving the session id I will again use this config over here.

370
00:19:25,000 --> 00:19:27,000
The first session id was chat one.

371
00:19:27,000 --> 00:19:29,000
Now this will be my config one.

372
00:19:29,000 --> 00:19:32,000
Okay let me just go ahead and give it.

373
00:19:32,000 --> 00:19:35,000
So this will basically be my config one okay.

374
00:19:35,000 --> 00:19:40,000
Now with respect to this particular config I will again go ahead and use my response and I'll say,

375
00:19:40,000 --> 00:19:47,000
hey, uh, with this particular message history right over here, I will go ahead and I dot invoke and

376
00:19:47,000 --> 00:19:50,000
here I will go ahead and give my message.

377
00:19:50,000 --> 00:19:52,000
The first message is nothing but my human message.

378
00:19:52,000 --> 00:19:59,000
The human message will be nothing but content is equal to I'll say, what's my name?

379
00:19:59,000 --> 00:19:59,000
Okay.

380
00:20:00,000 --> 00:20:02,000
Now see, before also I asked what's my name?

381
00:20:02,000 --> 00:20:05,000
But I give the previous session ID with respect to this particular configuration.

382
00:20:05,000 --> 00:20:07,000
But here I'm going to give my new configuration.

383
00:20:07,000 --> 00:20:14,000
So I'll go ahead and write config is equal to I will go ahead and write config one okay.

384
00:20:14,000 --> 00:20:17,000
Now do you think it will be able to remember or not.

385
00:20:17,000 --> 00:20:23,000
Just try to guess, take a guess and make sure that you write down in the comment section the Q&A section

386
00:20:23,000 --> 00:20:24,000
that we have.

387
00:20:24,000 --> 00:20:28,000
Okay, so here you will be able to see that if I go ahead and execute this response dot text.

388
00:20:28,000 --> 00:20:31,000
Uh sorry, it should be response dot content.

389
00:20:33,000 --> 00:20:36,000
The reason it is able to remember because I have given the same session ID.

390
00:20:36,000 --> 00:20:39,000
Now let me change this particular session ID I'll say chat two.

391
00:20:39,000 --> 00:20:40,000
Okay.

392
00:20:40,000 --> 00:20:41,000
Now if I go ahead and execute it.

393
00:20:41,000 --> 00:20:46,000
Hey, as an AI, I have no memory of past conversation and don't know your name.

394
00:20:46,000 --> 00:20:48,000
Now what exactly I've actually done over here.

395
00:20:48,000 --> 00:20:55,000
I've just changed the session ID now, whatever conversation I have right now, uh, it will entirely

396
00:20:55,000 --> 00:21:01,000
be saved in the session ID chat two okay, so that in the later stages, whenever we use this with message

397
00:21:01,000 --> 00:21:06,000
history, it will just give the session ID of chat two and it will retrieve the information.

398
00:21:06,000 --> 00:21:07,000
Now let me do one thing.

399
00:21:07,000 --> 00:21:10,000
Let me quickly go ahead and copy this entire thing.

400
00:21:10,000 --> 00:21:10,000
Okay.

401
00:21:11,000 --> 00:21:15,000
Let me go ahead and write and say, uh, I'll say, hey, my name is John.

402
00:21:15,000 --> 00:21:15,000
Okay.

403
00:21:15,000 --> 00:21:19,000
Hey, my name is John.

404
00:21:19,000 --> 00:21:20,000
Okay.

405
00:21:21,000 --> 00:21:23,000
And now I will go ahead and execute with this config okay.

406
00:21:23,000 --> 00:21:26,000
So if I go ahead and say, hey John, it's nice to meet you.

407
00:21:26,000 --> 00:21:33,000
Now if I go ahead and execute the same code with this particular config okay.

408
00:21:33,000 --> 00:21:36,000
Now you'll be able to see that it will understand.

409
00:21:36,000 --> 00:21:38,000
Your name is John, I remember.

410
00:21:38,000 --> 00:21:40,000
Is there anything I, uh, else I can help you with?

411
00:21:40,000 --> 00:21:47,000
Okay, so with the help of this session ID, you'll be able to see that how easily we are able to switch

412
00:21:47,000 --> 00:21:50,000
with respect to the context of conversation that we are having.

413
00:21:50,000 --> 00:21:51,000
Okay.

414
00:21:51,000 --> 00:21:53,000
So I hope you got an idea.

415
00:21:53,000 --> 00:21:57,000
I hope you got an idea with respect to this particular message history, why we are specifically using

416
00:21:57,000 --> 00:22:05,000
it, but just a easy way to understand if just highlight it over that particular class, let's say over

417
00:22:05,000 --> 00:22:07,000
here, base chat message history is there.

418
00:22:07,000 --> 00:22:11,000
This is nothing, but this is an abstract base class for storing chat message history and chat.

419
00:22:11,000 --> 00:22:13,000
Message history is basically present over here.

420
00:22:13,000 --> 00:22:16,000
This is an in memory implementation of chat message history.

421
00:22:16,000 --> 00:22:20,000
As soon as I give my session ID whenever I call this.

422
00:22:20,000 --> 00:22:23,000
So what is with underscore message underscore history.

423
00:22:23,000 --> 00:22:24,000
It is a runnable.

424
00:22:24,000 --> 00:22:26,000
It is a chain between model and get session history.

425
00:22:26,000 --> 00:22:32,000
Whenever I am going to interact with the model, it is first of all going to, uh, you know, probably

426
00:22:32,000 --> 00:22:34,000
go ahead and give your get session history.

427
00:22:34,000 --> 00:22:39,000
That basically means based on the session ID, it is going to pick up your entire chat session.

428
00:22:39,000 --> 00:22:40,000
Okay.

429
00:22:40,000 --> 00:22:42,000
And based on that it is going to remember the context.

430
00:22:42,000 --> 00:22:49,000
So this was just an idea with respect to implementing uh, you know, or how you can basically work

431
00:22:49,000 --> 00:22:52,000
with chat history, uh, whenever you're developing a chat bot.

432
00:22:52,000 --> 00:23:00,000
Now we will try to implement all this chat history, uh, in our, uh, prompt template.

433
00:23:00,000 --> 00:23:02,000
And that is what we are going to see in the next video.

434
00:23:02,000 --> 00:23:04,000
So here I hope you are able to understand this.

435
00:23:04,000 --> 00:23:06,000
This was it from my side.

436
00:23:06,000 --> 00:23:09,000
I'll see you all in the next video where I will be discussing about Prompt template.

437
00:23:09,000 --> 00:23:10,000
So thank you.

438
00:23:10,000 --> 00:23:11,000
I'll see you all in the next video.

