Skip to main content
Version: v0.64.0

Count registered models

This example demonstrates how you can use the MLOps Python client to count the number of registered models for a given project id 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.

The following steps demonstrate how you can use the MLOps Python client to to count the number of registered models for a given project in MLOps.

  1. Download the CountRegisteredModels.py file.

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

    CountRegisteredModels.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>"
  3. Run the CountRegisteredModels.py file.

    python3 CountRegisteredModels.py
  4. This provides the number of registered models in the project you have specified.

    {
    "registeredModelCount" : 3
    }

Example walkthrough

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

  1. Count the number of registered models for a given project id by calling the CreateModelVersion endpoint of the RegisteredModelVersionService. The request for this API call is prepared by the get_count_registered_model_request function, which specifies the project id.

    CountRegisteredModels.py
    def count_registered_models(
    mlops_client: mlops.Client,
    count_model_request: mlops.StorageCountRegisteredModelRequest
    ):
    return mlops_client.storage.registered_model.count_registered_models(
    count_model_request
    )

    def get_count_registered_model_request(project_id):
    return mlops.StorageCountRegisteredModelRequest(
    project_id=project_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.

  1. Call the count_registered_models function with the PROJECT_ID constant defined in the beginning of the script.

    CountRegisteredModels.py
    count_model_response: mlops.StorageCountRegisteredModelResponse = count_registered_models(
    mlops_client,
    get_count_registered_model_request(
    project_id=PROJECT_ID
    )
    )
    print(count_model_response)

Feedback