1
00:00:03,930 --> 00:00:15,450
Okay, so in this, uh, chapter, we are going to, uh, work in a another basic rack application,

2
00:00:15,450 --> 00:00:24,000
but in this case with a little bit more sophisticated vector database, we are going to use the database

3
00:00:24,000 --> 00:00:28,860
Deep Lake, which is a database vector database.

4
00:00:28,860 --> 00:00:36,270
Uh, that is going to ask a credentials, uh, from us in order to be able to connect with it.

5
00:00:36,270 --> 00:00:42,210
This is the most common way to operate in a professional way with vector databases, as you will see.

6
00:00:42,240 --> 00:00:49,980
And this is a process that is going to be, uh, used whenever you use a professional vector database

7
00:00:49,980 --> 00:00:52,770
pinecone, V8, Redis, whatever.

8
00:00:52,770 --> 00:00:59,400
So the process you are going to see in the exercise is going to be very similar to the process you are

9
00:00:59,400 --> 00:01:07,230
going to, uh, perform whenever you have to work with a professional vector database.

10
00:01:08,270 --> 00:01:16,100
So the problem we are going to solve in this exercise is to test a drug application with a different

11
00:01:16,100 --> 00:01:19,640
vector database and access credentials.

12
00:01:19,640 --> 00:01:28,580
And later we are going to add to the external document A used by the RAC application, a data to the

13
00:01:28,580 --> 00:01:31,280
external document used by the RAC applications.

14
00:01:31,280 --> 00:01:33,470
Okay, so a couple of things.

15
00:01:33,470 --> 00:01:38,990
We are already familiar with a basic applications.

16
00:01:38,990 --> 00:01:40,310
We have done it before.

17
00:01:40,310 --> 00:01:48,770
In this case we are going to go a little bit, uh, further and we are going to uh connect with a little

18
00:01:48,770 --> 00:01:52,400
bit more sophisticated vector database in this case.

19
00:01:52,670 --> 00:01:53,600
So.

20
00:01:54,440 --> 00:01:58,430
As I was saying, we are going to use a deep Lake vector database.

21
00:01:58,430 --> 00:02:04,010
Deep Lake is a database owned by the company Active Loop.

22
00:02:04,010 --> 00:02:06,440
This is how you write Active Loop.

23
00:02:06,440 --> 00:02:07,940
So you go to their website.

24
00:02:07,940 --> 00:02:16,550
You will see how you can create a free account there, how you can get, you know, your token in order

25
00:02:16,550 --> 00:02:24,680
to start using the database and how you can get the ID a the organization ID that is a requirement for

26
00:02:24,680 --> 00:02:26,630
you to start using the databases, etc..

27
00:02:26,630 --> 00:02:35,210
So all this you have to do in their website whatever question doubt you have regarding active loop or

28
00:02:35,210 --> 00:02:39,080
vector database or whatever operations we are doing here.

29
00:02:39,080 --> 00:02:47,960
Remember the usual places you can find answers chat, GPT, Google, StackOverflow, etc. etc..

30
00:02:47,960 --> 00:02:52,910
Okay, so this is the process we are going to follow in this exercise.

31
00:02:52,910 --> 00:02:53,750
At the beginning.

32
00:02:53,750 --> 00:03:01,880
As always, we uh, load the credentials in order to use the OpenAI API.

33
00:03:01,880 --> 00:03:02,480
Okay.

34
00:03:02,480 --> 00:03:07,280
So as you remember we have this credentials in the dot env file.

35
00:03:07,310 --> 00:03:10,280
Do you remember this was explained chapter seven.

36
00:03:10,430 --> 00:03:15,680
So the first thing we are going to do is to load the deep lake credentials.

37
00:03:15,680 --> 00:03:21,680
So we have our deep lake credentials in our dot m file.

38
00:03:21,680 --> 00:03:22,220
Okay.

39
00:03:22,220 --> 00:03:27,260
This is a confidential file that you have already created probably.

40
00:03:27,260 --> 00:03:32,150
And uh, this is what, uh, every uh, software engineer uses.

41
00:03:32,150 --> 00:03:39,200
Whenever we work with applications, we, we create a dot env file, and there we store all kind of

42
00:03:39,200 --> 00:03:40,850
passwords, credentials, etc..

43
00:03:40,850 --> 00:03:51,350
So what we are seeing here is, uh, getting the active look token and the active look or ID from the

