5-Langchain Series-Advanced RAG Q&A Chatbot With Chain And Retrievers Using Langchain

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello all my name is krishak and welcome to my YouTube channel so guys we are going to continue the langin series and now we will be developing Advanced rag pipeline using Retriever and chain Concepts that are available in Lang chain now before I go ahead I really want to talk about a funny incident that really happened uh recently just today itself so what I did what I do is that every day morning I usually go and play some badminton you know I play for 1 hours one and a half hour and usually a lot of my friends and neighbors usually come and play you know so today what happened after playing I went today in this look okay I played around three to four games and then one of my neighbors said hey Kish your Chris right you just got identified right now just by seeing your shoes I identified you but your look has completely changed so let me know if this is true in the comment section of this particular video but I am liking this look uh you know it's like so much of less maintenance you don't have to maintain your beard your mustach or your hair also right it looks super cool now uh let's go ahead and uh work towards this specific project in our previous tutorial what we had actually done is that we had created this simple rack pipeline okay we had a data data source like we took PDF we took website then we uh load that particular data set using different different data injection techniques uh that were available in Lang chain then we did transformation where in we broke down our bigger PDFs into chunks and then we converted all all these particular chunks into vectors and stored it in a vector store and then with the help of query we are able to probably retrieve some of the data that is available in the vector store now this is one step now the further step after this particular query Vector Now understand query Vector are not that efficient you know when in terms of retrieving the entire results here we will also specifically use llm models okay so now what we will try to do is that using some prompts okay and we will take this specific prompts we will take this particular data using the concept of chain and retriever okay and understand this topic is very important because this is where your advanc rack pipeline implementation will start using chain and retriever we will also use and in this chain and retriever what we do is that we specifically use llm models it can be open source it can be paid whatever model you want so so we will specifically use this llm model and based on this prompt we will try to get the response on what we are specifically looking right so there will be a lot of customization that will be added once we implement this specific part in the first part we discussed about this and this is the second part that we are going to discuss okay how we can use chain how we can use retriever what exactly is chain how you can integrate in this particular llm model there is a concept of something called a stuff document chain what exactly it it is so we will discuss everything all about it and here we also going to do a practical implementation so please make sure that you watch this video till the end and we are going to learn a lot of things okay so here the first step what we had done on already uh we have implemented this in our previous tutorial also so here you'll be able to see that I trying to read attention. PDF which is present in a folder and then we just write loader. load and we get the documents okay so these are all the documents that will be available in this specific PDF okay then what we are specifically doing next step is that from lin. text plat we will be using recursive character text splitter wherein we convert the entire document into chunks right and then we are probably using this text splitter and we are using a chunk size of thousand overlap of 20 so this everything is implemented in my previous videos right so we are going to split this entire document and save it in this particular documents in the previous tutorial you have implemented all these things now here what we are going to take do now we'll take all these documents and then we will convert it into a vector store right so Vector store for that we are using this F okay so here we are going we can use AMA embedding or open AI embedding right as I said you open AI embedding is very much Advanced and it will perform better than AMA embedding if you don't have opening a API key use AMA embedding instead of open AI embedding over here you can just write AMA embedding right so from here I'll be using fires which is is again a kind of vector store and it has been developed by meta so fest. from documents document of 20 so I'm just taking the first 20 documents and I'm just writing open AI Bings let's make it to 30 so that it will have some amount of data right now we are specifically using open IM bidding and this DB that you specifically see is a my Vector store okay so here you can see Vector store f f of type okay perfect now any question that I ask attention function can be described as a mapping query and then we can take this Vector store and just write do similarity search on this query and we will get the result over here okay so this all things we have actually done in our previous video now is the most important thing how I can combine prompt along with chains and Retriever and then probably get a response based on the prompt okay so since many people have the use of only open source uh they have the exess of Open Source model llm model so I'm going to use AMA from lin. community. llms import olama then I'm going to use AMA over here model will be Lama 2 if you don't have Lama 2 just do go to command prompt after downloading o Lama I hope everybody knows how to download it if you're seeing my series of videos here you can just write AMA run Lama 2 right so once you write like this then the model will get downloaded right if it is already downloaded it will be coming something like this okay so this is the first step that you really need to do then I have written from lin.com community. LMS AMA load olama so whatever AMA model we are specifically using that is Lama 2 so this is my open source model so if you see Lama llm it is nothing but AMA now is the time we will start designing our prompt template now in order to design the chat prompt template I will be using chat linore core. prompts import chat prompt template okay and then from chat pron template from template I'm just writing like this so I'm saying answer the following question based only on the provided context now see we are trying to develop a Q&A Q&A chatboard based on the context it should provide me the response previously what we are doing using Vector store we used to if you see the code over here we used to query the vector score right Vector store by using similarity search algorithm but here what we are doing here we are defining our own prompt and we saying hey answer the following question based on the provided context right I will and simply I'm writing away I'll tip you $1,000 if you find the answer helpful okay if the user find the answer helpful okay just at least by seeing money the AI May perform well and then we are giving our context and then question will be input okay how why I'm writing in this specific way because this chain and retriever right you'll be understanding this context will be autofilled and this input will also get autofilled okay how it will get autofilled I'll let you know so now what I will do I will execute this now I will go ahead and Implement about chain uh it is always a good idea okay to probably go to your browser and check about each and every topic that I'm explaining so what does chain refer to chain referred to a sequence of calls whether to an llm tool or data pre-processing step the primary supported way to do this is LCL okay now if you talk about chain over here there are multiple functions with respect to chain one of the function that I'm going to use is create stuff document chain now what this exactly does this chain takes a list of documents and formats them into all into a prompt then passes that prompt to an llm okay see this this chains take a list of documents and formats them based on the prompt formats them into a prompt sorry not based on the prompt into the prompt that basically means over here if I go ahead and open my browser here in the context I definitely require my document s self right based on that context and based on this input I will be able to give the answer right so based on this context and based on the input right context basically means all the documents that are there available in the vector store right inputs are what question I'm asking right so with the help of this create stuff document chain what is basically happening this chain takes a list of documents and formats them all into a prompt then passes that prompt to an llm it passes all the documents show that you should make sure that it fits within the context window the llm you are using so what it is exactly doing it'll take up all the documents from the vector store it will put that put inside that particular prom template and then it will send the to the llm and then we finally get the response okay and that is what we'll be using similarly there are different different uh things also over here like create stuff document chain is there create SQL query chain if you are working with respect to SQL database for natural language and this is one of the very important project that I'll also do in the future one or the other way I'll try to use one or the other functionalities to just make you understand how we can use all these functionalities itself right but it is always good that we have a specific use case okay now if I open this okay let's go ahead and create my chain how do I create my chain over here again it's very simple so I will write from Lang chain Lang chain uncore community so it is present inside Community itself or not Community sorry chains Lang chore chains dot combine documents import create stuff document chain now how do I know this okay I did not create this I have already seen the documentation okay that is the reason I'm writing then I will go ahead and create my document chain now inside this document chain as I said I'll be using create stuff document chain okay that we have already seen now inside this chain two things are basically required one is the llm model and the second one is the prompt that I have created because inside this prompt itself the list of documents will be added right whatever documents is basically coming from here right from my Vector store that will be added over here okay so once I create this so This basically becomes my document chain okay very much simple now after this we also have to learn about one very important thing which is called as retrievers okay so I will go ahead and write something called as retri retriever Lang chain okay now what exactly retriever Lang chain is it is an interface that Recs document given an unstructured query it is more General than a vector store right a retriever does not need to be able to store the documents only to return or retrieve them Vector store can be based as the back backbone of the retriever now see there is a vector store which is having some information some Vector stored in it right if I want to take out any data from there right I can actually do a similarity search which we have already seen okay but langen what it did is that since we usually do a lot of programming in a way right where in classes are used interfaces are used so it created a separate interface which is called as Retriever and that interface has a backend source to that particular Vector store to retrieve any information right whenever a query is given right that entire Vector store will be passing the information through this retriever okay so what we will do is that here I will quickly open this I have also written some amount of description so that it will be helpful for you whenever you probably go ahead and check it this entire materials okay so now what I will do I will just go ahead and write DB Dot DB dot as retriever now once I do like this DB do as retriever this basically has become my retriever right so what we have done DB is our Vector store already it is there we have connected to an interface which is basically this particular variable now okay so if you go ahead and ex uh probably display this what is retriever it is nothing but it is a vector store retriever internally you'll be also able to see that what all it is implemented F and open AI Bings and all all the information are there now retriever is done chain is also done okay now is the time that what I will do I will try to use this Retriever and document chain both together to probably see when we combine both of them then only we'll be able to get the response right so with respect to this now let's go ahead and create my retriever chain okay so the next step is what but since I need to combine both of them one is Retriever and one is the document chain right this document chain is responsible for putting the information in the context when we combine both of them then it becomes a retriever chain now what is the definition this chain takes an input as a user inquiry which is then passed to the retriever to fetch the relevant documents so it passes through the retriever it is connected to the vector store then those documents are then passed to an llm to generate the response and this l M that we are basically getting it is basically coming from what this document chain understand the flow okay so let me just go ahead and mention this flow again so that it becomes very much easy for you so whenever the users this is my user okay whenever the user asks for any inquiry okay any inquiry so first what it is going it is going to this retriever okay very much important okay so this this is my retriever this retriever is an interface to what Vector store which has all the information right so once we basically get the retriever then the next step what it happens it goes to what it goes to my llm model with some prompt right there will be some prompt involved to this and how this is basically happening with the help of stuff document chain so this stuff document chain has has already both these things combined llm and prompt right and then finally we get our response I hope you're able to understand this right and this is what we have basically implemented over here now how to basically create a retriever chain so first for for all of this again I will be using a library um with respect to chains okay so form Lang chain Lang chain Dot chains import create retrieval chain right and there are lot of things create Q with Source chain retrieval chain this chain that chain I will try to explain you all of things don't worry okay I will cast the right kind of use case and I will be showcasing you all these things don't worry about that okay then what I will do I will I will take this entire create retrieval chain and then I will create my uh retrieval chain so here I will write retrieval chain is equal to create retrieval chain and here I'm going to use first parameter that I'm going to use is retriever then the second parameter is nothing but document chain so once I have this chain right now I will be able to invoke any queries so I will go ahead and write retrieval chain dot invoke okay and now what are the parameters that I have to to give nothing I have to give my input so the input will be given over here colon and whatever input that I can give uh let's say from the PDF I have put some input over here let me just copy and paste it okay so this is one of the text that is available in the PDF that is attention. PDF and now if I invoke this you'll be able to see that I will be able to get the entire response okay so retrieval do chain. invoke and again we are using open source llm model that is Lama 2 okay yeah I've used openi bidding so here you can see this is my input this was the context right all the context information is there and finally I get the answer and this answer I will just try to save it over here something like response uh res response okay and then I will execute response response of answer so this finally becomes my output that is probably coming over here okay so this if I execute it the answer to the question is right and all the information you can see the answer to the question and it is retrieving all the details right so here you'll be able to see that how beautifully with the help of llm we have constructed this entire thing and we have used this chains Retriever and this is the first step towards developing a advanced rack pipeline right so whatever question you ask let let me just open this and probably show you some more examples okay so what I will do I will just open my download page let's see my download page I'll ask for any statement okay just a second um attention Okay so this is my thing uh let me just go ahead and search for anything the decoder is the this and this okay so I'll be searching from here to here okay now let me go ahead and change my input and search for it so chain. invoke I've done and here I've got the response now let me just go and everything uh the answer to this question is six um the decoder is also composed of stack of n6 oh it has basically taken this no worries okay let's take some more thing I'll write scaled do product attention some more examples oops uh error okay just a second no I think it should not be okay it is taking that question and it is trying to form some answers out of it okay got it got it it is not a mistake I thought it was a mistake mistake out there scale is a type of this this this see I'm getting all the answers over here so now it has become a perfect Q&A um Q&A with rack pipeline along with this Retriever and chain with Lang chain so I hope you like this particular video this was it from my side I'll see you in the next video have a great day thank you one all take care bye-bye
Info
Channel: Krish Naik
Views: 21,950
Rating: undefined out of 5
Keywords:
Id: tIwi92nkcu0
Channel Id: undefined
Length: 19min 48sec (1188 seconds)
Published: Wed Apr 10 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.