QdrantVectorStore(
self,
client: QdrantClient,
collection_name: str,
embedding: Embeddings| Name | Type |
|---|---|
| client | QdrantClient |
| collection_name | str |
| embedding | Embeddings | None |
| retrieval_mode | RetrievalMode |
| vector_name | str |
| content_payload_key | str |
| metadata_payload_key | str |
| distance | models.Distance |
| sparse_embedding | SparseEmbeddings | None |
| sparse_vector_name | str |
| validate_embeddings | bool |
| validate_collection_config | bool |
Qdrant vector store integration.
Setup:
Install langchain-qdrant package.
pip install -qU langchain-qdrant
Key init args — indexing params: collection_name: Name of the collection. embedding: Embedding function to use. sparse_embedding: Optional sparse embedding function to use.
Key init args — client params: client: Qdrant client to use. retrieval_mode: Retrieval mode to use.
Instantiate:
from langchain_qdrant import QdrantVectorStore
from qdrant_client import QdrantClient
from qdrant_client.http.models import Distance, VectorParams
from langchain_openai import OpenAIEmbeddings
client = QdrantClient(":memory:")
client.create_collection(
collection_name="demo_collection",
vectors_config=VectorParams(size=1536, distance=Distance.COSINE),
)
vector_store = QdrantVectorStore(
client=client,
collection_name="demo_collection",
embedding=OpenAIEmbeddings(),
)
Add Documents:
from langchain_core.documents import Document
from uuid import uuid4
document_1 = Document(page_content="foo", metadata={"baz": "bar"})
document_2 = Document(page_content="thud", metadata={"bar": "baz"})
document_3 = Document(page_content="i will be deleted :(")
documents = [document_1, document_2, document_3]
ids = [str(uuid4()) for _ in range(len(documents))]
vector_store.add_documents(documents=documents, ids=ids)
Delete Documents:
vector_store.delete(ids=[ids[-1]])
Search:
results = vector_store.similarity_search(
query="thud",
k=1,
)
for doc in results:
print(f"* {doc.page_content} [{doc.metadata}]")
*thud[
{
"bar": "baz",
"_id": "0d706099-6dd9-412a-9df6-a71043e020de",
"_collection_name": "demo_collection",
}
]
Search with filter:
from qdrant_client.http import models
results = vector_store.similarity_search(
query="thud",
k=1,
filter=models.Filter(
must=[
models.FieldCondition(
key="metadata.bar",
match=models.MatchValue(value="baz"),
)
]
),
)
for doc in results:
print(f"* {doc.page_content} [{doc.metadata}]")
*thud[
{
"bar": "baz",
"_id": "0d706099-6dd9-412a-9df6-a71043e020de",
"_collection_name": "demo_collection",
}
]
Search with score:
results = vector_store.similarity_search_with_score(query="qux", k=1)
for doc, score in results:
print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
* [SIM=0.832268] foo [{'baz': 'bar', '_id': '44ec7094-b061-45ac-8fbf-014b0f18e8aa', '_collection_name': 'demo_collection'}]
Async:
# add documents
# await vector_store.aadd_documents(documents=documents, ids=ids)
# delete documents
# await vector_store.adelete(ids=["3"])
# search
# results = vector_store.asimilarity_search(query="thud",k=1)
# search with score
results = await vector_store.asimilarity_search_with_score(query="qux", k=1)
for doc, score in results:
print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
* [SIM=0.832268] foo [{'baz': 'bar', '_id': '44ec7094-b061-45ac-8fbf-014b0f18e8aa', '_collection_name': 'demo_collection'}]
Use as Retriever:
retriever = vector_store.as_retriever(
search_type="mmr",
search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5},
)
retriever.invoke("thud")
[
Document(
metadata={
"bar": "baz",
"_id": "0d706099-6dd9-412a-9df6-a71043e020de",
"_collection_name": "demo_collection",
},
page_content="thud",
)
]