Skip to content

Create model version

This example demonstrates how you can use the MLOps Python client to register an experiment as a new version of a given registered model 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.
EXPERIMENT_ID Sets the id of the experiment which will be registered as a new version of the specified registered model.
REGISTERED_MODEL_ID Sets the registered model id.
REGISTERED_MODEL_NAME Sets the registered model name.

Note

Only one of the REGISTERED_MODEL_ID and REGISTERED_MODEL_NAME should be provided when creating a model version. If the REGISTERED_MODEL_ID is provided, a new version of that model will be created. If REGISTERED_MODEL_NAME is provided, a new model and version will be created with the given name. This example uses REGISTERED_MODEL_ID to create a new version of an existing model.

The following steps demonstrate how you can use the MLOps Python client to create a registered model version in MLOps.

  1. Download the CreateModelVersion.py file.

  2. Change the values of the following constants in your CreateModelVersion.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>"
    EXPERIMENT_ID = "<your-experiment-id>"
    REGISTERED_MODEL_ID = "<your-registered-model-id>"
    
  3. Run the CreateModelVersion.py file.

    python3 CreateModelVersion.py
    
  4. This registers the given experiment as new version of the registered model you have specified.

    {
        "registeredModelVersion" : {
            "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
        }
    }
    

Example walkthrough

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

  1. Register a new model version for the given registered model id by calling the CreateModelVersion endpoint of the RegisteredModelVersionService. The request for this API call is prepared by thecreate_model_version_request function, which specifies the registered model id and experiment id.

    Note

    If the model name and description are provided instead of the registered model id, a new registered model will be created with that name and description.

    CreateModelVersion.py
    def create_model_version(
        mlops_client: mlops.Client, 
        model_version_request: mlops.StorageCreateModelVersionRequest
    ):
        return mlops_client.storage.registered_model_version.create_model_version(
            model_version_request
        )
    
    
    def create_model_version_request(registered_model_id, experiment_id):
        return mlops.StorageCreateModelVersionRequest(
            registered_model_version=mlops.StorageRegisteredModelVersion(
                registered_model_id=registered_model_id, 
                experiment_id=experiment_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 create_model_version with the REGISTERED_MODEL_ID and EXPERIMENT_ID constants defined in the beginning of the script.

    CreateModelVersion.py
    create_model_version_response: mlops.StorageCreateModelVersionResponse = create_model_version(
        mlops_client=mlops_client,
        model_version_request=create_model_version_request(
            registered_model_id=REGISTERED_MODEL_ID,
            experiment_id=EXPERIMENT_ID
        )
    )
    print(create_model_version_response)