1
00:00:00,000 --> 00:00:00,000
Hello guys.

2
00:00:00,000 --> 00:00:03,000
So we are going to continue the discussion with respect to Lang Chain.

3
00:00:03,000 --> 00:00:08,000
And now in this particular video we are going to talk about conversational Q&A chatbot.

4
00:00:08,000 --> 00:00:12,000
So we will be building some amazing conversation Q&A chatbot.

5
00:00:12,000 --> 00:00:14,000
You will be understanding how you can go ahead and build this.

6
00:00:14,000 --> 00:00:17,000
Now see this in many Q&A application.

7
00:00:17,000 --> 00:00:22,000
We want to allow the users to have a back and forth conversation, meaning the application needs some

8
00:00:22,000 --> 00:00:28,000
sort of memory of past question and answers, and some logic for incorporating those into its current

9
00:00:28,000 --> 00:00:29,000
thinking.

10
00:00:29,000 --> 00:00:34,000
In this guide, we focus on adding logic for incorporating historical messages.

11
00:00:34,000 --> 00:00:38,000
Further details on the chat history management is covered in the previous videos, right?

12
00:00:38,000 --> 00:00:44,000
We have already discussed about some of the chat history management terms and now we will be covering

13
00:00:44,000 --> 00:00:44,000
two approaches.

14
00:00:44,000 --> 00:00:49,000
One is in chains in which we will always execute a retrieval step, and the second one is agents, in

15
00:00:49,000 --> 00:00:55,000
which we give an LLM discretion over whether or how to execute a retrieval step for multiple steps.

16
00:00:55,000 --> 00:00:59,000
So already we have discussed so much things about chat history.

17
00:00:59,000 --> 00:00:59,000
Right.

18
00:00:59,000 --> 00:01:04,000
But the main idea of using this chat history specifically for a Q and a chat bot.

19
00:01:04,000 --> 00:01:07,000
So in this video I will go ahead and explain you that okay.

20
00:01:07,000 --> 00:01:13,000
Now first of all, uh, over here also, I'll be making sure that I use open source models.

21
00:01:13,000 --> 00:01:17,000
So for this I will be again importing import OS.

22
00:01:17,000 --> 00:01:27,000
And then from here I will go ahead and uh write import or from dot env import load underscore dot env.

23
00:01:27,000 --> 00:01:27,000
Right.

24
00:01:27,000 --> 00:01:29,000
So we are also going to go ahead and import this.

25
00:01:29,000 --> 00:01:31,000
Let's see whether it is executing fine.

26
00:01:31,000 --> 00:01:34,000
So we will go ahead and initialize this load underscore dot env.

27
00:01:34,000 --> 00:01:39,000
Because we will be having all our environment variables now with respect to the environment variables.

28
00:01:39,000 --> 00:01:42,000
The first key that I will be requiring.

29
00:01:42,000 --> 00:01:49,000
And here I will go ahead and write from long chain underscore grok import chart grok okay.

30
00:01:49,000 --> 00:01:52,000
And I think we have repeated this code many multiple times.

31
00:01:52,000 --> 00:01:52,000
Right.

32
00:01:52,000 --> 00:01:56,000
So I'm just showing you again and again so that you don't forget this okay.

33
00:01:56,000 --> 00:02:02,000
Now when I'm importing this chart grok along with this you'll be able to see that I will also go ahead

34
00:02:02,000 --> 00:02:05,000
and take my API key from my environment variable.

35
00:02:05,000 --> 00:02:08,000
So for this I will go ahead and write OS dot get env.

36
00:02:09,000 --> 00:02:14,000
And inside this we will go ahead and write grok underscore api underscore key okay.

37
00:02:15,000 --> 00:02:22,000
So once we get this uh the next step will be that we will go ahead and call our uh LM model specifically

38
00:02:22,000 --> 00:02:22,000
over here.

39
00:02:22,000 --> 00:02:27,000
So for this we will go ahead and use llama three in this particular use case okay.

40
00:02:27,000 --> 00:02:30,000
So this is where we specifically get our LM model.

41
00:02:30,000 --> 00:02:36,000
So this is the fundamental thing that we should definitely do in each and every um, basically whenever

42
00:02:36,000 --> 00:02:39,000
you are developing most of the application, this is common.

43
00:02:39,000 --> 00:02:41,000
If you are really interested to just work with open source model.

44
00:02:41,000 --> 00:02:43,000
Grok is the grok.

45
00:02:43,000 --> 00:02:46,000
Infrastructure is the thing that you should really go ahead with.

46
00:02:46,000 --> 00:02:49,000
Okay, now let's work on some of the important things.

47
00:02:49,000 --> 00:02:55,000
So first of all, what I will do is that I will go ahead and install pip install bs4.

48
00:02:55,000 --> 00:02:55,000
Okay.

49
00:02:55,000 --> 00:03:00,000
So this is a library which is called as Beautifulsoup for this is used to extract a HTML page okay.

50
00:03:00,000 --> 00:03:02,000
Extract the content of the HTML page.

51
00:03:02,000 --> 00:03:05,000
So here you can see that the requirement is already satisfied.

52
00:03:05,000 --> 00:03:10,000
I have selected this v and v environment and make sure that whenever you do this installation, you

53
00:03:10,000 --> 00:03:14,000
update that in the requirement dot txt in this specific project.

54
00:03:14,000 --> 00:03:19,000
Okay, now the first thing what I am actually going to do over here is that I will quickly go ahead

55
00:03:19,000 --> 00:03:23,000
and upload or import some of the libraries.

56
00:03:23,000 --> 00:03:26,000
Okay, so I'm going to import something called as chroma.

57
00:03:26,000 --> 00:03:31,000
So here in this code I am going to go ahead and import chroma from lang underscore chroma.

58
00:03:31,000 --> 00:03:37,000
Along with this I am going to use a document loaders and will be importing from a web based loader.

59
00:03:37,000 --> 00:03:42,000
Because I want to read the content of a web page and we'll try to create a conversational queue and

60
00:03:42,000 --> 00:03:42,000
a chat bot.

61
00:03:42,000 --> 00:03:45,000
So that basically means I'll be having a website which will be my external data source.

62
00:03:45,000 --> 00:03:48,000
And from that I will try to chat through my chat bot.

63
00:03:48,000 --> 00:03:48,000
Okay.

