Skip to content

Serving

src.featureform.serving.ServingClient

The serving client is used to retrieve training sets and features for training and serving purposes.

Using the Serving Client:

import featureform as ff
from featureform import ServingClient

client = ServingClient(host="localhost:8000")

# example:
dataset = client.training_set("fraud_training", "quickstart")
training_dataset = dataset.repeat(10).shuffle(1000).batch(8)
for feature_batch in training_dataset:
    # Train model

Source code in src/featureform/serving.py
class ServingClient:
    """
    The serving client is used to retrieve training sets and features for training and serving purposes.


    **Using the Serving Client:**
    ``` py
    import featureform as ff
    from featureform import ServingClient

    client = ServingClient(host="localhost:8000")

    # example:
    dataset = client.training_set("fraud_training", "quickstart")
    training_dataset = dataset.repeat(10).shuffle(1000).batch(8)
    for feature_batch in training_dataset:
        # Train model
    ```
    """

    def __init__(self, host=None, local=False, insecure=False, cert_path=None):
        """
        Args:
            host (str): The hostname of the Featureform instance. Exclude if using Localmode.
            local (bool): True if using Localmode.
            insecure (bool): True if connecting to an insecure Featureform endpoint. False if using a self-signed or public TLS certificate
            cert_path (str): The path to a public certificate if using a self-signed certificate.
        """
        if local and host:
            raise ValueError("Host and local cannot both be set")
        if local:
            self.impl = LocalClientImpl()
        else:
            self.impl = HostedClientImpl(host, insecure, cert_path)

    def training_set(self, name, variant="default", include_label_timestamp=False, model: Union[str, Model] = None):
        """Return an iterator that iterates through the specified training set.

        **Examples**:
        ``` py
            client = ff.ServingClient()
            dataset = client.training_set("fraud_training", "quickstart")
            training_dataset = dataset.repeat(10).shuffle(1000).batch(8)
            for feature_batch in training_dataset:
                # Train model
        ```
        Args:
            name (str): Name of training set to be retrieved
            variant (str): Variant of training set to be retrieved

        Returns:
            training set (Dataset): A training set iterator
        """
        return self.impl.training_set(name, variant, include_label_timestamp, model)

    def features(self, features, entities, model: Union[str, Model] = None):
        """Returns the feature values for the specified entities.

        **Examples**:
        ``` py
            client = ff.ServingClient(local=True)
            fpf = client.features([("avg_transactions", "quickstart")], {"user": "C1410926"})
            # Run features through model
        ```
        Args:
            features (list[(str, str)], list[str]): List of Name Variant Tuples
            entities (dict): Dictionary of entity name/value pairs

        Returns:
            features (numpy.Array): An Numpy array of feature values in the order given by the inputs
        """
        features = check_feature_type(features)
        return self.impl.features(features, entities, model)

__init__(host=None, local=False, insecure=False, cert_path=None)

Parameters:

Name Type Description Default
host str

The hostname of the Featureform instance. Exclude if using Localmode.

None
local bool

True if using Localmode.

False
insecure bool

True if connecting to an insecure Featureform endpoint. False if using a self-signed or public TLS certificate

False
cert_path str

The path to a public certificate if using a self-signed certificate.

None
Source code in src/featureform/serving.py
def __init__(self, host=None, local=False, insecure=False, cert_path=None):
    """
    Args:
        host (str): The hostname of the Featureform instance. Exclude if using Localmode.
        local (bool): True if using Localmode.
        insecure (bool): True if connecting to an insecure Featureform endpoint. False if using a self-signed or public TLS certificate
        cert_path (str): The path to a public certificate if using a self-signed certificate.
    """
    if local and host:
        raise ValueError("Host and local cannot both be set")
    if local:
        self.impl = LocalClientImpl()
    else:
        self.impl = HostedClientImpl(host, insecure, cert_path)

features(features, entities, model=None)

Returns the feature values for the specified entities.

Examples:

    client = ff.ServingClient(local=True)
    fpf = client.features([("avg_transactions", "quickstart")], {"user": "C1410926"})
    # Run features through model

Parameters:

Name Type Description Default
features list[str, str], list[str]

List of Name Variant Tuples

required
entities dict

Dictionary of entity name/value pairs

required

Returns:

Name Type Description
features numpy.Array

An Numpy array of feature values in the order given by the inputs

