Qdrant Integration for LangChain
langchain-qdrant is an integration package connecting the LangChain framework with Qdrant, a high-performance vector similarity search engine. It enables developers to easily utilize Qdrant as a vector store for various LLM-based applications, including Retrieval-Augmented Generation (RAG). The current version is 1.1.0, and as a LangChain partner package, it follows semantic versioning, with minor releases adding new features without breaking changes and frequent patch versions for bug fixes.
Warnings
- deprecated The `Qdrant` class is deprecated in favor of `QdrantVectorStore`. While still supported, new development should use `QdrantVectorStore` for modern functionalities and better API design.
- gotcha When using `QdrantVectorStore` with Qdrant's new Query API (for features like sparse or hybrid retrieval), Qdrant server version 1.10.0 or above is required. Older Qdrant server versions may not support all features, leading to unexpected behavior or errors.
- gotcha Performance bottlenecks can occur when dealing with large document collections, primarily during the embedding process and initial data loading into Qdrant. Default settings for Qdrant collections might not be optimized for bulk inserts.
- gotcha Updating existing documents in a Qdrant collection via `add_documents` or `add_texts` requires passing the same `id` as the original document. Otherwise, new points will be created instead of overwriting existing ones, leading to duplicate entries or unexpected search results.
- breaking While `langchain-qdrant` itself adheres to semantic versioning, the broader LangChain ecosystem (especially the `langchain` and `langchain-community` packages) has undergone significant API changes, notably with the transition to LangChain Expression Language (LCEL) and a more modular structure around LangChain v1.0. This can lead to incompatibility if other LangChain components are not aligned.
Install
-
pip install langchain-qdrant
Imports
- QdrantVectorStore
from langchain_qdrant import QdrantVectorStore
- Qdrant
from langchain_qdrant import Qdrant
- QdrantClient
from qdrant_client import QdrantClient
Quickstart
import os
from qdrant_client import QdrantClient
from qdrant_client.http.models import Distance, VectorParams
from langchain_qdrant import QdrantVectorStore
from langchain_openai import OpenAIEmbeddings # Requires `pip install langchain-openai`
from langchain_core.documents import Document
# Set your OpenAI API key for embeddings (replace with your preferred embedding model)
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY", "sk-...")
# 1. Initialize an in-memory Qdrant client
client = QdrantClient(":memory:")
# 2. Define collection parameters (e.g., for OpenAI embeddings)
collection_name = "my_langchain_collection"
embedding_dimension = 1536 # For text-embedding-ada-002 or text-embedding-3-small
client.create_collection(
collection_name=collection_name,
vectors_config=VectorParams(size=embedding_dimension, distance=Distance.COSINE),
)
# 3. Initialize the embedding model
embeddings = OpenAIEmbeddings()
# 4. Create a QdrantVectorStore instance
vector_store = QdrantVectorStore(
client=client,
collection_name=collection_name,
embedding=embeddings,
)
# 5. Add documents to the vector store
documents = [
Document(page_content="The quick brown fox jumps over the lazy dog.", metadata={"source": "sentence 1"}),
Document(page_content="LangChain provides many integrations with vector stores.", metadata={"source": "sentence 2"}),
Document(page_content="Qdrant is an open-source vector database.", metadata={"source": "sentence 3"}),
]
vector_store.add_documents(documents)
# 6. Perform a similarity search
query = "What is Qdrant?"
found_docs = vector_store.similarity_search(query, k=1)
print(f"Query: {query}")
for doc in found_docs:
print(f"- Content: {doc.page_content}, Metadata: {doc.metadata}")