GeezerVison is showing github.com — feat: Improved metadata artifact and execution creation using python … · googleapis/python-aiplatform@6c4374f
File treeExpand file treeCollapse file tree Open diff view settings google/cloud/aiplatform/metadata
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/metadata/artifact.py +51Lines changed: 51 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 31 | 31 | from google.cloud.aiplatform.metadata import metadata_store | 32 | 32 | from google.cloud.aiplatform.metadata import resource | 33 | 33 | from google.cloud.aiplatform.metadata import utils as metadata_utils | | 34 | +from google.cloud.aiplatform.metadata.schema import base_artifact | 34 | 35 | from google.cloud.aiplatform.utils import rest_utils | 35 | 36 |
| 36 | 37 |
| @@ -326,6 +327,56 @@ def create( | 326 | 327 | credentials=credentials, | 327 | 328 | ) | 328 | 329 |
| | 330 | + @classmethod | | 331 | + def create_from_base_artifact_schema( | | 332 | + cls, | | 333 | + *, | | 334 | + base_artifact_schema: "base_artifact.BaseArtifactSchema", | | 335 | + metadata_store_id: Optional[str] = "default", | | 336 | + project: Optional[str] = None, | | 337 | + location: Optional[str] = None, | | 338 | + credentials: Optional[auth_credentials.Credentials] = None, | | 339 | + ) -> "Artifact": | | 340 | + """Creates a new Metadata Artifact from a BaseArtifactSchema class instance. | | 341 | +
| | 342 | + Args: | | 343 | + base_artifact_schema (BaseArtifactSchema): | | 344 | + Required. An instance of the BaseArtifactType class that can be | | 345 | + provided instead of providing artifact specific parameters. | | 346 | + metadata_store_id (str): | | 347 | + Optional. The <metadata_store_id> portion of the resource name with | | 348 | + the format: | | 349 | + projects/123/locations/us-central1/metadataStores/<metadata_store_id>/artifacts/<resource_id> | | 350 | + If not provided, the MetadataStore's ID will be set to "default". | | 351 | + project (str): | | 352 | + Optional. Project used to create this Artifact. Overrides project set in | | 353 | + aiplatform.init. | | 354 | + location (str): | | 355 | + Optional. Location used to create this Artifact. Overrides location set in | | 356 | + aiplatform.init. | | 357 | + credentials (auth_credentials.Credentials): | | 358 | + Optional. Custom credentials used to create this Artifact. Overrides | | 359 | + credentials set in aiplatform.init. | | 360 | +
| | 361 | + Returns: | | 362 | + Artifact: Instantiated representation of the managed Metadata Artifact. | | 363 | + """ | | 364 | + | | 365 | + return cls.create( | | 366 | + resource_id=base_artifact_schema.artifact_id, | | 367 | + schema_title=base_artifact_schema.schema_title, | | 368 | + uri=base_artifact_schema.uri, | | 369 | + display_name=base_artifact_schema.display_name, | | 370 | + schema_version=base_artifact_schema.schema_version, | | 371 | + description=base_artifact_schema.description, | | 372 | + metadata=base_artifact_schema.metadata, | | 373 | + state=base_artifact_schema.state, | | 374 | + metadata_store_id=metadata_store_id, | | 375 | + project=project, | | 376 | + location=location, | | 377 | + credentials=credentials, | | 378 | + ) | | 379 | + | 329 | 380 | @property | 330 | 381 | def uri(self) -> Optional[str]: | 331 | 382 | "Uri for this Artifact." | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/aiplatform/metadata/execution.py +52Lines changed: 52 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 31 | 31 | from google.cloud.aiplatform.metadata import artifact | 32 | 32 | from google.cloud.aiplatform.metadata import metadata_store | 33 | 33 | from google.cloud.aiplatform.metadata import resource | | 34 | +from google.cloud.aiplatform.metadata.schema import base_execution | 34 | 35 |
| 35 | 36 |
| 36 | 37 | class Execution(resource._Resource): | @@ -166,6 +167,57 @@ def create( | 166 | 167 |
| 167 | 168 | return self | 168 | 169 |
| | 170 | + @classmethod | | 171 | + def create_from_base_execution_schema( | | 172 | + cls, | | 173 | + *, | | 174 | + base_execution_schema: "base_execution.BaseExecutionSchema", | | 175 | + metadata_store_id: Optional[str] = "default", | | 176 | + project: Optional[str] = None, | | 177 | + location: Optional[str] = None, | | 178 | + credentials: Optional[auth_credentials.Credentials] = None, | | 179 | + ) -> "Execution": | | 180 | + """ | | 181 | + Creates a new Metadata Execution. | | 182 | +
| | 183 | + Args: | | 184 | + base_execution_schema (BaseExecutionSchema): | | 185 | + An instance of the BaseExecutionSchema class that can be | | 186 | + provided instead of providing schema specific parameters. | | 187 | + metadata_store_id (str): | | 188 | + Optional. The <metadata_store_id> portion of the resource name with | | 189 | + the format: | | 190 | + projects/123/locations/us-central1/metadataStores/<metadata_store_id>/artifacts/<resource_id> | | 191 | + If not provided, the MetadataStore's ID will be set to "default". | | 192 | + project (str): | | 193 | + Optional. Project used to create this Execution. Overrides project set in | | 194 | + aiplatform.init. | | 195 | + location (str): | | 196 | + Optional. Location used to create this Execution. Overrides location set in | | 197 | + aiplatform.init. | | 198 | + credentials (auth_credentials.Credentials): | | 199 | + Optional. Custom credentials used to create this Execution. Overrides | | 200 | + credentials set in aiplatform.init. | | 201 | +
| | 202 | + Returns: | | 203 | + Execution: Instantiated representation of the managed Metadata Execution. | | 204 | +
| | 205 | + """ | | 206 | + resource = Execution.create( | | 207 | + state=base_execution_schema.state, | | 208 | + schema_title=base_execution_schema.schema_title, | | 209 | + resource_id=base_execution_schema.execution_id, | | 210 | + display_name=base_execution_schema.display_name, | | 211 | + schema_version=base_execution_schema.schema_version, | | 212 | + metadata=base_execution_schema.metadata, | | 213 | + description=base_execution_schema.description, | | 214 | + metadata_store_id=metadata_store_id, | | 215 | + project=project, | | 216 | + location=location, | | 217 | + credentials=credentials, | | 218 | + ) | | 219 | + return resource | | 220 | + | 169 | 221 | def __enter__(self): | 170 | 222 | if self.state is not gca_execution.Execution.State.RUNNING: | 171 | 223 | self.update(state=gca_execution.Execution.State.RUNNING) | |
Collapse file Copy file name to clipboard +126Lines changed: 126 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# -*- coding: utf-8 -*- | | 2 | + | | 3 | +# Copyright 2022 Google LLC | | 4 | +# | | 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 6 | +# you may not use this file except in compliance with the License. | | 7 | +# You may obtain a copy of the License at | | 8 | +# | | 9 | +# http://www.apache.org/licenses/LICENSE-2.0 | | 10 | +# | | 11 | +# Unless required by applicable law or agreed to in writing, software | | 12 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 14 | +# See the License for the specific language governing permissions and | | 15 | +# limitations under the License. | | 16 | +# | | 17 | + | | 18 | +import abc | | 19 | + | | 20 | +from typing import Optional, Dict | | 21 | + | | 22 | +from google.auth import credentials as auth_credentials | | 23 | + | | 24 | +from google.cloud.aiplatform.compat.types import artifact as gca_artifact | | 25 | +from google.cloud.aiplatform.metadata import artifact | | 26 | +from google.cloud.aiplatform.metadata import constants | | 27 | + | | 28 | + | | 29 | +class BaseArtifactSchema(metaclass=abc.ABCMeta): | | 30 | + """Base class for Metadata Artifact types.""" | | 31 | + | | 32 | + @property | | 33 | + @classmethod | | 34 | + @abc.abstractmethod | | 35 | + def schema_title(cls) -> str: | | 36 | + """Identifies the Vertex Metadta schema title used by the resource.""" | | 37 | + pass | | 38 | + | | 39 | + def __init__( | | 40 | + self, | | 41 | + *, | | 42 | + artifact_id: Optional[str] = None, | | 43 | + uri: Optional[str] = None, | | 44 | + display_name: Optional[str] = None, | | 45 | + schema_version: Optional[str] = None, | | 46 | + description: Optional[str] = None, | | 47 | + metadata: Optional[Dict] = None, | | 48 | + state: Optional[gca_artifact.Artifact.State] = gca_artifact.Artifact.State.LIVE, | | 49 | + ): | | 50 | + | | 51 | + """Initializes the Artifact with the given name, URI and metadata. | | 52 | +
| | 53 | + This is the base class for defining various artifact types, which can be | | 54 | + passed to google.Artifact to create a corresponding resource. | | 55 | + Artifacts carry a `metadata` field, which is a dictionary for storing | | 56 | + metadata related to this artifact. Subclasses from ArtifactType can enforce | | 57 | + various structure and field requirements for the metadata field. | | 58 | +
| | 59 | + Args: | | 60 | + resource_id (str): | | 61 | + Optional. The <resource_id> portion of the Artifact name with | | 62 | + the following format, this is globally unique in a metadataStore: | | 63 | + projects/123/locations/us-central1/metadataStores/<metadata_store_id>/artifacts/<resource_id>. | | 64 | + uri (str): | | 65 | + Optional. The uniform resource identifier of the artifact file. May be empty if there is no actual | | 66 | + artifact file. | | 67 | + display_name (str): | | 68 | + Optional. The user-defined name of the Artifact. | | 69 | + schema_version (str): | | 70 | + Optional. schema_version specifies the version used by the Artifact. | | 71 | + If not set, defaults to use the latest version. | | 72 | + description (str): | | 73 | + Optional. Describes the purpose of the Artifact to be created. | | 74 | + metadata (Dict): | | 75 | + Optional. Contains the metadata information that will be stored in the Artifact. | | 76 | + state (google.cloud.gapic.types.Artifact.State): | | 77 | + Optional. The state of this Artifact. This is a | | 78 | + property of the Artifact, and does not imply or | | 79 | + capture any ongoing process. This property is | | 80 | + managed by clients (such as Vertex AI | | 81 | + Pipelines), and the system does not prescribe or | | 82 | + check the validity of state transitions. | | 83 | + """ | | 84 | + self.artifact_id = artifact_id | | 85 | + self.uri = uri | | 86 | + self.display_name = display_name | | 87 | + self.schema_version = schema_version or constants._DEFAULT_SCHEMA_VERSION | | 88 | + self.description = description | | 89 | + self.metadata = metadata | | 90 | + self.state = state | | 91 | + | | 92 | + def create( | | 93 | + self, | | 94 | + *, | | 95 | + metadata_store_id: Optional[str] = "default", | | 96 | + project: Optional[str] = None, | | 97 | + location: Optional[str] = None, | | 98 | + credentials: Optional[auth_credentials.Credentials] = None, | | 99 | + ) -> "artifact.Artifact": | | 100 | + """Creates a new Metadata Artifact. | | 101 | +
| | 102 | + Args: | | 103 | + metadata_store_id (str): | | 104 | + Optional. The <metadata_store_id> portion of the resource name with | | 105 | + the format: | | 106 | + projects/123/locations/us-central1/metadataStores/<metadata_store_id>/artifacts/<resource_id> | | 107 | + If not provided, the MetadataStore's ID will be set to "default". | | 108 | + project (str): | | 109 | + Optional. Project used to create this Artifact. Overrides project set in | | 110 | + aiplatform.init. | | 111 | + location (str): | | 112 | + Optional. Location used to create this Artifact. Overrides location set in | | 113 | + aiplatform.init. | | 114 | + credentials (auth_credentials.Credentials): | | 115 | + Optional. Custom credentials used to create this Artifact. Overrides | | 116 | + credentials set in aiplatform.init. | | 117 | + Returns: | | 118 | + Artifact: Instantiated representation of the managed Metadata Artifact. | | 119 | + """ | | 120 | + return artifact.Artifact.create_from_base_artifact_schema( | | 121 | + base_artifact_schema=self, | | 122 | + metadata_store_id=metadata_store_id, | | 123 | + project=project, | | 124 | + location=location, | | 125 | + credentials=credentials, | | 126 | + ) |
Collapse file Copy file name to clipboard +114Lines changed: 114 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# -*- coding: utf-8 -*- | | 2 | + | | 3 | +# Copyright 2022 Google LLC | | 4 | +# | | 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 6 | +# you may not use this file except in compliance with the License. | | 7 | +# You may obtain a copy of the License at | | 8 | +# | | 9 | +# http://www.apache.org/licenses/LICENSE-2.0 | | 10 | +# | | 11 | +# Unless required by applicable law or agreed to in writing, software | | 12 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 14 | +# See the License for the specific language governing permissions and | | 15 | +# limitations under the License. | | 16 | +# | | 17 | + | | 18 | +import abc | | 19 | + | | 20 | +from typing import Optional, Dict | | 21 | + | | 22 | +from google.auth import credentials as auth_credentials | | 23 | + | | 24 | +from google.cloud.aiplatform.compat.types import execution as gca_execution | | 25 | +from google.cloud.aiplatform.metadata import constants | | 26 | +from google.cloud.aiplatform.metadata import execution | | 27 | + | | 28 | + | | 29 | +class BaseExecutionSchema(metaclass=abc.ABCMeta): | | 30 | + """Base class for Metadata Execution schema.""" | | 31 | + | | 32 | + @property | | 33 | + @classmethod | | 34 | + @abc.abstractmethod | | 35 | + def schema_title(cls) -> str: | | 36 | + """Identifies the Vertex Metadta schema title used by the resource.""" | | 37 | + pass | | 38 | + | | 39 | + def __init__( | | 40 | + self, | | 41 | + *, | | 42 | + state: Optional[ | | 43 | + gca_execution.Execution.State | | 44 | + ] = gca_execution.Execution.State.RUNNING, | | 45 | + execution_id: Optional[str] = None, | | 46 | + display_name: Optional[str] = None, | | 47 | + schema_version: Optional[str] = None, | | 48 | + metadata: Optional[Dict] = None, | | 49 | + description: Optional[str] = None, | | 50 | + ): | | 51 | + | | 52 | + """Initializes the Execution with the given name, URI and metadata. | | 53 | +
| | 54 | + Args: | | 55 | + state (gca_execution.Execution.State.RUNNING): | | 56 | + Optional. State of this Execution. Defaults to RUNNING. | | 57 | + execution_id (str): | | 58 | + Optional. The <resource_id> portion of the Execution name with | | 59 | + the following format, this is globally unique in a metadataStore. | | 60 | + projects/123/locations/us-central1/metadataStores/<metadata_store_id>/executions/<resource_id>. | | 61 | + display_name (str): | | 62 | + Optional. The user-defined name of the Execution. | | 63 | + schema_version (str): | | 64 | + Optional. schema_version specifies the version used by the Execution. | | 65 | + If not set, defaults to use the latest version. | | 66 | + metadata (Dict): | | 67 | + Optional. Contains the metadata information that will be stored in the Execution. | | 68 | + description (str): | | 69 | + Optional. Describes the purpose of the Execution to be created. | | 70 | + """ | | 71 | + self.state = state | | 72 | + self.execution_id = execution_id | | 73 | + self.display_name = display_name | | 74 | + self.schema_version = schema_version or constants._DEFAULT_SCHEMA_VERSION | | 75 | + self.metadata = metadata | | 76 | + self.description = description | | 77 | + | | 78 | + def create( | | 79 | + self, | | 80 | + *, | | 81 | + metadata_store_id: Optional[str] = "default", | | 82 | + project: Optional[str] = None, | | 83 | + location: Optional[str] = None, | | 84 | + credentials: Optional[auth_credentials.Credentials] = None, | | 85 | + ) -> "execution.Execution": | | 86 | + """Creates a new Metadata Execution. | | 87 | +
| | 88 | + Args: | | 89 | + metadata_store_id (str): | | 90 | + Optional. The <metadata_store_id> portion of the resource name with | | 91 | + the format: | | 92 | + projects/123/locations/us-central1/metadataStores/<metadata_store_id>/executions/<resource_id> | | 93 | + If not provided, the MetadataStore's ID will be set to "default". | | 94 | + project (str): | | 95 | + Optional. Project used to create this Execution. Overrides project set in | | 96 | + aiplatform.init. | | 97 | + location (str): | | 98 | + Optional. Location used to create this Execution. Overrides location set in | | 99 | + aiplatform.init. | | 100 | + credentials (auth_credentials.Credentials): | | 101 | + Optional. Custom credentials used to create this Execution. Overrides | | 102 | + credentials set in aiplatform.init. | | 103 | + Returns: | | 104 | + Execution: Instantiated representation of the managed Metadata Execution. | | 105 | +
| | 106 | + """ | | 107 | + self.execution = execution.Execution.create_from_base_execution_schema( | | 108 | + base_execution_schema=self, | | 109 | + metadata_store_id=metadata_store_id, | | 110 | + project=project, | | 111 | + location=location, | | 112 | + credentials=credentials, | | 113 | + ) | | 114 | + return self.execution |
|