64
00:03:49,000 --> 00:03:53,000
And whatever conversation we basically add, we will also add our message history.

65
00:03:53,000 --> 00:03:54,000
Right then.

66
00:03:54,000 --> 00:03:58,000
Along with this we are going to use this chat prompt template and you know where it is available inside

67
00:03:58,000 --> 00:03:58,000
prompts.

68
00:03:58,000 --> 00:04:03,000
Now instead of using OpenAI embeddings, I will remove this okay, because I will not be using OpenAI

69
00:04:03,000 --> 00:04:06,000
embeddings because it will charge you some amount of money.

70
00:04:06,000 --> 00:04:12,000
Instead, what I will do, I will go ahead and use this particular code wherein I'll go ahead and import

71
00:04:12,000 --> 00:04:13,000
my TF underscore token.

72
00:04:13,000 --> 00:04:19,000
If you remember, how did I import h underscore token along with this in my environment variable, this

73
00:04:19,000 --> 00:04:20,000
is also present okay.

74
00:04:20,000 --> 00:04:23,000
So from Langston underscore Huggingface import Huggingface embeddings.

75
00:04:23,000 --> 00:04:28,000
And this h f underscore token from where do you get it is you get it from your huggingface, uh, account.

76
00:04:28,000 --> 00:04:29,000
Right.

77
00:04:29,000 --> 00:04:31,000
So this is the embedding that I'm actually going to use, right?

78
00:04:31,000 --> 00:04:32,000
Huggingface embedding.

79
00:04:32,000 --> 00:04:34,000
So let's go ahead and execute this.

80
00:04:34,000 --> 00:04:34,000
Perfect.

81
00:04:34,000 --> 00:04:37,000
Till here I think everybody everything looks fine.

82
00:04:37,000 --> 00:04:41,000
And then after this we are going to also import recursive character text splitter so that we will be

83
00:04:41,000 --> 00:04:44,000
able to split our document and chunks okay.

84
00:04:44,000 --> 00:04:53,000
So once we upload or once we load all these things now we are also going to use two important functions

85
00:04:53,000 --> 00:04:53,000
also.

86
00:04:53,000 --> 00:04:53,000
Right.

87
00:04:53,000 --> 00:04:55,000
And this functions will discuss about it.

88
00:04:55,000 --> 00:04:58,000
So one is from long chain dot chains right.

89
00:04:58,000 --> 00:05:04,000
So inside this whenever we try to create a chain by using this library or by using this function we

90
00:05:04,000 --> 00:05:06,000
will be able to create chains okay.

91
00:05:06,000 --> 00:05:11,000
So create retrieval chain like let's say if I if you know about retrieval, retrieval is nothing but

92
00:05:11,000 --> 00:05:15,000
it is an interface to a vector store database.

93
00:05:15,000 --> 00:05:15,000
Right.

94
00:05:15,000 --> 00:05:20,000
And if I want to create a chain with this particular retriever, I have to use this particular function

95
00:05:20,000 --> 00:05:21,000
which is called as create retrieval chain.

96
00:05:21,000 --> 00:05:22,000
Okay.

97
00:05:22,000 --> 00:05:24,000
We'll discuss more about this as we go ahead.

98
00:05:24,000 --> 00:05:29,000
The next thing that we are going to use is something called as combined documents create stuff document

99
00:05:29,000 --> 00:05:30,000
chain okay.

100
00:05:30,000 --> 00:05:33,000
So as you go ahead, uh, in the future, right.

101
00:05:33,000 --> 00:05:37,000
In an upcoming, uh, sessions and modules, I will be talking about text summarization.

102
00:05:38,000 --> 00:05:42,000
Now when we discuss about text summarization, there we will be discussing about three types of text

103
00:05:42,000 --> 00:05:43,000
summarization.

104
00:05:43,000 --> 00:05:50,000
One is stuff, one stuff document chain, one is map reduce and one is refine in stuff document chain.

105
00:05:50,000 --> 00:05:55,000
What it does is that it combines all the documents and then it sends it to the prompt template.

106
00:05:55,000 --> 00:05:59,000
And that is why we will be using this specific uh uh, function.

107
00:05:59,000 --> 00:06:00,000
Okay.

108
00:06:00,000 --> 00:06:02,000
So I will show you everything with an example.

109
00:06:02,000 --> 00:06:02,000
Okay.

110
00:06:02,000 --> 00:06:05,000
Now let us go ahead and execute this okay.

111
00:06:05,000 --> 00:06:07,000
So here we have got executed it.

112
00:06:07,000 --> 00:06:11,000
Now I'm going to use a web based loader.

113
00:06:11,000 --> 00:06:14,000
And let's say I'm going to use this particular website okay.

114
00:06:14,000 --> 00:06:17,000
So let me just go ahead and quickly import BS4 also.

115
00:06:17,000 --> 00:06:22,000
So I will go ahead and write import BS4 because I'm going to use BS Beautifulsoup for now here inside

116
00:06:22,000 --> 00:06:25,000
this web loader I'm giving my web path.

117
00:06:25,000 --> 00:06:28,000
This is the URL that I'm trying to read the content from okay.

118
00:06:28,000 --> 00:06:33,000
And then here you'll be able to see I'm using a class which is called as post content from that page

119
00:06:33,000 --> 00:06:34,000
and post title.

120
00:06:34,000 --> 00:06:41,000
So if I go ahead and hit this page, let me just show you by opening this page over here, okay.

121
00:06:41,000 --> 00:06:44,000
Let me open my browser and let me just hit this page.

122
00:06:44,000 --> 00:06:45,000
Okay.

123
00:06:45,000 --> 00:06:49,000
So here you can see that this is the entire page content.

124
00:06:49,000 --> 00:06:52,000
And I will be using this particular page content as my external data source.

125
00:06:52,000 --> 00:06:52,000
Okay.

126
00:06:53,000 --> 00:06:56,000
Now from this what all fields I'm really interested in.

127
00:06:57,000 --> 00:07:02,000
So I am interested in getting the post content field, post title field and post header field.

128
00:07:02,000 --> 00:07:05,000
So if I go back if I just go ahead and do the inspect.

129
00:07:05,000 --> 00:07:12,000
So here you'll be able to see I'm getting the post title field, post content field and post header.

130
00:07:12,000 --> 00:07:14,000
Post header will also be somewhere here only.

131
00:07:14,000 --> 00:07:15,000
Yeah here.

