Skip to content

Commit

Permalink
feat: sample code for Vertex AI Feature Store
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 640224428
  • Loading branch information
vertex-sdk-bot authored and copybara-github committed Jun 4, 2024
1 parent bc8b14a commit 6c14e8b
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 2 deletions.
25 changes: 25 additions & 0 deletions samples/model-builder/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from unittest.mock import MagicMock, patch

from google.cloud import aiplatform
import vertexai
import pytest


Expand Down Expand Up @@ -691,6 +692,30 @@ def mock_write_feature_values(mock_entity_type):
yield mock_write_feature_values


@pytest.fixture
def mock_feature_online_store():
mock = MagicMock(vertexai.resources.preview.FeatureOnlineStore)
yield mock


@pytest.fixture
def mock_create_feature_online_store(mock_feature_online_store):
with patch.object(
vertexai.resources.preview.FeatureOnlineStore, "create_bigtable_store"
) as mock_create_feature_online_store:
mock_create_feature_online_store.return_value = mock_feature_online_store
yield mock_create_feature_online_store


@pytest.fixture
def mock_create_optimized_public_online_store(mock_feature_online_store):
with patch.object(
vertexai.resources.preview.FeatureOnlineStore, "create_optimized_store"
) as mock_create_optimized_store:
mock_create_optimized_store.return_value = mock_feature_online_store
yield mock_create_optimized_store


"""
----------------------------------------------------------------------------
Experiment Tracking Fixtures
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2024 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.

# [START aiplatform_sdk_create_bigtable_feature_online_store_sample]

from google.cloud import aiplatform
import vertexai


def create_bigtable_feature_online_store_sample(
project: str,
location: str,
feature_online_store_id: str,
):
aiplatform.init(project=project, location=location)
fos = vertexai.resources.preview.FeatureOnlineStore.create_bigtable_store(
feature_online_store_id
)
return fos


# [END aiplatform_sdk_create_bigtable_feature_online_store_sample]
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2024 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 feature_store import create_bigtable_feature_online_store_sample

import test_constants as constants


def test_create_bigtable_feature_online_store_sample(
mock_sdk_init, mock_create_feature_online_store
):
create_bigtable_feature_online_store_sample.create_bigtable_feature_online_store_sample(
project=constants.PROJECT,
location=constants.LOCATION,
feature_online_store_id=constants.FEATURE_ONLINE_STORE_ID,
)

mock_sdk_init.assert_called_once_with(
project=constants.PROJECT, location=constants.LOCATION
)

mock_create_feature_online_store.assert_called_once_with(
constants.FEATURE_ONLINE_STORE_ID
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2024 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.

# [START aiplatform_sdk_create_optimized_public_feature_online_store_sample]

from google.cloud import aiplatform
import vertexai


def create_optimized_public_feature_online_store_sample(
project: str,
location: str,
feature_online_store_id: str,
):
aiplatform.init(project=project, location=location)
fos = vertexai.resources.preview.FeatureOnlineStore.create_optimized_store(
feature_online_store_id
)
return fos


# [END aiplatform_sdk_create_optimized_public_feature_online_store_sample]
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2024 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 feature_store import create_optimized_public_feature_online_store_sample
import test_constants as constants


def test_create_optimized_feature_online_store_sample(
mock_sdk_init, mock_create_optimized_public_online_store
):

create_optimized_public_feature_online_store_sample.create_optimized_public_feature_online_store_sample(
project=constants.PROJECT,
location=constants.LOCATION,
feature_online_store_id=constants.FEATURE_ONLINE_STORE_ID,
)

mock_sdk_init.assert_called_once_with(
project=constants.PROJECT, location=constants.LOCATION
)

mock_create_optimized_public_online_store.assert_called_once_with(
constants.FEATURE_ONLINE_STORE_ID
)
7 changes: 5 additions & 2 deletions samples/model-builder/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from random import randint
from uuid import uuid4

from google.protobuf import timestamp_pb2
from google.auth import credentials
from google.cloud import aiplatform
from google.protobuf import timestamp_pb2

PROJECT = "abc"
LOCATION = "us-central1"
Expand Down Expand Up @@ -208,7 +208,7 @@
PYTHON_MODULE_NAME = "trainer.task"
MODEL_TYPE = "CLOUD"

# Feature store constants
# Feature store constants (legacy)
FEATURESTORE_ID = "movie_prediction"
FEATURESTORE_NAME = (
f"projects/{PROJECT}/locations/{LOCATION}/featurestores/{FEATURESTORE_ID}"
Expand Down Expand Up @@ -252,6 +252,9 @@
GCS_SOURCE_TYPE = "avro"
WORKER_COUNT = 1

# Feature online store constants
FEATURE_ONLINE_STORE_ID = "sample_feature_online_store"

TABULAR_TARGET_COLUMN = "target_column"
FORECASTNG_TIME_COLUMN = "date"
FORECASTNG_TIME_SERIES_IDENTIFIER_COLUMN = "time_series_id"
Expand Down
2 changes: 2 additions & 0 deletions vertexai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

from google.cloud.aiplatform import init
from vertexai import preview
from vertexai import resources

__all__ = [
"init",
"preview",
"resources",
]
2 changes: 2 additions & 0 deletions vertexai/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""The vertexai resources module."""

from google.cloud.aiplatform import initializer
from vertexai.resources import preview

from google.cloud.aiplatform.datasets import (
ImageDataset,
Expand Down Expand Up @@ -177,4 +178,5 @@
"TimeSeriesDataset",
"TimeSeriesDenseEncoderForecastingTrainingJob",
"VideoDataset",
"preview",
)

0 comments on commit 6c14e8b

Please sign in to comment.