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.
-
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>"
-
Run the
CreateModelVersion.py
file.python3 CreateModelVersion.py
-
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.
-
Register a new model version for the given registered model id by calling the
CreateModelVersion
endpoint of theRegisteredModelVersionService
. 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.pydef 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 ) )
-
In the main function, set up the token provider using an existing refresh token and client secret, and then set up the MLOps client.
-
Call the
create_model_version
with theREGISTERED_MODEL_ID
andEXPERIMENT_ID
constants defined in the beginning of the script.CreateModelVersion.pycreate_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)