132
00:07:15,000 --> 00:07:18,000
So all this information will try to extract from this.

133
00:07:19,000 --> 00:07:22,000
Now, uh, let me close this console.

134
00:07:22,000 --> 00:07:23,000
Let me go back to my code.

135
00:07:23,000 --> 00:07:24,000
Okay.

136
00:07:24,000 --> 00:07:28,000
Now, once I probably, uh, use this loader that is a web based loader.

137
00:07:28,000 --> 00:07:30,000
This is for the data ingestion from a website.

138
00:07:30,000 --> 00:07:34,000
I will go ahead and write loader dot load and I will load all the documents.

139
00:07:34,000 --> 00:07:34,000
Okay.

140
00:07:34,000 --> 00:07:37,000
So here I will be able to get my documents okay.

141
00:07:37,000 --> 00:07:42,000
So if I go ahead and execute this and see my documents you'll be able to see that one single document

142
00:07:42,000 --> 00:07:44,000
I'm able to get over here.

143
00:07:44,000 --> 00:07:47,000
And all the information is basically shown over here inside this particular page.

144
00:07:47,000 --> 00:07:47,000
Content.

145
00:07:47,000 --> 00:07:48,000
Perfect.

146
00:07:48,000 --> 00:07:53,000
Now usually what we do is that whenever once we get all this content, it is a good practice that we

147
00:07:53,000 --> 00:07:56,000
try to divide this entire document into chunks of document.

148
00:07:56,000 --> 00:08:02,000
The reason is very simple, because every LM models specifically have some kind of context size, right.

149
00:08:02,000 --> 00:08:06,000
So it is better that we try to break all this particular documents into chunks.

150
00:08:06,000 --> 00:08:10,000
So for this we will be creating a text splitter variable.

151
00:08:11,000 --> 00:08:14,000
And here we will be using some recursive character text splitter.

152
00:08:14,000 --> 00:08:16,000
And let me go ahead and write.

153
00:08:16,000 --> 00:08:19,000
Our chunk size is equal to 1000.

154
00:08:19,000 --> 00:08:24,000
And here we are going to basically use chunk underscore overlap with 200 okay.

155
00:08:25,000 --> 00:08:30,000
So once we actually do this uh then we are basically going to create our text splitter over here.

156
00:08:30,000 --> 00:08:36,000
The next step, uh, what we are going to do is that I'm just going to write splits is equal to splits

157
00:08:36,000 --> 00:08:42,000
is equal to text underscore splitter, dot split underscore documents.

158
00:08:42,000 --> 00:08:45,000
And whatever docs I have I will be giving over here.

159
00:08:45,000 --> 00:08:47,000
So it will basically go to do the splitting.

160
00:08:47,000 --> 00:08:53,000
And after doing the splitting I will go ahead and store this entirely in our vector store database.

161
00:08:53,000 --> 00:08:53,000
Right?

162
00:08:53,000 --> 00:09:02,000
So for that I will write from chroma dot from documents and I will give this documents is equal to whatever

163
00:09:02,000 --> 00:09:05,000
splits I'm actually getting over here okay.

164
00:09:05,000 --> 00:09:10,000
And here I'm going to apply my embedding which will be equal to this particular embeddings that I have

165
00:09:10,000 --> 00:09:11,000
actually used.

166
00:09:11,000 --> 00:09:13,000
That is nothing but huggingface embedding.

167
00:09:13,000 --> 00:09:13,000
Right.

168
00:09:13,000 --> 00:09:16,000
So here you can see that I have initialized this Huggingface embedding.

169
00:09:16,000 --> 00:09:16,000
Perfect.

170
00:09:16,000 --> 00:09:18,000
So till here everything looks good.

171
00:09:18,000 --> 00:09:22,000
Uh, this will finally give me my vector store okay.

172
00:09:22,000 --> 00:09:23,000
Vector store.

173
00:09:23,000 --> 00:09:26,000
And this we have already reported in our previous video.

174
00:09:26,000 --> 00:09:27,000
Right?

175
00:09:27,000 --> 00:09:33,000
And if I really want to convert this into a retriever, all I have to write is that I have to write

176
00:09:33,000 --> 00:09:36,000
something like vector store dot as retriever.

177
00:09:36,000 --> 00:09:37,000
Right.

178
00:09:37,000 --> 00:09:41,000
And here is your entire retriever right now.

179
00:09:42,000 --> 00:09:45,000
So this is the retriever, uh, over here.

180
00:09:46,000 --> 00:09:47,000
Okay, perfect.

181
00:09:47,000 --> 00:09:48,000
Let's see.

182
00:09:49,000 --> 00:09:52,000
So here is your entire retriever that we are able to get it.

183
00:09:52,000 --> 00:09:53,000
Okay.

184
00:09:53,000 --> 00:09:54,000
Now, this is fine.

185
00:09:54,000 --> 00:09:58,000
Now it's time that, uh, once we get this entire retriever.

186
00:09:58,000 --> 00:10:03,000
And this is for the vector store retriever basically means it is an interface with respect to vector

187
00:10:03,000 --> 00:10:03,000
store.

188
00:10:03,000 --> 00:10:07,000
So that whenever I ask any query, I should be able to get it from the vector store itself.

189
00:10:07,000 --> 00:10:08,000
Now this is important.

190
00:10:08,000 --> 00:10:10,000
And this you have actually seen it.

191
00:10:10,000 --> 00:10:12,000
Okay, now it's time.

192
00:10:12,000 --> 00:10:15,000
We will go ahead and define our prompt template okay.

193
00:10:16,000 --> 00:10:17,000
See the same thing what we are doing.

194
00:10:17,000 --> 00:10:21,000
Because after this I'm going to execute more things over here okay.

195
00:10:21,000 --> 00:10:22,000
So let me do one thing.

196
00:10:22,000 --> 00:10:25,000
Let me go ahead and define my system prompt.

197
00:10:25,000 --> 00:10:27,000
So this is my system prompt.

198
00:10:27,000 --> 00:10:29,000
I'm saying hey you are an assistant for question answer task.

199
00:10:29,000 --> 00:10:32,000
Use the following piece of retrieve context to answer the question.

200
00:10:32,000 --> 00:10:34,000
If you don't know the answer, say that you don't know.

201
00:10:34,000 --> 00:10:36,000
Use three sentence maximum.

202
00:10:36,000 --> 00:10:37,000
Keep the answer concise.

