Skip to content

Fetching Online Features

This guide will show you how to retrieve a feature vector from the online store. This feature vector can then be used for inference in your production model.

We assume you've already created a Feature View and added it to a Feature Service, as shown in the prior guides.

Background

A model prediction service needs feature vectors delivered as input with low latency. Tecton makes pre-computed, or materialized, features available for low-latency retrieval.

You might also need to calculate features from raw data at request-time. For online features that can only be calculated at request time, use an OnDemandFeatureView.

Finally, you can use the Python SDK or the HTTP API to perform any of these calls. Examples for both are given below. Tecton recommends using the HTTP API to fetch feature values in production, as the Python SDK can add latency to online feature requests.

Fetching Online Features with the Python SDK

To fetch a feature vector, we need to provide a sample ID for the entities defined in our feature view.

  1. Fetch a Feature Service (FeatureService object) into your Python environment and use its get_feature_vector method to fetch a feature vector.
  2. To request a feature vector from the Tecton Feature Service, supply a list of join keys. The join keys identify the rows for which the service retrieves features. In the example below the join keys are ad_id and user_id.
import tecton

my_fs = tecton.get_feature_service('ad_ctr_feature_service')

keys = {
        "ad_id": "1000",
        "user_id": "b69f8dc3-6611-4d4a-bbce-032fd1d6eca9"
}

response = my_fs.get_feature_vector(keys)
print(response.to_dict())

get_feature_vector returns an object that can be cast to a Pandas DataFrame, NumPy array, or other programmatic data object. In the example above, it is cast to a Python dictionary. For more information about how to use get_feature_vector, see Get Feature Vector.

Getting Features from an OnDemandFeatureView

The Tecton SDK and HTTP API both call pre-computed data for prediction. Depending on the nature of your model, you might also need to transform raw data from the payload at the time of prediction.

An OnDemandFeatureView computes feature values at request-time. Instead of passing a list of keys that Tecton uses to retrieve feature values, you pass the data that Tecton uses to compute feature values.

import tecton

my_fs = tecton.get_feature_service('fraud_detection_feature_service')

keys = {
        "user_id": "C1000262126"
}

response = my_fs.get_feature_vector(join_keys=keys)
response.to_dict()

Fetching a feature vector with both materialized and on-demand features

To fetch online features from a Feature Service containing both OnDemand Feature Views and materialized Feature Views, provide both join_keys and request_data:

import tecton

my_fs = tecton.get_feature_service('fraud_detection_feature_service')

keys = {
        "user_id": "C1000262126"
}

request_data = {
  "amount" : 100,
  "type_CASH_IN": 1,
  "type_CASH_OUT": 0,
  "type_DEBIT": 0,
  "type_PAYMENT": 0,
  "type_TRANSFER": 0
}

response = my_fs.get_feature_vector(join_keys=keys, request_context_map=request_data)
response.to_dict()

Fetching Online Features with the HTTP API

The Tecton Python SDK demonstrated above is convenient, but it increases latency on feature vector requests. To maximize performance in production, fetch feature vectors instead using the Tecton HTTP API.

Authenticating HTTP requests requires a Tecton API key. In this example, you first generate an API key, then call the Tecton HTTP API.

Generating an API Key

Generate an API key from your CLI by running the following command:

tecton create-api-key --description "A sample key for the documentation".

Then, export the API key as an environment variable named TECTON_API_KEY or add the key to your secret manager.

export TECTON_API_KEY="my-key-code"

Making an HTTP API Call

In a prediction service application, make the HTTP API call from the service's HTTP client. The following example uses cURL as the HTTP client and can be executed from the command line, but the HTTP call is the same for any client.

$ curl -X POST https://<your_cluster>.tecton.ai/api/v1/feature-service/get-features\
     -H "Authorization: Tecton-key $TECTON_API_KEY" -d\
'{
  "params": {
    "feature_service_name": "fraud_detection_feature_service",
    "join_key_map": {
      "user_id": "$USER_ID_VALUE"
    },
    "request_context_map": {
      "amount": 100,
      "type_CASH_IN": "1",
      "type_CASH_OUT": "0",
      "type_DEBIT": "0",
      "type_PAYMENT": "0",
      "type_TRANSFER": "0"
    }
  }
}'