Source code in src/featureform/serving.py
def features(self, features, entities, model: Union[str, Model] = None):
    """Returns the feature values for the specified entities.

    **Examples**:
    ``` py
        client = ff.ServingClient(local=True)
        fpf = client.features([("avg_transactions", "quickstart")], {"user": "C1410926"})
        # Run features through model
    ```
    Args:
        features (list[(str, str)], list[str]): List of Name Variant Tuples
        entities (dict): Dictionary of entity name/value pairs

    Returns:
        features (numpy.Array): An Numpy array of feature values in the order given by the inputs
    """
    features = check_feature_type(features)
    return self.impl.features(features, entities, model)

training_set(name, variant='default', include_label_timestamp=False, model=None)

Return an iterator that iterates through the specified training set.

Examples:

    client = ff.ServingClient()
    dataset = client.training_set("fraud_training", "quickstart")
    training_dataset = dataset.repeat(10).shuffle(1000).batch(8)
    for feature_batch in training_dataset:
        # Train model

Parameters:

Name Type Description Default
name str

Name of training set to be retrieved

required
variant str

Variant of training set to be retrieved

'default'

Returns:

Type Description

training set (Dataset): A training set iterator

Source code in src/featureform/serving.py
def training_set(self, name, variant="default", include_label_timestamp=False, model: Union[str, Model] = None):
    """Return an iterator that iterates through the specified training set.

    **Examples**:
    ``` py
        client = ff.ServingClient()
        dataset = client.training_set("fraud_training", "quickstart")
        training_dataset = dataset.repeat(10).shuffle(1000).batch(8)
        for feature_batch in training_dataset:
            # Train model
    ```
    Args:
        name (str): Name of training set to be retrieved
        variant (str): Variant of training set to be retrieved

    Returns:
        training set (Dataset): A training set iterator
    """
    return self.impl.training_set(name, variant, include_label_timestamp, model)

src.featureform.serving.Dataset