203
00:10:37,000 --> 00:10:37,000
Okay.

204
00:10:37,000 --> 00:10:39,000
And here a new new line.

205
00:10:39,000 --> 00:10:41,000
And here we will be passing the context.

206
00:10:41,000 --> 00:10:45,000
Now what this context will be getting replaced with I will talk about it okay.

207
00:10:45,000 --> 00:10:47,000
Then I'm going to take the system prompt.

208
00:10:47,000 --> 00:10:49,000
I'm going to use it as a chat prompt template.

209
00:10:49,000 --> 00:10:53,000
I'll again dot from underscore message I'll use I'll give a system prompt over here.

210
00:10:53,000 --> 00:10:57,000
And along with that human whatever input I'm giving with respect to the human I will pass it over here.

211
00:10:57,000 --> 00:11:01,000
So here we can actually go ahead and pass this particular input.

212
00:11:01,000 --> 00:11:05,000
And this is my system prompt, which is basically saying how the system should behave or how the LM

213
00:11:05,000 --> 00:11:11,000
model should act like, okay, now this is absolutely fine.

214
00:11:11,000 --> 00:11:17,000
Now my time, like now is the time that we need to attach this retriever along with the system prompt

215
00:11:17,000 --> 00:11:19,000
and along with the prompt.

216
00:11:19,000 --> 00:11:19,000
Right.

217
00:11:19,000 --> 00:11:24,000
So here we really need to create this particular chain because this retriever is pointing to the vector

218
00:11:24,000 --> 00:11:25,000
store database.

219
00:11:25,000 --> 00:11:29,000
This is my system prompt based on which I should be able to get my information.

220
00:11:29,000 --> 00:11:32,000
Um, which is this is specifically my prompt.

221
00:11:32,000 --> 00:11:34,000
So I need to probably go ahead and create a chain.

222
00:11:34,000 --> 00:11:37,000
Now the one thing is that how do I get this context information?

223
00:11:37,000 --> 00:11:46,000
Now to get this context information I will first of all go ahead and create my question answer chain

224
00:11:46,000 --> 00:11:46,000
okay.

225
00:11:46,000 --> 00:11:52,000
And here we will go ahead and write create stuff document chain.

226
00:11:52,000 --> 00:11:55,000
And here we are going to pass LM comma prompt.

227
00:11:55,000 --> 00:12:01,000
Now the reason I'm passing my LM model and prompt okay what this exactly is going to do okay.

228
00:12:01,000 --> 00:12:05,000
And that is the reason we have imported this create stuff document chain I've already told that this

229
00:12:05,000 --> 00:12:09,000
create stuff document chain is going to make sure that it will combine all the document and it will

230
00:12:09,000 --> 00:12:13,000
pass in the form of the specific context in that specific chain.

231
00:12:13,000 --> 00:12:17,000
When we create the chain with respect to system, prompt with respect.

232
00:12:17,000 --> 00:12:18,000
Sorry.

233
00:12:18,000 --> 00:12:20,000
With respect to this particular prompt, LM model.

234
00:12:20,000 --> 00:12:21,000
Right.

235
00:12:21,000 --> 00:12:24,000
You'll be seeing that when we use Create stuff document chain.

236
00:12:24,000 --> 00:12:27,000
Here I'm passing two parameters LM and prompt.

237
00:12:27,000 --> 00:12:31,000
Now this prompt right write is requiring some context information.

238
00:12:31,000 --> 00:12:32,000
Right.

239
00:12:32,000 --> 00:12:35,000
And this context information needs to be provided with respect to all the list of documents.

240
00:12:35,000 --> 00:12:38,000
Like what list of documents you have over here.

241
00:12:38,000 --> 00:12:38,000
Right.

242
00:12:38,000 --> 00:12:39,000
All this list of documents.

243
00:12:39,000 --> 00:12:40,000
Right.

244
00:12:40,000 --> 00:12:42,000
We did this particular split and all right.

245
00:12:42,000 --> 00:12:47,000
So that it will combine and it will send it to this particular prompt itself in the form of context.

246
00:12:47,000 --> 00:12:54,000
Right now, if we are doing this, the next step will be that we need to go ahead and create our entire

247
00:12:54,000 --> 00:12:54,000
rack chain.

248
00:12:54,000 --> 00:12:58,000
For creating the rack chain, we will be using a create retrieval chain.

249
00:12:58,000 --> 00:13:02,000
Now inside this create retrieval chain I'm going to make sure that I pass my retrieval.

250
00:13:03,000 --> 00:13:07,000
Along with this I also pass my question answer chain right.

251
00:13:07,000 --> 00:13:10,000
So this create stop document chain will also get passed over here.

252
00:13:11,000 --> 00:13:14,000
Now I have retriever I have LM model I have prompt.

253
00:13:14,000 --> 00:13:20,000
So these all are connected in a case of a retrieval chain itself where I will be able to get the response

254
00:13:20,000 --> 00:13:20,000
okay.

255
00:13:21,000 --> 00:13:26,000
Now once I execute this, now it's time that we will go ahead and execute it.

256
00:13:26,000 --> 00:13:32,000
So in order to execute it I'll just write RAC underscore chain dot invoke.

257
00:13:32,000 --> 00:13:34,000
And here we are going to give my input.

258
00:13:34,000 --> 00:13:42,000
So I know my key is basically a input over here I will write hey what is uh.

259
00:13:42,000 --> 00:13:43,000
Let me see.

260
00:13:43,000 --> 00:13:45,000
What was the page over here that we use?

261
00:13:45,000 --> 00:13:47,000
What is?

262
00:13:47,000 --> 00:13:48,000
I'll ask any question.

263
00:13:48,000 --> 00:13:50,000
What is, uh, task?

264
00:13:50,000 --> 00:13:51,000
What is self reflection?

265
00:13:51,000 --> 00:13:53,000
Okay, I'll ask this question.

266
00:13:53,000 --> 00:13:53,000
Okay.

267
00:13:53,000 --> 00:13:54,000
From here.

268
00:13:54,000 --> 00:13:57,000
So here, if I go ahead and ask what is this?

269
00:13:58,000 --> 00:14:00,000
I should be able to get my response.

270
00:14:00,000 --> 00:14:02,000
And my response is nothing, but it will be over here.

271
00:14:03,000 --> 00:14:09,000
And with respect to this, I will go ahead and write a response off and I'll display the response.

