Q&A Document Retrieval With DPR

James Briggs · Beginner ·🧠 Large Language Models ·5y ago

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 Stoic Philosophy Text Generation with TensorFlow
Stoic Philosophy Text Generation with TensorFlow
James Briggs
2 How to Build TensorFlow Pipelines with tf.data.Dataset
How to Build TensorFlow Pipelines with tf.data.Dataset
James Briggs
3 Every New Feature in Python 3.10.0a2
Every New Feature in Python 3.10.0a2
James Briggs
4 How-to Build a Transformer for Language Classification in TensorFlow
How-to Build a Transformer for Language Classification in TensorFlow
James Briggs
5 How-to use the Kaggle API in Python
How-to use the Kaggle API in Python
James Briggs
6 Language Generation with OpenAI's GPT-2 in Python
Language Generation with OpenAI's GPT-2 in Python
James Briggs
7 Text Summarization with Google AI's T5 in Python
Text Summarization with Google AI's T5 in Python
James Briggs
8 How-to do Sentiment Analysis with Flair in Python
How-to do Sentiment Analysis with Flair in Python
James Briggs
9 Python Environment Setup for Machine Learning
Python Environment Setup for Machine Learning
James Briggs
10 Sequential Model - TensorFlow Essentials #1
Sequential Model - TensorFlow Essentials #1
James Briggs
11 Functional API - TensorFlow Essentials #2
Functional API - TensorFlow Essentials #2
James Briggs
12 Training Parameters - TensorFlow Essentials #3
Training Parameters - TensorFlow Essentials #3
James Briggs
13 Input Data Pipelines - TensorFlow Essentials #4
Input Data Pipelines - TensorFlow Essentials #4
James Briggs
14 6 of Python's Newest and Best Features (3.7-3.9)
6 of Python's Newest and Best Features (3.7-3.9)
James Briggs
15 Novice to Advanced RegEx in Less-than 30 Minutes + Python
Novice to Advanced RegEx in Less-than 30 Minutes + Python
James Briggs
16 Building a PlotLy $GME Chart in Python
Building a PlotLy $GME Chart in Python
James Briggs
17 How-to Use The Reddit API in Python
How-to Use The Reddit API in Python
James Briggs
18 How to Build Custom Q&A Transformer Models in Python
How to Build Custom Q&A Transformer Models in Python
James Briggs
19 How to Build Q&A Models in Python (Transformers)
How to Build Q&A Models in Python (Transformers)
James Briggs
20 How-to Decode Outputs From NLP Models (Python)
How-to Decode Outputs From NLP Models (Python)
James Briggs
21 Identify Stocks on Reddit with SpaCy (NER in Python)
Identify Stocks on Reddit with SpaCy (NER in Python)
James Briggs
22 Sentiment Analysis on ANY Length of Text With Transformers (Python)
Sentiment Analysis on ANY Length of Text With Transformers (Python)
James Briggs
23 Unicode Normalization for NLP in Python
Unicode Normalization for NLP in Python
James Briggs
24 The NEW Match-Case Statement in Python 3.10
The NEW Match-Case Statement in Python 3.10
James Briggs
25 Multi-Class Language Classification With BERT in TensorFlow
Multi-Class Language Classification With BERT in TensorFlow
James Briggs
26 How to Build Python Packages for Pip
How to Build Python Packages for Pip
James Briggs
27 How-to Structure a Q&A ML App
How-to Structure a Q&A ML App
James Briggs
28 How to Index Q&A Data With Haystack and Elasticsearch
How to Index Q&A Data With Haystack and Elasticsearch
James Briggs
Q&A Document Retrieval With DPR
Q&A Document Retrieval With DPR
James Briggs
30 How to Use Type Annotations in Python
How to Use Type Annotations in Python
James Briggs
31 Extractive Q&A With Haystack and FastAPI in Python
Extractive Q&A With Haystack and FastAPI in Python
James Briggs
32 Sentence Similarity With Sentence-Transformers in Python
Sentence Similarity With Sentence-Transformers in Python
James Briggs
33 Sentence Similarity With Transformers and PyTorch (Python)
Sentence Similarity With Transformers and PyTorch (Python)
James Briggs
34 NER With Transformers and spaCy (Python)
NER With Transformers and spaCy (Python)
James Briggs
35 Training BERT #1 - Masked-Language Modeling (MLM)
Training BERT #1 - Masked-Language Modeling (MLM)
James Briggs
36 Training BERT #2 - Train With Masked-Language Modeling (MLM)
Training BERT #2 - Train With Masked-Language Modeling (MLM)
James Briggs
37 Training BERT #3 - Next Sentence Prediction (NSP)
Training BERT #3 - Next Sentence Prediction (NSP)
James Briggs
38 Training BERT #4 - Train With Next Sentence Prediction (NSP)
Training BERT #4 - Train With Next Sentence Prediction (NSP)
James Briggs
39 FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
James Briggs
40 New Features in Python 3.10
New Features in Python 3.10
James Briggs
41 Training BERT #5 - Training With BertForPretraining
Training BERT #5 - Training With BertForPretraining
James Briggs
42 How-to Use HuggingFace's Datasets - Transformers From Scratch #1
How-to Use HuggingFace's Datasets - Transformers From Scratch #1
James Briggs
43 Build a Custom Transformer Tokenizer - Transformers From Scratch #2
Build a Custom Transformer Tokenizer - Transformers From Scratch #2
James Briggs
44 3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
James Briggs
45 3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
James Briggs
46 Building MLM Training Input Pipeline - Transformers From Scratch #3
Building MLM Training Input Pipeline - Transformers From Scratch #3
James Briggs
47 Training and Testing an Italian BERT - Transformers From Scratch #4
Training and Testing an Italian BERT - Transformers From Scratch #4
James Briggs
48 Faiss - Introduction to Similarity Search
Faiss - Introduction to Similarity Search
James Briggs
49 Angular App Setup With Material - Stoic Q&A #5
Angular App Setup With Material - Stoic Q&A #5
James Briggs
50 Why are there so many Tokenization methods in HF Transformers?
Why are there so many Tokenization methods in HF Transformers?
James Briggs
51 Choosing Indexes for Similarity Search (Faiss in Python)
Choosing Indexes for Similarity Search (Faiss in Python)
James Briggs
52 Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
James Briggs
53 How LSH Random Projection works in search (+Python)
How LSH Random Projection works in search (+Python)
James Briggs
54 IndexLSH for Fast Similarity Search in Faiss
IndexLSH for Fast Similarity Search in Faiss
James Briggs
55 Faiss - Vector Compression with PQ and IVFPQ (in Python)
Faiss - Vector Compression with PQ and IVFPQ (in Python)
James Briggs
56 Product Quantization for Vector Similarity Search (+ Python)
Product Quantization for Vector Similarity Search (+ Python)
James Briggs
57 How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
James Briggs
58 Metadata Filtering for Vector Search + Latest Filter Tech
Metadata Filtering for Vector Search + Latest Filter Tech
James Briggs
59 Build NLP Pipelines with HuggingFace Datasets
Build NLP Pipelines with HuggingFace Datasets
James Briggs
60 Composite Indexes and the Faiss Index Factory
Composite Indexes and the Faiss Index Factory
James Briggs

Related Reads

📰
After Artificial Intelligence: Why AI Is Not the Final Category
Explore the concept of Artificial Sapiens and its relation to AI's evolution, and why AI may not be the final category in intelligent systems
Medium · Machine Learning
📰
LLMs Can’t Say: “I Don’t Know”
LLMs lack the ability to say 'I don't know', which can lead to providing incorrect or harmful information, and it's crucial to acknowledge and address this limitation
Medium · LLM
📰
What Happens Inside an AI Model After Fine-Tuning?
Discover what happens inside an AI model after fine-tuning and why it matters for improving model performance
Medium · LLM
📰
Field Notes: The LLM is not a security boundary
Learn why LLMs cannot be trusted as a security boundary in production systems and how to design around this limitation
Dev.to · Raj Murugan
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →