Skip to content

Commit 1fc7dd9

Browse files
authored
feat: Add sample for create artifact and execution using the Metadata SDK. (#1462)
* Add sample for create artifact and execution using the SDK * correct tax for create_execution * lint updates * restarting tests * resolve lint issues
1 parent 298958f commit 1fc7dd9

5 files changed

+208
-0
lines changed

samples/model-builder/conftest.py

+7
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,13 @@ def mock_get_execution(mock_execution):
576576
yield mock_get_execution
577577

578578

579+
@pytest.fixture
580+
def mock_create_execution(mock_execution):
581+
with patch.object(aiplatform.Execution, "create") as mock_create_execution:
582+
mock_create_execution.return_value = mock_execution
583+
yield mock_create_execution
584+
585+
579586
@pytest.fixture
580587
def mock_get_artifact(mock_artifact):
581588
with patch.object(aiplatform, "Artifact") as mock_get_artifact:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import Dict, Optional
16+
17+
from google.cloud.aiplatform.metadata.schema.system import artifact_schema
18+
19+
20+
# [START aiplatform_sdk_create_artifact_with_sdk_sample]
21+
def create_artifact_sample(
22+
project: str,
23+
location: str,
24+
uri: Optional[str] = None,
25+
artifact_id: Optional[str] = None,
26+
display_name: Optional[str] = None,
27+
schema_version: Optional[str] = None,
28+
description: Optional[str] = None,
29+
metadata: Optional[Dict] = None,
30+
):
31+
system_artifact_schema = artifact_schema.Artifact(
32+
uri=uri,
33+
artifact_id=artifact_id,
34+
display_name=display_name,
35+
schema_version=schema_version,
36+
description=description,
37+
metadata=metadata,
38+
)
39+
40+
return system_artifact_schema.create(project=project, location=location,)
41+
42+
43+
# [END aiplatform_sdk_create_artifact_with_sdk_sample]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import create_artifact_with_sdk_sample
16+
17+
from google.cloud.aiplatform.compat.types import artifact as gca_artifact
18+
19+
import test_constants as constants
20+
21+
22+
def test_create_artifact_with_sdk_sample(mock_artifact, mock_create_artifact):
23+
artifact = create_artifact_with_sdk_sample.create_artifact_sample(
24+
project=constants.PROJECT,
25+
location=constants.LOCATION,
26+
uri=constants.MODEL_ARTIFACT_URI,
27+
artifact_id=constants.RESOURCE_ID,
28+
display_name=constants.DISPLAY_NAME,
29+
schema_version=constants.SCHEMA_VERSION,
30+
description=constants.DESCRIPTION,
31+
metadata=constants.METADATA,
32+
)
33+
34+
mock_create_artifact.assert_called_with(
35+
resource_id=constants.RESOURCE_ID,
36+
schema_title="system.Artifact",
37+
uri=constants.MODEL_ARTIFACT_URI,
38+
display_name=constants.DISPLAY_NAME,
39+
schema_version=constants.SCHEMA_VERSION,
40+
description=constants.DESCRIPTION,
41+
metadata=constants.METADATA,
42+
state=gca_artifact.Artifact.State.LIVE,
43+
metadata_store_id="default",
44+
project=constants.PROJECT,
45+
location=constants.LOCATION,
46+
credentials=None,
47+
)
48+
49+
assert artifact is mock_artifact
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import Any, Dict, List, Optional
16+
17+
from google.cloud import aiplatform
18+
from google.cloud.aiplatform.metadata.schema.system import execution_schema
19+
20+
21+
# [START aiplatform_sdk_create_execution_with_sdk_sample]
22+
def create_execution_sample(
23+
display_name: str,
24+
input_artifacts: List[aiplatform.Artifact],
25+
output_artifacts: List[aiplatform.Artifact],
26+
project: str,
27+
location: str,
28+
execution_id: Optional[str] = None,
29+
metadata: Optional[Dict[str, Any]] = None,
30+
schema_version: Optional[str] = None,
31+
description: Optional[str] = None,
32+
):
33+
aiplatform.init(project=project, location=location)
34+
35+
with execution_schema.ContainerExecution(
36+
display_name=display_name,
37+
execution_id=execution_id,
38+
metadata=metadata,
39+
schema_version=schema_version,
40+
description=description,
41+
).create() as execution:
42+
execution.assign_input_artifacts(input_artifacts)
43+
execution.assign_output_artifacts(output_artifacts)
44+
return execution
45+
46+
47+
# [END aiplatform_sdk_create_execution_with_sdk_sample]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import create_execution_with_sdk_sample
16+
17+
from google.cloud.aiplatform.compat.types import execution as gca_execution
18+
19+
import test_constants as constants
20+
21+
22+
def test_create_execution_sample(
23+
mock_sdk_init, mock_create_artifact, mock_create_execution, mock_execution,
24+
):
25+
26+
input_art = mock_create_artifact()
27+
output_art = mock_create_artifact()
28+
29+
exc = create_execution_with_sdk_sample.create_execution_sample(
30+
display_name=constants.DISPLAY_NAME,
31+
input_artifacts=[input_art],
32+
output_artifacts=[output_art],
33+
project=constants.PROJECT,
34+
location=constants.LOCATION,
35+
execution_id=constants.RESOURCE_ID,
36+
metadata=constants.METADATA,
37+
schema_version=constants.SCHEMA_VERSION,
38+
description=constants.DESCRIPTION,
39+
)
40+
41+
mock_sdk_init.assert_called_with(
42+
project=constants.PROJECT, location=constants.LOCATION,
43+
)
44+
45+
mock_create_execution.assert_called_with(
46+
state=gca_execution.Execution.State.RUNNING,
47+
schema_title="system.ContainerExecution",
48+
resource_id=constants.RESOURCE_ID,
49+
display_name=constants.DISPLAY_NAME,
50+
schema_version=constants.SCHEMA_VERSION,
51+
metadata=constants.METADATA,
52+
description=constants.DESCRIPTION,
53+
metadata_store_id="default",
54+
project=None,
55+
location=None,
56+
credentials=None,
57+
)
58+
59+
mock_execution.assign_input_artifacts.assert_called_with([input_art])
60+
mock_execution.assign_output_artifacts.assert_called_with([output_art])
61+
62+
assert exc is mock_execution

0 commit comments

Comments
 (0)