Skip to content

Commit 6c14e8b

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: sample code for Vertex AI Feature Store
PiperOrigin-RevId: 640224428
1 parent bc8b14a commit 6c14e8b

8 files changed

+170
-2
lines changed

samples/model-builder/conftest.py

+25
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from unittest.mock import MagicMock, patch
1616

1717
from google.cloud import aiplatform
18+
import vertexai
1819
import pytest
1920

2021

@@ -691,6 +692,30 @@ def mock_write_feature_values(mock_entity_type):
691692
yield mock_write_feature_values
692693

693694

695+
@pytest.fixture
696+
def mock_feature_online_store():
697+
mock = MagicMock(vertexai.resources.preview.FeatureOnlineStore)
698+
yield mock
699+
700+
701+
@pytest.fixture
702+
def mock_create_feature_online_store(mock_feature_online_store):
703+
with patch.object(
704+
vertexai.resources.preview.FeatureOnlineStore, "create_bigtable_store"
705+
) as mock_create_feature_online_store:
706+
mock_create_feature_online_store.return_value = mock_feature_online_store
707+
yield mock_create_feature_online_store
708+
709+
710+
@pytest.fixture
711+
def mock_create_optimized_public_online_store(mock_feature_online_store):
712+
with patch.object(
713+
vertexai.resources.preview.FeatureOnlineStore, "create_optimized_store"
714+
) as mock_create_optimized_store:
715+
mock_create_optimized_store.return_value = mock_feature_online_store
716+
yield mock_create_optimized_store
717+
718+
694719
"""
695720
----------------------------------------------------------------------------
696721
Experiment Tracking Fixtures
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2024 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+
# [START aiplatform_sdk_create_bigtable_feature_online_store_sample]
16+
17+
from google.cloud import aiplatform
18+
import vertexai
19+
20+
21+
def create_bigtable_feature_online_store_sample(
22+
project: str,
23+
location: str,
24+
feature_online_store_id: str,
25+
):
26+
aiplatform.init(project=project, location=location)
27+
fos = vertexai.resources.preview.FeatureOnlineStore.create_bigtable_store(
28+
feature_online_store_id
29+
)
30+
return fos
31+
32+
33+
# [END aiplatform_sdk_create_bigtable_feature_online_store_sample]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2024 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 feature_store import create_bigtable_feature_online_store_sample
16+
17+
import test_constants as constants
18+
19+
20+
def test_create_bigtable_feature_online_store_sample(
21+
mock_sdk_init, mock_create_feature_online_store
22+
):
23+
create_bigtable_feature_online_store_sample.create_bigtable_feature_online_store_sample(
24+
project=constants.PROJECT,
25+
location=constants.LOCATION,
26+
feature_online_store_id=constants.FEATURE_ONLINE_STORE_ID,
27+
)
28+
29+
mock_sdk_init.assert_called_once_with(
30+
project=constants.PROJECT, location=constants.LOCATION
31+
)
32+
33+
mock_create_feature_online_store.assert_called_once_with(
34+
constants.FEATURE_ONLINE_STORE_ID
35+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2024 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+
# [START aiplatform_sdk_create_optimized_public_feature_online_store_sample]
16+
17+
from google.cloud import aiplatform
18+
import vertexai
19+
20+
21+
def create_optimized_public_feature_online_store_sample(
22+
project: str,
23+
location: str,
24+
feature_online_store_id: str,
25+
):
26+
aiplatform.init(project=project, location=location)
27+
fos = vertexai.resources.preview.FeatureOnlineStore.create_optimized_store(
28+
feature_online_store_id
29+
)
30+
return fos
31+
32+
33+
# [END aiplatform_sdk_create_optimized_public_feature_online_store_sample]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2024 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 feature_store import create_optimized_public_feature_online_store_sample
16+
import test_constants as constants
17+
18+
19+
def test_create_optimized_feature_online_store_sample(
20+
mock_sdk_init, mock_create_optimized_public_online_store
21+
):
22+
23+
create_optimized_public_feature_online_store_sample.create_optimized_public_feature_online_store_sample(
24+
project=constants.PROJECT,
25+
location=constants.LOCATION,
26+
feature_online_store_id=constants.FEATURE_ONLINE_STORE_ID,
27+
)
28+
29+
mock_sdk_init.assert_called_once_with(
30+
project=constants.PROJECT, location=constants.LOCATION
31+
)
32+
33+
mock_create_optimized_public_online_store.assert_called_once_with(
34+
constants.FEATURE_ONLINE_STORE_ID
35+
)

samples/model-builder/test_constants.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
from random import randint
1616
from uuid import uuid4
1717

18+
from google.protobuf import timestamp_pb2
1819
from google.auth import credentials
1920
from google.cloud import aiplatform
20-
from google.protobuf import timestamp_pb2
2121

2222
PROJECT = "abc"
2323
LOCATION = "us-central1"
@@ -208,7 +208,7 @@
208208
PYTHON_MODULE_NAME = "trainer.task"
209209
MODEL_TYPE = "CLOUD"
210210

211-
# Feature store constants
211+
# Feature store constants (legacy)
212212
FEATURESTORE_ID = "movie_prediction"
213213
FEATURESTORE_NAME = (
214214
f"projects/{PROJECT}/locations/{LOCATION}/featurestores/{FEATURESTORE_ID}"
@@ -252,6 +252,9 @@
252252
GCS_SOURCE_TYPE = "avro"
253253
WORKER_COUNT = 1
254254

255+
# Feature online store constants
256+
FEATURE_ONLINE_STORE_ID = "sample_feature_online_store"
257+
255258
TABULAR_TARGET_COLUMN = "target_column"
256259
FORECASTNG_TIME_COLUMN = "date"
257260
FORECASTNG_TIME_SERIES_IDENTIFIER_COLUMN = "time_series_id"

vertexai/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121
from google.cloud.aiplatform import init
2222
from vertexai import preview
23+
from vertexai import resources
2324

2425
__all__ = [
2526
"init",
2627
"preview",
28+
"resources",
2729
]

vertexai/resources/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"""The vertexai resources module."""
1818

1919
from google.cloud.aiplatform import initializer
20+
from vertexai.resources import preview
2021

2122
from google.cloud.aiplatform.datasets import (
2223
ImageDataset,
@@ -177,4 +178,5 @@
177178
"TimeSeriesDataset",
178179
"TimeSeriesDenseEncoderForecastingTrainingJob",
179180
"VideoDataset",
181+
"preview",
180182
)

0 commit comments

Comments
 (0)