272
00:14:09,000 --> 00:14:12,000
So here you can see what is self reflection.

273
00:14:12,000 --> 00:14:13,000
And I am getting this entire context.

274
00:14:13,000 --> 00:14:19,000
See this context is basically having this particular documents, how it is combining all the documents

275
00:14:19,000 --> 00:14:20,000
and giving it to the prompt.

276
00:14:20,000 --> 00:14:23,000
It is because of this create stuff document chain.

277
00:14:23,000 --> 00:14:29,000
And this stuff document chain is basically used in text summarization, where it combines all the document

278
00:14:29,000 --> 00:14:30,000
and send it to the prompt.

279
00:14:30,000 --> 00:14:33,000
Write further from the prompt it goes to the LM model.

280
00:14:33,000 --> 00:14:33,000
Right.

281
00:14:33,000 --> 00:14:36,000
So here you can see this is my entire context.

282
00:14:36,000 --> 00:14:38,000
And from that I'm able to get the answer.

283
00:14:38,000 --> 00:14:43,000
So if I just want the answer over here I will go ahead and write a response of answer.

284
00:14:44,000 --> 00:14:44,000
Perfect.

285
00:14:45,000 --> 00:14:50,000
And here you can see self reflection is a mechanism that allows autonomous agents to improve iteratively

286
00:14:50,000 --> 00:14:53,000
by refining action and more information.

287
00:14:53,000 --> 00:15:00,000
Okay, now here you can see we have used built in chain constructors, which is called as create stuff

288
00:15:00,000 --> 00:15:03,000
document chain and create a retrieval chain.

289
00:15:03,000 --> 00:15:04,000
Right.

290
00:15:04,000 --> 00:15:08,000
But at the end of the day, the basic ingredients that you will be able to find is something like a

291
00:15:08,000 --> 00:15:09,000
retrieval prompt LMS.

292
00:15:09,000 --> 00:15:10,000
Right.

293
00:15:10,000 --> 00:15:16,000
Just a way to how to replace data with respect to this particular context and how to replace, um,

294
00:15:16,000 --> 00:15:19,000
how to probably get the answer with the help of this particular retrieval.

295
00:15:19,000 --> 00:15:19,000
Right.

296
00:15:19,000 --> 00:15:27,000
But now it's time that we start working towards adding chat history.

297
00:15:27,000 --> 00:15:27,000
Okay.

298
00:15:27,000 --> 00:15:31,000
So here we are going to basically go ahead and add our chat history.

299
00:15:31,000 --> 00:15:34,000
Now how to add this chat history right.

300
00:15:35,000 --> 00:15:38,000
Let's say I've asked a question over here right.

301
00:15:38,000 --> 00:15:39,000
What is self reflection.

302
00:15:39,000 --> 00:15:46,000
Now I will again go to go to this and ask another question over here.

303
00:15:46,000 --> 00:15:50,000
Let's say I'll say I'll just continue and ask.

304
00:15:50,000 --> 00:15:51,000
This is my invoke.

305
00:15:51,000 --> 00:15:55,000
And here I'm just going to take the input.

306
00:15:55,000 --> 00:16:04,000
Now instead of writing what is self reflection I'll say how how do we achieve it?

307
00:16:04,000 --> 00:16:04,000
Okay.

308
00:16:04,000 --> 00:16:06,000
So this is what I'm going to ask.

309
00:16:07,000 --> 00:16:10,000
Now here you can see achieving task decomposition.

310
00:16:10,000 --> 00:16:17,000
It is basically same okay I have I was trying to ask with respect to what is self reflection.

311
00:16:17,000 --> 00:16:21,000
But here it is probably giving me some other thing like how do we achieve it.

312
00:16:21,000 --> 00:16:24,000
Achieving task decomposition is is basically selecting.

313
00:16:24,000 --> 00:16:27,000
It is a tree of thoughts involves so and so.

314
00:16:27,000 --> 00:16:29,000
But I wanted the information with respect to self self-reflection.

315
00:16:30,000 --> 00:16:35,000
Now, the problem with this rank chain is that it's not able to understand the context, right?

316
00:16:35,000 --> 00:16:38,000
It is because it obviously does not have any kind of chat history.

317
00:16:38,000 --> 00:16:45,000
So what I will do now is that I will go ahead and show you how we can go ahead and add chat history,

318
00:16:45,000 --> 00:16:51,000
along with the prompt template, and how we can do the same task and see how things work with chat history

319
00:16:51,000 --> 00:16:51,000
over here.

320
00:16:51,000 --> 00:16:52,000
Okay.

321
00:16:52,000 --> 00:16:53,000
So let's go ahead.

322
00:16:53,000 --> 00:16:58,000
And uh, first of all, what I'm actually going to do is that for adding chat history, I will be importing

323
00:16:58,000 --> 00:16:59,000
from Lang Chain.

324
00:17:00,000 --> 00:17:01,000
Okay.

325
00:17:01,000 --> 00:17:06,000
And this chat history specifically for conversational Q and a chat bot I will go ahead and write lang

326
00:17:06,000 --> 00:17:12,000
chain dot chains import create history aware retriever.

327
00:17:13,000 --> 00:17:18,000
Now the kind of retriever that I am going to create or the chain of retriever that I'm actually going

328
00:17:18,000 --> 00:17:19,000
to create.

329
00:17:19,000 --> 00:17:21,000
See here also we created a chain of retriever only.

330
00:17:21,000 --> 00:17:22,000
Right.

331
00:17:22,000 --> 00:17:24,000
So this is nothing but create retrieval chain.

332
00:17:24,000 --> 00:17:24,000
Right.

333
00:17:24,000 --> 00:17:26,000
So this is nothing but a retrieval chain.

334
00:17:26,000 --> 00:17:31,000
But when we use this create history aware retriever chain, that basically means the retriever will

335
00:17:31,000 --> 00:17:36,000
also know about the history of information or history of conversation that has been basically made with

336
00:17:36,000 --> 00:17:37,000
the LM model.

337
00:17:37,000 --> 00:17:38,000
Okay.

338
00:17:38,000 --> 00:17:43,000
So we are going to probably create our chain with respect to with the help of this particular function.

339
00:17:43,000 --> 00:17:43,000
Okay.

340
00:17:43,000 --> 00:17:46,000
The next thing is that I will go ahead and use from lang chain.

