Hello guys.
So we are going to continue the discussion with respect to Lang Chen.
Already.
We had uh, in our previous module, we had discussed about building chat bots with, uh, you know,
uh, chat message history.
Also now in this video we are going to discuss about vector stores and retrievers.
And uh, in this video tutorial we will familiarize you with the Lang and vector store and retriever
abstraction.
These abstraction are designed to support retrieval of data from vector databases and other sources
for integration with LLM workflows.
They are important for application that fetch data to be reasoned over as part of model inference already.
Uh, if you remember in previously also we have discussed about vector stores and retrieval, but here
I really want to add more features and even play with, uh, chat message history.
Okay, so first of all, what we are basically going to do over here is that we will quickly go ahead
and install this important libraries like pip install Lang Chin, uh, along with the Lang Chin, we
will also go ahead and install pip install Lang Chin Chroma.
I hope we have done all this installation beforehand in our same virtual environment.
And then along with this you have also done the installation of pip install lang chin slash crock.
Okay, so all these installation you have specifically done I guess so because you may.
Sure.
You should also have written this in your requirement dot txt okay.
So here you can see requirement is already satisfied.
So I will just go ahead and make this right okay.
But just understand that I've done this all uh installation with respect to the libraries that I require
okay.
So please make sure that you update all these things in requirement dot txt.
Now the next thing that we are basically going to do is that I will take one example that is even given
in the documentation page of Lang Chain.
So how to probably go ahead and create a document okay.
So you need to understand what exactly this document is.
So here I'm going to write some information about the document so that you will be able to get get this
and even be able to refer this.
So I will quickly add some comments over here.
Um, let's say this is my page content.
Okay I will go ahead and add over here page content.
This will basically be my metadata okay.
And then we will read it and we'll try to see it okay.
So here you can see Lang Chen implements a document abstraction which is intended to represent a unit
of text and associated metadata.
It has two attributes uh page content a string representing the content.
And it has metadata where a dictionary is containing arbitrary metadata.
The metadata attribute can capture information about the source of the document, its relationship to
other document, and other information.
Okay, uh, note that an individual document object often represents a chunk of a large document.
Okay.
So let's say, uh, here, in order to work with this specific document, I will go ahead and import
from long chain underscore dot documents.
I will be importing this document class.
Now in order to define my documents, I will be using a list of documents which I'm actually creating.
This right.
As said documents has two important, uh, properties, right.
Two important attributes.
One is the page content and the other one is the metadata.
So metadata over here will be a dictionary containing arbitrary metadata.
The metadata attributes can capture information about the source of the documents, right?
So here you can see that the source is nothing but this specific doc.
Okay.
We have just written it over here in this way.
So this will basically become my one document second document third document fourth document.
Right.
And remember this document can be a part of a large document.
It can represent a chunk of a large document.
Let's say if I have one PDF where I have eight pages, this can probably be my first page.
This can be my second page.
This can be my third page.
So that kind of information will have right.
So this is what a document is.
So let me just go ahead and execute this.
Now if I go ahead and print this particular document I will be able to see this documents over here.
Right.
And again the type will be specifically document itself okay.
Now let's go ahead and work with Vector Store.
Now already I have told you what is the main importance of Vector Store is that we will be able to convert
this text into embeddings, right into word vectors, into vectors.
We basically say, and then we can store all those vectors inside our vector store db.
Right.
So in this case I will take an example of chroma.
So I will go ahead and write lang chain underscore uh lang chain underscore chroma I'm going to import
my chroma DB.
Okay.
Now what I'm actually going to do over here is that quickly, um, will do one thing over here.
I will use completely open source libraries.
Okay.
Open source, LM model, open source, uh, embedding techniques and all.
Now, you know, in our env file we have also used this HDF underscore token.
Right.
This token is specifically coming from hugging face now with the help of this particular token, I can
call open source libraries that are available with respect to embedding or even, uh, LM models.
Right.
So in this case, first of all, what I will do is that, uh, I will just make a new cell over here.
I will go ahead and write import OS.
And along with this I will say from uh, from dot env dot env.
Sorry from dot env.
Import load underscore dot env.
Right.
Uh I will go ahead and initialize this load underscore dot env because I really want the environment
variable.
Now I will go ahead and first of all take my grok underscore API key because I will be able to refer
my um, I will be able to usually use the open source model that are present in the grok AI infrastructure.
Okay.
so here I will go ahead and write OS dot get env.
And here I'm just going to give my grok API key.
Please make sure that you have created that in your environment variable.
Okay.
The next thing that I'm going to set is OS dot environ.
I will just going to call this particular HDF underscore token.
So let me just go ahead and call this HDF underscore token.
I hope the spelling is right.
It should be capital.
So here you have my f underscore token.
And I'm going to initialize this value to os dot get env.
And inside this I'm just going to use this same f underscore token.
Right.
So this is quite simple quite easy till now okay.
We have set up all the environment variable.
Now let me just go ahead and call my uh LM model.
So here for using the LM model I also have to import from long chain underscore grok.
Import grok itself for Chad grok right.
So this will be the Chad grok.
And with the help of this API key we will try to access all the models.
So this time for this particular uh for this particular problem statement, I'm going to take my different
model name over here.
So over here, first of all I will go ahead and apply my grok API key which will be equal to my grok
API key itself.
Okay.
This will be my first parameter.
And the second parameter will basically be my model.
Now this time if you go ahead and see the documentation page of grok, this time I'm going to take another
model.
Previously we used uh, gamma two, but now I'm going to use llama three and eight B and 8192.
Okay.
So from where I'm actually getting this let me just open the browser and show it to you.
So this is my browser I will go to google.com.
And here I've already logged in.
So you can see I'm using this meta lambda 317 billion sorry 8,000,000,008 192.
So this is my uh model.
It is 8 billion parameter version of meta llama model delivers compelling performance at best in class
speed and price.
Okay, so we will be specifically using this okay.
So let's quickly go ahead and use that.
So over here I've actually used this.
And now I will go ahead and execute my LM model okay.
Now this is fine.
Now as you all know why do we use chroma.
Chroma is a kind of vector store.
And in order to use chroma, first of all we need to convert our text into vectors.
Now how do we convert a text into vectors?
Obviously they are embedding techniques we have seen about different different embedding techniques
like OpenAI embedding llama embedding.
Right?
In this video I will be showing you one embedding technique, which we have used with respect to hugging
face because it is open source.
Okay.
So with respect to hugging face, what we are actually going to do is that first of all, uh, I have
set up this h f underscore token with respect to the environment.
Now I will quickly go ahead and install one library which is called as pip install lang chain huggingface.
But I hope we have done this before also.
So let me just go ahead and write Huggingface.
And uh, we are just going to install this particular library.
So here you can see that this library is basically getting installed and it shows requirement already
satisfied.
We have already done this installation.
So I'm just doing it in front of you.
If you have not done it, please make sure that after you do the installation, write this in your requirement
dot txt to keep a track of it.
Okay, now what I'm actually going to do.
I'll go ahead and write from Lang Chen from Lang Chen.
Um Lang Chen underscore hugging face.
Okay I'm going to import hugging face embeddings okay.
So this hugging face embedding.
So it actually helps us to download the embedding techniques uh embedding model.
Uh with respect to the different kind of models that are available over there.
So I'm just going to go ahead and create my embeddings.
So I'll go ahead and write.
Embeddings is equal to hugging face embedding.
And here my I'm going to give my model name the model name that I'm actually going to specify.
Okay.
Now let's go ahead and open my hugging face okay.
So here I will go ahead and open my hugging face.
Dot Co okay.
Now please make sure that you create an account in this uh c for embedding.
We have a lot of different different models okay.
So if I go ahead and search for embeddings or let's say that I'll go ahead and click on models over
here.
So once I click on models uh you'll be able to see computer vision natural language processing, audio
tabular.
Different different models are basically there for our work use case.
I have searched for one model which is called as all mini LM.
Okay, so this will basically help us in doing the.
So here you can see all mini uh all mini over here.
Right.
Uh it is L6 v2 right.
The same model, uh, we can basically use and from different different companies.
Also, it is provided they have made some kind of changes and all.
but if you understand this is a kind of a sentence transformer, but I'm going to just use this sentence
transformer over here.
Now what this model is all about.
This is a sentence transformer model.
It maps sentence and paragraph to a 384 dimension dense vector space, and can be used for tasks like
clustering or semantic search.
Okay, so whatever words it basically takes, it basically converts into a 384 dimensional vector space.
Okay.
So we will try to use this over here okay.
And based on this uh, we will try to use this specific model.
Now in order to call this model what we need to do is that in the model name I will just go and write
and paste this all mini LM six v2.
Okay.
So once I go ahead and execute this, this will basically be my embeddings that I'm actually going to
use.
Okay.
This will take some time because it is just going to download it from the hugging face and it is downloading
it in your local machine, okay.
So that it can be used at any point of time.
And uh, again.
So now you can see that it has got downloaded.
Okay.
Now it's time.
We will go ahead and use this embedding techniques.
Okay.
Along with our uh, where we will be converting our text into vectors.
So we will be using this chroma vector store.
Now let me just go ahead and create my vector store.
In this vector store I will just go ahead and write chroma dot uh from documents.
Okay.
And inside this from documents we will be giving our documents.
Okay.
And we are going to give our embedding techniques.
So embedding is equal to nothing but embeddings.
And this embeddings is nothing but uh, the same embeddings that we have basically used called as hugging
face embeddings.
Now, what exactly is happening over here?
When I write chroma dot from documents with respect to documents, and embedding is equal to embedding,
first of all, all these documents is going to get converted into vectors, okay, into vectors by using
this embedding technique.
And then it is going to get stored in this chroma DB.
So that is what is basically happening inside this particular line of code.
Okay.
So uh, this was, uh, most of the thing that we really want.
Okay.
And, uh, now let's go ahead and execute it.
And if I go, just go ahead and, uh, see the vector store.
So this it will show it to me that it is a chroma vector store.
Okay.
And, uh, already in my previous video, I've shown you how to store this vector store in your local
if you really want.
Okay, so but this is just like a chroma DB, which is storing the specific information in the form
of vectors okay.
Now this is fine.
Now with the help of these vectors, what exactly we can do with the help of this vector store I can
apply something called a similarity search.
I can go ahead and apply this.
And let's say if I'm going and searching with respect to Cat, I will be getting all the sentences which
has cat information over here which are similar to this particular cat word.
Okay.
So any word that you write you will be able to do this.
Now along with this in the vector store we can also go ahead and hit with Sync query okay.
So a sync query also functionality is supported by this particular vector store.
So let me just go ahead and write.
In order to implement this a sync I will just go ahead and hit or I'll use this await keyword.
And then I will be using the same thing that is called as vector store similarity search by cat.
Now here you will be able to see that, um, a weight uh object list cannot be used in a weight expression.
Okay.
Let's see.
Okay.
Instead of using this similarity search there is another function which is called as a similarity search.
Okay.
So now if I go ahead and hit this here you will be able to get this particular information.
But this entire query will be uh, hitting with respect to using it will not be um, like, uh, it
need not wait till the response is probably coming back.
It will just use this async method.
Okay.
Over here.
Now this, uh, resource, uh, see this information that we are specifically getting here, we are
only getting the page content sometime.
I would also like to get the scores like how much match is basically happening.
So what we can do is that I will just go ahead and use this.
And instead of using just similarity search, I will say, hey, use similarity search with score.
And again they are with vectors.
Also you can convert into vectors and you can also do it.
So here you will be able to see that I'll be getting okay.
This is this much score.
And this is over here 1.571.5.
Name whichever is the nearest.
I think it uses a distance metric to calculate the sentence and the words okay, whichever is the nearest.
So .93 is the nearest, then 1.57.
After that you are able to get this.
Then 1.591.66 okay.
So in short this is about vector stores where you are able to do it.
Now let's do one thing and let's talk about one very important thing which is called as retrievers.
Okay.
Now let's worry about this retriever topic.
And I'm just going to put a description over here.
Okay.
So that you have this for the reference.
Let's read this okay.
And then we'll try to understand it.
Lankan vector store object.
Do not subclass runnable and so cannot immediately be integrated into the Lankan expression uh language
chains like LCL right.
RIT Lankan retrievers are runnable.
So they implement a standard set of methods synchronous and asynchronous invoke and batch operation,
and are designed to be incorporated LCL chains.
So vector store, you can see that it cannot be integrated with your entire chain.
That is what this sentence basically say.
The reason is very simple because it is not a subclass of runnable, whereas in the case of retrievers
they are runnables.
Okay.
And this can specifically be integrated right or incorporated with the LCL chain.
LCL chain.
Now what exactly is this long chain retrieval?
It is.
Uh, you can just say it is a standard set of methods which will be able to perform synchronous and
asynchronous invoke and batch operations.
Okay, so if Vector store is not a subclass of runnable, we cannot integrate with long chain.
We cannot integrate with chains by using LCL.
So what do we do is that we have a way, how we can basically convert this vector store into a retriever,
which is a runnable, so that it can be integrated to the LCL chain.
Right.
How to do it?
I will be talking about that.
Okay.
So first of all, what I will do over here is that and then uh, you can see we, we, we can create
a simple version of this ourself without subclassing retriever.
If we choose what we wish to use to retrieve documents, we can create a runnable easily.
Okay, let me just show you.
I know I have spoken about many things over here.
So first of all I'm going and I'll import this from typing import list.
Okay.
This list we will be specifically using this is uh see the list functionality is almost different,
but this is a kind of a different list.
Uh, when compared to the data structure list that we are using.
Okay.
So now I'm going to import from lang chain dot core underscore core dot document import document okay.
So we are going to just import this document lang chain okay.
Then I'm going to go ahead and right from lang chain underscore core dot runnables.
We are just going to import runnable lambda okay.
Runnable lambda.
Now what exactly is this runnable lambda.
Everything.
We will be talking about it okay.
Now see this here we will try to create retriever by two ways one by using this runnable lambda okay.
So here I will go ahead and write retriever is equal to runnable lambda okay.
And here what we can do we can basically go ahead and write vector store dot similarity search okay.
And here we can just say dot bind and give the top top result.
Whichever result you are able to get okay.
Select the top result k is equal to one.
Now I will just go ahead and call this retriever.
I will explain you right what exactly is happening.
Dot batch okay I'll be using a batch function in the retriever okay.
And this is I feel this way.
You should not do it because, uh, the next method that we, uh, I will be talking about how you can
retrieve the data from the vector store.
That will be the best way.
Let's say inside this batch, I give two words.
One is cat and one is shark or dog.
Let's say I'll go ahead and pick dog.
Okay, now what this is going to basically do please see to this.
Okay.
So here I am creating.
This is just like a runnable lambda function okay.
And this function is going to get applied to all the parameters that we are giving inside this particular
batch okay.
Let's say if we give cat over here.
Right.
So first of all it is going to do the vector similarity search on cat.
And whichever will be the topmost result I will get that particular value and it will get displayed.
Then.
Since this is a runnable lambda that basically means a lambda function to execute this runnables then
I will be passing this particular.
This dog parameter will get passed.
Again, the similarity search will be done and based on the similarity search, whichever will be the
top result.
I will be getting that.
Okay, That is what is basically going to happen over here.
Now if I go ahead and execute it here, you can see cats are independent and they often this and this
is my related to dog.
So two content are we have been able to retrieve from the vector store.
So vector store is a DB right.
And we are able to convert this into retrievers just by using this runnable lambda okay.
And what is the mean name of this runnable lambda based on the method that I'm calling, whether it
is invoke or batch.
In batch I can probably call multiple parameters multiple inputs.
It will get executed with respect to the similarity search that we are doing.
Okay.
Now, the second technique that we are going to use over here is something called as vector store implement
as retrieval method.
This will generate a retriever, specifically a vector store retrieval.
This retrieval includes specific search type and search queries.
Arguments attributes that identify what method of the underlying vector to call okay and how to parameterize
them.
Now, this is probably the best way how you can go ahead and query from a vector store, right?
We really need to convert this into a retrieval.
So I will go ahead and write vector store dot as retriever.
Okay.
Now this retriever acts as an interface right to pass or to take any information from the vector store
and give it as a response.
Okay.
So here I will go ahead and give my parameter.
The first parameter will be my search type.
So here you have the functionality of search type and the search type.
Over here I can go ahead and write similarity okay.
Then the search argument search search underscore k w a args.
Okay.
Now this argument specifically I will be mentioning with k colon one.
So this I'm saying hey give the top first result.
See here we have written dot bind with k is equal to one.
Again we are using similarity search.
Here we are giving two parameters over here.
One is search type and one is search underscore k w a args okay.
And finally here you'll be able to see that once I create this retriever.
Okay.
Let's go ahead and create this retriever.
R e t r e v e r is equal to this.
Okay I'm just going to go ahead and write retriever dot batch.
And now I will give the same two words that I've actually given.
One is cat and one is dog.
So this is the next way.
And here is all the information that I am able to get.
Okay, so this is how you can go ahead and probably use any of this with respect to, uh, you know,
implementing things.
Okay.
And this is how you can actually go ahead and work with it.
Right.
So here we have store.
We have basically what we have actually done is that we have taken a document content.
This may be coming from a PDF.
It may be coming from any website and all.
We have converted this into a vector store.
And finally, with the help of retriever we are able to get the response.
Okay, so uh, this was mostly about all the things that we have actually discussed till here, right
now.
Um, I hope you were able to understand this.
Okay.
Now, one final thing that I really want to show you is that how we can integrate this retriever along
with the chain.
Okay, So guys let's go ahead.
And here I will be putting up some information.
Let's say this is my chat prompt template.
And this is my runnable pass through.
This is the basic uh, prompt that I'm actually trying to create.
Answer this question using the provided context only.
Right.
And this particular context, uh, needs to be filled by the vector store.
Right.
And this is the question that what user is going to give.
Okay.
So here what I will do I will go ahead and create my prompt quickly.
So this will basically be my prompt.
And we have discussed all these things earlier also.
Right.
So chat prompt template dot from message and human message I'm giving the message whatever message we
are giving it over here okay.
And uh this message uh I'm just specifically saying that hey, uh, what is the information that I really
want to give?
Okay, so after this, you'll be able to see that, uh, I will go ahead and create this prompt.
And after that I will go ahead and create my entire rack chain.
Now, in order to create this rack chain, how do I pass this retriever?
Right.
How do I pass this retriever along with my chain?
Because now retriever is a type of runnable over there.
It is a subclass of runner.
Uh, it is.
You can just see over here, right.
It is a subclass of, uh.
So here, uh, where it went.
Okay.
So retrievers are runnables, right?
So now it has become a subclass of runnable, right?
So how do we integrate it over here?
So first of all, I will just go ahead and replace whatever context I have written over here.
So instead of this context, I should be getting this entire context from retriever okay.
This retriever will be responsible.
The second parameter that I really want is my question.
Right.
And this particular question will again be in the form of key value pairs.
Right.
And this value what we are basically going to pass, we will go ahead and write runnable pass through.
Runnable pass through basically means we are going to get this particular information in the invoke
method.
Okay.
Then you have prompt.
After this I will go ahead and pass through prompt.
Then my LM model okay.
So so this basically becomes my rank, uh, the entire chain itself.
Now in order to execute it, I will go ahead and write a rag underscore chain dot invoke.
Okay.
And let me just say hey tell me about dogs okay?
And once I get this response I will go ahead and print it.
So print response dot content okay.
So here once I execute this according to the provider context, the dog are great companions known for
their loyalty and friendliness.
So from all the documents we are basically getting this.
Now what we have actually implemented.
This is just a basic rag right?
This is rag retrieval augmented generation.
Right.
So here what we have done we have used chat prompt template.
We have used runnable pass through.
We have created our prompt.
Here we have given two inputs.
One is question and one is context.
This context needs to be replaced by our retriever.
So this retriever if I am writing over here this is basically the information from our vector store.
All the documents will get added over here right.
And finally you'll be able to see that I'm able to also integrate this in the rank rag underscore chain.
Your question will be runnable pass through.
That basically is whatever input I'm actually giving over here that is going to get placed over here.
And then we are going to use prompt and LM to create the long chain.
Finally we get the response.
So I hope you are able to understand and get some idea with respect to vector store retrieval.
And this is the entire thing that we really need to learn.
And these are something basics.
Because later on, once we start building projects you will be able to learn more amazing things.
So yes, this was it from my side.
I hope you liked this particular video.
I'll see you all in the next video.
Thank you.