Skip to content

Commit 4e357d5

Browse files
lingyinwcopybara-github
authored andcommitted
feat: add support for per_crowding_attribute_num_neighbors approx_num_neighborsto MatchingEngineIndexEndpoint match()
PiperOrigin-RevId: 578666956
1 parent 77dec1e commit 4e357d5

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

google/cloud/aiplatform/matching_engine/matching_engine_index_endpoint.py

+13
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,8 @@ def match(
10711071
self,
10721072
deployed_index_id: str,
10731073
queries: List[List[float]],
1074+
per_crowding_attribute_num_neighbors: int,
1075+
approx_num_neighbors: int,
10741076
num_neighbors: int = 1,
10751077
filter: Optional[List[Namespace]] = [],
10761078
) -> List[List[MatchNeighbor]]:
@@ -1081,6 +1083,15 @@ def match(
10811083
Required. The ID of the DeployedIndex to match the queries against.
10821084
queries (List[List[float]]):
10831085
Required. A list of queries. Each query is a list of floats, representing a single embedding.
1086+
per_crowding_attribute_num_neighbors (int):
1087+
Optional. Crowding is a constraint on a neighbor list produced by nearest neighbor
1088+
search requiring that no more than some value k' of the k neighbors
1089+
returned have the same value of crowding_attribute.
1090+
It's used for improving result diversity.
1091+
This field is the maximum number of matches with the same crowding tag.
1092+
approx_num_neighbors (int):
1093+
The number of neighbors to find via approximate search before exact reordering is performed.
1094+
If not set, the default value from scam config is used; if set, this value must be > 0.
10841095
num_neighbors (int):
10851096
Required. The number of nearest neighbors to be retrieved from database for
10861097
each query.
@@ -1123,6 +1134,8 @@ def match(
11231134
num_neighbors=num_neighbors,
11241135
deployed_index_id=deployed_index_id,
11251136
float_val=query,
1137+
per_crowding_attribute_num_neighbors=per_crowding_attribute_num_neighbors,
1138+
approx_num_neighbors=approx_num_neighbors,
11261139
)
11271140
for namespace in filter:
11281141
restrict = match_service_pb2.Namespace()

tests/unit/aiplatform/test_matching_engine_index_endpoint.py

+6
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@
232232
Namespace(name="class", allow_tokens=["token_1"], deny_tokens=["token_2"])
233233
]
234234
_TEST_IDS = ["123", "456", "789"]
235+
_TEST_PER_CROWDING_ATTRIBUTE_NUM_NEIGHBOURS = 3
236+
_TEST_APPROX_NUM_NEIGHBORS = 2
235237

236238

237239
def uuid_mock():
@@ -866,6 +868,8 @@ def test_index_endpoint_match_queries(self, index_endpoint_match_queries_mock):
866868
queries=_TEST_QUERIES,
867869
num_neighbors=_TEST_NUM_NEIGHBOURS,
868870
filter=_TEST_FILTER,
871+
per_crowding_attribute_num_neighbors=_TEST_PER_CROWDING_ATTRIBUTE_NUM_NEIGHBOURS,
872+
approx_num_neighbors=_TEST_APPROX_NUM_NEIGHBORS,
869873
)
870874

871875
batch_request = match_service_pb2.BatchMatchRequest(
@@ -884,6 +888,8 @@ def test_index_endpoint_match_queries(self, index_endpoint_match_queries_mock):
884888
deny_tokens=["token_2"],
885889
)
886890
],
891+
per_crowding_attribute_num_neighbors=_TEST_PER_CROWDING_ATTRIBUTE_NUM_NEIGHBOURS,
892+
approx_num_neighbors=_TEST_APPROX_NUM_NEIGHBORS,
887893
)
888894
],
889895
)

0 commit comments

Comments
 (0)