341
00:17:48,000 --> 00:17:55,000
Underscore core dot prompts okay prompts I'm going to go ahead and import.

342
00:17:57,000 --> 00:18:00,000
Messages placeholder okay.

343
00:18:00,000 --> 00:18:05,000
And why I have to use this because I will be able to create a chat history.

344
00:18:05,000 --> 00:18:06,000
Uh.

345
00:18:06,000 --> 00:18:08,000
Uh, I'll just say that, okay.

346
00:18:08,000 --> 00:18:12,000
This can be a key value pairs, uh, where I will be storing all the information.

347
00:18:12,000 --> 00:18:12,000
Right.

348
00:18:12,000 --> 00:18:14,000
And that is where I will be using this message placeholder.

349
00:18:14,000 --> 00:18:16,000
Just wait for a second.

350
00:18:16,000 --> 00:18:20,000
I will just make sure that in the coding will be understanding where we use this message placeholder.

351
00:18:20,000 --> 00:18:21,000
Right.

352
00:18:21,000 --> 00:18:25,000
If we define a message placeholder with any variable in that particular variable, only all the chat

353
00:18:25,000 --> 00:18:27,000
history will be saved.

354
00:18:27,000 --> 00:18:36,000
Okay, this is what I really want to talk about Okay, now I will go ahead and create a prompt okay.

355
00:18:36,000 --> 00:18:41,000
So this is basically called as continuous uh contextualized queue system prompt okay.

356
00:18:41,000 --> 00:18:46,000
So I'll say given a chat history and the latest user question which might reference the question in

357
00:18:46,000 --> 00:18:51,000
the chat history, formulate the standalone question which can be understood without the chat history.

358
00:18:51,000 --> 00:18:54,000
Do not answer the question, just reformulate it if needed.

359
00:18:54,000 --> 00:18:56,000
Otherwise return it as it is.

360
00:18:57,000 --> 00:19:01,000
Okay, so this is the kind of, uh, prompt that I have actually created over here.

361
00:19:01,000 --> 00:19:04,000
I'm saying, hey, please, you have the chat history.

362
00:19:04,000 --> 00:19:09,000
You have the later user question which might reference the context in the chat history, formulate a

363
00:19:09,000 --> 00:19:11,000
standalone question which can be understood without the chat.

364
00:19:12,000 --> 00:19:15,000
Do not answer the question, just reformulated if needed.

365
00:19:15,000 --> 00:19:16,000
Otherwise return.

366
00:19:16,000 --> 00:19:16,000
That is okay.

367
00:19:16,000 --> 00:19:19,000
So simple uh, prompt that we are going to use over here.

368
00:19:19,000 --> 00:19:23,000
Now for this we will be using uh I will be creating a prompt template.

369
00:19:23,000 --> 00:19:26,000
Now see I have given the system message with respect to this.

370
00:19:26,000 --> 00:19:29,000
And this is where I'm going to use message placeholder.

371
00:19:29,000 --> 00:19:35,000
Now in this message placeholder I'm creating a uh variable like chat underscore history where all my

372
00:19:35,000 --> 00:19:37,000
history of conversation will be stored.

373
00:19:37,000 --> 00:19:38,000
Okay.

374
00:19:38,000 --> 00:19:40,000
And here is my human input okay.

375
00:19:40,000 --> 00:19:44,000
So guys now here you can see that I have created my chat prompt template.

376
00:19:44,000 --> 00:19:50,000
Now instead of using a retriever I will go ahead and create this, uh, history aware retriever.

377
00:19:50,000 --> 00:19:55,000
Right now I will be getting a new type of retriever instead of the previous retriever.

378
00:19:55,000 --> 00:20:01,000
And here I will write history aware retriever is nothing but create history aware uh, retriever.

379
00:20:01,000 --> 00:20:06,000
And here I'm going to provide the parameters LM, uh retriever.

380
00:20:06,000 --> 00:20:08,000
So let me just go ahead and write this retriever.

381
00:20:08,000 --> 00:20:12,000
Along with this, I will also go ahead and provide my contextual cue prompt.

382
00:20:12,000 --> 00:20:12,000
Okay.

383
00:20:13,000 --> 00:20:13,000
Q prompt.

384
00:20:14,000 --> 00:20:20,000
In short, what we are doing over here is that we are upgrading this retriever to history aware retriever,

385
00:20:20,000 --> 00:20:27,000
which will also be able to retrieve the results, uh, or retrieve the uh, get the results from the

386
00:20:27,000 --> 00:20:28,000
vector store DB.

387
00:20:28,000 --> 00:20:33,000
Considering considering the considering the chat history.

388
00:20:33,000 --> 00:20:35,000
Okay, that is what we are basically going to do.

389
00:20:35,000 --> 00:20:40,000
And now if I just go ahead and display this, you'll be able to see this entire thing over here right

390
00:20:40,000 --> 00:20:41,000
now.

391
00:20:41,000 --> 00:20:43,000
This is perfectly fine till here.

392
00:20:43,000 --> 00:20:44,000
Everything looks good.

393
00:20:44,000 --> 00:20:44,000
Okay.

394
00:20:44,000 --> 00:20:48,000
And, uh, we are going on very on, very on, like on point.

395
00:20:48,000 --> 00:20:49,000
Okay.

396
00:20:49,000 --> 00:20:51,000
Now it's time to go ahead and create the chain.

397
00:20:51,000 --> 00:20:51,000
Okay.

398
00:20:51,000 --> 00:20:53,000
Now for to create the chain.

399
00:20:53,000 --> 00:20:58,000
What I will actually do is that again, we will go ahead and write the same question answer chain.

400
00:20:58,000 --> 00:21:02,000
Here we are going to use the create stuff document chain okay.

401
00:21:02,000 --> 00:21:10,000
And here we are basically going to use my LM comma QA, uh QA or what is the prompt that I had actually

402
00:21:10,000 --> 00:21:14,000
used over here, which is nothing, but it is a contextualized Q prompt.

403
00:21:14,000 --> 00:21:18,000
And instead of this I can go ahead and create the new prompt over here okay.

404
00:21:18,000 --> 00:21:20,000
And I will name it as Q a prompt.

405
00:21:20,000 --> 00:21:20,000
Okay.

406
00:21:20,000 --> 00:21:22,000
The same prompt, only nothing different.

