Better Retrieval Augmented Generation (RAG) with LangChain Parent-Child Retriever

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to learn about Lang Chain's parent child retriever which can be used in retrieval augmented generation or rag apps now in a rag app we'll create embeddings of chunks of text in our documents and store those embeddings somewhere and we'll then retrieve the appropriate chunks based on the questions asked now with the parent child retriever we still embed those same chunks but each chunk also has a link to a bigger parent chunk that it's part of and in the retrieval step we'll still look up those child chunks to find the most specific piece of text to answer our question but we'll then return the text for the chunks parent so that we give the llm more context to work with let's now come over to a Jupiter notebook and we're going to start by loading the text from a recent BBC article about the AI safety Summit in the UK so we've got it in a file called ai. text we'll just read that into the text variable and if we scroll down you can see it's about an interview between Elon Musk and rishy sunuk we're now going to con convert that text into a lang chain document using the the page content will be the text and we'll pass in some metadata as well and then if we return the documents array you can see it's exactly the same text that we just saw now we're going to store these that document in chroma DB so let's import some libraries and we're then going to create a parent splitter so that's going to get the bigger chunks of text a Char splitter so that's going to get the smaller chunks of text inside the the the the those bigger ones we're then going to create our chroma database we'll be using the fast embed library to embed our chunks and then we're going to create an inmemory store for the parent documents we'll then create the Retriever and that's going to take in the parent and child stores as well as the child and parent splits and then let's add the our single document to the retriever we're also going to create a variable that has the child retriever so that we can compare the two against each other now we're going to query the parent and child retrievers and see what we get back so let's let's ask the child one first get me some documents for Elon Musk and you can see it comes back with some chunks of text the ones which mentioned his name what about if we do the same thing with the parent one so you can see this time it's it's again it's pulling in the like the same sort of area of the article but it's also getting in a whole bunch of extra context as well so it's getting in much bigger chunks let's now have a look what happens if we pass that into an llm we're going to use the Alama tool which we can use to run llms on our machine and we're going to use the zepha model which is a a fine-tuned model on top of the mystal AI llm so let's create a the let's initialize the an alarm model and we'll pass in the The Zephyr as the model name and then we're going to create a prompt template to tell it hey I want you to answer some questions I'm going to pass you some context I don't want you to use any other information and then we'll initialize a prompt template and now we're going to create what we call a QA chain so this is really good for for doing question and answer so we'll create one for the using the child retriever so noit the the retriever this time is child and then we'll do exactly the same but this time changing the retriever to be that parent retriever otherwise everything else is the same and on both of them were telling it I want you to return me the source document so that we can have a look what it used to answer the questions now let's answer some questions so what does Elon Musk have to say about jobs and AI so we'll ask the child one first as you can see it comes back Elon M predicts AI will eventually make work redundant and there will come a point when no job is needed what about if we ask the the parent chain instead so you can see this time same answer but it then also adds in a bunch of context about humanoid robots and and some other some other information as well potentially being able to use AI to improve the government's website if we then have a look at the source documents for the child you can see it's kind of interesting it does have more context than was used in the answer but it hasn't actually used everything what about the parent one so you can see this time if we look through we can see that it does actually in the in the in the parent context it mentions the idea of having a referee and if we scroll down a little bit more you can see there it also mentions the government website and that wasn't in the the child chunk so that is some extra information that it was able to get let's ask it another question so this time tell me about apparently Elon mus tweeted a cartoon just be just before they were they were speaking so we'll ask the child one uh and so it actually gives me a really detailed answer this time sometimes I found it can be quite T and it just says hey yeah there was a cartoon interestingly if you know look at the end there's a hallucination about as if uh musk was talking with Boris Johnson who was actually the the previous but one prime minister to rishy sunx that's kind of an interesting thing it definitely doesn't I don't think it mentions Boris Johnson in there but it's kind of pulled him in and again if we do the same with the the the parent one actually the answer is almost almost the same there's not not too much extra that it's giving here and again if we look through the documents we can see the child One is using much smaller bits of text and the the parent One is using much bigger ones as this looks like quite an interesting Evolution as part of the retrieval part of a rag app I definitely got to play around with those chunk sizes to see if you make it bigger smaller do you get better or even worse results uh if you liked this video you might also like this one up here where I give an introduction to retrieval augmented generation
Info
Channel: Learn Data with Mark
Views: 3,957
Rating: undefined out of 5
Keywords:
Id: wSi0fxkH6e0
Channel Id: undefined
Length: 5min 27sec (327 seconds)
Published: Thu Nov 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.