Skip to content

Commit d4b0091

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
chore: feature store code sample - feature view from BQ source w/embedding management
PiperOrigin-RevId: 646156912
1 parent a90ee8d commit d4b0091

3 files changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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_view_from_bq_source_with_embedding_management]
16+
17+
from google.cloud import aiplatform
18+
from vertexai.resources.preview import feature_store
19+
from typing import List
20+
21+
22+
def create_feature_view_from_bq_source_with_embedding_management(
23+
project: str,
24+
location: str,
25+
existing_feature_online_store_id: str,
26+
feature_view_id: str,
27+
bq_table_uri: str,
28+
entity_id_columns: List[str],
29+
embedding_column: str,
30+
embedding_dimensions: str,
31+
):
32+
aiplatform.init(project=project, location=location)
33+
34+
fos = feature_store.FeatureOnlineStore(existing_feature_online_store_id)
35+
36+
bigquery_source = feature_store.utils.FeatureViewBigQuerySource(
37+
uri=bq_table_uri,
38+
entity_id_columns=entity_id_columns,
39+
)
40+
index_config = feature_store.utils.IndexConfig(
41+
embedding_column=embedding_column,
42+
dimensions=embedding_dimensions,
43+
algorithm_config=feature_store.utils.TreeAhConfig(),
44+
)
45+
fv = fos.create_feature_view(
46+
name=feature_view_id,
47+
source=bigquery_source,
48+
index_config=index_config,
49+
)
50+
return fv
51+
52+
53+
# [END aiplatform_sdk_create_feature_view_from_bq_source_with_embedding_management]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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_view_from_bq_source_with_embedding_management
16+
import test_constants as constants
17+
18+
19+
def test_create_feature_view_from_bq_source_with_embedding_management(
20+
mock_sdk_init,
21+
mock_get_feature_online_store,
22+
):
23+
create_feature_view_from_bq_source_with_embedding_management.create_feature_view_from_bq_source_with_embedding_management(
24+
project=constants.PROJECT,
25+
location=constants.LOCATION,
26+
existing_feature_online_store_id=constants.FEATURE_ONLINE_STORE_ID,
27+
feature_view_id=constants.FEATURE_VIEW_ID,
28+
bq_table_uri=constants.FEATURE_VIEW_BQ_URI,
29+
entity_id_columns=constants.FEATURE_VIEW_BQ_ENTITY_ID_COLUMNS,
30+
embedding_column=constants.FEATURE_VIEW_BQ_EMBEDDING_COLUMN,
31+
embedding_dimensions=constants.FEATURE_VIEW_BQ_EMBEDDING_DIMENSIONS,
32+
)
33+
34+
mock_sdk_init.assert_called_once_with(
35+
project=constants.PROJECT, location=constants.LOCATION
36+
)
37+
38+
mock_get_feature_online_store.assert_called_once()
39+
mock_get_feature_online_store.return_value.create_feature_view.assert_called_once_with(
40+
name=constants.FEATURE_VIEW_ID,
41+
source=constants.FEATURE_VIEW_BQ_SOURCE,
42+
index_config=constants.FEATURE_VIEW_BQ_INDEX_CONFIG,
43+
)

samples/model-builder/test_constants.py

+9
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,15 @@
264264
entity_id_columns=FEATURE_VIEW_BQ_ENTITY_ID_COLUMNS,
265265
)
266266
)
267+
FEATURE_VIEW_BQ_EMBEDDING_COLUMN = "embedding"
268+
FEATURE_VIEW_BQ_EMBEDDING_DIMENSIONS = 10
269+
FEATURE_VIEW_BQ_INDEX_CONFIG = (
270+
vertexai.resources.preview.feature_store.utils.IndexConfig(
271+
embedding_column=FEATURE_VIEW_BQ_EMBEDDING_COLUMN,
272+
dimensions=FEATURE_VIEW_BQ_EMBEDDING_DIMENSIONS,
273+
algorithm_config=vertexai.resources.preview.feature_store.utils.TreeAhConfig(),
274+
)
275+
)
267276
FEATURE_GROUP_ID = "sample_feature_group"
268277
PROJECT_ALLOWLISTED = ["test-project"]
269278

0 commit comments

Comments
 (0)