44
00:03:51,350 --> 00:03:52,850
dot m file.

45
00:03:53,120 --> 00:04:01,700
The next thing we do is we, uh, name the new database, uh, we are going to create.

46
00:04:01,700 --> 00:04:09,680
So in this case, uh, this is the name we are giving to the data set we are creating.

47
00:04:09,950 --> 00:04:13,310
And, uh, we create, we load the dependencies.

48
00:04:13,310 --> 00:04:20,750
In this case, you are already familiar with OpenAI embeddings with recursive characters, uh, text

49
00:04:20,750 --> 00:04:27,710
splitter and with retrieval QA because this, uh, we have already used in previous exercises whenever

50
00:04:27,710 --> 00:04:30,350
we have created a basic application.

51
00:04:30,350 --> 00:04:40,760
But in this case we are using a new module called Deep Lake in order to connect long chain with this

52
00:04:40,760 --> 00:04:41,510
vector store.

53
00:04:41,510 --> 00:04:49,100
So as easy as that, once we have these dependencies loaded or these modules or plugins, whatever you

54
00:04:49,100 --> 00:04:57,020
want to call them, what we do is, uh, pay attention to the external knowledge document we are going

55
00:04:57,020 --> 00:04:57,440
to use.

56
00:04:57,440 --> 00:05:08,390
So remember, a rack application is an application that provides the LM foundation model with an additional

57
00:05:08,390 --> 00:05:09,530
information.

58
00:05:09,530 --> 00:05:17,870
So as you know, ChatGPT for example, is trained with all the information and that is on the internet

59
00:05:17,870 --> 00:05:25,070
or more or less all of all of it, but it doesn't know anything about your company, your book, your

60
00:05:25,070 --> 00:05:26,570
user guide, whatever.

61
00:05:26,570 --> 00:05:34,010
All the private documents you have are not included in the knowledge that ChatGPT has right now.

62
00:05:34,010 --> 00:05:38,540
And the way we can provide this knowledge and we can start interacting.

63
00:05:38,540 --> 00:05:45,740
Interacting with this knowledge via a LM application is uh, with a rack application, right?

64
00:05:45,740 --> 00:05:51,290
So the first thing we do is we need to, uh, uh, we need to.

65
00:05:52,100 --> 00:05:56,900
Tell the application what is our where is our private document.

66
00:05:57,380 --> 00:06:06,800
So you can use a PDF as a private document or a text file or whatever word document, whatever kind

67
00:06:06,800 --> 00:06:07,490
of file.

68
00:06:07,700 --> 00:06:12,320
In this case, we are going to use a very simple document.

69
00:06:12,320 --> 00:06:18,740
We are going to create a list of only two, uh, items.

70
00:06:18,890 --> 00:06:27,410
The first item we are going to talk about, one, uh, curious fact about United States in this case

71
00:06:27,410 --> 00:06:32,330
is, uh, the date where United States celebrates the Independence Day.

72
00:06:32,330 --> 00:06:40,310
And in the same chunk of information we tell them, however, the Declaration of Independence was passed

73
00:06:40,310 --> 00:06:43,010
on July the 2nd.

74
00:06:43,010 --> 00:06:43,670
Right.

75
00:06:43,670 --> 00:06:48,950
And it was only officially ratified on, uh, July the 4th.

76
00:06:48,950 --> 00:06:52,250
So this is the special information.

77
00:06:52,250 --> 00:06:53,360
We are included here.

78
00:06:53,360 --> 00:06:59,990
And this is something that we are going to be asking about when we test this application.

79
00:06:59,990 --> 00:07:06,920
The second piece of information we we include in this private document is okay.

80
00:07:06,920 --> 00:07:13,160
The first documented European to arrive in North America was the Spaniard Juan Ponce de Leon.

81
00:07:13,160 --> 00:07:18,470
Okay, so this is the external knowledge document we are going to use.

82
00:07:18,470 --> 00:07:21,860
In this case, we are creating right now the external document.

83
00:07:21,860 --> 00:07:27,980
But you could use, you know, a document that you have already in your company or, or personal use

84
00:07:27,980 --> 00:07:28,640
or whatever.

85
00:07:28,880 --> 00:07:37,640
So remember how we work in a large application, the first thing we do is to divide the document in

86
00:07:37,640 --> 00:07:45,530
smaller chunks of text in order to this is the document, and we are going to divide this document in

