Skip to content

Commit e6d3df8

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
fix: Add MatchingEngineIndexConfig enums to proto value converters
PiperOrigin-RevId: 700481419
1 parent c52e3e4 commit e6d3df8

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

google/cloud/aiplatform/matching_engine/matching_engine_index.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def create_tree_ah_index(
443443
contents_delta_uri="gs://my_bucket/embeddings",
444444
dimensions=1,
445445
approximate_neighbors_count=150,
446-
distance_measure_type="SQUARED_L2_DISTANCE",
446+
distance_measure_type=matching_engine_index_config.DistanceMeasureType.SQUARED_L2_DISTANCE,
447447
leaf_node_embedding_count=100,
448448
leaf_nodes_to_search_percent=50,
449449
description="my description",
@@ -604,7 +604,7 @@ def create_brute_force_index(
604604
contents_delta_uri="gs://my_bucket/embeddings",
605605
dimensions=1,
606606
approximate_neighbors_count=150,
607-
distance_measure_type="SQUARED_L2_DISTANCE",
607+
distance_measure_type=matching_engine_index_config.DistanceMeasureType.SQUARED_L2_DISTANCE,
608608
description="my description",
609609
labels={ "label_name": "label_value" },
610610
)

google/cloud/aiplatform/matching_engine/matching_engine_index_config.py

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import abc
1919
import enum
2020
from dataclasses import dataclass
21+
from google.protobuf.struct_pb2 import Value
2122
from typing import Any, Dict, Optional
2223

2324

@@ -34,6 +35,10 @@ class DistanceMeasureType(enum.Enum):
3435
# Cosine Distance. Defined as 1 - cosine similarity.
3536
COSINE_DISTANCE = "COSINE_DISTANCE"
3637

38+
def to_value(self) -> str:
39+
"""Returns the value of the distance measure type."""
40+
return Value(string_value=self.name)
41+
3742

3843
class FeatureNormType(enum.Enum):
3944
"""Type of normalization to be carried out on each vector."""
@@ -43,6 +48,10 @@ class FeatureNormType(enum.Enum):
4348
# No normalization type is specified.
4449
NONE = "NONE"
4550

51+
def to_value(self) -> str:
52+
"""Returns the value of the feature norm type."""
53+
return Value(string_value=self.name)
54+
4655

4756
class AlgorithmConfig(abc.ABC):
4857
"""Base class for configuration options for matching algorithm."""

tests/unit/aiplatform/test_matching_engine_index.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
index_service_client,
3333
)
3434

35+
from google.cloud.aiplatform.matching_engine import (
36+
matching_engine_index_config,
37+
)
38+
3539
from google.cloud.aiplatform.compat.types import (
3640
index as gca_index,
3741
encryption_spec as gca_encryption_spec,
@@ -52,8 +56,12 @@
5256
test_constants.MatchingEngineConstants._TEST_INDEX_DISPLAY_NAME
5357
)
5458
_TEST_CONTENTS_DELTA_URI = "gs://contents"
55-
_TEST_INDEX_DISTANCE_MEASURE_TYPE = "SQUARED_L2_DISTANCE"
56-
_TEST_INDEX_FEATURE_NORM_TYPE = "UNIT_L2_NORM"
59+
_TEST_INDEX_DISTANCE_MEASURE_TYPE = (
60+
matching_engine_index_config.DistanceMeasureType.SQUARED_L2_DISTANCE
61+
)
62+
_TEST_INDEX_FEATURE_NORM_TYPE = (
63+
matching_engine_index_config.FeatureNormType.UNIT_L2_NORM
64+
)
5765

5866
_TEST_CONTENTS_DELTA_URI_UPDATE = "gs://contents_update"
5967
_TEST_IS_COMPLETE_OVERWRITE_UPDATE = True
@@ -510,8 +518,8 @@ def test_create_tree_ah_index_backward_compatibility(self, create_index_mock):
510518
contents_delta_uri=_TEST_CONTENTS_DELTA_URI,
511519
dimensions=_TEST_INDEX_CONFIG_DIMENSIONS,
512520
approximate_neighbors_count=_TEST_INDEX_APPROXIMATE_NEIGHBORS_COUNT,
513-
distance_measure_type=_TEST_INDEX_DISTANCE_MEASURE_TYPE,
514-
feature_norm_type=_TEST_INDEX_FEATURE_NORM_TYPE,
521+
distance_measure_type=_TEST_INDEX_DISTANCE_MEASURE_TYPE.value,
522+
feature_norm_type=_TEST_INDEX_FEATURE_NORM_TYPE.value,
515523
leaf_node_embedding_count=_TEST_LEAF_NODE_EMBEDDING_COUNT,
516524
leaf_nodes_to_search_percent=_TEST_LEAF_NODES_TO_SEARCH_PERCENT,
517525
description=_TEST_INDEX_DESCRIPTION,

0 commit comments

Comments
 (0)