Skip to content

Commit 97df5fc

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
chore: Add vector search sample for PSC automation deployment
PiperOrigin-RevId: 698867449
1 parent c13b6a8 commit 97df5fc

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

samples/model-builder/test_constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,7 @@
416416
VECTOR_SEARCH_PRIVATE_ENDPOINT_SIGNED_JWT = "fake-signed-jwt"
417417
VECTOR_SEARCH_VPC_NETWORK = "vpc-network"
418418
VECTOR_SEARCH_PSC_PROJECT_ALLOWLIST = ["test-project", "test-project-2"]
419+
VECTOR_SEARCH_PSC_AUTOMATION_CONFIGS = [
420+
("test-project", "network1"),
421+
("test-project2", "network2"),
422+
]

samples/model-builder/vector_search/vector_search_deploy_index_sample.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from typing import Sequence, Tuple
16+
1517
from google.cloud import aiplatform
1618

1719

@@ -109,3 +111,53 @@ def vector_search_deploy_autoscaling_index(
109111

110112

111113
# [END aiplatform_sdk_vector_search_deploy_autoscaling_index_sample]
114+
115+
116+
# [START aiplatform_sdk_vector_search_deploy_psc_automation_sample]
117+
def vector_search_deploy_psc_automation_index(
118+
project: str,
119+
location: str,
120+
index_name: str,
121+
index_endpoint_name: str,
122+
deployed_index_id: str,
123+
psc_automation_configs: Sequence[Tuple[str, str]],
124+
) -> None:
125+
"""Deploy a vector search index to an index endpoint using PSC automation.
126+
127+
Args:
128+
project (str): Required. Project ID
129+
location (str): Required. The region name
130+
index_name (str): Required. The index to update. A fully-qualified index
131+
resource name or a index ID. Example:
132+
"projects/123/locations/us-central1/indexes/my_index_id" or
133+
"my_index_id".
134+
index_endpoint_name (str): Required. Index endpoint to deploy the index
135+
to.
136+
deployed_index_id (str): Required. The user specified ID of the
137+
DeployedIndex.
138+
psc_automation_config (Sequence[Tuple[str, str]]): Required. A list of
139+
(project_id, network) pairs where PSC endpoints will be setup for the
140+
deployed index. Example:
141+
[("123", "{projects/123/global/networks/my-network1"),
142+
("123", "{projects/123/global/networks/my-network2")]
143+
"""
144+
# Initialize the Vertex AI client
145+
aiplatform.init(project=project, location=location)
146+
147+
# Create the index instance from an existing index
148+
index = aiplatform.MatchingEngineIndex(index_name=index_name)
149+
150+
# Create the index endpoint instance from an existing endpoint.
151+
index_endpoint = aiplatform.MatchingEngineIndexEndpoint(
152+
index_endpoint_name=index_endpoint_name
153+
)
154+
155+
# Deploy Index to Endpoint with PSC automation enabled.
156+
index_endpoint.deploy_index(
157+
index=index,
158+
deployed_index_id=deployed_index_id,
159+
psc_automation_configs=psc_automation_configs,
160+
)
161+
162+
163+
# [END aiplatform_sdk_vector_search_deploy_psc_automation_sample]

samples/model-builder/vector_search/vector_search_deploy_index_sample_test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,39 @@ def test_vector_search_deploy_autoscaling_index_sample(
8686
min_replica_count=constants.MIN_REPLICA_COUNT,
8787
max_replica_count=constants.MAX_REPLICA_COUNT,
8888
)
89+
90+
91+
def test_vector_search_deploy_psc_automation_index_sample(
92+
mock_sdk_init,
93+
mock_index_init,
94+
mock_index_endpoint_init,
95+
mock_index_endpoint_deploy_index,
96+
mock_index,
97+
):
98+
vector_search_deploy_index_sample.vector_search_deploy_psc_automation_index(
99+
project=constants.PROJECT,
100+
location=constants.LOCATION,
101+
index_name=constants.VECTOR_SEARCH_INDEX,
102+
index_endpoint_name=constants.VECTOR_SEARCH_INDEX_ENDPOINT,
103+
deployed_index_id=constants.VECTOR_SEARCH_DEPLOYED_INDEX_ID,
104+
psc_automation_configs=constants.VECTOR_SEARCH_PSC_AUTOMATION_CONFIGS,
105+
)
106+
107+
# Check client initialization
108+
mock_sdk_init.assert_called_with(
109+
project=constants.PROJECT, location=constants.LOCATION
110+
)
111+
112+
# Check index initialization with right index name
113+
mock_index_init.assert_called_with(index_name=constants.VECTOR_SEARCH_INDEX)
114+
115+
# Check index endpoint initialization with right index endpoint name
116+
mock_index_endpoint_init.assert_called_with(
117+
index_endpoint_name=constants.VECTOR_SEARCH_INDEX_ENDPOINT
118+
)
119+
# Check index deployment
120+
mock_index_endpoint_deploy_index.assert_called_with(
121+
index=mock_index,
122+
deployed_index_id=constants.VECTOR_SEARCH_DEPLOYED_INDEX_ID,
123+
psc_automation_configs=constants.VECTOR_SEARCH_PSC_AUTOMATION_CONFIGS,
124+
)

0 commit comments

Comments
 (0)