Deploy and Serve a Feature List for Model Predictions¶
Once you have a feature list ready, it's essential to serve these features effectively to make real-time or batch predictions using your machine learning model.
In this section, we'll take the feature list we previously crafted and explore two primary ways to serve its values:
REST API: Ideal for real-time predictions where you need instantaneous results. For instance, in applications where user interactions require immediate feedback based on model predictions.
Batch Processing: Best suited for scenarios where you have a bulk of data and don't require instant results.
Important Note for FeatureByte Enterprise Users¶
In Catalogs with Approval Flow enabled, moving features to production-ready status involves a comprehensive approval process.
This includes several evaluations, such as checking the feature's compliance with default cleaning operations and the feature job setting of its source tables. It also involves confirming the status of these tables and backtesting the feature job setting to prevent future training-serving inconsistencies. Additionally, essential details of the feature, particularly its feature definition file, are shared and subjected to a thorough review.
import featurebyte as fb
from datetime import datetime
# Set your profile to the tutorial environment
fb.use_profile("tutorial")
catalog_name = "Grocery Dataset Tutorial"
catalog = fb.Catalog.activate(catalog_name)
15:35:52 | INFO | SDK version: 1.0.2.dev46 15:35:52 | INFO | No catalog activated. 15:35:52 | INFO | Using profile: tutorial 15:35:53 | INFO | Using configuration file at: /Users/gxav/.featurebyte/config.yaml 15:35:53 | INFO | Active profile: tutorial (https://tutorials.featurebyte.com/api/v1) 15:35:53 | INFO | SDK version: 1.0.2.dev46 15:35:53 | INFO | No catalog activated. 15:35:53 | INFO | Catalog activated: Grocery Dataset Tutorial
List feature lists in Catalog¶
catalog.list_feature_lists()
id | name | num_feature | status | deployed | readiness_frac | online_frac | tables | entities | primary_entity | created_at | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 662b59023bbb17418af6e696 | Customer x ProductGroup Simple FeatureList | 9 | DRAFT | False | 0.0 | 0.0 | [GROCERYCUSTOMER, GROCERYINVOICE, INVOICEITEMS... | [customer, productgroup] | [customer, productgroup] | 2024-04-26T07:34:53.267000 |
Get a feature list from Catalog¶
simple_feature_list = catalog.get_feature_list("Customer x ProductGroup Simple FeatureList")
Loading Feature(s) |████████████████████████████████████████| 9/9 [100%] in 0.5s
Deploy feature list¶
# Create a deployment
deployment = simple_feature_list.deploy(
deployment_name="In-Store Customer x ProductGroup Spending 2w Spending - 9 features",
make_production_ready=True,
)
Done! |████████████████████████████████████████| 100% in 6.2s (0.16%/s) Done! |████████████████████████████████████████| 100% in 6.1s (0.17%/s)
# Enable deployment
deployment.enable()
Done! |████████████████████████████████████████| 100% in 1:37.4 (0.01%/s)
# Check that the deployment is enabled
catalog.list_deployments()
id | name | feature_list_name | feature_list_version | num_feature | enabled | |
---|---|---|---|---|---|---|
0 | 662b5962d9a443d42ef26718 | In-Store Customer x ProductGroup Spending 2w S... | Customer x ProductGroup Simple FeatureList | V240426 | 9 | True |
# Check status of the feature list
print("simple_feature_list.status:", simple_feature_list.status)
simple_feature_list.status: DEPLOYED
Get template for online serving¶
# Get a python template for consuming the feature serving API
deployment.get_online_serving_code(language="python")
from typing import Any, Dict
import pandas as pd
import requests
def request_features(entity_serving_names: Dict[str, Any]) -> pd.DataFrame:
"""
Send POST request to online serving endpoint
Parameters
----------
entity_serving_names: Dict[str, Any]
Entity serving name values to used for serving request
Returns
-------
pd.DataFrame
"""
response = requests.post(
url="https://tutorials.featurebyte.com/api/v1/deployment/662b5962d9a443d42ef26718/online_features",
headers={"Content-Type": "application/json", "active-catalog-id": "662b5752b408f15615a00d7d", "Authorization": "Bearer alZJY8HvLxoi_emU_eukghMpSMzxL0eQ4ufUSWxvsLk"},
json={"entity_serving_names": entity_serving_names},
)
assert response.status_code == 200, response.json()
return pd.DataFrame.from_dict(response.json()["features"])
request_features([{"GROCERYCUSTOMERGUID": "016d7230-8602-42ef-9861-c672f48fb010", "PRODUCTGROUP": "Gla\u00e7ons"}])
# Get shell script
deployment.get_online_serving_code(language="sh")
#!/bin/sh
curl -X POST \
-H 'Content-Type: application/json' \
-H 'active-catalog-id: 662b5752b408f15615a00d7d' \
-H 'Authorization: Bearer alZJY8HvLxoi_emU_eukghMpSMzxL0eQ4ufUSWxvsLk' \
-d '{"entity_serving_names": [{"GROCERYCUSTOMERGUID": "016d7230-8602-42ef-9861-c672f48fb010", "PRODUCTGROUP": "Gla\u00e7ons"}]}' \
https://tutorials.featurebyte.com/api/v1/deployment/662b5962d9a443d42ef26718/online_features
Create batch request table¶
Although batch request is not suitable for our "In-Store" use case, let's run a batch request for all current customers for a campaign for "Fromages".
# Get view of current customer
customer_table = catalog.get_table("GROCERYCUSTOMER")
customer_view = customer_table.get_view(view_mode="manual")
cond = customer_view.CurrentRecord == True
current_customer_view = customer_view[cond]
# Check primary entity of the deployed feature list to obtain its serving name
simple_feature_list.primary_entity
[<featurebyte.api.entity.Entity at 0x1546a4c80> { 'name': 'customer', 'created_at': '2024-04-26T07:28:15.128000', 'updated_at': '2024-04-26T07:28:24.021000', 'description': None, 'serving_names': [ 'GROCERYCUSTOMERGUID' ], 'catalog_name': 'Grocery Dataset Tutorial' }, <featurebyte.api.entity.Entity at 0x154b793c0> { 'name': 'productgroup', 'created_at': '2024-04-26T07:28:17.313000', 'updated_at': '2024-04-26T07:28:27.128000', 'description': None, 'serving_names': [ 'PRODUCTGROUP' ], 'catalog_name': 'Grocery Dataset Tutorial' }]
# Add PRODUCTGROUP to the view with "Fromages" for a specific targeted campaign.
current_customer_view["PRODUCTGROUP"] = "Fromages"
# Create batch request table from the view
batch_request_table = current_customer_view.create_batch_request_table(
name="Current Customers for Fromages at " + datetime.now().strftime("%Y%m%d:%H%M"),
columns=["GroceryCustomerGuid", "PRODUCTGROUP"],
columns_rename_mapping={
"GroceryCustomerGuid": "GROCERYCUSTOMERGUID",
}
)
Done! |████████████████████████████████████████| 100% in 12.5s (0.08%/s)
# Get name of the batch request table
batch_request_table.name
'Current Customers for Fromages at 20240426:1537'
# List batch request tables in catalog
catalog.list_batch_request_tables()
id | name | type | shape | feature_store_name | created_at | |
---|---|---|---|---|---|---|
0 | 662b59d4d9a443d42ef26719 | Current Customers for Fromages at 20240426:1537 | view | [500, 2] | playground | 2024-04-26T07:38:03.469000 |
Compute batch feature table¶
# Get deployment and batch request table
deployment = catalog.get_deployment("In-Store Customer x ProductGroup Spending 2w Spending - 9 features")
batch_request_table = catalog.get_batch_request_table(batch_request_table.name)
# Compute batch features
batch_features = deployment.compute_batch_feature_table(
batch_request_table=batch_request_table,
batch_feature_table_name =
f"Customer Simple FeatureList for Spending forecast with {batch_request_table.name}"
)
Done! |████████████████████████████████████████| 100% in 9.6s (0.10%/s)
# List observation tables
catalog.list_batch_feature_tables()
id | name | feature_store_name | batch_request_table_name | shape | created_at | |
---|---|---|---|---|---|---|
0 | 662b59e2d9a443d42ef2671a | Customer Simple FeatureList for Spending forec... | playground | Current Customers for Fromages at 20240426:1537 | [500, 11] | 2024-04-26T07:38:16.321000 |
# Convert to pandas
batch_features.to_pandas()
Downloading table |████████████████████████████████████████| 500/500 [100%] in 0
GROCERYCUSTOMERGUID | PRODUCTGROUP | CUSTOMER_Age_band | CUSTOMER_Latest_invoice_Amount | CUSTOMER_Count_of_invoice_14d | CUSTOMER_Avg_of_invoice_Amount_14d | CUSTOMER_Std_of_invoice_Amount_14d | CUSTOMER_Latest_invoice_Amount_Z_Score_to_invoice_Amount_28d | CUSTOMER_vs_OVERALL_item_TotalCost_across_product_ProductGroups_26w | CUSTOMER_x_PRODUCTGROUP_Sum_of_item_TotalCost_14d | CUSTOMER_x_PRODUCTGROUP_Time_Since_Latest_Timestamp | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 3671f019-0bb3-46b0-be1b-17e7cf9c5239 | Fromages | 65-69 | 9.49 | 1.0 | 9.490000 | 0.000000 | NaN | 0.234423 | 0.00 | 1573.678678 |
1 | a3dae335-faa3-46de-93c0-fbd7854e0da4 | Fromages | 60-64 | 32.12 | 1.0 | 32.120000 | 0.000000 | 1.000000 | 0.643859 | 9.65 | 212.580067 |
2 | 6a9598c3-26ba-4784-8551-9c20819c5690 | Fromages | 65-69 | 4.37 | 0.0 | NaN | NaN | -1.000000 | 0.646209 | 0.00 | 1032.467289 |
3 | 29dc945d-1c9e-46a7-a5d7-ba6d24182195 | Fromages | 65-69 | 28.11 | 2.0 | 23.690000 | 4.420000 | 1.000000 | 0.507266 | 0.00 | 3451.564789 |
4 | c63196a3-0226-4a7e-96b9-18d50953ec39 | Fromages | 20-24 | 6.18 | 0.0 | NaN | NaN | NaN | 0.628182 | 0.00 | 1770.345067 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
495 | 0716d17b-7117-4ef0-a9c5-f4a0595a08f6 | Fromages | 65-69 | 2.69 | 0.0 | NaN | NaN | NaN | 0.093593 | 0.00 | NaN |
496 | f9920ddc-de54-4588-ad2f-f65a1a386138 | Fromages | 20-24 | 13.68 | 3.0 | 5.886667 | 5.513138 | 1.413593 | 0.601433 | 0.00 | NaN |
497 | 4c66085a-389d-4cfd-9ab8-e0b90bcf6acf | Fromages | 80-84 | 13.47 | 0.0 | NaN | NaN | NaN | 0.423913 | 0.00 | NaN |
498 | 7e4e042b-6c14-4ba1-a74d-ecb70664fb2f | Fromages | 20-24 | 1.88 | 0.0 | NaN | NaN | NaN | 0.208342 | 0.00 | NaN |
499 | 903378a9-5bb5-46d4-8598-292fbb5f664f | Fromages | 20-24 | 2.59 | 0.0 | NaN | NaN | NaN | 0.305591 | 0.00 | NaN |
500 rows × 11 columns
# download parquet file
batch_features.download()
Downloading table |████████████████████████████████████████| 500/500 [100%] in 0
PosixPath('BATCH_FEATURE_TABLE_662b59e29385e38271fc9a8d.parquet')
# delete if not needed any more
batch_features.delete()
batch_request_table.delete()
Done! |████████████████████████████████████████| 100% in 6.1s (0.17%/s) Done! |████████████████████████████████████████| 100% in 6.1s (0.17%/s)
Manage Deployment¶
deployment = catalog.get_deployment("In-Store Customer x ProductGroup Spending 2w Spending - 9 features")
# get feature jobs status (this will produce meaningful results once multiple jobs have been run)
deployment.get_feature_jobs_status(job_history_window=12)
Loading Feature(s) |████████████████████████████████████████| 9/9 [100%] in 0.7s
Job statistics (last 12 hours)
request_date | job_history_window | job_duration_tolerance | |
---|---|---|---|
0 | 2024-04-26T07:38:47.995915 | 12 | 60 |
feature_name | aggregation_hash | |
---|---|---|
0 | CUSTOMER_Avg_of_invoice_Amount_14d | ccbdbb50 |
1 | CUSTOMER_Count_of_invoice_14d | decd9c4b |
2 | CUSTOMER_Latest_invoice_Amount | a8359d79 |
3 | CUSTOMER_Latest_invoice_Amount_Z_Score_to_invoice_Amount_28d | a8359d79 |
4 | CUSTOMER_Latest_invoice_Amount_Z_Score_to_invoice_Amount_28d | ccbdbb50 |
5 | CUSTOMER_Latest_invoice_Amount_Z_Score_to_invoice_Amount_28d | e432ef49 |
6 | CUSTOMER_Std_of_invoice_Amount_14d | e432ef49 |
7 | CUSTOMER_vs_OVERALL_item_TotalCost_across_product_ProductGroups_26w | 46496de4 |
8 | CUSTOMER_vs_OVERALL_item_TotalCost_across_product_ProductGroups_26w | 98e84952 |
9 | CUSTOMER_x_PRODUCTGROUP_Sum_of_item_TotalCost_14d | da3173df |
10 | CUSTOMER_x_PRODUCTGROUP_Time_Since_Latest_Timestamp | 80d7b6bd |
aggregation_hash | frequency(min) | completed_jobs | max_duration(s) | 95 percentile | frac_late | exceed_period | failed_jobs | incomplete_jobs | time_since_last | |
---|---|---|---|---|---|---|---|---|---|---|
0 | a8359d79 | 60 | 0 | NaN | NaN | NaN | 0 | 0 | 12 | NaT |
1 | decd9c4b | 60 | 0 | NaN | NaN | NaN | 0 | 0 | 12 | NaT |
2 | ccbdbb50 | 60 | 0 | NaN | NaN | NaN | 0 | 0 | 12 | NaT |
3 | e432ef49 | 60 | 0 | NaN | NaN | NaN | 0 | 0 | 12 | NaT |
4 | 46496de4 | 60 | 0 | NaN | NaN | NaN | 0 | 0 | 12 | NaT |
5 | 98e84952 | 60 | 0 | NaN | NaN | NaN | 0 | 0 | 12 | NaT |
6 | da3173df | 60 | 0 | NaN | NaN | NaN | 0 | 0 | 12 | NaT |
7 | 80d7b6bd | 60 | 0 | NaN | NaN | NaN | 0 | 0 | 12 | NaT |