Getting started

Getting started with Model Registry using examples

This guide shows how to get started with Model Registry and run a few examples using the command line or Python clients.

At this time, the Model Registry does not include a web-based User Interface (UI), therefore this documentation focuses on backend services and APIs.

For an overview of the logical model of model registry, check the Model Registry logical model. The logical model is exposed via the Model Registry REST API.

Prerequisites

To follow along the examples in this guide, you will need a Kubeflow installation and the Model Registry installed:

Setup

To use Model Registry on a notebook you should first install the Python client:

!pip install model-registry=="0.2.10"
!pip install kserve=="0.13"

Note that depending on your environment there might be conflicting dependency versions for packages that depend on pydantic.

You can get a client pointing to your deployed Model Registry from the previous steps:

from model_registry import ModelRegistry

registry = ModelRegistry(
    server_address="http://model-registry-service.kubeflow.svc.cluster.local",
    port=8080,
    author="your name",
    is_secure=False
)

For more information on client setup and capabilities, refer to the Model Registry Python client documentation.

Register metadata

You can use the register_model method to index a model’s artifacts and its metadata, for instance:

rm = registry.register_model(
    "iris",
    "gs://kfserving-examples/models/sklearn/1.0/model",
    model_format_name="sklearn",
    model_format_version="1",
    version="v1",
    description="Iris scikit-learn model",
    metadata={
        "accuracy": 3.14,
        "license": "BSD 3-Clause License",
    }
)

Retrieving metadata

Continuing on the previous example, you can use the following methods to retrieve the metadata associated with a given Model Artifact:

model = registry.get_registered_model("iris")
print("Registered Model:", model, "with ID", model.id)

version = registry.get_model_version("iris", "v1")
print("Model Version:", version, "with ID", version.id)

art = registry.get_model_artifact("iris", "v1")
print("Model Artifact:", art, "with ID", art.id)

These can be used to create a KServe inference endpoint.

Deploy an inference endpoint

Normally you would need to provide your deployment metadata manually resulting in an error-prone process, especially when such data has to be gathered from several sources. Using Model Registry ensures simplified access to accurate metadata, and enables you to automate deployment based on the Model Registry values, as also shown in the examples below.

Note: the provided examples uses the Model Registry Python client and KServe Python SDK. You can analogously make use of the Model Registry REST APIs, and your own Add-on SDK as needed.

Using Model Registry metadata

You can use the retrieved metadata from the previous step with the KServe Python SDK to create an inference endpoint, for example:

from kubernetes import client
import kserve

isvc = kserve.V1beta1InferenceService(
    api_version=kserve.constants.KSERVE_GROUP + "/v1beta1",
    kind=kserve.constants.KSERVE_KIND,
    metadata=client.V1ObjectMeta(
        name="iris-model",
        namespace=kserve.utils.get_default_target_namespace(),
        labels={
            "modelregistry/registered-model-id": model.id,
            "modelregistry/model-version-id": version.id,
        },
    ),
    spec=kserve.V1beta1InferenceServiceSpec(
        predictor=kserve.V1beta1PredictorSpec(
            model=kserve.V1beta1ModelSpec(
                storage_uri=art.uri,
                model_format=kserve.V1beta1ModelFormat(
                    name=art.model_format_name, version=art.model_format_version
                ),
            )
        )
    ),
)
ks_client = kserve.KServeClient()
ks_client.create(isvc)

An inference endpoint is now created, using the artifact metadata retrieved from the Model Registry (previous step), specifying the serving runtime to be used to serve the model, and references to the original entities in Model Registry.

Using Model Registry Custom Storage Initializer

The Model Registry Custom Storage Initializer (CSI) is a custom implementation of the KServe Storage Initializer that allows you to use Model Registry metadata to download and deploy models (see Installation instructions). You can create an InferenceService that references the model and version in the Model Registry:

from kubernetes import client
import kserve

isvc = kserve.V1beta1InferenceService(
    api_version=kserve.constants.KSERVE_GROUP + "/v1beta1",
    kind=kserve.constants.KSERVE_KIND,
    metadata=client.V1ObjectMeta(
        name="iris-model",
        namespace=kserve.utils.get_default_target_namespace(),
        labels={
            "modelregistry/registered-model-id": model.id,
            "modelregistry/model-version-id": version.id,
        },
    ),
    spec=kserve.V1beta1InferenceServiceSpec(
        predictor=kserve.V1beta1PredictorSpec(
            model=kserve.V1beta1ModelSpec(
                storage_uri="model-registry://iris/v1", # The protocol is model-registry://{modelName}/{modelVersion}
                model_format=kserve.V1beta1ModelFormat(
                    name=art.model_format_name, version=art.model_format_version
                ),
            )
        )
    ),
)
ks_client = kserve.KServeClient()
ks_client.create(isvc)

The InferenceService is now created, the CSI retrieves the latest artifact data associated with the model version from the Model Registry, and then downloads the model from its URI.

Next steps

Feedback

Was this page helpful?