|
19 | 19 |
|
20 | 20 | import proto
|
21 | 21 |
|
| 22 | +from google.auth import credentials as auth_credentials |
| 23 | + |
22 | 24 | from google.cloud.aiplatform import base
|
23 | 25 | from google.cloud.aiplatform import utils
|
24 | 26 | from google.cloud.aiplatform.metadata import utils as metadata_utils
|
|
31 | 33 | )
|
32 | 34 | from google.cloud.aiplatform.metadata import artifact
|
33 | 35 | from google.cloud.aiplatform.metadata import execution
|
| 36 | +from google.cloud.aiplatform.metadata import metadata_store |
34 | 37 | from google.cloud.aiplatform.metadata import resource
|
35 | 38 |
|
36 | 39 |
|
37 |
| -class _Context(resource._Resource): |
| 40 | +class Context(resource._Resource): |
38 | 41 | """Metadata Context resource for Vertex AI"""
|
39 | 42 |
|
40 | 43 | _resource_noun = "contexts"
|
@@ -81,6 +84,153 @@ def get_artifacts(self) -> List[artifact.Artifact]:
|
81 | 84 | credentials=self.credentials,
|
82 | 85 | )
|
83 | 86 |
|
| 87 | + @classmethod |
| 88 | + def create( |
| 89 | + cls, |
| 90 | + schema_title: str, |
| 91 | + *, |
| 92 | + resource_id: Optional[str] = None, |
| 93 | + display_name: Optional[str] = None, |
| 94 | + schema_version: Optional[str] = None, |
| 95 | + description: Optional[str] = None, |
| 96 | + metadata: Optional[Dict] = None, |
| 97 | + metadata_store_id: Optional[str] = "default", |
| 98 | + project: Optional[str] = None, |
| 99 | + location: Optional[str] = None, |
| 100 | + credentials: Optional[auth_credentials.Credentials] = None, |
| 101 | + ) -> "Context": |
| 102 | + """Creates a new Metadata Context. |
| 103 | +
|
| 104 | + Args: |
| 105 | + schema_title (str): |
| 106 | + Required. schema_title identifies the schema title used by the Context. |
| 107 | + Please reference https://cloud.google.com/vertex-ai/docs/ml-metadata/system-schemas. |
| 108 | + resource_id (str): |
| 109 | + Optional. The <resource_id> portion of the Context name with |
| 110 | + the format. This is globally unique in a metadataStore: |
| 111 | + projects/123/locations/us-central1/metadataStores/<metadata_store_id>/Contexts/<resource_id>. |
| 112 | + display_name (str): |
| 113 | + Optional. The user-defined name of the Context. |
| 114 | + schema_version (str): |
| 115 | + Optional. schema_version specifies the version used by the Context. |
| 116 | + If not set, defaults to use the latest version. |
| 117 | + description (str): |
| 118 | + Optional. Describes the purpose of the Context to be created. |
| 119 | + metadata (Dict): |
| 120 | + Optional. Contains the metadata information that will be stored in the Context. |
| 121 | + metadata_store_id (str): |
| 122 | + Optional. The <metadata_store_id> portion of the resource name with |
| 123 | + the format: |
| 124 | + projects/123/locations/us-central1/metadataStores/<metadata_store_id>/Contexts/<resource_id> |
| 125 | + If not provided, the MetadataStore's ID will be set to "default". |
| 126 | + project (str): |
| 127 | + Optional. Project used to create this Context. Overrides project set in |
| 128 | + aiplatform.init. |
| 129 | + location (str): |
| 130 | + Optional. Location used to create this Context. Overrides location set in |
| 131 | + aiplatform.init. |
| 132 | + credentials (auth_credentials.Credentials): |
| 133 | + Optional. Custom credentials used to create this Context. Overrides |
| 134 | + credentials set in aiplatform.init. |
| 135 | +
|
| 136 | + Returns: |
| 137 | + Context: Instantiated representation of the managed Metadata Context. |
| 138 | + """ |
| 139 | + return cls._create( |
| 140 | + resource_id=resource_id, |
| 141 | + schema_title=schema_title, |
| 142 | + display_name=display_name, |
| 143 | + schema_version=schema_version, |
| 144 | + description=description, |
| 145 | + metadata=metadata, |
| 146 | + metadata_store_id=metadata_store_id, |
| 147 | + project=project, |
| 148 | + location=location, |
| 149 | + credentials=credentials, |
| 150 | + ) |
| 151 | + |
| 152 | + # TODO() refactor code to move _create to _Resource class. |
| 153 | + @classmethod |
| 154 | + def _create( |
| 155 | + cls, |
| 156 | + resource_id: str, |
| 157 | + schema_title: str, |
| 158 | + display_name: Optional[str] = None, |
| 159 | + schema_version: Optional[str] = None, |
| 160 | + description: Optional[str] = None, |
| 161 | + metadata: Optional[Dict] = None, |
| 162 | + metadata_store_id: Optional[str] = "default", |
| 163 | + project: Optional[str] = None, |
| 164 | + location: Optional[str] = None, |
| 165 | + credentials: Optional[auth_credentials.Credentials] = None, |
| 166 | + ) -> "Context": |
| 167 | + """Creates a new Metadata resource. |
| 168 | +
|
| 169 | + Args: |
| 170 | + resource_id (str): |
| 171 | + Required. The <resource_id> portion of the resource name with |
| 172 | + the format: |
| 173 | + projects/123/locations/us-central1/metadataStores/<metadata_store_id>/<resource_noun>/<resource_id>. |
| 174 | + schema_title (str): |
| 175 | + Required. schema_title identifies the schema title used by the resource. |
| 176 | + display_name (str): |
| 177 | + Optional. The user-defined name of the resource. |
| 178 | + schema_version (str): |
| 179 | + Optional. schema_version specifies the version used by the resource. |
| 180 | + If not set, defaults to use the latest version. |
| 181 | + description (str): |
| 182 | + Optional. Describes the purpose of the resource to be created. |
| 183 | + metadata (Dict): |
| 184 | + Optional. Contains the metadata information that will be stored in the resource. |
| 185 | + metadata_store_id (str): |
| 186 | + The <metadata_store_id> portion of the resource name with |
| 187 | + the format: |
| 188 | + projects/123/locations/us-central1/metadataStores/<metadata_store_id>/<resource_noun>/<resource_id> |
| 189 | + If not provided, the MetadataStore's ID will be set to "default". |
| 190 | + project (str): |
| 191 | + Project used to create this resource. Overrides project set in |
| 192 | + aiplatform.init. |
| 193 | + location (str): |
| 194 | + Location used to create this resource. Overrides location set in |
| 195 | + aiplatform.init. |
| 196 | + credentials (auth_credentials.Credentials): |
| 197 | + Custom credentials used to create this resource. Overrides |
| 198 | + credentials set in aiplatform.init. |
| 199 | +
|
| 200 | + Returns: |
| 201 | + resource (_Resource): |
| 202 | + Instantiated representation of the managed Metadata resource. |
| 203 | +
|
| 204 | + """ |
| 205 | + api_client = cls._instantiate_client(location=location, credentials=credentials) |
| 206 | + |
| 207 | + parent = utils.full_resource_name( |
| 208 | + resource_name=metadata_store_id, |
| 209 | + resource_noun=metadata_store._MetadataStore._resource_noun, |
| 210 | + parse_resource_name_method=metadata_store._MetadataStore._parse_resource_name, |
| 211 | + format_resource_name_method=metadata_store._MetadataStore._format_resource_name, |
| 212 | + project=project, |
| 213 | + location=location, |
| 214 | + ) |
| 215 | + |
| 216 | + resource = cls._create_resource( |
| 217 | + client=api_client, |
| 218 | + parent=parent, |
| 219 | + resource_id=resource_id, |
| 220 | + schema_title=schema_title, |
| 221 | + display_name=display_name, |
| 222 | + schema_version=schema_version, |
| 223 | + description=description, |
| 224 | + metadata=metadata, |
| 225 | + ) |
| 226 | + |
| 227 | + self = cls._empty_constructor( |
| 228 | + project=project, location=location, credentials=credentials |
| 229 | + ) |
| 230 | + self._gca_resource = resource |
| 231 | + |
| 232 | + return self |
| 233 | + |
84 | 234 | @classmethod
|
85 | 235 | def _create_resource(
|
86 | 236 | cls,
|
@@ -147,7 +297,7 @@ def _list_resources(
|
147 | 297 | )
|
148 | 298 | return client.list_contexts(request=list_request)
|
149 | 299 |
|
150 |
| - def add_context_children(self, contexts: List["_Context"]): |
| 300 | + def add_context_children(self, contexts: List["Context"]): |
151 | 301 | """Adds the provided contexts as children of this context.
|
152 | 302 |
|
153 | 303 | Args:
|
|
0 commit comments