87
00:07:45,650 --> 00:07:47,840
smaller chunks of text.

88
00:07:47,960 --> 00:07:52,310
In order to do that, we use the text splitter.

89
00:07:52,310 --> 00:07:55,850
So the first thing we do is we configure the text splitter.

90
00:07:55,850 --> 00:07:59,930
And the second we create the document chunks.

91
00:08:00,020 --> 00:08:07,850
And if we print the length of this document chunks we will see that we have two chunks.

92
00:08:07,850 --> 00:08:15,170
So we have divided this document into different chunks or fragments of text.

93
00:08:15,800 --> 00:08:24,290
As you remember, the next operation we have to, uh, perform in a drag application is to transform

94
00:08:24,290 --> 00:08:30,320
this chunks of text into numbers, what we call the embeddings.

95
00:08:30,320 --> 00:08:34,100
Because as you remember, the computer works.

96
00:08:34,750 --> 00:08:38,650
Much better with numbers than with letters, right?

97
00:08:38,650 --> 00:08:42,820
So we convert our chunks of text into embeddings.

98
00:08:42,820 --> 00:08:44,440
How do we do it?

99
00:08:44,470 --> 00:08:47,530
Same thing we did in the previous exercise.

100
00:08:47,530 --> 00:08:55,570
We are going to use the OpenAI embeddings module in order to build these embeddings, and we are going

101
00:08:55,570 --> 00:09:01,480
to use it in the a that database we have created.

102
00:09:01,480 --> 00:09:12,100
So using the Deep Lake module we are going to tell uh, our application where is our uh database and

103
00:09:12,100 --> 00:09:20,710
what uh, way are we going to use in order to transform the chunks of text into a number.

104
00:09:20,710 --> 00:09:23,440
So how are we going to build the embeddings?

105
00:09:23,440 --> 00:09:24,070
Okay.

106
00:09:24,070 --> 00:09:32,260
So in this case what you are actually doing is to create or, you know, start including data in your

107
00:09:32,260 --> 00:09:33,850
vector database.

108
00:09:33,850 --> 00:09:43,360
Since this is not the first time I execute this command, the the notebook is telling me, okay, this

109
00:09:43,360 --> 00:09:45,640
database already exists.

110
00:09:45,730 --> 00:09:48,070
Uh, it's already there.

111
00:09:48,070 --> 00:09:53,170
If you are creating a new one, you are going to have a different message here telling you, okay,

112
00:09:53,170 --> 00:10:00,880
your database has been created or whatever, pay attention because here we are including in the pip

113
00:10:00,880 --> 00:10:03,220
install the Blake enterprise.

114
00:10:03,220 --> 00:10:11,380
So this is a command that we will need in order to execute this command okay.

115
00:10:11,380 --> 00:10:19,420
If you don't have the Blake Enterprise installed in your virtual environment you will need to do that.

116
00:10:19,420 --> 00:10:20,350
You can do it.

117
00:10:20,350 --> 00:10:24,970
You can do you can install this module from the Jupyter notebook.

118
00:10:24,970 --> 00:10:28,720
And in order to do that you have to write this one.

119
00:10:28,720 --> 00:10:36,250
What I have here is this commented because since I already have it, uh, install and I don't want to

120
00:10:36,250 --> 00:10:38,560
install it again, it's not necessary for me.

121
00:10:38,560 --> 00:10:46,120
But in your case, if it is the first time you install it, you can do this and then, uh, you know,

122
00:10:46,120 --> 00:10:54,160
uh, caps and enter and you have a, you have that installed or you can do it, uh, via the terminal.

123
00:10:54,160 --> 00:11:00,100
So you can write pip install, uh, Deep Lake Enterprise in the terminal.

124
00:11:00,100 --> 00:11:03,250
And it will be installed in the virtual environment as well.

125
00:11:03,250 --> 00:11:11,470
Remember that we use this exclamation mark in order to tell the notebook that we are going to do a terminal

126
00:11:11,470 --> 00:11:14,440
operation from the notebook.

127
00:11:14,440 --> 00:11:14,770
Okay.

128
00:11:14,770 --> 00:11:22,210
So here we are creating the well we are entering content in the database okay.

129
00:11:22,630 --> 00:11:23,350
With this.

130
00:11:23,350 --> 00:11:30,430
With this one we are going to load the chunks and transform that the chunks into embeddings okay.