407
00:21:22,000 --> 00:21:24,000
So I'll use this Q a prompt okay.

408
00:21:24,000 --> 00:21:25,000
It is same thing right.

409
00:21:25,000 --> 00:21:27,000
So here I'm going to use this particular prompt.

410
00:21:27,000 --> 00:21:32,000
And along with this prompt what I will do in the next step I will go ahead and create my action.

411
00:21:32,000 --> 00:21:38,000
But this time my rack chain will be having this create retrieval uh, chain.

412
00:21:38,000 --> 00:21:42,000
I will go ahead and create this retrieval chain inside this instead of just giving the retrieval.

413
00:21:43,000 --> 00:21:46,000
Uh, I will be giving my history aware retrieval.

414
00:21:46,000 --> 00:21:47,000
Comma.

415
00:21:48,000 --> 00:21:49,000
The question answer chain.

416
00:21:49,000 --> 00:21:52,000
See, uh, sorry, the question answer chain.

417
00:21:52,000 --> 00:21:58,000
If you remember previously when we created this create retrieval chain what all inputs we specifically

418
00:21:58,000 --> 00:21:58,000
give.

419
00:21:58,000 --> 00:22:02,000
See over here we gave the retrieval we gave question answer chain.

420
00:22:02,000 --> 00:22:07,000
But this time we are giving history of a retriever and question answer chain.

421
00:22:07,000 --> 00:22:10,000
Now it's time that we can go ahead and use this rack chain.

422
00:22:10,000 --> 00:22:18,000
Okay, now let's go ahead and add it and see what exactly action will basically do and what all things

423
00:22:18,000 --> 00:22:19,000
we can specifically do it okay.

424
00:22:19,000 --> 00:22:28,000
So first of all what I will do I will go ahead and import from long chain underscore core dot messages

425
00:22:29,000 --> 00:22:34,000
import I message comma human message.

426
00:22:34,000 --> 00:22:37,000
These are the list of messages that we usually append here.

427
00:22:37,000 --> 00:22:40,000
I'm just going to go ahead and create my chart history.

428
00:22:40,000 --> 00:22:45,000
And remember the variable name should be same like how we have defined over there right in the message

429
00:22:45,000 --> 00:22:45,000
placeholder.

430
00:22:45,000 --> 00:22:51,000
Then my first question will be something like question is equal to I will go ahead and write what is

431
00:22:51,000 --> 00:22:52,000
okay.

432
00:22:52,000 --> 00:22:55,000
What was the question that I had asked earlier?

433
00:22:55,000 --> 00:22:58,000
Uh, what is self reflection?

434
00:22:58,000 --> 00:22:59,000
Okay.

435
00:22:59,000 --> 00:23:05,000
So from that particular page I'm just going to ask this particular question what is self reflection?

436
00:23:06,000 --> 00:23:06,000
Okay.

437
00:23:07,000 --> 00:23:09,000
So this basically becomes my question okay.

438
00:23:09,000 --> 00:23:13,000
Now if I want to execute it.

439
00:23:13,000 --> 00:23:15,000
So let's say this will be my response one.

440
00:23:16,000 --> 00:23:19,000
And inside this response one I will go ahead and use the same chain.

441
00:23:19,000 --> 00:23:21,000
I will write dot invoke okay.

442
00:23:21,000 --> 00:23:24,000
And let me just go ahead and give my input.

443
00:23:24,000 --> 00:23:29,000
The first input will be nothing, but it will be go question.

444
00:23:29,000 --> 00:23:30,000
Okay.

445
00:23:30,000 --> 00:23:32,000
And the second input will be nothing but chat history.

446
00:23:32,000 --> 00:23:33,000
Okay.

447
00:23:33,000 --> 00:23:37,000
So here we are going to basically go ahead and use chat underscore history okay.

448
00:23:37,000 --> 00:23:40,000
Which will be assigned to my chat history.

449
00:23:40,000 --> 00:23:41,000
Right.

450
00:23:41,000 --> 00:23:43,000
Whatever chat history we are creating over here.

451
00:23:43,000 --> 00:23:45,000
And this is the message placeholder that we are giving.

452
00:23:45,000 --> 00:23:45,000
Right.

453
00:23:45,000 --> 00:23:47,000
And here only we are going to save all our chat history.

454
00:23:48,000 --> 00:23:51,000
Now along with this, what I will do since this is a list right?

455
00:23:51,000 --> 00:23:55,000
I'm going to go ahead and append this with every conversation that I have.

456
00:23:55,000 --> 00:24:00,000
So here I'm going to write extend and let's say here I create a list.

457
00:24:00,000 --> 00:24:02,000
Here I'm just going to add my human message.

458
00:24:02,000 --> 00:24:07,000
So the content will be assigned to this particular question, whatever question I'm actually giving.

459
00:24:07,000 --> 00:24:13,000
And along with this I will go ahead and write I message I message because we need to append it right.

460
00:24:13,000 --> 00:24:16,000
And content is equal to nothing but response.

461
00:24:16,000 --> 00:24:18,000
Whatever response one we are getting.

462
00:24:18,000 --> 00:24:24,000
And inside that there will be a variable which is called as answer because see, whenever we get, uh,

463
00:24:24,000 --> 00:24:27,000
the output from the retriever, you'll be seeing that there is a answer key.

464
00:24:27,000 --> 00:24:28,000
Okay.

465
00:24:28,000 --> 00:24:31,000
So we need to append this in the chat history over here okay.

466
00:24:31,000 --> 00:24:36,000
So once it gets appended or extended, expanded basically means it will just get added at the last.

467
00:24:36,000 --> 00:24:36,000
Okay.

468
00:24:36,000 --> 00:24:43,000
Now similarly once I extend execute this I will go ahead and write my question to the question.

469
00:24:43,000 --> 00:24:54,000
Two will be that how or what are the common ways of achieving it.

470
00:24:54,000 --> 00:24:57,000
Okay, I'll just go ahead and write it.

471
00:24:57,000 --> 00:25:01,000
Or let's say I'll just go ahead and ask a question.

472
00:25:01,000 --> 00:25:02,000
Tell me more about it.

473
00:25:02,000 --> 00:25:05,000
Tell me more about it.

474
00:25:05,000 --> 00:25:05,000
Okay.

475
00:25:05,000 --> 00:25:06,000
Something like this.

476
00:25:07,000 --> 00:25:10,000
So this will be my second question that I'm going to go ahead and write.

