Skip to main content
Version: v0.61.1

List model versions for model

This example demonstrates how you can use the MLOps Python client to get the model versions for the given model id using the RegisteredModelVersionService 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.
REGISTERED_MODEL_IDSets the registered model id.

The following steps demonstrate how you can use the MLOps Python client to get the model versions for the given model id in MLOps.

  1. Download the ListModelVersionsForModel.py file.

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

    ListModelVersionsForModel.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>"
    EXPERIMENT_ID = "<your-experiment-id>"
    PROJECT_ID = "<your-project-id>"
    REGISTERED_MODEL_ID = "<your-registered-model-id>"
  3. Run the ListModelVersionsForModel.py file.

    python3 ListModelVersionsForModel.py
  4. This provides the list of model versions that satisfy the search criteria you have specified.

    {
    "modelVersions" : [ {
    "updatedTime" : "2000-01-23T04:56:07.000+00:00",
    "updatedBy" : "updatedBy",
    "createdBy" : "createdBy",
    "registeredModelId" : "registeredModelId",
    "createdTime" : "2000-01-23T04:56:07.000+00:00",
    "experimentId" : "experimentId",
    "id" : "id",
    "version" : 0
    }, {
    "updatedTime" : "2000-01-23T04:56:07.000+00:00",
    "updatedBy" : "updatedBy",
    "createdBy" : "createdBy",
    "registeredModelId" : "registeredModelId",
    "createdTime" : "2000-01-23T04:56:07.000+00:00",
    "experimentId" : "experimentId",
    "id" : "id",
    "version" : 0
    } ],
    "paging" : {
    "nextPageToken" : "nextPageToken"
    }
    }

Example walkthrough

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

  1. Add the following function to filter the model versions.

    ListModelVersionsForModel.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 model versions.

    ListModelVersionsForModel.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 model versions for the given registered model id by calling the ListModelVersionsForModel endpoint of the RegisteredModelVersionService.

    ListModelVersionsForModel.py
    def list_model_versions_for_model(
    mlops_client: mlops.Client,
    list_model_versions_request: mlops.StorageListModelVersionsForModelRequest
    ):
    return mlops_client.storage.registered_model_version.list_model_versions_for_model(list_model_versions_request)
  4. Filter the models by the REGISTERED_MODEL_ID and sort them by the last_modified_time.

    ListModelVersionsForModel.py
    def list_model_versions_for_model_request(registered_model_id):
    return mlops.StorageListModelVersionsForModelRequest(
    registered_model_id=registered_model_id,
    paging=mlops.StoragePagingRequest(),
    filter=_get_filter_by("registered_model_id", REGISTERED_MODEL_ID),
    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 registered_model_id 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.

  1. Call the list_model_versions_for_model function with the REGISTERED_MODEL_ID constant defined in the beginning of the script.

    ListModelVersionsForModel.py
    list_model_versions_response: mlops.StorageListModelVersionsForModelResponse = list_model_versions_for_model(
    mlops_client=mlops_client,
    list_model_versions_request=list_model_versions_for_model_request(
    registered_model_id=REGISTERED_MODEL_ID,
    )
    )
    print(list_model_versions_response)

Feedback