mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
@@ -8,8 +8,6 @@ from operator import itemgetter
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Any,
|
||||
Optional,
|
||||
Union,
|
||||
)
|
||||
|
||||
import numpy as np
|
||||
@@ -218,13 +216,13 @@ class QdrantVectorStore(VectorStore):
|
||||
self,
|
||||
client: QdrantClient,
|
||||
collection_name: str,
|
||||
embedding: Optional[Embeddings] = None,
|
||||
embedding: Embeddings | None = None,
|
||||
retrieval_mode: RetrievalMode = RetrievalMode.DENSE,
|
||||
vector_name: str = VECTOR_NAME,
|
||||
content_payload_key: str = CONTENT_KEY,
|
||||
metadata_payload_key: str = METADATA_KEY,
|
||||
distance: models.Distance = models.Distance.COSINE,
|
||||
sparse_embedding: Optional[SparseEmbeddings] = None,
|
||||
sparse_embedding: SparseEmbeddings | None = None,
|
||||
sparse_vector_name: str = SPARSE_VECTOR_NAME,
|
||||
validate_embeddings: bool = True, # noqa: FBT001, FBT002
|
||||
validate_collection_config: bool = True, # noqa: FBT001, FBT002
|
||||
@@ -278,7 +276,7 @@ class QdrantVectorStore(VectorStore):
|
||||
return self._client
|
||||
|
||||
@property
|
||||
def embeddings(self) -> Optional[Embeddings]:
|
||||
def embeddings(self) -> Embeddings | None:
|
||||
"""Get the dense embeddings instance that is being used.
|
||||
|
||||
Returns:
|
||||
@@ -347,31 +345,31 @@ class QdrantVectorStore(VectorStore):
|
||||
def from_texts(
|
||||
cls: type[QdrantVectorStore],
|
||||
texts: list[str],
|
||||
embedding: Optional[Embeddings] = None,
|
||||
metadatas: Optional[list[dict]] = None,
|
||||
ids: Optional[Sequence[str | int]] = None,
|
||||
collection_name: Optional[str] = None,
|
||||
location: Optional[str] = None,
|
||||
url: Optional[str] = None,
|
||||
port: Optional[int] = 6333,
|
||||
embedding: Embeddings | None = None,
|
||||
metadatas: list[dict] | None = None,
|
||||
ids: Sequence[str | int] | None = None,
|
||||
collection_name: str | None = None,
|
||||
location: str | None = None,
|
||||
url: str | None = None,
|
||||
port: int | None = 6333,
|
||||
grpc_port: int = 6334,
|
||||
prefer_grpc: bool = False, # noqa: FBT001, FBT002
|
||||
https: Optional[bool] = None, # noqa: FBT001
|
||||
api_key: Optional[str] = None,
|
||||
prefix: Optional[str] = None,
|
||||
timeout: Optional[int] = None,
|
||||
host: Optional[str] = None,
|
||||
path: Optional[str] = None,
|
||||
https: bool | None = None, # noqa: FBT001
|
||||
api_key: str | None = None,
|
||||
prefix: str | None = None,
|
||||
timeout: int | None = None,
|
||||
host: str | None = None,
|
||||
path: str | None = None,
|
||||
distance: models.Distance = models.Distance.COSINE,
|
||||
content_payload_key: str = CONTENT_KEY,
|
||||
metadata_payload_key: str = METADATA_KEY,
|
||||
vector_name: str = VECTOR_NAME,
|
||||
retrieval_mode: RetrievalMode = RetrievalMode.DENSE,
|
||||
sparse_embedding: Optional[SparseEmbeddings] = None,
|
||||
sparse_embedding: SparseEmbeddings | None = None,
|
||||
sparse_vector_name: str = SPARSE_VECTOR_NAME,
|
||||
collection_create_options: Optional[dict[str, Any]] = None,
|
||||
vector_params: Optional[dict[str, Any]] = None,
|
||||
sparse_vector_params: Optional[dict[str, Any]] = None,
|
||||
collection_create_options: dict[str, Any] | None = None,
|
||||
vector_params: dict[str, Any] | None = None,
|
||||
sparse_vector_params: dict[str, Any] | None = None,
|
||||
batch_size: int = 64,
|
||||
force_recreate: bool = False, # noqa: FBT001, FBT002
|
||||
validate_embeddings: bool = True, # noqa: FBT001, FBT002
|
||||
@@ -442,25 +440,25 @@ class QdrantVectorStore(VectorStore):
|
||||
def from_existing_collection(
|
||||
cls: type[QdrantVectorStore],
|
||||
collection_name: str,
|
||||
embedding: Optional[Embeddings] = None,
|
||||
embedding: Embeddings | None = None,
|
||||
retrieval_mode: RetrievalMode = RetrievalMode.DENSE,
|
||||
location: Optional[str] = None,
|
||||
url: Optional[str] = None,
|
||||
port: Optional[int] = 6333,
|
||||
location: str | None = None,
|
||||
url: str | None = None,
|
||||
port: int | None = 6333,
|
||||
grpc_port: int = 6334,
|
||||
prefer_grpc: bool = False, # noqa: FBT001, FBT002
|
||||
https: Optional[bool] = None, # noqa: FBT001
|
||||
api_key: Optional[str] = None,
|
||||
prefix: Optional[str] = None,
|
||||
timeout: Optional[int] = None,
|
||||
host: Optional[str] = None,
|
||||
path: Optional[str] = None,
|
||||
https: bool | None = None, # noqa: FBT001
|
||||
api_key: str | None = None,
|
||||
prefix: str | None = None,
|
||||
timeout: int | None = None,
|
||||
host: str | None = None,
|
||||
path: str | None = None,
|
||||
distance: models.Distance = models.Distance.COSINE,
|
||||
content_payload_key: str = CONTENT_KEY,
|
||||
metadata_payload_key: str = METADATA_KEY,
|
||||
vector_name: str = VECTOR_NAME,
|
||||
sparse_vector_name: str = SPARSE_VECTOR_NAME,
|
||||
sparse_embedding: Optional[SparseEmbeddings] = None,
|
||||
sparse_embedding: SparseEmbeddings | None = None,
|
||||
validate_embeddings: bool = True, # noqa: FBT001, FBT002
|
||||
validate_collection_config: bool = True, # noqa: FBT001, FBT002
|
||||
**kwargs: Any,
|
||||
@@ -503,8 +501,8 @@ class QdrantVectorStore(VectorStore):
|
||||
def add_texts( # type: ignore[override]
|
||||
self,
|
||||
texts: Iterable[str],
|
||||
metadatas: Optional[list[dict]] = None,
|
||||
ids: Optional[Sequence[str | int]] = None,
|
||||
metadatas: list[dict] | None = None,
|
||||
ids: Sequence[str | int] | None = None,
|
||||
batch_size: int = 64,
|
||||
**kwargs: Any,
|
||||
) -> list[str | int]:
|
||||
@@ -529,12 +527,12 @@ class QdrantVectorStore(VectorStore):
|
||||
self,
|
||||
query: str,
|
||||
k: int = 4,
|
||||
filter: Optional[models.Filter] = None, # noqa: A002
|
||||
search_params: Optional[models.SearchParams] = None,
|
||||
filter: models.Filter | None = None, # noqa: A002
|
||||
search_params: models.SearchParams | None = None,
|
||||
offset: int = 0,
|
||||
score_threshold: Optional[float] = None,
|
||||
consistency: Optional[models.ReadConsistency] = None,
|
||||
hybrid_fusion: Optional[models.FusionQuery] = None,
|
||||
score_threshold: float | None = None,
|
||||
consistency: models.ReadConsistency | None = None,
|
||||
hybrid_fusion: models.FusionQuery | None = None,
|
||||
**kwargs: Any,
|
||||
) -> list[Document]:
|
||||
"""Return docs most similar to query.
|
||||
@@ -560,12 +558,12 @@ class QdrantVectorStore(VectorStore):
|
||||
self,
|
||||
query: str,
|
||||
k: int = 4,
|
||||
filter: Optional[models.Filter] = None, # noqa: A002
|
||||
search_params: Optional[models.SearchParams] = None,
|
||||
filter: models.Filter | None = None, # noqa: A002
|
||||
search_params: models.SearchParams | None = None,
|
||||
offset: int = 0,
|
||||
score_threshold: Optional[float] = None,
|
||||
consistency: Optional[models.ReadConsistency] = None,
|
||||
hybrid_fusion: Optional[models.FusionQuery] = None,
|
||||
score_threshold: float | None = None,
|
||||
consistency: models.ReadConsistency | None = None,
|
||||
hybrid_fusion: models.FusionQuery | None = None,
|
||||
**kwargs: Any,
|
||||
) -> list[tuple[Document, float]]:
|
||||
"""Return docs most similar to query.
|
||||
@@ -654,11 +652,11 @@ class QdrantVectorStore(VectorStore):
|
||||
self,
|
||||
embedding: list[float],
|
||||
k: int = 4,
|
||||
filter: Optional[models.Filter] = None, # noqa: A002
|
||||
search_params: Optional[models.SearchParams] = None,
|
||||
filter: models.Filter | None = None, # noqa: A002
|
||||
search_params: models.SearchParams | None = None,
|
||||
offset: int = 0,
|
||||
score_threshold: Optional[float] = None,
|
||||
consistency: Optional[models.ReadConsistency] = None,
|
||||
score_threshold: float | None = None,
|
||||
consistency: models.ReadConsistency | None = None,
|
||||
**kwargs: Any,
|
||||
) -> list[tuple[Document, float]]:
|
||||
"""Return docs most similar to embedding vector.
|
||||
@@ -708,11 +706,11 @@ class QdrantVectorStore(VectorStore):
|
||||
self,
|
||||
embedding: list[float],
|
||||
k: int = 4,
|
||||
filter: Optional[models.Filter] = None, # noqa: A002
|
||||
search_params: Optional[models.SearchParams] = None,
|
||||
filter: models.Filter | None = None, # noqa: A002
|
||||
search_params: models.SearchParams | None = None,
|
||||
offset: int = 0,
|
||||
score_threshold: Optional[float] = None,
|
||||
consistency: Optional[models.ReadConsistency] = None,
|
||||
score_threshold: float | None = None,
|
||||
consistency: models.ReadConsistency | None = None,
|
||||
**kwargs: Any,
|
||||
) -> list[Document]:
|
||||
"""Return docs most similar to embedding vector.
|
||||
@@ -739,10 +737,10 @@ class QdrantVectorStore(VectorStore):
|
||||
k: int = 4,
|
||||
fetch_k: int = 20,
|
||||
lambda_mult: float = 0.5,
|
||||
filter: Optional[models.Filter] = None, # noqa: A002
|
||||
search_params: Optional[models.SearchParams] = None,
|
||||
score_threshold: Optional[float] = None,
|
||||
consistency: Optional[models.ReadConsistency] = None,
|
||||
filter: models.Filter | None = None, # noqa: A002
|
||||
search_params: models.SearchParams | None = None,
|
||||
score_threshold: float | None = None,
|
||||
consistency: models.ReadConsistency | None = None,
|
||||
**kwargs: Any,
|
||||
) -> list[Document]:
|
||||
"""Return docs selected using the maximal marginal relevance with dense vectors.
|
||||
@@ -782,10 +780,10 @@ class QdrantVectorStore(VectorStore):
|
||||
k: int = 4,
|
||||
fetch_k: int = 20,
|
||||
lambda_mult: float = 0.5,
|
||||
filter: Optional[models.Filter] = None, # noqa: A002
|
||||
search_params: Optional[models.SearchParams] = None,
|
||||
score_threshold: Optional[float] = None,
|
||||
consistency: Optional[models.ReadConsistency] = None,
|
||||
filter: models.Filter | None = None, # noqa: A002
|
||||
search_params: models.SearchParams | None = None,
|
||||
score_threshold: float | None = None,
|
||||
consistency: models.ReadConsistency | None = None,
|
||||
**kwargs: Any,
|
||||
) -> list[Document]:
|
||||
"""Return docs selected using the maximal marginal relevance with dense vectors.
|
||||
@@ -816,10 +814,10 @@ class QdrantVectorStore(VectorStore):
|
||||
k: int = 4,
|
||||
fetch_k: int = 20,
|
||||
lambda_mult: float = 0.5,
|
||||
filter: Optional[models.Filter] = None, # noqa: A002
|
||||
search_params: Optional[models.SearchParams] = None,
|
||||
score_threshold: Optional[float] = None,
|
||||
consistency: Optional[models.ReadConsistency] = None,
|
||||
filter: models.Filter | None = None, # noqa: A002
|
||||
search_params: models.SearchParams | None = None,
|
||||
score_threshold: float | None = None,
|
||||
consistency: models.ReadConsistency | None = None,
|
||||
**kwargs: Any,
|
||||
) -> list[tuple[Document, float]]:
|
||||
"""Return docs selected using the maximal marginal relevance.
|
||||
@@ -870,9 +868,9 @@ class QdrantVectorStore(VectorStore):
|
||||
|
||||
def delete( # type: ignore[override]
|
||||
self,
|
||||
ids: Optional[list[str | int]] = None,
|
||||
ids: list[str | int] | None = None,
|
||||
**kwargs: Any,
|
||||
) -> Optional[bool]:
|
||||
) -> bool | None:
|
||||
"""Delete documents by their ids.
|
||||
|
||||
Args:
|
||||
@@ -905,20 +903,20 @@ class QdrantVectorStore(VectorStore):
|
||||
@classmethod
|
||||
def construct_instance(
|
||||
cls: type[QdrantVectorStore],
|
||||
embedding: Optional[Embeddings] = None,
|
||||
embedding: Embeddings | None = None,
|
||||
retrieval_mode: RetrievalMode = RetrievalMode.DENSE,
|
||||
sparse_embedding: Optional[SparseEmbeddings] = None,
|
||||
client_options: Optional[dict[str, Any]] = None,
|
||||
collection_name: Optional[str] = None,
|
||||
sparse_embedding: SparseEmbeddings | None = None,
|
||||
client_options: dict[str, Any] | None = None,
|
||||
collection_name: str | None = None,
|
||||
distance: models.Distance = models.Distance.COSINE,
|
||||
content_payload_key: str = CONTENT_KEY,
|
||||
metadata_payload_key: str = METADATA_KEY,
|
||||
vector_name: str = VECTOR_NAME,
|
||||
sparse_vector_name: str = SPARSE_VECTOR_NAME,
|
||||
force_recreate: bool = False, # noqa: FBT001, FBT002
|
||||
collection_create_options: Optional[dict[str, Any]] = None,
|
||||
vector_params: Optional[dict[str, Any]] = None,
|
||||
sparse_vector_params: Optional[dict[str, Any]] = None,
|
||||
collection_create_options: dict[str, Any] | None = None,
|
||||
vector_params: dict[str, Any] | None = None,
|
||||
sparse_vector_params: dict[str, Any] | None = None,
|
||||
validate_embeddings: bool = True, # noqa: FBT001, FBT002
|
||||
validate_collection_config: bool = True, # noqa: FBT001, FBT002
|
||||
) -> QdrantVectorStore:
|
||||
@@ -1053,8 +1051,8 @@ class QdrantVectorStore(VectorStore):
|
||||
def _generate_batches(
|
||||
self,
|
||||
texts: Iterable[str],
|
||||
metadatas: Optional[list[dict]] = None,
|
||||
ids: Optional[Sequence[str | int]] = None,
|
||||
metadatas: list[dict] | None = None,
|
||||
ids: Sequence[str | int] | None = None,
|
||||
batch_size: int = 64,
|
||||
) -> Generator[tuple[list[str | int], list[models.PointStruct]], Any, None]:
|
||||
texts_iterator = iter(texts)
|
||||
@@ -1088,7 +1086,7 @@ class QdrantVectorStore(VectorStore):
|
||||
@staticmethod
|
||||
def _build_payloads(
|
||||
texts: Iterable[str],
|
||||
metadatas: Optional[list[dict]],
|
||||
metadatas: list[dict] | None,
|
||||
content_payload_key: str,
|
||||
metadata_payload_key: str,
|
||||
) -> list[dict]:
|
||||
@@ -1170,7 +1168,7 @@ class QdrantVectorStore(VectorStore):
|
||||
vector_name: str,
|
||||
sparse_vector_name: str,
|
||||
distance: models.Distance,
|
||||
embedding: Optional[Embeddings],
|
||||
embedding: Embeddings | None,
|
||||
) -> None:
|
||||
if retrieval_mode == RetrievalMode.DENSE:
|
||||
cls._validate_collection_for_dense(
|
||||
@@ -1197,7 +1195,7 @@ class QdrantVectorStore(VectorStore):
|
||||
collection_name: str,
|
||||
vector_name: str,
|
||||
distance: models.Distance,
|
||||
dense_embeddings: Union[Embeddings, list[float], None],
|
||||
dense_embeddings: Embeddings | list[float] | None,
|
||||
) -> None:
|
||||
collection_info = client.get_collection(collection_name=collection_name)
|
||||
vector_config = collection_info.config.params.vectors
|
||||
@@ -1289,8 +1287,8 @@ class QdrantVectorStore(VectorStore):
|
||||
def _validate_embeddings(
|
||||
cls: type[QdrantVectorStore],
|
||||
retrieval_mode: RetrievalMode,
|
||||
embedding: Optional[Embeddings],
|
||||
sparse_embedding: Optional[SparseEmbeddings],
|
||||
embedding: Embeddings | None,
|
||||
sparse_embedding: SparseEmbeddings | None,
|
||||
) -> None:
|
||||
if retrieval_mode == RetrievalMode.DENSE and embedding is None:
|
||||
msg = "'embedding' cannot be None when retrieval mode is 'dense'"
|
||||
|
||||
Reference in New Issue
Block a user