mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
[docs]: vector store integration pages (#24858)
Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
@@ -39,14 +39,138 @@ class RetrievalMode(str, Enum):
|
||||
|
||||
|
||||
class QdrantVectorStore(VectorStore):
|
||||
"""`QdrantVectorStore` - Vector store implementation using https://qdrant.tech/
|
||||
"""Qdrant vector store integration.
|
||||
|
||||
Example:
|
||||
Setup:
|
||||
Install ``langchain-qdrant`` and ``qdrant-client[fastembed]`` packages.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
pip install -qU langchain-qdrant 'qdrant-client[fastembed]'
|
||||
|
||||
Key init args — indexing params:
|
||||
collection_name: str
|
||||
Name of the collection.
|
||||
embedding: Embeddings
|
||||
Embedding function to use.
|
||||
|
||||
Key init args — client params:
|
||||
client: QdrantClient
|
||||
Qdrant client to use.
|
||||
retrieval_mode: RetrievalMode
|
||||
Retrieval mode to use.
|
||||
|
||||
Instantiate:
|
||||
.. code-block:: python
|
||||
from langchain_qdrant import QdrantVectorStore
|
||||
|
||||
store = QdrantVectorStore.from_existing_collection("my-collection", embedding, url="http://localhost:6333")
|
||||
"""
|
||||
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:
|
||||
.. code-block:: python
|
||||
|
||||
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:
|
||||
.. code-block:: python
|
||||
|
||||
vector_store.delete(ids=[ids[-1]])
|
||||
|
||||
Search:
|
||||
.. code-block:: python
|
||||
|
||||
results = vector_store.similarity_search(query="thud",k=1)
|
||||
for doc in results:
|
||||
print(f"* {doc.page_content} [{doc.metadata}]")
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
* thud [{'bar': 'baz', '_id': '0d706099-6dd9-412a-9df6-a71043e020de', '_collection_name': 'demo_collection'}]
|
||||
|
||||
Search with filter:
|
||||
.. code-block:: python
|
||||
|
||||
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}]")
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
* thud [{'bar': 'baz', '_id': '0d706099-6dd9-412a-9df6-a71043e020de', '_collection_name': 'demo_collection'}]
|
||||
|
||||
|
||||
Search with score:
|
||||
.. code-block:: python
|
||||
|
||||
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}]")
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
* [SIM=0.832268] foo [{'baz': 'bar', '_id': '44ec7094-b061-45ac-8fbf-014b0f18e8aa', '_collection_name': 'demo_collection'}]
|
||||
|
||||
Async:
|
||||
.. code-block:: python
|
||||
|
||||
# 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}]")
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
* [SIM=0.832268] foo [{'baz': 'bar', '_id': '44ec7094-b061-45ac-8fbf-014b0f18e8aa', '_collection_name': 'demo_collection'}]
|
||||
|
||||
Use as Retriever:
|
||||
.. code-block:: python
|
||||
|
||||
retriever = vector_store.as_retriever(
|
||||
search_type="mmr",
|
||||
search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5},
|
||||
)
|
||||
retriever.invoke("thud")
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
[Document(metadata={'bar': 'baz', '_id': '0d706099-6dd9-412a-9df6-a71043e020de', '_collection_name': 'demo_collection'}, page_content='thud')]
|
||||
|
||||
""" # noqa: E501
|
||||
|
||||
CONTENT_KEY: str = "page_content"
|
||||
METADATA_KEY: str = "metadata"
|
||||
|
||||
Reference in New Issue
Block a user