跟讀練習: Master AI in 60 Minutes with Vector Databases! - 通過影片學習英語口語
選擇練習模式
每種模式都會開啟全屏練習室。
712 句
如果句子過短或過長,請點選 Edit 進行調整。
1
All right, so in this video, we are going to continue talking about vector databases.
2
So we are now going to start pulling all the things we've learned so far and put it all together.
3
So the idea is that we're going to take all the knowledge we already have about vector databases, about how to create a vector database,
4
and then how to put information into the vector database, so vectorized data essentially,
5
and then being able to search to use the vector database to do similar research
6
so we can get the information that is closest to the answer
7
or at least the pieces of data that is closest to the answer that we're looking for.
8
So we've done that part.
9
By the way, this is the second part of this vector database series.
10
So if you want to continue to follow, I would recommend you to watch this video somewhere at the top here,
11
here or over there, where we got started with vector databases.
12
So go ahead and watch that and come back and watch this video.
13
All right.
14
So that's what we're going to be doing in this video, in this second part of vector databases.
15
So it's going to be more of a hands -on, but I want also to continue giving you the pieces,
16
the building blocks for you to really have this bigger picture of vector databases, and how to use them ultimately.
17
Because remember, ultimately, we are going to be using the vector databases with large language models,
18
because the idea is to have the vector database to do all the heavy lifting per se,
19
to be able to query things into our vector database, to get those chunks of information,
20
because it's real good at doing similarity search and all the great things
21
that we need to get the semantic meaning of certain documents, certain words, and so forth.
22
And then the idea is to take that information and then pass
23
that through a large language model because the large language model knows more.
24
It knows how to take those pieces of information and sort of summarize everything so we can get the final result.
25
And that is the main goal of vector databases and the main goal in this video, the second part.
26
But before we go and we start doing the real world use case coding, showing everything, there is another concept I need to share with you
27
when it comes to understanding as a building block of vector databases, which is embeddings.
28
So as you will see, I'll show you in this video, of course, before we start coding,
29
what embeddings are and what is the importance or the significance of embeddings in this realm of AI -based application vector databases.
30
That is something to look forward to in this video.
31
So without any further ado, let's go ahead and get started.
32
We understand that vector databases are real good at dealing with unstructured data,
33
which is more than 80 % of overall data floating around in the world.
34
So let's look at the unstructured data, how that is actually transformed into vectors.
35
So the idea, of course, is to take this unstructured data and then transform that into the vectors database,
36
into vectors, essentially what we call ALT's embeddings.
37
We'll get into that.
38
So that is the whole idea.
39
We'll get this unstructured data, transform that into vectors, just the numerical representation with a little bit more to it,
40
so that then that is what is actually saved into the vector store or vector database.
41
But let's look essentially what really happens.
42
So the idea in the grand scheme of things is that when data comes in, let's say we have a huge amount of documents.
43
It could be different kind of documents types.
44
It could be videos.
45
It could be text, articles, audio, and so forth.
46
Let's just assume this is just documents in general.
47
The idea is that we need to split that information.
48
That needs to be split into smaller chunks and then transformed into embeddings and then that embedding, which essentially is a vector,
49
we'll see the differences in a second here.
50
This embedding or vector is what is stored in the vector database or vector store.
51
So essentially, that's what's happening here.
52
A database, essentially, a database only saves this format.
53
That's why it's called vector store.
54
That's the idea.
55
But now we need to understand what is this embedding.
56
So embedding vector, so we can call this embedding or embedding vector.
57
Essentially, it's the same thing, okay?
58
So what embedding vector is, is that it keeps the content and the meaning of the text.
59
That's very important.
60
So it has a little bit more of metadata.
61
more than just the vector as we know in mathematics.
62
And the idea is that text with similar content and meaning will have similar vectors.
63
And that's what we've discussed and we now know that's the whole idea.
64
Anything that is similar in content and meaning, right?
65
Similar in content and meaning will have similar vectors.
66
Makes sense.
67
So essentially, as we saw before, if we have all of these words, for instance, cat, kitty, flower, eat, run, walk, Europe, and so forth,
68
cat and kitty have essentially the same similar meaning.
69
Therefore, their vectors will have similar numbers.
70
Therefore, their vectors will be also similar.
71
That's the whole idea.
72
And so going back to what we've just discussed, that is indeed what is saved in the vector database.
73
Now let's look at the vector database of vector store.
74
It's actually the same thing.
75
Some people use vector store, other people call vector database, just the same thing.
76
Let's look at an overview of the whole process.
77
We really need to understand how the vector store internally,
78
how things work, and how the actual vectors and data
79
and everything is embedded together so that it is put into a vector store or a vector database.
80
Same thing.
81
So again, the idea is that we have documents.
82
Again, it could be the actual documents or structured data.
83
It doesn't matter really.
84
Is that this has to go through a splitting process, generally speaking.
85
Okay.
86
And embeds are created.
87
In this case here, these are pieces of data that are representing these text splits.
88
And these are essentially what we call the embedding vector.
89
So now we have, in this case, the embedding vector, which is the numerical presentation of the texts or the documents that were split up,
90
that were transformed into this different dimensional space.
91
Okay.
92
But the good thing here is, in most cases, when we talk about embedding in general, it has vectors, as you can see here,
93
but also attached to it, we have the original text splits.
94
Okay?
95
So, they are attached together.
96
So, we have the representation of the numerical representation, but also the original text attached.
97
So, would be how internally, in most cases, the embeddings are organized.
98
And as I said, if we have a database, that means then something has to happen to that data.
99
So, The data is just there for the sake of being there, right?
100
So that means we have to query that data.
101
We have to query the vector database, the vector store.
102
So how does that really work?
103
Well, the idea is very simple.
104
You have to remember that this is all about the formats that we have here, the differences in formats.
105
Remember, this unstructured data, it's now being saved as embeddings or vectors.
106
Essentially the same thing, really.
107
And then when we are then trying to query our vector store,
108
obviously the querying, it will come in as a natural language spoken,
109
which needs to be transformed into something which then the vector database will understand to be able to query.
110
What that means is as follows.
111
If you look at this diagram here, I'm going to put all the thing together here.
112
So, the idea is that, remember, we already have this embedding vector here, right?
113
Now, when a query comes, a question, that also needs to be transformed into embed.
114
So, essentially, into a format that the embedding system here will understand, the vector space will understand.
115
Makes sense.
116
So, as I mentioned earlier, we need to create what we call an index.
117
So the index is another data structure that is created by the database, in this case, the vector database.
118
Vector database uses various algorithms to create or to build an index that makes retrieval,
119
ah, important, that makes the retrieval more efficient.
120
And that is what is between the embed, which is part of the query question, has to be transformed into embed so that then,
121
so that this data structure here that is created, essentially an algorithm created by a vector database,
122
is able then to query and find what needs to be found,
123
find the similar or search and compare all of the entries in the vector database.
124
So essentially important to note here, an index is what makes the whole querying the vector database fast
125
and efficient because an index is a data structure that facilitates this search process.
126
So this query or question comes in is transformed into an embedding.
127
And that will be then used to search and compare all the embeddings entries in the Vector Store and the Vector Database.
128
And what will happen internally, it will go ahead and pick the most similar entry,
129
which is then what is going to be sent out to be processed for further processing.
130
So it's going to go ahead and find something that's similar to this embed because it was transformed, and it's all numbers, mathematical numbers.
131
It's going to look what is similar to whatever this embedding that was transformed from the query.
132
And then spits out, per se, picks the most similar entry as such represented here.
133
Does that make sense?
134
And so what happens then, because the whole idea is to use large language models such as GPT and so forth, with that result, the processing,
135
what will happen then, the most similar embeddings that were received from previous process, the most similar entries are here now.
136
These are the ones that are sent to a large language model, take the most similar entry found,
137
and pass it on the large language model along with the question query and so forth.
138
And then, of course, that's when we get the result or answer.
139
So, at the end of the day, that is the whole basis
140
or the whole reason why we have vector databases is to save all that information from raw data,
141
if you will, into embeddings, essentially a vector with a little bit more information, more metadata.
142
And that is what's actually taken
143
and fed into a large language model to with queries
144
or any other questions that need be
145
and the large language model as an algorithm a complex algorithm will know how to take
146
that and spits out the result or answer
147
that we're looking for in the past videos or
148
so i introduced a new word a new concept called embeddings now let's look at the differences between embeddings
149
and vectors the truth of the matter is embeddings and vectors are actually referring to the same thing.
150
However, there are some differences in the definition as well as in roles.
151
When we talk about a vector, as we saw earlier, mathematically speaking, vectors do represent data.
152
It's a mathematical representation of data in an n dimensional space.
153
So, each dimension would be, in this case, a feature of that data as we saw.
154
Now, embeddings are a specific type of vector that are used in most cases in machine learning and artificial intelligence,
155
because they are a little bit loaded, if I may say, than just a simple mathematical vector.
156
Now, the terms embeddings and vectors are often used interchangeably in data science, particularly in machine learning.
157
But they have distinct definition and roles.
158
Let's go ahead and take a look at them.
159
So here are the key differences and relation.
160
So vectors are used in different fields per se.
161
So if you say vector, as we see, we can look at vector mathematically, but also they are used in computations and so forth.
162
So if you were to define vectors,
163
vectors could be considered as generic and are used for a wide range of applications, right?
164
So they are really good at handling numerical computations.
165
Whereas embeddings, when we talk about embeddings,
166
we are talking about a data structure per se that allows this mapping of write data into the vector space, into a vector space.
167
But most importantly, this is very important,
168
is that embeddings also preserve or attach the actual vectors with semantic relationships.
169
Meaning, this is very, very crucial because this is the core of AI as we know it.
170
Artificial intelligence, machine learning, and so forth.
171
Is that we need to have these vectors that also have some sort of metadata attached to it.
172
So, embeddings are that, allows us to have this raw data into vector space, but also we are attaching the semantic relationships meaning to that data.
173
So, the bottom line here is that embeddings are vectors, but not all vectors are embeddings.
174
So, embeddings are vectors that encode semantic similarities between the items they represent,
175
which in this case makes it really great for AI applications.
176
In a nutshell, the term embedding refers to specifically vectors
177
that have been learned in a way that encodes semantic similarities between the items they represent.
178
Think of vectors as vectors, essentially, but loaded with something else.
179
This something else being semantic relationships, meaning that it's encoded to those vectors.
180
So that is the distinction.
181
So whenever I say vectors as it relates to machine learning, AI, vector databases, and embeddings, I'm essentially talking about the same thing.
182
So I may be saying embeddings and vectors is essentially the same thing, but now you know the differences.
183
So now you have pretty much all the tools that we need to move forward and start building our AI -based application,
184
this hands -on that I have for you, which is going to be essentially using the large language model
185
and the data that we have saved we will have saved i should say into our vector database
186
so the idea is that we're going to take certain pieces of documents
187
or large documents or document
188
and then we are going to chunk them up cut them up into smaller pieces
189
and save those into a vector database along with
190
that we're going to be saving of course with embeddings now you should know what an embedding is
191
and so now when we have those embeddings
192
that is what's going to constitute part of this vectorization of our data
193
which is really important as you know
194
because it's easier to do the similarity search in this vectorized data realm per se
195
and now we're going to be using a large language model attach
196
that with our vector database which knows how to go
197
and find things really fast so that we're able to converse with our data, meaning we're able to chat, ask questions pertaining to our data,
198
in this case, our chunks of pieces of our data or our documents and get answers.
199
So now we're ready to go ahead and do that.
200
And let's go ahead and get started.
201
So far, we're able to do the most important thing, which is load the source data.
202
In this case, we have just a few documents or just a few pieces of documents, although they're small, but we could scale it up, right?
203
So what happens now is that we load that into our vector store or vector database.
204
In this case, we're talking about the Chroma DB.
205
And what happens then is we're able to then pass a query in form of text,
206
and that is indeed transformed into an embedding or a vector space,
207
which then is sent out into the vector store or vector database to do the search.
208
So it does the similarity search, and then we're able to see the result.
209
So we get the most similar pieces of that document.
210
So this is wonderful.
211
But what we really want to accomplish is something that is a little bit fuller, because the whole idea behind
212
Vector Databases is that we want to be able to query them with the help, of course, of large language models or models in general.
213
That's the whole idea.
214
So what we want to be able to do is, for instance, we have a bunch of documents.
215
We want to be able to take those documents and split them into smaller chunks.
216
And then once they're split, we are going to transform those into embeds like embeddings.
217
And these embeddings are the ones that are going to be stored in the vector store.
218
And as you know by now, we're able to pass these embeddings, which are vectors that not only have attached them vector,
219
but they also have the original test, the splits that will happen, the splits that correspond to that vector space.
220
So that's what we have so far.
221
But what happens next is as follows.
222
We want to be able, again, you've seen this before, we want to be able to have a question again,
223
which is going to be transformed into an embedding.
224
And then we have the index here.
225
And the index is going to help make this whole process of querying
226
and searching and comparing all the entries in the vector database way faster.
227
Okay.
228
And so what will happen internally then is that we are going to, according to the question or query
229
that was passed in is going to be able to search
230
and compare all the entries in the Factor database and then pick the most similar entries,
231
which is exactly what we've been doing and then showing that.
232
But now we're going to take this to the next step because we don't want just to see, for instance, if I ask,
233
you know, tell me about physics and just give me the chunks in the parts of documents that are related to physics.
234
That is fine.
235
We got that.
236
But we want to go even further to say, tell me about physics.
237
And then what will happen is that we're going to get this comparison that happens in a vector to b.
238
And we're going to pick the most similar entry.
239
This is where we have stopped and get the result so far.
240
We want to take this result, the similarities that were these embeddings that are similar results, right?
241
We're going to pick those.
242
And then we're going to pass them through a large language model.
243
So it's going to take the most similar entry found
244
and pass it onto the large language model with the question and query and everything.
245
So now we have the first question that was asked by the user and went through the whole process.
246
And we're going to take that question and put it all together with the most similar entry.
247
In this case, the chunks of those documents
248
that are most similar related to that question query and pass
249
that through the large language model and then we're going to get the actual answer or the result.
250
So that is what we're going to be seeing how to do in the next video.
251
So now we have this data directory with new articles.
252
So this is just a bunch of articles that are found online.
253
Each one of these articles is inside of its own document, txt document, as you see here.
254
So we are going to use all of these documents.
255
So essentially, we're going to load all these documents into a database or a vector database.
256
But before that, we're going to split them up
257
and then create embeddings as we split them up and then put them in a database,
258
persist that information, and make sure that we're able to do what we did before, which is to query the database,
259
the vector database space, the vector database space, and then be able to extract that information that we received,
260
which is the query as well as the results we got from the query querying our vector base
261
and then push all of that into a large language model.
262
In this case, we're just going to use GPT, use some of their models to actually to be able to ask questions and receive answers,
263
direct answers, not just approximations from our query.
264
So I went ahead and created, of course, a new file here called vector db llm large language model okay
265
so first i'm going to go ahead and do some imports here
266
so most importantly remember i'm getting chroma utils and embedding functions
267
that way we can also pass along not default embeddings
268
we're going to use open ai embeddings just like what we did before
269
i have the chroma db of course the open ai all of
270
that and then let's go ahead and pass in here these variables for our .env file.
271
So load all of those environment variables, which means then I'm going to be able to create our OpenAI API key.
272
Then I'm going to go ahead and get the OpenAI API key.
273
Make sure that you have that under your .env file, just like what we did before.
274
All right.
275
So the next part here, I'm going to create the OpenAI embedding function.
276
So I'll just say OpenAI embedding functions, and I'm using the text embedding three small model there.
277
And of course, I'm passing the OpenAI key.
278
So we're able to actually go and get through the API, get the embedding object functions that we can use here.
279
Okay.
280
So next we're initializing Chroma client with persistence.
281
So this is very important because as you can see here, we've seen this before.
282
We have client, ChromaDB persistent client.
283
We're passing just the path where we want this to be.
284
I'm going to just put actually something like DB so that it goes under this folder here, DB.
285
Okay.
286
We have the previous ones, but that doesn't matter.
287
It's going to go ahead and create a new one.
288
And we have a collection name here.
289
We call document QA collection and I can say that's very good
290
and for then here I'm creating the client so get
291
or create a collection the action
292
so here we're creating the actual collection we pass the collection name
293
which we specify the top
294
and we pass along the embedding function in this case we're going to use the open ai embedding function
295
which we instantiated there so now we're going to put together here all the functions
296
that will allow us to do what we want to do
297
so what we really want do is you want to be able to load all of this data, which is this new articles, into our database.
298
Okay.
299
So to do so, I'm going to get the...
300
I already have the function that does all of that.
301
So I'm going to just go ahead and get it.
302
This is the function, of course, that loads documents from a directory.
303
So we pass the directory path.
304
So we're passing here the directory path, and I have this print that says loading documents from directory.
305
So essentially here we go into this directory,
306
find this directory where we have data and new articles and then we find all of these files that are, they have txt at the end.
307
As you can see if I hover over, these all of them have txt at the end.
308
It's kind of hard to see but there we go.
309
So going through all of that and then create the actual document and then we return the document.
310
So each time we loop through, we are appending the actual document format into ID and...
311
It's going to be the file name and the text is going to be the contents of that document.
312
So we're creating those documents and then we'll return.
313
Now the next part here is going to be to actually split those texts into chunks, right?
314
Because we need to make sure that those are small pieces, those chunks.
315
These are the ones that are going to be transformed into embeddings and then added into the vector database.
316
So we have another function for that, which is this.
317
So splits text, we pass the actual text to be split and the chunk size.
318
For each chunk size, we're going to say a thousand.
319
That's just the number I got.
320
You can make it smaller or larger.
321
And chunk overlap is 20.
322
So this here is to allow for the context to not be lost.
323
Now this is a little bit more advanced, but the idea is that if we have different chunks,
324
larger this overlap, the better the context for each one of these pieces we are going to preserve.
325
Okay.
326
So essentially we're going through everything, the text that we are receiving, and we're just splitting into different chunks, into different chunks and minding,
327
of course, the overlap to be there.
328
And then we'll return all those chunks.
329
Okay.
330
So now let's go ahead and do the actual loading of everything.
331
So let's see here.
332
So I'm going to load the documents from our folder directory here, data, news articles that's all I'm doing here.
333
So I created this function first because it would be easier for us to call them.
334
As you can see here, we're actually calling load documents from directory.
335
Okay, so we have here, first of all, we're specifying the directory path.
336
So we say it's data news articles, which is exactly what we have here.
337
If you change these names, of course, make sure you change this path as well.
338
So we're going to this articles, new articles.
339
In fact, I made a mistake here.
340
let's see, it should be new news article, so I'm going to just say it's new article because I made a mistake here,
341
it should be news, but it says new, that's okay alright, so there we go, we have that set up,
342
and then we are getting all the documents by calling load documents from directory, because remember, this is returning load documents from directory,
343
we pass in the directory path and returns those actual documents, alright, so now we're using those functions we just created here.
344
And now let's go ahead and just for us to see, I'm going to just print and I'm going just to go ahead
345
and say loaded documents and get the length of those documents.
346
This comes in as a list, as you can see here, and I can just get the length.
347
Let's go ahead and run this real quick.
348
Okay, loading 21 documents.
349
Went ahead and created about 21 documents, actually loaded 21 documents, as you see here.
350
So we have probably about 21 documents.
351
Very good.
352
Now I can also print.
353
Just want to see the actual documents like this.
354
We should get a list of documents which we can also see.
355
Okay, you can see now we have this list of all of these documents that we have downloaded.
356
It's a very long list of everything.
357
All right, very good.
358
All right, so we know that the documents are loaded
359
because we can actually see all the documents here when we do print as you see there.
360
Next we need to split the documents into small chunks.
361
So here's what we do.
362
We create a list, create an empty list and we loop through all those documents and we call this split text.
363
So this split text we created, we pass in text, the chunk size, and the chunk overlap.
364
So it will return a list of those documents, the splits.
365
Okay, so that's all we're doing here.
366
I've added some of these prints here so that we can see what's happening as these are called.
367
So now that we have those chunks, we need to transform them into embeddings.
368
So essentially, we're following exactly what we've seen before, what we've talked about.
369
To do so, I'm going to also create a function that does that and I'm going to put that right here.
370
But before we do that, below here, I'm going to say client,
371
openAI, and then I'm going to pass the API key, which I know I have access to that,
372
which is this guy here.
373
So now I can go and create those embeddings.
374
So get OpenAI embeddings.
375
So we pass the text that was going to be transformed into embeddings.
376
So we use here client embeddings .create.
377
Now how do I know how to do this?
378
Well, we'll go back to OpenAI documentation.
379
We can go and let's go to tutorials.
380
So here it will show you exactly.
381
Let me just make sure this is.
382
Let's just go to quick start.
383
I think that's the best place and it tells you exactly how to get started.
384
Install Python.
385
and all that great stuff.
386
And if you scroll all the way down here, we have this chat completion.
387
Now, they have changed a few of their, they've changed some of the way we call things, but it's okay.
388
You can always come here to make sure that you are up to date.
389
So the idea is that we instantiate our client.
390
In this case, I passed along the API key.
391
And then we just say client, chat completion and create and then I'm past the model and then the messages.
392
So we have the role and we have the role for the system and role for user.
393
So this is the prompt for the user, the question that comes in, as you will see in a second.
394
And this is for the system to say, okay, what is it that I want the system to know?
395
That way when we ask questions, this is going to be contextualized.
396
So that's where I got
397
it from this is where you should come to get understand more how to use OpenAI to do things.
398
Now this is actually for the completion create
399
but I can go to embeddings by the way which is the one that I wanted to show you.
400
Let's go to embeddings if you click embeddings you should have the code
401
that says client .embeddings .create and then you have the input and then you have the model that you pass along.
402
All right so that's what we're doing here so I say client embeddings the input the text model.
403
I got the text embedding 3 small because that's all we need.
404
And then we go ahead and from this response here, I get the actual embedding for the text that we passed in,
405
which then is returned here.
406
So it's going to be a list of floats.
407
As you know, all embeddings are vector spaces, really, and so they all come as floats.
408
So now that we have the embedding function,
409
now I need to take each one of those chunks of this document that we created here and create the actual embeddings.
410
All I'm going to do is I'm going to go through this chunked documents, which we have access to here because we got those chunked documents,
411
and we're going to loop through.
412
So as we loop through, we are going to create the actual embeddings.
413
So we're going to generate embeddings for the document chunks.
414
That's all we're doing here.
415
So for the getOpenAI embedding, here we pass the doc, the text field in our doc,
416
as we loop through, and then we pass to the field embedding for that document, for each one of those documents.
417
So now that we have the embeddings, notice the process.
418
We have the documents, we chop it up in small pieces, and now we have to create those embeddings,
419
which are then used to be added into the vector database.
420
You see the flow?
421
Right.
422
And so now we're going to insert into our database because we have those embeddings.
423
So I'm going to copy this and pass in there.
424
So now we can, now you can see here, we have our chunk, chunk documents.
425
So we're going to loop through and start inserting all of that information collections.
426
And so we use collection, appsert, and we pass ID.
427
So this is the same thing we've seen before.
428
We pass the actual documents.
429
And notice here, now what we're doing is we're saying embeddings is doc embedding, because that is what we have been doing here, right?
430
So the document, each document now should have its own embedding as we loop through, because we're calling the getOpenAI embedding,
431
we're passing the text, that chunk of text, and then we create embedding and put that into the document.
432
So the document now has embeddings, that's why we say embeddings.
433
As we loop through, or rather as we loop through each one of those chunked documents,
434
we're going to insert each one and add it in ID, as well as adding the text, but also importantly, appending, in this case,
435
the embeddings along for this vector, this document, as we insert everything.
436
So if you open DB, you can see we have the previous databases here, but now if I were to run this,
437
if all goes well, I'm going to be able to actually insert everything into the document, into the database.
438
So let's clear this and run again.
439
You can see here splitting embeddings and soon you should see something happening here.
440
So it's generating embeddings because it will take a little bit because it's a lot of information.
441
you can see now inserting chunks into db okay let's open this all right
442
so this was created earlier but now you can see
443
if you go to chroma persistence
444
or persist you can see we have our chroma persist database here
445
so what i can do again command p or command shift p
446
and i'm going to say open database and let me go ahead
447
and get the chroma persist because we have a few here so
448
So let's see.
449
I think this is the one.
450
Okay.
451
So I deleted everything.
452
Let's start over.
453
I think that would probably be better.
454
Let's run this again.
455
Okay, so splitting, generating embeddings.
456
That's very good.
457
You can see now database was created.
458
Let's give it a moment.
459
We went ahead and inserted everything into the database, right?
460
Let's see if this is true.
461
So let's go back and say
462
Command -Shift -P.
463
Let's say open database.
464
That's the only one we have right now.
465
Let's open that.
466
And let's go SQLite Explorer just refresh just in case.
467
And you can see if I were to run any of these, you can see that we have all of these.
468
Okay, see all of these are actually the database with all of the text.
469
And you can see this week, apps, Apple, and very good.
470
So all the chunks are actually added to the database.
471
And I can continue to run something else.
472
For instance, search, we can see that we have full text search
473
and each one of these is different chunks with all the overlap and everything.
474
So that is beautiful.
475
Okay, so this actually works.
476
So let's look at collection.
477
That should give us document, QR collection and dimension and everything.
478
Okay, so we're able to get all those chunks converted into embeddings and then take those embeddings and added them into ChromaDB, be the vector database.
479
So we know that we have our database, our vector database, and has all of the embeddings.
480
Everything is good.
481
So at this point here, what I would do actually is I would go ahead
482
and comment all of this out because we don't want to make it so that every time we run this,
483
we ended up doing the same thing.
484
We don't need to do that anymore.
485
So I'm going to go ahead and just comment all of this out.
486
Let's see, all the way, pretty much all the way here.
487
So everything we've written so far, it's okay, just comment it all out.
488
So the next thing we'll do is we are going to go ahead and get the function to query documents.
489
So I'm going to put that function here. So it's very simple.
490
These query documents, once it's called, we pass the question, which is going to be the question, the query, and then the end results.
491
In this case, we just want to at this point, but this is actually not going to be relevant, but I'll just keep it there
492
Because the idea here is that we're going to attach, do this final part, which is we're going to attach all this information,
493
all these embeddings that come there similar to the large language model as well as the question.
494
That's why we're passing this to then make it so
495
that we can ask the large language model to questions about the data that we have.
496
Okay.
497
So we're calling collection query, passing the query text question, which is what we have here.
498
And I'm also passing the end results.
499
Now this is going to be redundant.
500
It would only work or would be needed if we were just to go ahead and do what we did before,
501
which was just to get some of the documents that have indeed the close that gives us the distances between the documents.
502
But we have moved on from that.
503
So this is no longer necessary, but I'm going to leave there anyway.
504
The other thing you can do also, you can actually create the query embedding by calling the getOpenAi embedding
505
and passing the question so we can make it that as embedding.
506
In this case here, I'm just going to go straight and pass for our query here the actual text question.
507
Okay, either way.
508
All right.
509
So then I get relevant chunks here.
510
What I'm doing here after I get the results here, I'm just getting all the documents and then just retrieving the relevant chunks that I need.
511
And I'm printing here, returning relevant chunks so that we have something to look at.
512
And then we'll return the relevant chunks.
513
Now we have the relevant chunks.
514
Let's go ahead and I'm going to just call that so we can see something.
515
So at the bottom here, I'm going to add a question.
516
So this is about, tell me about AI replacing TV writer strikes.
517
So I know this because in the document here that we have,
518
there's something related to writers, right?
519
AI replaced TV writers.
520
So I just ask a question related to that to see if this would actually give us something useful.
521
So I call relevant chunks.
522
I call the query documents, which is what we just created here, and then does everything.
523
It should return relevant chunks for us.
524
So in this case here, these relevant chunks, it's going to be a list of documents.
525
I'm going to go ahead and print them so we can hopefully see.
526
Make sure again that you commented out everything else so that we don't have to go through the whole process again,
527
which takes a little bit of time because everything is in database now.
528
So we can just be.
529
Okay, and just like that, it went ahead and said returning relevant chunks in the must -watch final season of Succession,
530
and it gives all this stuff.
531
So, very good.
532
So, it's giving something related to picture and television producers, refused to engage with the propo, ed, and all the things.
533
So remember, these chunks may look a little bit weird because when we created them, when they were split up,
534
we made sure that we have the overlap to make sure that we don't lose the context.
535
Okay?
536
So there we go.
537
So we went ahead and got the correct information, which is relevant to AI and writing and so forth.
538
Okay?
539
So this is for writer's strike.
540
If I were to ask something like, let's say Databricks.
541
Okay?
542
Databricks acquires AI.
543
I'm going to ask a question about that.
544
Tell me about Databricks acquiring acquisition of AI, something like this.
545
Okay, let's go ahead and save.
546
I'm going to go ahead and clear this and run.
547
And you can see it should go and get Databricks.
548
Voila, today announced that it has acquired Okera.
549
Very good.
550
So this is indeed working, which is exactly what we want.
551
So the next step would be to take these chunks, because remember, these are pieces of data,
552
pieces of information, that then we are going to be able to take this chunk
553
and then pass it through the large language model, all right, so that we can ask actual questions.
554
Instead of just getting these chunks, we want to get the actual answer to say, yes, Databricks did X, Y, and Z, everything direct.
555
So that is the beauty of this whole workflow from having the documents as we saw, chopping them up into smaller pieces
556
and then passing them through the process of creating embeddings and then putting all of that into database.
557
At this point, we're able to get the documents that are related to that question, which is exactly what we've been doing so far.
558
But now we're going to put all of
559
that information with the question through the large language model and then get the direct answer.
560
So we're able to get the relevant chunks, which is a win.
561
Now we're going to get these relevant chunks with the query and pass that through the large language model.
562
So we're going to use, of course, OpenAI API here.
563
If you go back and go to Kickstart, and you can see that we can actually,
564
we have declined already, and we're going to invoke the completion endpoint here.
565
So client, chat, completions at create.
566
So this is what we're going to do.
567
So what we'll do here is we're going to train our system to say, okay, you are a person who does X, Y, and Z.
568
So I want you to take the information I'm going to give you right now
569
and then answer because you are really knowledgeable.
570
And then we pass for the user this is the question, the query, we're going to pass it along here.
571
And then we should have that information, right?
572
The actual result or the actual answer.
573
Okay.
574
So how do we do that?
575
Well, first let's go ahead and get the function to generate a response from OpenAI.
576
Okay.
577
So this is the function to generate a response.
578
So we have the question that needs to be passed along as well as the relevant chunks.
579
So the first thing we need to do here is we're going to add a context.
580
So the context is to say is what we're going to add with our prompt
581
so that the system knows what is the context, what is it that I need to focus on,
582
what kind of skills I have to have to be able to do X, Y, and Z.
583
So essentially that's what we're doing here.
584
So essentially how I'm saying here you're an assistant of question answering tasks.
585
Use the following piece of retrieved context to answer the question.
586
If you don't know the answer, say that you don't know.
587
Use three sentences maximum and keep the answer concise.
588
So this is something that I've wrote for the prompt.
589
You can modify this to whatever else you want.
590
Now here I'm passing the context.
591
So the context I'm passing here as a variable, which is going to be this.
592
So the context is going to be all of the relevant chunks.
593
So it's essentially saying, hey, take all of these relevant chunks that we're going to pass through here
594
and then answer the question that I'm also going to pass through. Because you're knowledgeable.
595
This is your prompt now then we use the response then we use the completions
596
and api here client chat completions
597
that create we need to pass a model in this case just pass gpt3 .5 turbo
598
because it's cheaper and is just really good now there's gpt4
599
and by the end by the time you finish watching
600
this course maybe something way way better you can use gpt4 oh it doesn't matter
601
so this is just an example you we're just going to use gpt3 .5 turbo
602
and then in the messages here this is where we pass the role system
603
so essentially this is exactly what we had here
604
okay so in fact i just got it from here
605
so for the prompt here for the system to say hey system
606
this is your context this is what you're what you're expert in we pass the prompt
607
which is what we added here which which in turn we have the context
608
and the question to be answered by this
609
system by the system
610
and then the user here this is where we pass the actual question the question
611
which is going to pass here very good
612
and all we have to do now because we know
613
that this is the beauty that response requires a question
614
and the relevant chunks to do the magic
615
so we already we already have all
616
that now all we have to do really is just call the generate response
617
so answer to generate response and pass the question
618
which is what we have here and the relevant chunks
619
which is what we have here because we get that from query documents and pass the question.
620
Okay, so now I can just get rid of that and just say print answer and print the answer as such.
621
Look what will happen.
622
So I'm going to, so before we had this, we get all the chunks,
623
that's fine, but now we're going to use the large language model to give us magic.
624
So we're going to object with content that says Databricks acquired Ocara,
625
a data governance platform with a focus on AI.
626
So now I can probe in and get the actual content instead of just getting the object.
627
Okay.
628
So I can say here, answer, get the content.
629
If I save now, if I do the same thing, you can see that now instead of the object, which if you scroll down,
630
the object has all these other metadata too.
631
So, it can be handy.
632
This one should get the same answer mainly, but now we get the actual answer.
633
Look at this.
634
I can even ask something like this.
635
Tell me about the strike in the company or something like that.
636
Because we remember that the writers, there should be a document about about Riders strikes and so forth.
637
So let's run this and see if it's going to work.
638
Okay.
639
Look at this.
640
The Riders Guild of America is striking for the first time since 2007.
641
I can ask other questions.
642
For instance, let's say Fintech's Fintech Space or SpaceX Starship.
643
Something related to that.
644
Tell me about SpaceX.
645
Okay, look at this.
646
Space X Starship is a super heavy launch system, blah, blah, blah.
647
And there you go.
648
So now we can ask questions about all of those articles, all of that information.
649
So I can say, give me a summary, let's say brief overview of the articles, concise.
650
This is pretty fun.
651
okay there we go the articles cover topics such lifts equity
652
uh first quarter results potential upsides amidst its new strategic direction
653
and impact of of downed rounds
654
it also mentions upcoming events like tc city spotlight index venture tech crunch off all of these great great things
655
so now we're able to go through the whole flow of taking documents
656
and splitting them up creating the actual embeddings and then adding those embeddings into the vector store and its vector database, Chroma, that's what we're using,
657
and then be able to, of course, query that, but then get all those results, and then with the question,
658
pass that through the large language model so we get a very concise answer within the context of our documents.
659
And there you have it.
660
We just finished the whole workflow.
661
So we're able to get documents and split them up
662
and then create embeddings and then save those embeddings along with the original text splits into the vector database.
663
So this is wonderful.
664
But then we took things a little bit further.
665
So we took the question, we were able to get the query in question and turn that into embedding.
666
And then we're able then to search and compare all entries in the vector database and then pick the most similar entry.
667
And then the final part, which is the whole idea behind what we've been learning about vector databases and so forth,
668
is that we then pick the most similar entries.
669
Those are picked and they were passed through an LLM, a large language model.
670
In this case, you were just using the OpenAI large language model,
671
model in this case, but you could use any kind of large language model or model out there, okay?
672
And so we put all of these similar entries with the question query, and we passed through the large language model,
673
and we got the actual result.
674
This is the culmination.
675
This is the reason why, main reason why, that we have vector databases, so that they can work with large language models to give users,
676
in this case, create results, direct results and responses.
677
I hope this is making sense and you're seeing the value in learning vector databases.
678
All right, my friends, I hope this was helpful.
679
I hope you learned a lot in this series, starting from the first series,
680
which again, you can go and find somewhere here and you know pretty much all the fundamentals of vector databases.
681
You know how they work, you should know how they work,
682
how to create one and how to now use all this knowledge to actually create something useful.
683
In this case, we're able to use vector databases along with large language models to ask questions about documents,
684
about our own data, in this case, documents.
685
Now, I'm using documents in this case, but could be a database, could be anything else out there,
686
any other unstructured data that we can pass through a large language model, this would also work, right?
687
So I hope this was helpful.
688
I hope you were able to see how vector databases really work and how amazing they are.
689
And I think it's important for you to learn these concepts, even if you are not wanting to be an AI engineer,
690
but understanding these things, understanding these concepts,
691
I think it's going to really put you at a very high level when it comes to understanding how AI works,
692
which is exactly where we're going.
693
AI is not going to go back.
694
It's moving forward.
695
So I think you should really consider continuing learning about vector databases, machine learning, a large language model,
696
understanding this whole AI world.
697
The other thing is this two -part series
698
that I put together for you is actually part of a larger course where I go deeper into vector databases,
699
understanding really how they work and how to use them
700
and leverage them so you can actually start building AI -based applications that are very efficient,
701
that are well put together and scale better, right?
702
So it's part of a larger course, which is available.
703
And if you're interested in learning more about vector databases, then I'll leave a link in the description where you can go and check it out.
704
All right.
705
Thank you so much for your time.
706
And I really appreciate your time.
707
If you're new here, please consider subscribing and liking this video and make sure to share with people, friends, colleagues, grandma, grandpa as well,
708
if they are wanting to learn more about ai large language models
709
and programming in general this is the place to be
710
and let me know what you think of these videos what i'm doing here
711
because your feedback is always always always welcome i always love to hear from you
712
so well i'm here for you so thank you so much again and till next time be well
FOR YOU
推薦影片
本課的詞彙與口語要點
這節口語課以影片“Master AI in 60 Minutes with Vector Databases!”為素材。 影片中反覆出現的詞有:vector, embedding, document, database, chunk。 這段影片共有 712 個句子、8815 個單詞可供跟讀。 講話部分時長為 53:24。 說話人語速自然,每分鐘約 165 個詞,接近日常對話。 86% 的單詞屬於英語最常用的 3,000 詞,其餘的詞建議在練習前先學一下。
影片中的重點詞彙
影片中 15 個值得學習的單詞,附發音和釋義:
| 單詞 | 發音 | 釋義 |
|---|---|---|
| vector 名詞 | /ˈvɛktɚ/ | 向量, 向量 |
| database 名詞 | /ˈdeɪtəˌbeɪs/ | 資料庫 /資料庫 |
| chunk 名詞 | /t͡ʃʌŋk/ | 塊 /塊 |
| query 名詞 | /ˈkwɪɹ.i/ | 問題 /問題, 質問 /質問 |
| essentially 副詞 | /ɪˈsɛnʃəli/ | 本質上 /本質上, 基本上 |
| transform 動詞 | /tɹænsˈfɔɹm/ | 變換 /變換, 變形 /變形 |
| directory 名詞 | /dɪˈɹɛktəɹi/ | 名錄 /名錄, 簿 |
| loop 名詞 | /luːp/ | 圈, 環 /環 |
| insert 動詞 | /ɪnˈsɝt/ | 插入, 插上 |
| overlap 動詞 | /ˌəʊvəˈlæp/ | 重疊 |
| similarity 名詞 | /ˌsɪm.ɪˈlæɹ.ɪ.ti/ | 相似, 類似 /類似 |
| format 名詞 | /ˈfɔːɹ.mæt/ | 格式, 開本 /開本 |
| compare 動詞 | /kəmˈpɛɚ/ | 比, 比較 /比較 |
| attach 動詞 | /əˈtæt͡ʃ/ | 附上, 附 |
| acquire 動詞 | /əˈkwaɪɚ/ | 獲得 /獲得, 取得 |
影片中出現的短語動詞
| 單詞 | 發音 | 釋義 |
|---|---|---|
| go ahead 動詞 | /ˌɡoʊ əˈhɛd/ | 請便 /請便 |
| go through 動詞 | 通過 /通過 | |
| go back 動詞 | 回去, 返回 | |
| put together 動詞 | 拼合, 組合 /組合 | |
| come back 動詞 | 回來 /回來, 返回 | |
| end up 動詞 | 到達 | |
| get through 動詞 | /ɡɛt ˈθɹu/ | 度過 /度過 |
| look forward to 動詞 | 期待, 嚮往 |
值得反覆跟讀的句子
影片中簡短而完整的句子,可以直接用在日常對話裡:
- Let's look at an overview of the whole process.
- Let's go ahead and take a look at them.
- We're going to pick those.
- We have the previous ones, but that doesn't matter.
- That's just the number I got.
影片中的語法
說話人最常用的結構,並附上影片中的原話:
| 結構 | 影片中的用法 |
|---|---|
| 現在完成進行時 have/has been + -ing — 以前開始、現在仍在繼續的動作 | we've been doing · have been doing · we've been learning |
| 現在完成時 have/has + 過去分詞 — 過去發生但與現在仍有關聯的事 | we've learned · we've done · we've discussed |
| 被動語態 be + 過去分詞 — 強調發生了什麼,而不是誰做的 | is actually transformed · is actually saved · be split |
| 定語從句 who / which + 從句 — 補充說明人或事物 | databases, which is · vector, which is · embed, which is |
需要注意的發音
說話人用了 181 次縮略和弱讀形式,例如 we're, I'm, we've。請按聽到的簡短形式來說。
- “th” 音: algorithm /ˈælɡəɹɪðm̩/, mathematical /ˌmæθ(.ə)ˈmæt.ɪ.kəl/, mathematics /mæθ(.ə)ˈmæt.ɪks/
- “sh” 和 “zh” 音: essentially /ɪˈsɛnʃəli/, completion /kəmˈpliː.ʃən/, representation /ˌɹɛp.ɹə.zɛnˈteɪ.ʃən/, efficient /ɪˈfɪʃənt/, artificial /ˌɑː.tɪˈfɪʃ.əl/
- 長單詞——注意重音位置: essentially /ɪˈsɛnʃəli/, directory /dɪˈɹɛktəɹi/, similarity /ˌsɪm.ɪˈlæɹ.ɪ.ti/, representation /ˌɹɛp.ɹə.zɛnˈteɪ.ʃən/, internally /ɪnˈtɜːnəli/
中文母語者容易讀錯的音:
- /v/ — 不要讀成 /w/,上齒輕觸下唇: vector /ˈvɛktɚ/, overlap /ˌəʊvəˈlæp/, variable [ˈvæɹ.i.ə.bl̩], preserve /pɹəˈzɝv/, overview /ˈoʊ.vɚˌvjuː/
- 詞尾子音 — 要讀清楚,後面不要加母音: database /ˈdeɪtəˌbeɪs/, chunk /t͡ʃʌŋk/, loop /luːp/, generate /ˈd͡ʒɛn.ə.ɹeɪt/, insert /ɪnˈsɝt/
- /r/ 和 /l/ 的區別: similarity /ˌsɪm.ɪˈlæɹ.ɪ.ti/, algorithm /ˈælɡəɹɪðm̩/, numerical /n(j)uːˈmɛɹ.ɪ.kəl/, variable [ˈvæɹ.i.ə.bl̩], importantly /ɪmˈpɔɹ.tənt.li/
如何用這段影片練習
- 先完整聽一遍影片,不要開口,記下不認識的單詞。
- 先用 0.75 倍速逐句跟讀,熟練之後再回到正常速度。
- 錄下自己的聲音並與原聲對比,特別注意 vector, database, chunk 這類單詞。
什麼是跟讀法?
跟讀法 (Shadowing) 是一種有科學依據的語言學習技巧,最初開發用於專業口譯員的培訓,並由多語言者Alexander Arguelles博士普及。這個方法簡單而強大:您在聽英語母語原聲的同時立即大聲重複——就像是一個延遲1-2秒緊跟說話者的影子。與被動聽力或語法練習不同,跟讀法強迫您的大腦和口腔肌肉同時處理並模模擬實的講話模式。研究表明它能顯著提高發音準確性,語調,節奏,連讀,聽力理解和口語流利度——使其成為雅思口語備考和真實英語交流最有效的方法之一。












