Qdrant - Vector Database
Qdrant + LangChain
You already have a Qdrant collection with thousands of documents. Now you want to add 'Ask your knowledge base' - a chat that answers questions. LangChain makes this possible in 15 lines of code. Let's break it down.
- **Enterprise search:** company documentation → QdrantVectorStore → RetrievalQA → employees ask questions in natural language
- **Customer support:** ticket and FAQ database → retriever with product filter → automatic answers to similar questions
- **Research tool:** scientific paper database → MMR search for diverse results → LLM synthesizes a topic overview
Предварительные знания
Installation and QdrantVectorStore Initialization
**LangChain** is the most popular framework for building LLM applications. Qdrant has a native integration via `@langchain/qdrant`. This lets you use all Qdrant features (filtering, hybrid search) through the familiar LangChain API.
**`collectionName`** - if the collection doesn't exist, LangChain creates it automatically with the vector dimension matching the selected embeddings model. OpenAI `text-embedding-3-small` → 1536 dim, `text-embedding-3-large` → 3072 dim.
You need to connect to an already existing Qdrant collection via LangChain. Which method should you use?
addDocuments, similaritySearch, and MMR Search
**QdrantVectorStore** implements the standard LangChain `VectorStore` interface with three key search methods: `similaritySearch` - basic similarity search, `similaritySearchWithScore` - with score values, `maxMarginalRelevanceSearch` (MMR) - search accounting for result diversity.
**MMR (Maximal Marginal Relevance)** is especially useful in RAG: if the database has 10 similar documents about one fact, MMR returns a diverse subset of them. This gives the LLM a broader context. `lambda: 0.7` is a good starting balance.
You're building a RAG for documentation. The user asks about 'installation'. The database has 15 documents about installation, but they're very similar. Which search method should you prefer?
RetrievalQA Chain and Custom Retriever
**Retriever** - a LangChain abstraction for a document source. `QdrantVectorStore.asRetriever()` creates a retriever that can be used in any LangChain chain: `RetrievalQA`, `ConversationalRetrievalChain`, LCEL pipelines.
**LCEL vs RetrievalQAChain:** `RetrievalQAChain` is a legacy API - convenient but less flexible. LCEL (LangChain Expression Language) is the modern approach: composable, streaming-ready, and easy to customize prompts and logic. For new projects, LCEL is recommended.
"LangChain completely hides Qdrant - you can't use native features like filters"
LangChain passes filters directly to Qdrant, supporting the full filter syntax: must/should/must_not, range, geo, nested filters.
QdrantVectorStore is a thin wrapper over @qdrant/js-client-rest. The filter parameter is passed as-is to the Qdrant query. You can use any Qdrant filters including nested conditions and geosearch.
You want RetrievalQA to only return documents from the 'technical' category. Where do you set the filter?
Key Ideas
- **QdrantVectorStore** - LangChain wrapper around Qdrant: fromExistingCollection, fromDocuments, fromTexts
- **addDocuments** - adds with automatic embedding generation; returns point IDs
- **similaritySearch** - basic search; **maxMarginalRelevanceSearch** - with result diversity
- **Filters** - passed directly in Qdrant syntax via the third argument of search or in asRetriever()
- **asRetriever()** → **RetrievalQA** / LCEL pipeline - the standard pattern for RAG applications
What's Next
LangChain is not the only framework for working with Qdrant. LlamaIndex offers a different approach focused on data ingestion and structured retrieval.
- Qdrant + LlamaIndex — Alternative framework: a more structured approach to ingestion and retrieval
- Production RAG Pipeline — Full pipeline: chunking → embeddings → upsert → search → reranking → LLM
- Payload Filtering — Deep understanding of Qdrant filters used in LangChain
Вопросы для размышления
- What is the difference between similaritySearch and maxMarginalRelevanceSearch? Which tasks are each better suited for?
- How does an LCEL pipeline differ from RetrievalQAChain? What advantages does LCEL offer for production systems?
- You're using LangChain + Qdrant in production. How do you handle document updates - use addDocuments for new ones, or delete + reindex for changed ones?