-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): add Model Registry samples to Vertex AI Python SDK (#1602
) * docs(samples): add samples for vertex ai model registry python sdk * linter test fix * fix list model versions mock test * nox passed Co-authored-by: Andrew Ferlitsch <[email protected]> Co-authored-by: nayaknishant <[email protected]>
- Loading branch information
1 parent
0b48b50
commit 72fd36d
Showing
28 changed files
with
1,315 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
samples/model-builder/model_registry/assign_aliases_model_version_sample.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed 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 | ||
# | ||
# https://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. | ||
|
||
# [START aiplatform_model_registry_assign_aliases_model_version_sample] | ||
|
||
from typing import List | ||
|
||
from google.cloud import aiplatform | ||
|
||
|
||
def assign_aliases_model_version_sample( | ||
model_id: str, | ||
version_aliases: List[str], | ||
version_id: str, | ||
project: str, | ||
location: str, | ||
): | ||
""" | ||
Assign aliases to a model version. | ||
Args: | ||
model_id: The ID of the model. | ||
version_aliases: The version aliases to assign. | ||
version_id: The version ID of the model to assign the aliases to. | ||
project: The project name. | ||
location: The location name. | ||
Returns | ||
None. | ||
""" | ||
# Initialize the client. | ||
aiplatform.init(project=project, location=location) | ||
|
||
# Initialize the Model Registry resource with the ID 'model_id'.The parent_name of create method can be also | ||
# 'projects/<your-project-id>/locations/<your-region>/models/<your-model-id>' | ||
model_registry = aiplatform.models.ModelRegistry(model=model_id) | ||
|
||
# Assign the version aliases to the model with the version 'version_id'. | ||
model_registry.add_version_aliases(new_aliases=version_aliases, version=version_id) | ||
|
||
|
||
# [END aiplatform_model_registry_assign_aliases_model_version_sample] |
46 changes: 46 additions & 0 deletions
46
samples/model-builder/model_registry/assign_aliases_model_version_sample_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed 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 | ||
# | ||
# https://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. | ||
|
||
|
||
import assign_aliases_model_version_sample | ||
|
||
import test_constants as constants | ||
|
||
|
||
def test_assign_aliases_model_version_sample( | ||
mock_sdk_init, mock_init_model_registry, mock_add_version_aliases, mock_model | ||
): | ||
|
||
# Assign aliases to a model version. | ||
assign_aliases_model_version_sample.assign_aliases_model_version_sample( | ||
model_id=constants.MODEL_NAME, | ||
version_id=constants.VERSION_ID, | ||
version_aliases=constants.VERSION_ALIASES, | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
) | ||
|
||
# Check client initialization. | ||
mock_sdk_init.assert_called_with( | ||
project=constants.PROJECT, location=constants.LOCATION | ||
) | ||
|
||
# Check model registry initialization. | ||
mock_init_model_registry.assert_called_with(model=constants.MODEL_NAME) | ||
|
||
# Check that the model version was assigned the aliases. | ||
mock_add_version_aliases.assert_called_with( | ||
new_aliases=constants.VERSION_ALIASES, | ||
version=constants.VERSION_ID, | ||
) |
45 changes: 45 additions & 0 deletions
45
samples/model-builder/model_registry/create_aliased_model_sample.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed 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 | ||
# | ||
# https://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. | ||
|
||
# [START aiplatform_model_registry_create_aliased_model_sample] | ||
|
||
from google.cloud import aiplatform | ||
|
||
|
||
def create_aliased_model_sample( | ||
model_id: str, version_id: str, project: str, location: str | ||
): | ||
""" | ||
Initialize a Model resource to represent an existing model version with custom alias. | ||
Args: | ||
model_id: The ID of the model to initialize. Parent resource name of the model is also accepted. | ||
version_id: The version ID or version alias of the model to initialize. | ||
project: The project. | ||
location: The location. | ||
Returns: | ||
Model resource. | ||
""" | ||
# Initialize the client. | ||
aiplatform.init(project=project, location=location) | ||
|
||
# Initialize the Model resource with the ID 'model_id'. The version can be also provided using @ annotation in | ||
# the parent resource name: | ||
# 'projects/<your-project-id>/locations/<your-region>/models/<your-model-id>@<your-version-id>'. | ||
|
||
aliased_model = aiplatform.Model(model_name=model_id, version=version_id) | ||
|
||
return aliased_model | ||
|
||
|
||
# [END aiplatform_model_registry_create_aliased_model_sample] |
38 changes: 38 additions & 0 deletions
38
samples/model-builder/model_registry/create_aliased_model_sample_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed 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 | ||
# | ||
# https://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. | ||
|
||
|
||
import create_aliased_model_sample | ||
|
||
import test_constants as constants | ||
|
||
|
||
def test_create_aliased_model_sample(mock_sdk_init, mock_init_model): | ||
# Create a model with alias 'default'. | ||
create_aliased_model_sample.create_aliased_model_sample( | ||
model_id=constants.MODEL_NAME, | ||
version_id=constants.VERSION_ID, | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
) | ||
|
||
# Check client initialization. | ||
mock_sdk_init.assert_called_with( | ||
project=constants.PROJECT, location=constants.LOCATION | ||
) | ||
|
||
# Check that the model was created. | ||
mock_init_model.assert_called_with( | ||
model_name=constants.MODEL_NAME, version=constants.VERSION_ID | ||
) |
40 changes: 40 additions & 0 deletions
40
samples/model-builder/model_registry/create_default_model_sample.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed 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 | ||
# | ||
# https://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. | ||
|
||
# [START aiplatform_model_registry_create_default_model_sample] | ||
|
||
from google.cloud import aiplatform | ||
|
||
|
||
def create_default_model_sample(model_id: str, project: str, location: str): | ||
""" | ||
Initialize a Model resource to represent an existing model version with alias 'default'. | ||
Args: | ||
model_id: The ID of the model to initialize. Parent resource name of the model is also accepted. | ||
project: The project. | ||
location: The location. | ||
Returns: | ||
Model resource. | ||
""" | ||
# Initialize the client. | ||
aiplatform.init(project=project, location=location) | ||
|
||
# Initialize the Model resource with the ID 'model_id'. The parent_name of create method can be also | ||
# 'projects/<your-project-id>/locations/<your-region>/models/<your-model-id>' | ||
default_model = aiplatform.Model(model_name=model_id) | ||
|
||
return default_model | ||
|
||
|
||
# [END aiplatform_model_registry_create_default_model_sample] |
Oops, something went wrong.