477
00:25:10,000 --> 00:25:14,000
Now I will go ahead and write in this question dot invoke.

478
00:25:14,000 --> 00:25:21,000
And here the same thing I'm going to pass right with respect to this history, I'll get my response

479
00:25:21,000 --> 00:25:21,000
to.

480
00:25:22,000 --> 00:25:25,000
So this will basically be my response to.

481
00:25:26,000 --> 00:25:31,000
And now I will go ahead and print my response to.

482
00:25:33,000 --> 00:25:36,000
Response to off my answer.

483
00:25:37,000 --> 00:25:38,000
Okay.

484
00:25:38,000 --> 00:25:41,000
Now I think it should be able to understand the context and it should be able to give the answer.

485
00:25:41,000 --> 00:25:42,000
Let's see.

486
00:25:43,000 --> 00:25:44,000
So self mechanism.

487
00:25:44,000 --> 00:25:49,000
Self reflection is a mechanism that allows autonomous agents to improve iteratively by refining past

488
00:25:49,000 --> 00:25:51,000
action decisions and correcting previous mistakes.

489
00:25:51,000 --> 00:25:54,000
He initially asked what is self reflection?

490
00:25:54,000 --> 00:25:57,000
Then I told tell me more about this and this is what we are basically printing.

491
00:25:57,000 --> 00:26:02,000
Okay, if I go ahead and see with respect to the chat history, you'll also be able to see that first

492
00:26:02,000 --> 00:26:05,000
one is this particular message that we got right.

493
00:26:05,000 --> 00:26:07,000
Errors are inevitable.

494
00:26:07,000 --> 00:26:07,000
Okay.

495
00:26:07,000 --> 00:26:12,000
And second time when I asked tell me more about it, it was able to understand the context and it was

496
00:26:12,000 --> 00:26:13,000
able to give us the answer.

497
00:26:14,000 --> 00:26:19,000
So, uh, I hope, uh, you're able to understand this amazing thing.

498
00:26:19,000 --> 00:26:20,000
Okay.

499
00:26:20,000 --> 00:26:24,000
Now, uh, there are some more things that you can actually do with, uh, whenever you are working

500
00:26:24,000 --> 00:26:24,000
with LCL.

501
00:26:25,000 --> 00:26:25,000
Right.

502
00:26:25,000 --> 00:26:31,000
Uh, that is adding, uh, chat history with respect to session IDs and all that we have already discussed.

503
00:26:31,000 --> 00:26:37,000
So let me quickly copy and paste some of the code over here so that you will be able to understand.

504
00:26:37,000 --> 00:26:43,000
So if I go ahead and write this, if you remember this base chat history in our runnable with message

505
00:26:43,000 --> 00:26:44,000
history and chat history.

506
00:26:44,000 --> 00:26:44,000
Right.

507
00:26:44,000 --> 00:26:45,000
We used it.

508
00:26:45,000 --> 00:26:49,000
So here you can create this particular session.

509
00:26:49,000 --> 00:26:53,000
And based on the session ID you will be able to retrieve all the chat history.

510
00:26:53,000 --> 00:26:55,000
The output key will be the answer.

511
00:26:55,000 --> 00:26:57,000
The input key is nothing but input, right?

512
00:26:57,000 --> 00:27:03,000
So here if I go ahead and execute this, here is nothing but it is your entire conversational chain.

513
00:27:03,000 --> 00:27:04,000
In short, right.

514
00:27:04,000 --> 00:27:09,000
Now I can go ahead and invoke anything based on my session ID.

515
00:27:09,000 --> 00:27:14,000
So here I will go ahead and write conversation rack uh chain dot invoke.

516
00:27:14,000 --> 00:27:16,000
What is this composition is another one question.

517
00:27:16,000 --> 00:27:17,000
I can write my config.

518
00:27:17,000 --> 00:27:22,000
This config is going to make sure that it will store all the information with respect to the session

519
00:27:22,000 --> 00:27:24,000
ID, and then we can also get the answer right.

520
00:27:24,000 --> 00:27:29,000
So here you can see our task decomposition is the same thing.

521
00:27:29,000 --> 00:27:31,000
And here you can probably see the same answer okay.

522
00:27:32,000 --> 00:27:38,000
Now similarly if I go ahead and use the same session like I'll go ahead and ask what are the common

523
00:27:38,000 --> 00:27:39,000
ways of doing it.

524
00:27:39,000 --> 00:27:41,000
So I'm actually talking about task decomposition.

525
00:27:41,000 --> 00:27:44,000
The reason is why I have actually used the same session ID.

526
00:27:44,000 --> 00:27:46,000
You can go ahead and see the answer.

527
00:27:46,000 --> 00:27:51,000
Now it will be able to understand that yes, we are fine talking about this, uh, task decomposition.

528
00:27:51,000 --> 00:27:55,000
So here you can say that according to context, most common ways of doing task decomposition includes

529
00:27:55,000 --> 00:27:57,000
NN1 something new line characters.

530
00:27:57,000 --> 00:28:00,000
Using a large language model, everything is basically displayed.

531
00:28:00,000 --> 00:28:01,000
Let me see.

532
00:28:01,000 --> 00:28:01,000
One more thing.

533
00:28:01,000 --> 00:28:03,000
Uh, did I print okay?

534
00:28:03,000 --> 00:28:05,000
Yes, I printed the right response.

535
00:28:05,000 --> 00:28:11,000
So yes, this was all about the conversational Q&A chat bot with memory, right?

536
00:28:11,000 --> 00:28:12,000
With chat history.

537
00:28:12,000 --> 00:28:14,000
And I hope you are able to understand this particular question.

538
00:28:14,000 --> 00:28:17,000
This is how we basically create a history aware retriever.

539
00:28:17,000 --> 00:28:20,000
And you can actually use all these things.

540
00:28:20,000 --> 00:28:20,000
Right?

541
00:28:20,000 --> 00:28:23,000
So yeah, uh, this was it for my side.

542
00:28:23,000 --> 00:28:28,000
I hope you like this particular video and uh, yeah, I will see you all in the next video.

543
00:28:28,000 --> 00:28:29,000
Have a great day.

544
00:28:29,000 --> 00:28:30,000
Thank you all.

545
00:28:30,000 --> 00:28:30,000
Take care.

546
00:28:30,000 --> 00:28:30,000
Bye bye.

