Skip to content

Count model versions

This example demonstrates how you can use the MLOps Python client to get the model version count 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.
Constant
Description
MLOPS_API_URL Defines the URL for the MLOps gateway component. Usually: https://api.mlops.my.domain
TOKEN_ENDPOINT_URL Defines the token endpoint URL of the identity provider. This uses Keycloak as the identity provider. Keycloak realm should be provided.
REFRESH_TOKEN Defines the user's refresh token.
CLIENT_ID Sets the client id for authentication. This is the client you will be using to connect to MLOps.
CLIENT_SECRET Sets the client secret for authentication.
REGISTERED_MODEL_ID Sets the registered model id.

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

  1. Download the CountModelVersions.py file.

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

    RegisteredModelService.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_ID = "<your-registered-model-id>"
    
  3. Run the CountModelVersions.py file.

    python3 CountModelVersions.py
    
  4. This provides the number of models versions in the registered model you have specified.

    {
        "count" : 2
    }
    

Example walkthrough

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

  1. Count the number of model versions for a given model id by calling the CountModelVersions endpoint of the RegisteredModelVersionService. The request for this API call is prepared by the count_model_version_request function, which specifies the registered model id.

    CountModelVersions.py
    def count_model_version(
        mlops_client: mlops.Client,
        count_model_versions_request: mlops.StorageCountModelVersionsRequest
    ):
        return mlops_client.storage.registered_model_version.count_model_versions(
            count_model_versions_request
        )
    
    
    def count_model_version_request(registered_model_id):
        return mlops.StorageCountModelVersionsRequest(
            registered_model_id=registered_model_id
        )
    
  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 count_model_version function with the REGISTERED_MODEL_ID constant defined in the beginning of the script.

    CountModelVersions.py
    count_model_version_response: mlops.StorageCountModelVersionsRequest = count_model_version(
        mlops_client=mlops_client, 
        count_model_versions_request=count_model_version_request(
            registered_model_id=REGISTERED_MODEL_ID
        )
    )
    print(count_model_version_response)