Hello guys.
So we are going to continue the discussion with respect to Lang Chain.
And in this video and in the upcoming series of video, we are going to discuss about Vector Store DB
uh, again, uh, I have listed down some of the vector stores that I will be showing you with respect
to practicals.
One is uh, files, another one is chroma DB.
And after doing both of them I will be also showing you more couple of examples.
There is also a vector store db which is called as Astra db.
This I will show you, uh, with the help of some end to end projects.
Uh, because, uh, we will be using this Astra DB from cloud.
Specifically, it uses Cassandra DB itself.
Right.
So, uh, later on I will be showing you the specific example, but right now we'll focus on this two,
uh uh, vector store and you'll be seeing, uh, each and everything, like how you can actually work
with this.
What is this entire vector store?
And many more things.
Okay, so let's quickly go ahead and open my file.
Uh, so here I will just go ahead and open my file.
So here is my files dot Ipynb file okay.
Now you need to understand first of all what exactly is this particular vector store okay.
So files is nothing, but it is Facebook AI similarity search.
Uh, it is a library for efficient similarity search and clustering of dense vectors.
It contains algorithms that search in set of vectors of any size, up to ones that possibly do not fit
in the Ram.
It also contains supporting code for evaluation and parameter tuning.
Okay.
So uh, let's see some of the example and I'll just show you one very good example with considering
uh txt file will try to read that particular data from that will convert whatever things we have learned
till now right from data ingestion till embeddings, and then finally storing it into a vector store
DB.
Everything will be doing okay so far.
Initially.
We will go ahead.
And first of all let me go ahead and select the kernel.
Uh, so I will go ahead and select the kernel over here.
So here you can see first of all I will go ahead and import from long chain underscore community.
First of all we are going to go ahead with document loaders.
Okay.
Here we are going to import text loader.
Then uh we are going to go ahead with long chain long chain underscore community dot vector store.
Since I'm going to use this particular vector store which is nothing but files.
Okay.
I'm going to go with this.
Okay.
Please remember one thing that you need to have installed the files vector store.
So here what I will do I'll go over here I'll go ahead and write files and let me use the CPU version.
Because right now I'm doing it in my local machine.
Later on when we take this into cloud you can actually also use GPU.
Okay.
So quickly let's go ahead and install this.
So I will go ahead and add pip install r requirements.txt.
Here you can see requirements will already be satisfied because I have already done the installation.
Okay.
Now uh, the next step will be that after importing files I will not use OpenAI embedding, but I'll
use an open source embedding which is llama embedding.
So here I'm going to go ahead and write for long chain underscore community dot embeddings.
Okay I'm going to import over llama embeddings.
Perfect.
Then again from long chain underscore text.
Uh so let's go ahead and use a text splitter.
So I'm going to use long chain underscore text splitter.
And let me go ahead and use uh some character text splitter which we have already seen.
Okay, now I'll take my loader, I will use this text loader and then I will go ahead and upload one
txt file which is called a speech dot txt.
And if you go ahead and see over here in here I have the speech dot txt file.
Okay since I'm working in this vector store folder.
Okay I put it already then.
Uh, uh, from this particular, uh, txt file, I'm just going to load all the documents.
So I'll be using loader dot load this.
We have already discussed uh in the data ingestion by using document loaders.
Then after this this will be my next, uh, important step is that text splitter.
And here I'm going to use my character text splitter.
Okay.
Let's consider the chunk size.
Chunk underscore size will be 200 and my chunk overlap.
Let's say that I don't want any chunk overlap, so I'll just go ahead and make it as zero okay.
Um, otherwise I'll just make it as 30 okay.
This is smaller number.
Now uh, we will go ahead and use this particular text splitter and we'll split all the documents.
So here I'm just going to, uh, go ahead and write split underscore O.
So text splitter underscore split underscore documents okay.
And here I'm just going to use my documents.
So let's execute this.
So here you can see uh created a chunk of 470.
Everything is shown over here.
But let me make this as 1000.
So sorry.
Let's make me this as 1000.
Okay.
So now let's go ahead and see my docs.
This is my docs.
And this is the splitting that has already occurred.
Okay.
Perfect.
Till here all the splitting has been done.
Now the next thing that we do is create our embeddings.
So for this I'm going to use my llama embedding uh, by default.
I've also shown you that llama embedding uses which kind of embeddings itself.
So db will be nothing but fires dot from underscore documents.
So I'm going to use this fire.
So um vector store and I'll probably convert all these documents with the help of this particular embedding.
So in order to do that the first parameter after fast dot from underscore documents have to give all
my documents.
And then I will just go ahead and use my embeddings okay.
So once I do that uh, this basically becomes my vector store DB right.
So this if I go ahead and execute it, it will take up all this particular characters.
Then it will perform all these all my embeddings.
It is going to take some amount of time because this embedding is running in my local machine.
Okay, since it is running in my local machine, so it is going to take some amount of time for your
system.
Also, if you are doing it in a local with the help of Allama, it is going to take some time.
Okay, because we are going to use this five CPU to convert all the documents into vectors, right?
So here you have this Langston underscore community dot this at this specific memory location.
This is perfect till here.
Now what I'm actually going to do.
I will take this particular vector database and start querying it okay.
So in order to do the query I'll go ahead and create a query.
Let's say my query from that particular speech text is that I will just go ahead and show you this.
Okay.
So this is one of the question that I have in my speech text speech dot text.
What does the speaker believe is the main reason United States should enter the war?
Okay, so that is based on the speech text over here.
Um, and I will be considering this example.
Or what you can do is that you can just go ahead and open the speech dot txt, okay.
And you can search for any of the questions that you really want.
Okay.
It is up to you.
Uh, whatever things you specifically want okay.
Uh, so let's let's take one more question Okay.
So I have actually searched for one of the question.
So I will just go ahead and paste it over here.
Okay.
How does the speaker, uh, basically talk about this, you know, describe the, uh, describe the
desired outcome of the war.
Okay.
And here, uh, now we will take up this query, and here we're just going to go ahead and write docs
is equal to DB dot similarity search.
We'll use this.
And here I'm just going to give my query okay.
So finally you'll be able to see if I go ahead and print my docs as soon as I do the similarity search
with respect to the query.
This is the entire response that I get with respect to the context.
Whatever things are basically matching, we get every of these things right.
What I will do, I will just go ahead and display the first one, which is nothing but this one, right?
Page content.
So if I go ahead and execute it here, you will be able to see, hey, it is a distressing and oppressive
duty.
And gentlemen of the Congress which I have performed so so, so so information is basically there.
Okay.
Now this is one way how you can do the query.
Uh, but as I said, uh, after this, you know, I'm also going to show you with respect to retriever.
So let's go ahead and see uh, example with respect to retriever.
And I'll just give you an example.
What does, uh, the retriever actually do.
Right.
So I'm just going to put a comment over here.
So here you can see we can also convert the vector stored into a retriever.
Cause this allows us to easily use it in other methods which largely works with retrieval.
In order to just give you a brief idea, uh, we will be discussing about this retrieval in a separate
module.
But just to give you an idea, this retrievers are like an interface, which actually, whenever we
put any kind of query, it it it is just like connected to the vector store DB.
It can be connected to the vector store DB.
Um, only when we convert this vector store db into retriever class.
So it lacks acts like an interface which will be able to retrieve the details from the vector store
and provide you the response.
Okay.
So here, uh, what I will do in order to convert my vector database as a retriever, I will just go
ahead and use DB dot as retriever.
And I'm just going to use this particular variable.
So here I will go ahead and write retriever okay.
Then let's go ahead and use this retrieval dot invoke okay.
And I will just go ahead and give my query.
So once I do that uh you'll also be able to see that I will also be able to get the same answer like
how I used to get it over here.
But the reason of using this is that later on when we work with different, different LLM models, right
at that point of time, I cannot directly use vector store DB, but initially I need to convert this
db into a vector store db okay, and then finally use it.
Okay.
Uh, sorry, I need to convert this particular vector store DB into a retriever, and then I will be
able to use with any LLM models.
Right.
So uh, that is the importance of using retriever.
So that is the reason I have written over here.
This allows you to easily work with another other Lang chain methods which largely works with retriever.
Right.
So this basically becomes my docs.
So here you have docs of zero dot page underscore content.
Okay.
So I have this entire information over here right now.
Uh, in uh in in files.
Right.
You also have one beautiful mechanism which is basically called as similarity search with score.
There are some five specific methods.
One of them is similarity score search with score, which allows you to return not only documents,
but also the distance score of the query to them.
Right.
The return distance score is L2 distance, which is also called as Manhattan distance.
Therefore lower score is better.
Okay.
So this is important to understand.
Uh, we can also use similarity search score.
Uh search with score okay.
So here I will just go ahead and write docs underscore and underscore score.
And here I'm just going to go ahead and write dim sum db dot similarity search similarity search with
score.
And here I will just give him my query whatever query I'm actually writing.
And let's go ahead and print this particular DB score.
Okay docs and score.
So once I execute it here you will be able to see that here.
Uh, it is a distressing and with respect to every information that I get right, there will be a score
that will be attached.
And this is nothing but your L2 score, which is also called as Manhattan score.
Uh, Manhattan distance.
Okay.
So based on that particular information, whichever is the nearest that we will be getting as the first
context from that particular text file.
Right.
So here one more additional thing that you are actually getting is with respect to the square.
Okay.
Now you may be also thinking, Krish, can we not directly pass vectors instead of sentences?
Yes.
We can actually also do that or instead of any documents.
So for that we I have to probably go ahead and make my vector.
So I will go ahead and write embedding underscore vector.
And then here you will be able to see embeddings dot embed underscore query.
And here I can give my query.
So once I give this.
So this basically becomes my embedding vector okay.
Based on my embedding that I have actually used.
Once I have this particular vector then what I can do I can just go ahead and write db dot similarity
search by vector okay.
And here I can actually go ahead and give my embedding vectors right.
And once I do this.
So here also I will be able to get the docs and score.
Okay.
Score.
So finally, if you go ahead and see this docs and score, sorry docs underscore score, you will be
able to get the same information how we used to get it right.
So here, uh, the query that has been done is with respect to your, uh, embedding vectors.
Okay.
Now, finally, uh, you should also know this.
How I can probably save this vector DB vector store db in my local.
Right.
So here I will just go ahead and create some cells.
Okay.
Let me copy this and write some comments over here.
Here we are going to basically do the saving and loading Saving and loading.
So here let me just go ahead and write db dot save underscore local.
So there will be a function which is called as save underscore local.
And here I can actually use one index name okay.
It'll be five index.
So once I execute this you'll be able to see that uh here the file will get saved.
It will be saved in the form of pickle.
And here you also have something called as index dot file, some of the configuration information.
Right.
So this is your files underscore index folder.
Now what you can do is that you can load this particular folder also very much easily.
Now in order to load this folder you will go ahead and write new underscore DB.
Let's say here I'm just going to use this files.
And there will be a function like load local.
I will give my uh index name that is fast index.
And, uh, here, along with that, you know what all embeddings also we need to use.
Right.
So embeddings, uh, I've already defined all of my embedding.
You can directly initialize over here.
Now, once I execute this you'll be able to see, hey, uh, we are getting an error.
The deserialization relies loading a pickle file.
Pickle files can be modified to delivered a malicious payload.
So during this point of time, you know, it is just saying, hey, something is fishy is happening
because the reason is very simple.
Simple.
I'm trying to load a pickle file over there whenever you get this kind of issue.
Before we used to not get it, but right now in the recent version of files we are able to get this.
So what you do is that you just go ahead and use one parameter, which is called as allow dangerous
serialization.
Okay.
Because you are just saying that, hey, I trust this particular file, right?
So allow the dangerous Deserialization is equal to true and just use this.
Okay.
Now if I go ahead and use this docs is equal to and I will just go ahead and use my why I have written
new underscore df file read and write new underscore DB right.
You just go ahead and use this similarity search.
I think it should be able to work okay.
Again you can also do similarity search by vector or whatever things you want.
So once you set this allowed dangerous serialization, you will be able to see the answer.
Okay.
So, uh, this was uh, more about Feis.
I think we have discussed each and every thing, you know, working with Feis, how you can actually
go ahead and see with respect to vectors, how you can do the similarity search by vectors.
Each and every thing is discussed.
I hope you were able to understand the specific video.
Um, yeah, this was it.
I will see you all in the next video.
Thank you.