131
00:11:30,430 --> 00:11:32,530
With this operation here.

132
00:11:32,530 --> 00:11:35,650
And once we have that ready we can start.

133
00:11:35,650 --> 00:11:37,900
You know testing our application.

134
00:11:37,900 --> 00:11:39,760
It's as easy as that.

135
00:11:40,370 --> 00:11:48,770
So we create our LM instance and we create our retrieval QA chain.

136
00:11:49,040 --> 00:11:57,800
Uh, so we we give the, the the model we are using the chain type stuff as you know, is the most,

137
00:11:57,800 --> 00:12:03,890
uh, popular, the most used type of chain we are going to use for retrieval QA.

138
00:12:03,920 --> 00:12:09,680
You have a couple of alternatives here, but usually staff is the most common.

139
00:12:09,680 --> 00:12:15,650
You can go to the documentation to look about the other 2 or 3 you have.

140
00:12:15,650 --> 00:12:22,070
And then we have to tell, uh, how are we going to retrieve the information in this case we enter the

141
00:12:22,070 --> 00:12:27,050
database, we have created the function as retriever and we are ready to go.

142
00:12:27,470 --> 00:12:34,700
In order to start asking questions to our private document, we run the new chain.

143
00:12:35,180 --> 00:12:43,490
And in this case, as you can see, we are asking, uh, the, the, the application about, uh, you

144
00:12:43,490 --> 00:12:50,960
know, the actual date when the Declaration of Independence in United States was actually passed, as

145
00:12:50,960 --> 00:12:55,160
we know it was July the 2nd, not July the 4th.

146
00:12:55,640 --> 00:13:06,470
Uh, so then we have here an additional, uh, operation, how to add data to a vector database that

147
00:13:06,470 --> 00:13:07,850
we have already created.

148
00:13:07,850 --> 00:13:14,810
So let's see that you have new information you want to add to your private knowledge document.

149
00:13:14,810 --> 00:13:15,230
Right.

150
00:13:15,230 --> 00:13:17,360
So this is the way you can do that.

151
00:13:17,810 --> 00:13:21,410
So this is the additional information we can include to our document.

152
00:13:21,410 --> 00:13:28,790
And what we are going to do is we are going to add this document directly to our vector database.

153
00:13:28,790 --> 00:13:35,480
In order to do that, the first thing we do is we use the text splitter to divide this information into

154
00:13:35,480 --> 00:13:36,500
text chunks.

155
00:13:36,500 --> 00:13:44,540
And second, we use the add documents function in order to transform the text chunks into numbers into

156
00:13:44,540 --> 00:13:45,230
embeddings.

157
00:13:45,230 --> 00:13:45,890
Okay.

158
00:13:45,890 --> 00:13:54,860
So uh, once you have this, you can start asking questions about the new information you have added

159
00:13:54,860 --> 00:13:56,420
to the vector database.

160
00:13:56,420 --> 00:14:00,560
In this case, we are asking what is the largest state in the US.

161
00:14:00,560 --> 00:14:02,990
And here we are saying that this is Alaska.

162
00:14:02,990 --> 00:14:10,460
And in the second piece of uh, knowledge we enter here, what we are saying is that in some states

163
00:14:10,460 --> 00:14:17,660
and in some cities, big cities of the United States, you have one special style of pizza, right?

164
00:14:17,660 --> 00:14:19,880
So we are giving this information here.

165
00:14:19,880 --> 00:14:26,120
And in the last question we are asking, tell me three states with their own style of pizza.

166
00:14:26,120 --> 00:14:33,170
In this case, you can see that the application is giving us the right answer, but just in part because

167
00:14:33,170 --> 00:14:38,000
it is giving us a states that have a particular pizza style.

168
00:14:38,000 --> 00:14:47,000
But in this case, it is, uh, giving us five, uh, states, not three as we, uh, as we asked.

169
00:14:47,000 --> 00:14:56,090
So we see that we will, uh, face, you know, this kind of, uh, not 100% accurate responses from

170
00:14:56,450 --> 00:14:57,350
applications.

171
00:14:57,350 --> 00:15:01,610
And we will learn ways to make them more and more accurate.

172
00:15:01,610 --> 00:15:02,180
Okay.

173
00:15:02,180 --> 00:15:11,390
So in this chapter we have seen how to create a basic lag application with a little bit more sophisticated

174
00:15:11,390 --> 00:15:12,530
database.

