Skip to content

Commit 71464e7

Browse files
holtskinnercopybara-github
authored andcommitted
docs(samples): adding code sample for vector search create streaming index
PiperOrigin-RevId: 667564368
1 parent 3d68777 commit 71464e7

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

samples/model-builder/vector_search/vector_search_create_index_sample.py

+33
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,36 @@ def vector_search_create_index(
4848

4949

5050
# [END aiplatform_sdk_vector_search_create_index_sample]
51+
52+
53+
# [START aiplatform_sdk_vector_search_create_streaming_index_sample]
54+
def vector_search_create_streaming_index(
55+
project: str, location: str, display_name: str, gcs_uri: Optional[str] = None
56+
) -> None:
57+
"""Create a vector search index.
58+
59+
Args:
60+
project (str): Required. Project ID
61+
location (str): Required. The region name
62+
display_name (str): Required. The index display name
63+
gcs_uri (str): Optional. The Google Cloud Storage uri for index content
64+
"""
65+
# Initialize the Vertex AI client
66+
aiplatform.init(project=project, location=location, staging_bucket=gcs_uri)
67+
68+
# Create Index
69+
index = aiplatform.MatchingEngineIndex.create_tree_ah_index(
70+
display_name=display_name,
71+
description="Matching Engine Index",
72+
dimensions=100,
73+
approximate_neighbors_count=150,
74+
leaf_node_embedding_count=500,
75+
leaf_nodes_to_search_percent=7,
76+
index_update_method="stream_update", # Options: stream_update, batch_update
77+
distance_measure_type=aiplatform.matching_engine.matching_engine_index_config.DistanceMeasureType.DOT_PRODUCT_DISTANCE,
78+
)
79+
80+
print(index.name)
81+
82+
83+
# [END aiplatform_sdk_vector_search_create_streaming_index_sample]

samples/model-builder/vector_search/vector_search_create_index_sample_test.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,37 @@ def test_vector_search_create_index_sample(
4444
approximate_neighbors_count=ANY,
4545
leaf_node_embedding_count=ANY,
4646
leaf_nodes_to_search_percent=ANY,
47-
index_update_method=ANY,
47+
index_update_method="batch_update",
48+
distance_measure_type=ANY,
49+
)
50+
51+
52+
def test_vector_search_create_streaming_index_sample(
53+
mock_sdk_init,
54+
mock_index_create_tree_ah_index,
55+
):
56+
vector_search_create_index_sample.vector_search_create_streaming_index(
57+
project=constants.PROJECT,
58+
location=constants.LOCATION,
59+
display_name=constants.VECTOR_SEARCH_INDEX_DISPLAY_NAME,
60+
gcs_uri=constants.VECTOR_SEARCH_GCS_URI,
61+
)
62+
63+
# Check client initialization
64+
mock_sdk_init.assert_called_with(
65+
project=constants.PROJECT,
66+
location=constants.LOCATION,
67+
staging_bucket=constants.VECTOR_SEARCH_GCS_URI,
68+
)
69+
70+
# Check index creation
71+
mock_index_create_tree_ah_index.assert_called_with(
72+
display_name=constants.VECTOR_SEARCH_INDEX_DISPLAY_NAME,
73+
description=ANY,
74+
dimensions=ANY,
75+
approximate_neighbors_count=ANY,
76+
leaf_node_embedding_count=ANY,
77+
leaf_nodes_to_search_percent=ANY,
78+
index_update_method="stream_update",
4879
distance_measure_type=ANY,
4980
)

0 commit comments

Comments
 (0)