pangwangshu commented on code in PR #40629:
URL: https://github.com/apache/beam/pull/40629#discussion_r4447811847


##########
sdks/python/apache_beam/ml/rag/enrichment/qdrant_search.py:
##########
@@ -0,0 +1,472 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from dataclasses import dataclass
+from dataclasses import field
+from dataclasses import replace
+from typing import Any
+from typing import Optional
+from typing import Union
+
+import grpc
+import httpx
+from qdrant_client import QdrantClient
+from qdrant_client import models
+from qdrant_client.common.client_exceptions import ResourceExhaustedResponse
+from qdrant_client.http.exceptions import ResponseHandlingException
+from qdrant_client.http.exceptions import UnexpectedResponse
+
+from apache_beam.io.requestresponse import UserCodeQuotaException
+from apache_beam.io.requestresponse import UserCodeTimeoutException
+from apache_beam.ml.rag.ingestion.qdrant import QdrantConnectionParameters
+from apache_beam.ml.rag.types import EmbeddableItem
+from apache_beam.transforms.enrichment import EnrichmentSourceHandler
+
+__all__ = [
+    'QdrantConnectionParameters',
+    'QdrantDenseSearchParameters',
+    'QdrantSparseSearchParameters',
+    'QdrantHybridSearchParameters',
+    'QdrantSearchParameters',
+    'QdrantSearchResult',
+    'QdrantSearchEnrichmentHandler',
+]
+
+
+@dataclass
+class QdrantDenseSearchParameters:
+  """Parameters for dense vector similarity search.
+
+  Args:
+    vector_name: Name of the dense vector in the Qdrant collection. Defaults to
+      "dense", matching the default `dense_embedding_key` of
+      :class:`~apache_beam.ml.rag.ingestion.qdrant.QdrantWriteConfig`.
+    limit: Maximum number of results to return per query. Must be positive.
+      Defaults to 3 search results.
+    filter: Optional payload filter applied to the search, e.g.
+      `models.Filter(must=[models.FieldCondition(key="source",
+      match=models.MatchValue(value="docs"))])`.
+    score_threshold: Optional minimal score; results scoring worse are dropped.
+    search_params: Optional `models.SearchParams` such as `hnsw_ef` or `exact`.
+    kwargs: Optional keyword arguments for additional `models.QueryRequest`
+      fields. Enables forward compatibility. Explicit non-None fields take
+      precedence over the corresponding entries in `kwargs`.
+  """
+  vector_name: str = "dense"
+  limit: int = 3
+  filter: Optional[models.Filter] = None
+  score_threshold: Optional[float] = None
+  search_params: Optional[models.SearchParams] = None
+  kwargs: dict[str, Any] = field(default_factory=dict)
+
+  def __post_init__(self):
+    if not self.vector_name:
+      raise ValueError("Vector name must be provided")
+
+    if self.limit <= 0:
+      raise ValueError(f"Search limit must be positive, got {self.limit}")
+
+
+@dataclass
+class QdrantSparseSearchParameters:
+  """Parameters for sparse vector (keyword) search.
+
+  Args:
+    vector_name: Name of the sparse vector in the Qdrant collection. Defaults 
to
+      "sparse", matching the default `sparse_embedding_key` of
+      :class:`~apache_beam.ml.rag.ingestion.qdrant.QdrantWriteConfig`.
+    limit: Maximum number of results to return per query. Must be positive.
+      Defaults to 3 search results.
+    filter: Optional payload filter applied to the search.
+    score_threshold: Optional minimal score; results scoring worse are dropped.
+    search_params: Optional `models.SearchParams`.
+    kwargs: Optional keyword arguments for additional `models.QueryRequest`
+      fields. Enables forward compatibility. Explicit non-None fields take
+      precedence over the corresponding entries in `kwargs`.
+  """
+  vector_name: str = "sparse"
+  limit: int = 3
+  filter: Optional[models.Filter] = None
+  score_threshold: Optional[float] = None
+  search_params: Optional[models.SearchParams] = None
+  kwargs: dict[str, Any] = field(default_factory=dict)
+
+  def __post_init__(self):
+    if not self.vector_name:
+      raise ValueError("Vector name must be provided")
+
+    if self.limit <= 0:
+      raise ValueError(f"Search limit must be positive, got {self.limit}")
+
+
+@dataclass
+class QdrantHybridSearchParameters:
+  """Parameters for hybrid (dense + sparse) search.
+
+  The dense and sparse searches run as prefetches and their results are fused
+  server side with the Qdrant Query API. Configure `search_params` on `dense`
+  and `sparse` to control their respective prefetches; fusion does not run a
+  vector search.
+
+  Args:
+    dense: Parameters for the dense search component. Its `limit` is the
+      number of candidates prefetched before fusion.
+    sparse: Parameters for the sparse search component. Its `limit` is the
+      number of candidates prefetched before fusion.
+    fusion: Fusion method used to combine both result lists. Defaults to
+      reciprocal rank fusion (`models.Fusion.RRF`).
+    limit: Maximum number of fused results to return per query. Defaults to 3
+      search results.
+    filter: Optional common payload filter on the outer query. Qdrant also
+      applies it to the prefetches, together with their component filters.
+    score_threshold: Optional minimum fused score. Component thresholds apply
+      to the individual dense or sparse scores before fusion instead.
+    kwargs: Optional keyword arguments for additional `models.QueryRequest`
+      fields. Explicit non-None `filter` and `score_threshold` fields take
+      precedence over the corresponding entries in `kwargs`.
+  """
+  dense: QdrantDenseSearchParameters
+  sparse: QdrantSparseSearchParameters
+  fusion: models.Fusion = models.Fusion.RRF
+  limit: int = 3
+  kwargs: dict[str, Any] = field(default_factory=dict)
+  filter: Optional[models.Filter] = None
+  score_threshold: Optional[float] = None
+
+  def __post_init__(self):
+    if not self.dense or not self.sparse:
+      raise ValueError(
+          "Dense and sparse search parameters must be provided for "
+          "hybrid search")
+
+    if not self.fusion:
+      raise ValueError("Fusion must be provided for hybrid search")
+
+    if self.limit <= 0:
+      raise ValueError(f"Search limit must be positive, got {self.limit}")
+
+
+QdrantSearchStrategyType = Union[QdrantDenseSearchParameters,
+                                 QdrantSparseSearchParameters,
+                                 QdrantHybridSearchParameters]
+
+
+@dataclass
+class QdrantSearchParameters:
+  """Parameters configuring Qdrant search operations.
+
+  Args:
+    collection_name: Name of the collection to search in.
+    search_strategy: Dense, sparse or hybrid search parameters.
+    with_payload: Whether to return the payload of each point. Either a bool
+      or a list of payload keys to return. Defaults to True.
+    consistency: Optional read consistency for the search, e.g.
+      `models.ReadConsistencyType.MAJORITY`.
+    timeout: Optional search timeout in seconds. If not specified, the client's
+      default timeout is used.
+  """
+  collection_name: str
+  search_strategy: QdrantSearchStrategyType
+  with_payload: Union[bool, list[str]] = True
+  consistency: Optional[models.ReadConsistency] = None
+  timeout: Optional[int] = None
+
+  def __post_init__(self):
+    if not self.collection_name:
+      raise ValueError("Collection name must be provided")
+
+    if not self.search_strategy:
+      raise ValueError("Search strategy must be provided")
+
+
+@dataclass
+class QdrantSearchResult:
+  """Search result from Qdrant per embeddable_item.
+
+  Args:
+    id: List of point IDs returned from the search. Can be either string (UUID)
+      or integer IDs.
+    score: List of scores for each returned point. Higher is more similar for
+      cosine and dot product, lower is more similar for Euclidean and Manhattan
+      distances.
+    payload: List of payload dictionaries, one for each returned point. Empty
+      if the point has no payload or `with_payload` is False.
+  """
+  id: list[Union[str, int]] = field(default_factory=list)
+  score: list[float] = field(default_factory=list)
+  payload: list[dict[str, Any]] = field(default_factory=list)
+
+
+InputT, OutputT = (Union[EmbeddableItem, list[EmbeddableItem]],
+                    list[tuple[EmbeddableItem, dict[str, Any]]])
+
+
+class QdrantSearchEnrichmentHandler(EnrichmentSourceHandler[InputT, OutputT]):
+  """Enrichment handler for Qdrant vector database searches.
+
+  This handler is designed to work with the
+  :class:`apache_beam.transforms.enrichment.EnrichmentSourceHandler` transform.
+  It enables enriching data through dense, sparse, or hybrid searches against
+  Qdrant collections, such as ones written by
+  :class:`~apache_beam.ml.rag.ingestion.qdrant.QdrantWriteConfig`.
+
+  The handler supports different search strategies:
+  * Dense search - For finding similar embeddings based on vector similarity
+  * Sparse search - For keyword-style retrieval with sparse embeddings
+  * Hybrid search - For fusing dense and sparse search results server side
+
+  This handler queries Qdrant per element by default. To enable batching for
+  improved performance, set the `min_batch_size` and `max_batch_size`
+  parameters. These control the batching behavior in the
+  :class:`apache_beam.transforms.utils.BatchElements` transform. Each batch is
+  sent to Qdrant as a single batch query request.
+
+  REST and gRPC quota and timeout errors are translated to Beam exceptions
+  so that `Enrichment` can retry them using its configured repeater. Other
+  client errors propagate unchanged.
+  """
+  def __init__(
+      self,
+      connection_parameters: QdrantConnectionParameters,
+      search_parameters: QdrantSearchParameters,
+      *,
+      min_batch_size: int = 1,
+      max_batch_size: int = 1000,
+      **kwargs):

Review Comment:
   Done in b79b82a. Removed the unused **kwargs, so unknown arguments now raise 
a TypeError instead of being silently dropped.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to