Skip to main content
Version: v0.64.0

List model versions

This example demonstrates how you can use the MLOps Python client to list all the model versions for a given registered model in the external model registry using the ExternalRegisteredModelVersionService of the MLOps API.

Before you begin
ConstantValueDescription
MLOPS_API_URLUsually: https://api.mlops.my.domainDefines the URL for the MLOps Gateway component.
TOKEN_ENDPOINT_URL
https://mlops.keycloak.domain/auth/realms/[fill-in-realm-name]/protocol/openid-connect/token
Defines the token endpoint URL of the Identity Provider. This uses Keycloak as the Identity Provider. Keycloak Realm should be provided.
REFRESH_TOKEN<your-refresh-token>Defines the user's refresh token.
CLIENT_ID<your-client-id>Sets the client id for authentication. This is the client you will be using to connect to MLOps.
CLIENT_SECRET<your-client-secret>Sets the client secret.
REGISTERED_MODEL_NAME<model-name>Provide the registered model name in the external model registry for which you want to list the versions.

The following steps demonstrate how you can use the MLOps Python client to list all the model versions for a given registered model in the external model registry.

  1. Download the ListModelVersions.py file.

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

    ListModelVersions.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>"
    REGISTERED_MODEL_NAME = "<model-name>"
  3. Run the ListModelVersions.py file.

    python3 ListModelVersions.py
  4. This lists all the model versions for the registered model name you have specified.

    {
    "externalRegisteredModelVersions" : [ {
    "sourceKey" : "sourceKey",
    "updatedTime" : "2000-01-23T04:56:07.000+00:00",
    "statusDescription" : "statusDescription",
    "sourceCreatedBy" : "sourceCreatedBy",
    "adapterModelVersionId" : "adapterModelVersionId",
    "createdBy" : "createdBy",
    "artefactId" : "artefactId",
    "createdTime" : "2000-01-23T04:56:07.000+00:00",
    "mlflowStage" : "mlflowStage",
    "version" : "version"
    }, {
    "sourceKey" : "sourceKey",
    "updatedTime" : "2000-01-23T04:56:07.000+00:00",
    "statusDescription" : "statusDescription",
    "sourceCreatedBy" : "sourceCreatedBy",
    "adapterModelVersionId" : "adapterModelVersionId",
    "createdBy" : "createdBy",
    "artefactId" : "artefactId",
    "createdTime" : "2000-01-23T04:56:07.000+00:00",
    "mlflowStage" : "mlflowStage",
    "version" : "version"
    } ],
    "paging" : {
    "nextPageToken" : "nextPageToken"
    }
    }

Example walkthrough

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

  1. List the model versions for the given registered model name by calling the ListRegisteredModelVersionsForModel endpoint of the ExternalRegisteredModelVersionService.

    ListModelVersions.py
    def _form_list_model_version_request(model_name:str):
    return mlops.ModelregistryListRegisteredModelVersionsForModelRequest(
    source_key=model_name
    )

    def _list_all_versions_for_model(
    mlops_client: mlops.Client, model_name: str
    ):
    return mlops_client.external_registry.external_registered_model_version_service.list_registered_model_versions_for_model(
    _form_list_model_version_request(model_name)
    )
  2. In the main function, set up the token provider using an existing refresh token and client secret, and then set up the MLOps client.

  3. Call the _list_all_versions_for_model function with the REGISTERED_MODEL_NAME defined at the beginning of the script.

    ListModelVersions.py
    version_list = _list_all_versions_for_model(mlops_client, REGISTERED_MODEL_NAME)

    print(version_list)

Feedback