Skip to main content
Version: v0.67.0

List registered models

This example demonstrates how you can use the MLOps Python client to list the registered models based on the given search criteria using the RegisteredModelService of the MLOps API.

Before you begin

You will need the values for the following constants in order to successfully carry out the task. Contact your administrator to obtain deployment specific values.

ConstantDescription
MLOPS_API_URLDefines the URL for the MLOps gateway component. Usually: https://api.mlops.my.domain
TOKEN_ENDPOINT_URLDefines the token endpoint URL of the identity provider. This uses Keycloak as the identity provider. Keycloak realm should be provided.
REFRESH_TOKENDefines the user's refresh token.
CLIENT_IDSets the client id for authentication. This is the client you will be using to connect to MLOps.
CLIENT_SECRETSets the client secret for authentication.
PROJECT_IDSets the project id.
REGISTERED_MODEL_NAMESets the registered model name.

The following steps demonstrate how you can use the MLOps Python client to list the registered models based on the given search criteria in MLOps.

  1. Download the ListRegisteredModels.py file.

  2. Change the values of the following constants in your ListRegisteredModels.py file as given in the preceding data table.

    ListRegisteredModels.py
    ### Constants
    MLOPS_API_URL = "https://api.mlops.my.domain"
    TOKEN_ENDPOINT_URL="https://mlops.keycloak.domain/auth/realms/[fill-in-realm-name]/protocol/openid-connect/token"
    REFRESH_TOKEN="<your-refresh-token>"
    CLIENT_ID="<your-mlops-client>"
    CLIENT_SECRET = "<your-client-secret>"
    PROJECT_ID = "<your-project-id>"
    REGISTERED_MODEL_NAME = "<your-model-name>"
  3. Run the ListRegisteredModels.py file.

    python3 ListRegisteredModels.py
  4. This provides the list of registered models that satisfy the search criteria you have specified.

    {
    "paging" : {
    "nextPageToken" : "nextPageToken"
    },
    "registeredModels" : [ {
    "updatedTime" : "2000-01-23T04:56:07.000+00:00",
    "updatedBy" : "updatedBy",
    "createdBy" : "createdBy",
    "displayName" : "displayName",
    "description" : "description",
    "createdTime" : "2000-01-23T04:56:07.000+00:00",
    "id" : "id",
    "projectId" : "projectId"
    }, {
    "updatedTime" : "2000-01-23T04:56:07.000+00:00",
    "updatedBy" : "updatedBy",
    "createdBy" : "createdBy",
    "displayName" : "displayName",
    "description" : "description",
    "createdTime" : "2000-01-23T04:56:07.000+00:00",
    "id" : "id",
    "projectId" : "projectId"
    } ]
    }

Example walkthrough

This section provides a walkthrough of the ListRegisteredModels.py file.

  1. Add the following function to filter the models by a given parameter.

    ListRegisteredModels.py
    def _get_filter_by(type, value):
    return mlops.StorageFilterRequest(
    query=mlops.StorageQuery(
    clause=[
    mlops.StorageClause(
    property_constraint=[
    mlops.StoragePropertyConstraint(
    _property=mlops.StorageProperty(
    field=type
    ),
    operator=mlops.StorageOperator.EQUAL_TO,
    value=mlops.StorageValue(
    string_value=value
    ),
    )
    ]
    )
    ]
    )
    )
  2. Add the following function to sort the models.

    ListRegisteredModels.py
    def _get_sort_by(field, order=mlops.ModelregistryOrder.DESCENDING):
    return mlops.StorageSortingRequest(
    _property=[
    mlops.StorageSortProperty(
    _property=mlops.StorageProperty(
    field=field
    ),
    order=order,
    )
    ]
    )
  3. List all the registered models for the given project by calling the ListRegisteredModels endpoint of the RegisteredModelService.

    ListRegisteredModels.py
    def list_registered_models(
    mlops_client: mlops.Client,
    list_model_request: mlops.StorageListRegisteredModelsRequest
    ):
    return mlops_client.storage.registered_model.list_registered_models(list_model_request)
  4. Filter the models by the REGISTERED_MODEL_NAME and sort them by the last_modified_time.

    ListRegisteredModels.py
    def get_registered_model_list_request(project_id):
    return mlops.StorageListRegisteredModelsRequest(
    project_id=project_id,
    paging=mlops.StoragePagingRequest(),
    filter=_get_filter_by("name", REGISTERED_MODEL_NAME),
    sorting=_get_sort_by("last_modified_time")
    )
    Note

    Models can be filtered and sorted by several parameters such as the registered model name, created_time, last_modified_time, updated_by, owner_id and description. This example uses the name to filter and last_modified_time to sort.

  5. In the main function, set up the token provider using an existing refresh token and client secret, and then set up the MLOps client.

  6. Call the list_registered_models function with the PROJECT_ID constant defined in the beginning of the script.

    ListRegisteredModels.py
    list_model_response: mlops.StorageListRegisteredModelsResponse = list_registered_models(
    mlops_client,
    get_registered_model_list_request(
    project_id=PROJECT_ID
    )
    )
    print(list_model_response)

Feedback