-
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.
feat: Add samples for Metadata context list, get, and create (#1525)
* feat: Add samples for context list,get and create * fix lint issues. * Change import path to aiplatform.Context * Fix create mock. * remove duplicate mock method * Update samples/model-builder/experiment_tracking/get_context_sample_test.py Co-authored-by: Dan Lee <[email protected]> * Update samples/model-builder/experiment_tracking/list_context_sample_test.py Co-authored-by: Dan Lee <[email protected]> * Update samples/model-builder/experiment_tracking/list_context_sample_test.py Co-authored-by: Dan Lee <[email protected]> * Update samples/model-builder/experiment_tracking/get_context_sample_test.py Co-authored-by: Dan Lee <[email protected]> * update formatting and comments based on review feedback Co-authored-by: Dan Lee <[email protected]>
- Loading branch information
1 parent
b53e2b5
commit d913e1d
Showing
20 changed files
with
267 additions
and
51 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
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
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
41 changes: 41 additions & 0 deletions
41
samples/model-builder/experiment_tracking/create_context_with_sdk_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,41 @@ | ||
# 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. | ||
|
||
from typing import Any, Dict, Optional | ||
|
||
from google.cloud import aiplatform | ||
from google.cloud.aiplatform.metadata.schema.system import context_schema | ||
|
||
|
||
# [START aiplatform_sdk_create_context_with_sdk_sample] | ||
def create_context_sample( | ||
display_name: str, | ||
project: str, | ||
location: str, | ||
context_id: Optional[str] = None, | ||
metadata: Optional[Dict[str, Any]] = None, | ||
schema_version: Optional[str] = None, | ||
description: Optional[str] = None, | ||
): | ||
aiplatform.init(project=project, location=location) | ||
|
||
return context_schema.Experiment( | ||
display_name=display_name, | ||
context_id=context_id, | ||
metadata=metadata, | ||
schema_version=schema_version, | ||
description=description, | ||
).create() | ||
|
||
# [END aiplatform_sdk_create_context_with_sdk_sample] |
38 changes: 38 additions & 0 deletions
38
samples/model-builder/experiment_tracking/create_context_with_sdk_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_context_with_sdk_sample | ||
|
||
import test_constants as constants | ||
|
||
|
||
def test_create_context_sample( | ||
mock_sdk_init, mock_create_schema_base_context, mock_context, | ||
): | ||
exc = create_context_with_sdk_sample.create_context_sample( | ||
display_name=constants.DISPLAY_NAME, | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
context_id=constants.RESOURCE_ID, | ||
metadata=constants.METADATA, | ||
schema_version=constants.SCHEMA_VERSION, | ||
description=constants.DESCRIPTION, | ||
) | ||
|
||
mock_sdk_init.assert_called_with( | ||
project=constants.PROJECT, location=constants.LOCATION, | ||
) | ||
|
||
mock_create_schema_base_context.assert_called_with() | ||
assert exc is mock_context |
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
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
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
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
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 |
---|---|---|
|
@@ -27,5 +27,4 @@ def get_artifact_sample( | |
|
||
return artifact | ||
|
||
|
||
# [END aiplatform_sdk_get_artifact_sample] |
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
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
28 changes: 28 additions & 0 deletions
28
samples/model-builder/experiment_tracking/get_context_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,28 @@ | ||
# 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. | ||
|
||
from google.cloud import aiplatform | ||
|
||
|
||
# [START aiplatform_sdk_get_context_sample] | ||
def get_context_sample( | ||
context_id: str, | ||
project: str, | ||
location: str, | ||
): | ||
context = aiplatform.Context.get( | ||
resource_id=context_id, project=project, location=location) | ||
return context | ||
|
||
# [END aiplatform_sdk_get_context_sample] |
33 changes: 33 additions & 0 deletions
33
samples/model-builder/experiment_tracking/get_context_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,33 @@ | ||
# 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 get_context_sample | ||
|
||
import test_constants as constants | ||
|
||
|
||
def test_get_context_sample(mock_context, mock_context_get): | ||
context = get_context_sample.get_context_sample( | ||
context_id=constants.RESOURCE_ID, | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
) | ||
|
||
mock_context_get.assert_called_with( | ||
resource_id=constants.RESOURCE_ID, | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
) | ||
|
||
assert context is mock_context |
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
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
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
Oops, something went wrong.