|
15 | 15 | # limitations under the License.
|
16 | 16 | #
|
17 | 17 |
|
18 |
| -from typing import ( |
19 |
| - Sequence, |
20 |
| - Tuple, |
21 |
| - Dict, |
22 |
| - List, |
23 |
| - Optional, |
24 |
| -) |
| 18 | +from typing import Dict, List, Optional, Sequence, Tuple |
25 | 19 | from google.auth import credentials as auth_credentials
|
26 | 20 | from google.cloud.aiplatform import base, initializer
|
27 | 21 | from google.cloud.aiplatform import utils
|
28 | 22 | from google.cloud.aiplatform.compat.types import (
|
29 | 23 | feature as gca_feature,
|
30 | 24 | feature_group as gca_feature_group,
|
31 | 25 | io as gca_io,
|
| 26 | + feature_monitor_v1beta1 as gca_feature_monitor, |
32 | 27 | )
|
33 | 28 | from vertexai.resources.preview.feature_store.utils import (
|
34 | 29 | FeatureGroupBigQuerySource,
|
@@ -428,6 +423,127 @@ def get_feature_monitor(
|
428 | 423 | credentials=credentials,
|
429 | 424 | )
|
430 | 425 |
|
| 426 | + def create_feature_monitor( |
| 427 | + self, |
| 428 | + name: str, |
| 429 | + description: Optional[str] = None, |
| 430 | + labels: Optional[Dict[str, str]] = None, |
| 431 | + schedule_config: Optional[str] = None, |
| 432 | + feature_selection_configs: Optional[List[Tuple[str, float]]] = None, |
| 433 | + project: Optional[str] = None, |
| 434 | + location: Optional[str] = None, |
| 435 | + credentials: Optional[auth_credentials.Credentials] = None, |
| 436 | + request_metadata: Optional[Sequence[Tuple[str, str]]] = None, |
| 437 | + create_request_timeout: Optional[float] = None, |
| 438 | + ) -> FeatureMonitor: |
| 439 | + """Creates a new feature monitor. |
| 440 | +
|
| 441 | + Args: |
| 442 | + name: The name of the feature monitor. |
| 443 | + description: Description of the feature monitor. |
| 444 | + labels: |
| 445 | + The labels with user-defined metadata to organize your FeatureMonitors. |
| 446 | + Label keys and values can be no longer than 64 characters |
| 447 | + (Unicode codepoints), can only contain lowercase letters, |
| 448 | + numeric characters, underscores and dashes. International |
| 449 | + characters are allowed. |
| 450 | +
|
| 451 | + See https://goo.gl/xmQnxf for more information on and examples |
| 452 | + of labels. No more than 64 user labels can be associated with |
| 453 | + one FeatureMonitor (System labels are excluded)." System reserved label |
| 454 | + keys are prefixed with "aiplatform.googleapis.com/" and are |
| 455 | + immutable. |
| 456 | + schedule_config: |
| 457 | + Configures when data is to be monitored for this |
| 458 | + FeatureMonitor. At the end of the scheduled time, |
| 459 | + the stats and drift are generated for the selected features. |
| 460 | + Example format: "TZ=America/New_York 0 9 * * *" (monitors |
| 461 | + daily at 9 AM EST). |
| 462 | + feature_selection_configs: |
| 463 | + List of tuples of feature id and monitoring threshold. If unset, |
| 464 | + all features in the feature group will be monitored, and the |
| 465 | + default thresholds 0.3 will be used. |
| 466 | + project: |
| 467 | + Project to create feature in. If unset, the project set in |
| 468 | + aiplatform.init will be used. |
| 469 | + location: |
| 470 | + Location to create feature in. If not set, location set in |
| 471 | + aiplatform.init will be used. |
| 472 | + credentials: |
| 473 | + Custom credentials to use to create this feature. Overrides |
| 474 | + credentials set in aiplatform.init. |
| 475 | + request_metadata: |
| 476 | + Strings which should be sent along with the request as metadata. |
| 477 | + create_request_timeout: |
| 478 | + The timeout for the create request in seconds. |
| 479 | +
|
| 480 | + Returns: |
| 481 | + FeatureMonitor - the FeatureMonitor resource object. |
| 482 | + """ |
| 483 | + |
| 484 | + gapic_feature_monitor = gca_feature_monitor.FeatureMonitor() |
| 485 | + |
| 486 | + if description: |
| 487 | + gapic_feature_monitor.description = description |
| 488 | + |
| 489 | + if labels: |
| 490 | + utils.validate_labels(labels) |
| 491 | + gapic_feature_monitor.labels = labels |
| 492 | + |
| 493 | + if request_metadata is None: |
| 494 | + request_metadata = () |
| 495 | + |
| 496 | + if schedule_config: |
| 497 | + gapic_feature_monitor.schedule_config = gca_feature_monitor.ScheduleConfig( |
| 498 | + cron=schedule_config |
| 499 | + ) |
| 500 | + |
| 501 | + if feature_selection_configs is None: |
| 502 | + raise ValueError( |
| 503 | + "Please specify feature_configs: features to be monitored and" |
| 504 | + " their thresholds." |
| 505 | + ) |
| 506 | + |
| 507 | + if feature_selection_configs is not None: |
| 508 | + gapic_feature_monitor.feature_selection_config.feature_configs = [ |
| 509 | + gca_feature_monitor.FeatureSelectionConfig.FeatureConfig( |
| 510 | + feature_id=feature_id, |
| 511 | + drift_threshold=threshold if threshold else 0.3, |
| 512 | + ) |
| 513 | + for feature_id, threshold in feature_selection_configs |
| 514 | + ] |
| 515 | + |
| 516 | + api_client = self.__class__._instantiate_client( |
| 517 | + location=location, credentials=credentials |
| 518 | + ) |
| 519 | + |
| 520 | + create_feature_monitor_lro = api_client.select_version( |
| 521 | + "v1beta1" |
| 522 | + ).create_feature_monitor( |
| 523 | + parent=self.resource_name, |
| 524 | + feature_monitor=gapic_feature_monitor, |
| 525 | + feature_monitor_id=name, |
| 526 | + metadata=request_metadata, |
| 527 | + timeout=create_request_timeout, |
| 528 | + ) |
| 529 | + |
| 530 | + _LOGGER.log_create_with_lro(FeatureMonitor, create_feature_monitor_lro) |
| 531 | + |
| 532 | + created_feature_monitor = create_feature_monitor_lro.result() |
| 533 | + |
| 534 | + _LOGGER.log_create_complete( |
| 535 | + FeatureMonitor, created_feature_monitor, "feature_monitor" |
| 536 | + ) |
| 537 | + |
| 538 | + feature_monitor_obj = FeatureMonitor( |
| 539 | + name=created_feature_monitor.name, |
| 540 | + project=project, |
| 541 | + location=location, |
| 542 | + credentials=credentials, |
| 543 | + ) |
| 544 | + |
| 545 | + return feature_monitor_obj |
| 546 | + |
431 | 547 | @property
|
432 | 548 | def source(self) -> FeatureGroupBigQuerySource:
|
433 | 549 | return FeatureGroupBigQuerySource(
|
|
0 commit comments