Q&A Document Retrieval With DPR
Key Takeaways
This video teaches Q&A document retrieval using DPR
Full Transcript
okay so in the previous video what we did was sell our elasticsearch document store to contain all of our paragraphs from meditations so we did that in this script here and all together we only have it's not that much data 508 paragraphs or documents within our document store so what we now want to do is set up the next part of our retriever reader sack which is the retriever and what the retriever will do is given a query it will communicate with our elasticsearch document store and return a certain number of contacts which are the paragraphs in our case that i think are most relevant to our query so that's what we are going to be doing here and the first thing that we need to do is initialize our document store again so i'm just going to copy these and paste them here and this would just initialize it from what we've already built so it's using the same index that already exists so just initialize that and once you have a document store okay cool we have that now now what we want to do is set up our dpr which is a dense passage retriever which essentially uses dense vectors and a type of efficient similarity search to embed these indexes as dense vectors and then once it comes to actually searching and finding the most similar or the most relevant documents later on it will use those dense vectors and find the most similar ones so i'll explain that a little bit better in a moment so first what we want to do is actually initialize that so we do from haystack dense retriever import dense passage retriever sorry it's the other way around here so retriever dense and then we'll put into a a variable called retriever which uses the dense passage retriever from up here and in here we need to pass a few parameters so the first thing is the document store so the document store is just what we've already initialized doc sol and then we need to initialize two different models so let's see query embedding model and the passage embedding model now behind the scenes haystack is using the hooking face transformers library so what we'll do is we'll head over to the models over there and see which embedding models we can use for dpr okay so here let's just search for dpr and you'll find we have all these models from facebook ai now with dpr the reason that it's so useful for question answering is that we have what are two different models that encode the text that we pass into it so we have this sort of setup during training and what we see down here are these two models we have this ep encoder and we also have this eq encoder now the ep encoder encodes the passages or the context so essentially the paragraphs that we have fed into our elasticsearch model this is what we'll be encoding them into these vectors here now this is during training this whole graph so all we will actually see when we're encoding these vectors is we will see the ep encoder and this will create the ep vectors and all we're going to do is feed in all of the documents from elasticsearch into this now once all of these have been encoded we then have a new set of dense vectors and all of those will be fed back into our document store so back into elastic now when it comes to performing similarity search later on we're going to ask question and that question will be processed by the eq encoder so here we have our eq encoder and we have our questions so that we'll go into here and that will encode our question and then send it over to elastic and say okay what are the most similar vectors to this vector that we've created from a question and the reason that dpr is so good is that if you look at the training down here we are creating these ep vectors and these eq vectors that are matching so where we have a matching question to a matching context we are training them to maximize the dot product because the dot product measures the alignment between those two vectors so what happens is that a relevant passage and a relevant question will come out to have a very similar vector so one example that i like to use is if our question was what is the capital of france the embedding that i will create from that will create a context that looks something like the capital of france is and you know something here we don't know why we'll pop because it doesn't actually know what capital france is it's just doing linguistic transformations to try and figure out what sort of context the answer would come from then of course when you feed this context into elastic the most similar vector will be the one which contains the answer to our question okay because the answer to our question which something like the capital france is paris now we don't have paris here but it will be able to figure that out because it will be the most similar sequence to the context that dpr has produced now back to hugging face here you can see we have these multiple dpr models and what we want is a pair we want a question encoder and a ctx which is context encoder now we'll be using this single nq base so what i'll do is just copy this and in here we just add in our model okay so that's a question encoder now what we also need is the context encoder which is instead of question here we just add ctx now we have two other parameters that we need to add in here which is use gpu which is if you're using a gpu obviously you set this to true if not you go with false it will take a little bit of time to process this if you are not using a gpu though then we also add embed title equals true as well now what we should see is this will execute without error hopefully okay great and then what we need to do is update the embeddings within elasticsearch so what we've done here is kind of set up the process and now what we need to do is update the documents that we have in elasticsearch to have dpr embeddings so to do that we go dot store update embeddings and then in here we pass our retriever okay now this may take well this would be really quick for me without that main documents and even on cpu actually with the lack of documents we have it should be pretty quick so what we see here we created these embeddings and then we posted them again to our index so that is pretty cool and now what we need to do is just test that actually works now let's go with retriever and this is how we get contacts from our elasticsearch document store sorry retrieve and then we pass in a query here so let me just find something here like what did you learn from your great grandfather maybe or from varys yeah let's go from grandfather let's go grandfather so what did you what did your grandfather teach you i don't know if this is going to work but let's see okay so you see that we return quite a few contacts here now we haven't set up the full thing so we're just returning what it sees as being relevant context we are not actually extracting an answer out yet because that will be the job of our reader model so what we have is for my great grandfather we have that one so it's okay some other ones here let's type in grandfather okay so it's just returning that one which is fine it's not perfect but what we would expect to do in reality is return more so let's try another one as well and let's say who taught you about freedom of will who taught the freedom of will and we see here okay in the first one we don't get the correct answer that we want or the correct context and we go down and i saw there is so here is the context that we wanted to return so it returns that as the fourth best context which is fine because when we build our reader model later on we kind of expect that to sort those a little bit better than our retriever model this is pretty cool and i think definitely a good start so now what we have retrieved meditations so our document store and now we have also sell our retriever so we can also cross that off and next thing is our reader model so i think that's it for this video in the next one of course we'll move on to that reader model and let's just see how that goes but so far i'm pretty happy with that so thank you for watching and i'll see you again in the next one
Original Description
▶️ Stoic Q&A App Playlist: https://www.youtube.com/playlist?list=PLIUOU7oqGTLixb-CatMxNCO-mJioMmZEB
The third video in building our Stoic Q&A app.
In open-domain question answering, we typically design a model architecture that contains a data source, retriever, and reader/generator.
The first of these components is typically a document store. The two most popular stores we use here are Elasticsearch and FAISS.
Next up is our retriever — the topic of this video. The job of the retriever is to filter through our document store for relevant chunks of information (the documents) and pass them to the reader/generator model.
DPR (dense passage retriever) is a dense vector retriever that is trained on question-context pairs. Encoding both accordingly - enabling super accurate similarity indexing.
🤖 70% Discount on the NLP With Transformers in Python course:
https://bit.ly/3DFvvY5
If you're interested in learning more about DPR, I wrote about it on Medium here:
https://towardsdatascience.com/how-to-create-an-answer-from-a-question-with-dpr-d76e29cc5d60
(Free link):
https://towardsdatascience.com/how-to-create-an-answer-from-a-question-with-dpr-d76e29cc5d60?sk=1bdd7c1bff80bf51410962691c690c69
🕹️ Free AI-Powered Code Refactoring with Sourcery:
https://sourcery.ai/?utm_source=YouTub&utm_campaign=JBriggs&utm_medium=aff
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from James Briggs · James Briggs · 29 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
▶
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Stoic Philosophy Text Generation with TensorFlow
James Briggs
How to Build TensorFlow Pipelines with tf.data.Dataset
James Briggs
Every New Feature in Python 3.10.0a2
James Briggs
How-to Build a Transformer for Language Classification in TensorFlow
James Briggs
How-to use the Kaggle API in Python
James Briggs
Language Generation with OpenAI's GPT-2 in Python
James Briggs
Text Summarization with Google AI's T5 in Python
James Briggs
How-to do Sentiment Analysis with Flair in Python
James Briggs
Python Environment Setup for Machine Learning
James Briggs
Sequential Model - TensorFlow Essentials #1
James Briggs
Functional API - TensorFlow Essentials #2
James Briggs
Training Parameters - TensorFlow Essentials #3
James Briggs
Input Data Pipelines - TensorFlow Essentials #4
James Briggs
6 of Python's Newest and Best Features (3.7-3.9)
James Briggs
Novice to Advanced RegEx in Less-than 30 Minutes + Python
James Briggs
Building a PlotLy $GME Chart in Python
James Briggs
How-to Use The Reddit API in Python
James Briggs
How to Build Custom Q&A Transformer Models in Python
James Briggs
How to Build Q&A Models in Python (Transformers)
James Briggs
How-to Decode Outputs From NLP Models (Python)
James Briggs
Identify Stocks on Reddit with SpaCy (NER in Python)
James Briggs
Sentiment Analysis on ANY Length of Text With Transformers (Python)
James Briggs
Unicode Normalization for NLP in Python
James Briggs
The NEW Match-Case Statement in Python 3.10
James Briggs
Multi-Class Language Classification With BERT in TensorFlow
James Briggs
How to Build Python Packages for Pip
James Briggs
How-to Structure a Q&A ML App
James Briggs
How to Index Q&A Data With Haystack and Elasticsearch
James Briggs
Q&A Document Retrieval With DPR
James Briggs
How to Use Type Annotations in Python
James Briggs
Extractive Q&A With Haystack and FastAPI in Python
James Briggs
Sentence Similarity With Sentence-Transformers in Python
James Briggs
Sentence Similarity With Transformers and PyTorch (Python)
James Briggs
NER With Transformers and spaCy (Python)
James Briggs
Training BERT #1 - Masked-Language Modeling (MLM)
James Briggs
Training BERT #2 - Train With Masked-Language Modeling (MLM)
James Briggs
Training BERT #3 - Next Sentence Prediction (NSP)
James Briggs
Training BERT #4 - Train With Next Sentence Prediction (NSP)
James Briggs
FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
James Briggs
New Features in Python 3.10
James Briggs
Training BERT #5 - Training With BertForPretraining
James Briggs
How-to Use HuggingFace's Datasets - Transformers From Scratch #1
James Briggs
Build a Custom Transformer Tokenizer - Transformers From Scratch #2
James Briggs
3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
James Briggs
3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
James Briggs
Building MLM Training Input Pipeline - Transformers From Scratch #3
James Briggs
Training and Testing an Italian BERT - Transformers From Scratch #4
James Briggs
Faiss - Introduction to Similarity Search
James Briggs
Angular App Setup With Material - Stoic Q&A #5
James Briggs
Why are there so many Tokenization methods in HF Transformers?
James Briggs
Choosing Indexes for Similarity Search (Faiss in Python)
James Briggs
Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
James Briggs
How LSH Random Projection works in search (+Python)
James Briggs
IndexLSH for Fast Similarity Search in Faiss
James Briggs
Faiss - Vector Compression with PQ and IVFPQ (in Python)
James Briggs
Product Quantization for Vector Similarity Search (+ Python)
James Briggs
How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
James Briggs
Metadata Filtering for Vector Search + Latest Filter Tech
James Briggs
Build NLP Pipelines with HuggingFace Datasets
James Briggs
Composite Indexes and the Faiss Index Factory
James Briggs
🎓
Tutor Explanation
DeepCamp AI