Skip to content

Commit 2c28b4e

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: sample code for Vertex AI Feature Store
PiperOrigin-RevId: 646188701
1 parent 380c9d9 commit 2c28b4e

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

samples/model-builder/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,21 @@ def mock_create_feature_group(mock_feature_group):
731731
yield mock_create_feature_group
732732

733733

734+
@pytest.fixture
735+
def mock_registry_feature():
736+
mock = MagicMock(preview_resources.Feature)
737+
yield mock
738+
739+
740+
@pytest.fixture
741+
def mock_create_registry_feature(mock_feature_group, mock_registry_feature):
742+
with patch.object(
743+
mock_feature_group, "create_feature"
744+
) as mock_create_registry_feature:
745+
mock_create_registry_feature.return_value = mock_registry_feature
746+
yield mock_create_registry_feature
747+
748+
734749
@pytest.fixture
735750
def mock_create_optimized_private_online_store(mock_feature_online_store):
736751
with patch.object(
Lines changed: 35 additions & 0 deletions
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+
# [START aiplatform_sdk_create_feature_sample]
16+
17+
from google.cloud import aiplatform
18+
from vertexai.resources.preview import feature_store
19+
20+
21+
def create_feature_sample(
22+
project: str,
23+
location: str,
24+
feature_group_id: str,
25+
feature_id: str,
26+
):
27+
aiplatform.init(project=project, location=location)
28+
feature_group = feature_store.FeatureGroup.create(
29+
feature_group_id
30+
)
31+
feature = feature_group.create_feature(feature_id)
32+
return feature
33+
34+
35+
# [END aiplatform_sdk_create_feature_sample]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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_feature_sample
16+
17+
import test_constants as constants
18+
19+
20+
def test_create_feature_sample(
21+
mock_sdk_init, mock_create_registry_feature, mock_create_feature_group
22+
):
23+
create_feature_sample.create_feature_sample(
24+
project=constants.PROJECT,
25+
location=constants.LOCATION,
26+
feature_group_id=constants.FEATURE_GROUP_ID,
27+
feature_id=constants.REGISTRY_FEATURE_ID,
28+
)
29+
30+
mock_sdk_init.assert_called_once_with(
31+
project=constants.PROJECT, location=constants.LOCATION
32+
)
33+
34+
mock_create_feature_group.assert_called_once_with(
35+
constants.FEATURE_GROUP_ID
36+
)
37+
38+
mock_create_registry_feature.assert_called_once_with(
39+
constants.REGISTRY_FEATURE_ID
40+
)

samples/model-builder/test_constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@
274274
)
275275
)
276276
FEATURE_GROUP_ID = "sample_feature_group"
277+
REGISTRY_FEATURE_ID = "sample_feature"
277278
PROJECT_ALLOWLISTED = ["test-project"]
278279

279280
TABULAR_TARGET_COLUMN = "target_column"

0 commit comments

Comments
 (0)