Skip to content
tezvyn:

Dense Passage Retrieval (DPR): Semantic Search for QA

Source: huggingface.coMediumHow cards are made

Dense Passage Retrieval (DPR): Semantic Search for QA

DPR finds answers by meaning, not just keywords. It converts questions and documents into vectors and finds the closest matches, forming the core of Retrieval-Augmented Generation (RAG).

Why it exists

Traditional search methods like TF-IDF or BM25 excel at matching keywords but fail when a question and its answer use different words for the same concept. For open-domain question answering, you need passages that are semantically relevant, not just lexically similar. DPR was created to bridge this semantic gap using dense vector embeddings.

The mental model

Think of DPR as a librarian who understands the meaning of your question, not just the words you used. It takes a user's query and a library of documents and maps them both into a shared "meaning space" represented by numerical vectors. The documents whose vectors are closest to the question's vector are the most relevant, even if they share no keywords. It replaces word counting with measuring conceptual distance.

How it works

DPR uses two separate encoder models, typically based on a Transformer architecture like BERT. One model is fine-tuned to encode questions into a query vector, and the other is fine-tuned to encode document passages into context vectors. During indexing, all passages in your knowledge base are passed through the context encoder, and their resulting vectors are stored in a vector database. At query time, the user's question is passed through the query encoder. The resulting vector is used to search the database for the nearest neighbors, typically using dot-product or cosine similarity. These top-k passages are the retrieved context.

When to use it

DPR is the retrieval engine in modern Retrieval-Augmented Generation (RAG) systems. It is ideal for building question-answering applications over large, unstructured text corpora, such as internal wikis, technical documentation, or a customer support knowledge base. It is the foundation for most semantic search implementations.

When not to use it

DPR can be overkill for simple keyword search where exact matches are sufficient. It requires significant upfront computation to embed the entire corpus and a specialized vector database for efficient search. If your documents are highly structured or your search needs are purely lexical, older methods like BM25 can be more efficient and equally effective.

One canonical example

A user asks a support bot, "How do I regain access to my account if I've lost my phone?" DPR encodes this question into a vector. It then searches a vector database containing embeddings of all help articles. It finds that the vector for an article titled "Account Recovery Options" is the closest match, even though that article doesn't contain the exact phrase "lost my phone". This article is then passed to an LLM to generate a precise, contextualized answer.

Interview question

Which statement best describes how Dense Passage Retrieval (DPR) primarily achieves semantic search for question answering?

  • a.It enhances traditional keyword search by incorporating advanced synonym dictionaries and linguistic rules.
  • b.It transforms questions and document passages into numerical vectors and identifies relevant passages by vector similarity.Correct
  • c.It uses a single neural network to directly generate answers from relevant document snippets.
  • d.It pre-indexes all possible question variations and their corresponding exact answer passages.
Why?

DPR's core mechanism is to convert both questions and document passages into dense numerical vectors using separate encoder models. It then finds semantically relevant passages by identifying the closest vectors in a shared 'meaning space'. Option C is incorrect because DPR is a retrieval system, not a generative one, and it uses two distinct encoders for queries and documents.

Just read this? Test yourself on what you have been reading.

Read the original → huggingface.co

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on llm — each one lists the topics its interview covers.

See open roles