Source code in src/featureform/serving.py
class Dataset:

    def __init__(self, stream, dataframe=None):
        """Repeats the Dataset for the specified number of times

        Args:
            stream (Iterator): An iterable object.

        Returns:
            self (Dataset): Returns a Dataset created from the iterable object.
        """
        self._stream = stream
        self._dataframe = dataframe

    def from_stub(self, name, version, model: Union[str, Model] = None):
        stream = Stream(self._stream, name, version, model)
        return Dataset(stream)

    def from_dataframe(dataframe, include_label_timestamp):
        stream = LocalStream(dataframe.values.tolist(), include_label_timestamp)
        return Dataset(stream, dataframe)

    def pandas(self):
        return self._dataframe

    def repeat(self, num):
        """Repeats the Dataset for the specified number of times

        **Examples**:
        ``` py
            client = ff.ServingClient()
            dataset = client.training_set("fraud_training", "quickstart")
            training_dataset = dataset.repeat(10) # Repeats data 10 times
            for feature_batch in training_dataset:
                # Train model
        ```
        Args:
            num (int): The number of times the dataset will be repeated

        Returns:
            self (Dataset): Returns the current Dataset
        """
        if num <= 0:
            raise Exception("Must repeat 1 or more times")
        self._stream = Repeat(num, self._stream)
        if self._dataframe is not None:
            temp_df = self._dataframe
            for i in range(num):
                self._dataframe = self._dataframe.append(temp_df)
        return self

    def shuffle(self, buffer_size):
        """Swaps random rows within the Dataset.

        **Examples**:
        ``` py
            client = ff.ServingClient()
            dataset = client.training_set("fraud_training", "quickstart")
            training_dataset = dataset.shuffle(100) # Swaps 100 Rows
            for feature_batch in training_dataset:
                # Train model
        ```
        Args:
            buffer_size (int): The number of Dataset rows to be randomly swapped

        Returns:
            self (Dataset): Returns the current Dataset
        """
        if buffer_size <= 0:
            raise Exception("Buffer size must be greater than or equal to 1")
        self._stream = Shuffle(buffer_size, self._stream)
        if self._dataframe is not None:
            self._dataframe = self._dataframe.sample(frac=1) 
        return self

    def batch(self, batch_size):
        """Creates a batch row in the Dataset.

        **Examples**:
        ``` py
            client = ff.ServingClient()
            dataset = client.training_set("fraud_training", "quickstart")
            training_dataset = dataset.batch(8) # Creates a batch of 8 Datasets for each row
            for feature_batch in training_dataset:
                # Train model
        ```
        Args:
            batch_size (int): The number of items to be added to each batch

        Returns:
            self (Dataset): Returns the current Dataset
        """
        if batch_size <= 0:
            raise Exception("Batch size must be greater than or equal to 1")
        self._stream = Batch(batch_size, self._stream)
        if self._dataframe is not None:
            self._dataframe = np.array_split(self._dataframe, math.ceil(len(self._dataframe) // batch_size))
        return self

    def __iter__(self):
        return self

    def __next__(self):
        next_val = next(self._stream)
        return next_val

__init__(stream, dataframe=None)

Repeats the Dataset for the specified number of times

Parameters:

Name Type Description Default
stream Iterator

An iterable object.

required

Returns:

Name Type Description
self Dataset

Returns a Dataset created from the iterable object.

Source code in src/featureform/serving.py
def __init__(self, stream, dataframe=None):
    """Repeats the Dataset for the specified number of times

    Args:
        stream (Iterator): An iterable object.

    Returns:
        self (Dataset): Returns a Dataset created from the iterable object.
    """
    self._stream = stream
    self._dataframe = dataframe

batch(batch_size)

Creates a batch row in the Dataset.

Examples:

    client = ff.ServingClient()
    dataset = client.training_set("fraud_training", "quickstart")
    training_dataset = dataset.batch(8) # Creates a batch of 8 Datasets for each row
    for feature_batch in training_dataset:
        # Train model

Parameters:

Name Type Description Default
batch_size int

The number of items to be added to each batch

required

Returns:

Name Type Description
self Dataset

Returns the current Dataset

Source code in src/featureform/serving.py
def batch(self, batch_size):
    """Creates a batch row in the Dataset.

    **Examples**:
    ``` py
        client = ff.ServingClient()
        dataset = client.training_set("fraud_training", "quickstart")
        training_dataset = dataset.batch(8) # Creates a batch of 8 Datasets for each row
        for feature_batch in training_dataset:
            # Train model
    ```
    Args:
        batch_size (int): The number of items to be added to each batch

    Returns:
        self (Dataset): Returns the current Dataset
    """
    if batch_size <= 0:
        raise Exception("Batch size must be greater than or equal to 1")
    self._stream = Batch(batch_size, self._stream)
    if self._dataframe is not None:
        self._dataframe = np.array_split(self._dataframe, math.ceil(len(self._dataframe) // batch_size))
    return self

repeat(num)

Repeats the Dataset for the specified number of times

Examples:

    client = ff.ServingClient()
    dataset = client.training_set("fraud_training", "quickstart")
    training_dataset = dataset.repeat(10) # Repeats data 10 times
    for feature_batch in training_dataset:
        # Train model

Parameters:

Name Type Description Default
num int

The number of times the dataset will be repeated

required

Returns:

Name Type Description
self Dataset

Returns the current Dataset

Source code in src/featureform/serving.py
def repeat(self, num):
    """Repeats the Dataset for the specified number of times

    **Examples**:
    ``` py
        client = ff.ServingClient()
        dataset = client.training_set("fraud_training", "quickstart")
        training_dataset = dataset.repeat(10) # Repeats data 10 times
        for feature_batch in training_dataset:
            # Train model
    ```
    Args:
        num (int): The number of times the dataset will be repeated

    Returns:
        self (Dataset): Returns the current Dataset
    """
    if num <= 0:
        raise Exception("Must repeat 1 or more times")
    self._stream = Repeat(num, self._stream)
    if self._dataframe is not None:
        temp_df = self._dataframe
        for i in range(num):
            self._dataframe = self._dataframe.append(temp_df)
    return self

shuffle(buffer_size)

Swaps random rows within the Dataset.

Examples:

    client = ff.ServingClient()
    dataset = client.training_set("fraud_training", "quickstart")
    training_dataset = dataset.shuffle(100) # Swaps 100 Rows
    for feature_batch in training_dataset:
        # Train model

Parameters:

Name Type Description Default
buffer_size int

The number of Dataset rows to be randomly swapped

required

Returns:

Name Type Description
self Dataset

Returns the current Dataset

Source code in src/featureform/serving.py
def shuffle(self, buffer_size):
    """Swaps random rows within the Dataset.

    **Examples**:
    ``` py
        client = ff.ServingClient()
        dataset = client.training_set("fraud_training", "quickstart")
        training_dataset = dataset.shuffle(100) # Swaps 100 Rows
        for feature_batch in training_dataset:
            # Train model
    ```
    Args:
        buffer_size (int): The number of Dataset rows to be randomly swapped

    Returns:
        self (Dataset): Returns the current Dataset
    """
    if buffer_size <= 0:
        raise Exception("Buffer size must be greater than or equal to 1")
    self._stream = Shuffle(buffer_size, self._stream)
    if self._dataframe is not None:
        self._dataframe = self._dataframe.sample(frac=1) 
    return self