List model versions for model
This example demonstrates how you can use the MLOps Python client to get the model versions for the given model id using the RegisteredModelVersionService
of the MLOps API.
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 versions for the given model id in MLOps.
Change the values of the following constants in your
ListModelVersionsForModel.py
file as given in the preceding data table.ListModelVersionsForModel.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>"
PROJECT_ID = "<your-project-id>"
REGISTERED_MODEL_ID = "<your-registered-model-id>"Run the
ListModelVersionsForModel.py
file.python3 ListModelVersionsForModel.py
This provides the list of model versions that satisfy the search criteria you have specified.
{
"modelVersions" : [ {
"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
}, {
"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
} ],
"paging" : {
"nextPageToken" : "nextPageToken"
}
}
Example walkthrough
This section provides a walkthrough of the ListModelVersionsForModel.py
file.
Add the following function to filter the model versions.
ListModelVersionsForModel.pydef _get_filter_by(type, value):
return mlops.StorageFilterRequest(
query=mlops.StorageQuery(
clause=[
mlops.StorageClause(
property_constraint=[
mlops.StoragePropertyConstraint(
_property=mlops.StorageProperty(
field=type
),
operator=mlops.StorageOperator.EQUAL_TO,
value=mlops.StorageValue(
string_value=value
),
)
]
)
]
)
)Add the following function to sort the model versions.
ListModelVersionsForModel.pydef _get_sort_by(field, order=mlops.ModelregistryOrder.DESCENDING):
return mlops.StorageSortingRequest(
_property=[
mlops.StorageSortProperty(
_property=mlops.StorageProperty(
field=field
),
order=order,
)
]
)List all the model versions for the given registered model id by calling the
ListModelVersionsForModel
endpoint of theRegisteredModelVersionService
.ListModelVersionsForModel.pydef list_model_versions_for_model(
mlops_client: mlops.Client,
list_model_versions_request: mlops.StorageListModelVersionsForModelRequest
):
return mlops_client.storage.registered_model_version.list_model_versions_for_model(list_model_versions_request)Filter the models by the
REGISTERED_MODEL_ID
and sort them by thelast_modified_time
.ListModelVersionsForModel.pydef list_model_versions_for_model_request(registered_model_id):
return mlops.StorageListModelVersionsForModelRequest(
registered_model_id=registered_model_id,
paging=mlops.StoragePagingRequest(),
filter=_get_filter_by("registered_model_id", REGISTERED_MODEL_ID),
sorting=_get_sort_by("last_modified_time")
)NoteModels can be filtered and sorted by several parameters such as the registered model name, created_time, last_modified_time, updated_by, owner_id and description. This example uses the
registered_model_id
to filter andlast_modified_time
to sort.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
list_model_versions_for_model
function with theREGISTERED_MODEL_ID
constant defined in the beginning of the script.ListModelVersionsForModel.pylist_model_versions_response: mlops.StorageListModelVersionsForModelResponse = list_model_versions_for_model(
mlops_client=mlops_client,
list_model_versions_request=list_model_versions_for_model_request(
registered_model_id=REGISTERED_MODEL_ID,
)
)
print(list_model_versions_response)
- Submit and view feedback for this page
- Send feedback about H2O MLOps to cloud-feedback@h2o.ai