List registered models
This example demonstrates how you can use the MLOps Python client to list the registered models based on the given search criteria using the RegisteredModelService
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. |
PROJECT_ID | Sets the project id. |
REGISTERED_MODEL_NAME | Sets the registered model name. |
The following steps demonstrate how you can use the MLOps Python client to list the registered models based on the given search criteria in MLOps.
Change the values of the following constants in your
ListRegisteredModels.py
file as given in the preceding data table.ListRegisteredModels.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>"
REGISTERED_MODEL_NAME = "<your-model-name>"Run the
ListRegisteredModels.py
file.python3 ListRegisteredModels.py
This provides the list of registered models that satisfy the search criteria you have specified.
{
"paging" : {
"nextPageToken" : "nextPageToken"
},
"registeredModels" : [ {
"updatedTime" : "2000-01-23T04:56:07.000+00:00",
"updatedBy" : "updatedBy",
"createdBy" : "createdBy",
"displayName" : "displayName",
"description" : "description",
"createdTime" : "2000-01-23T04:56:07.000+00:00",
"id" : "id",
"projectId" : "projectId"
}, {
"updatedTime" : "2000-01-23T04:56:07.000+00:00",
"updatedBy" : "updatedBy",
"createdBy" : "createdBy",
"displayName" : "displayName",
"description" : "description",
"createdTime" : "2000-01-23T04:56:07.000+00:00",
"id" : "id",
"projectId" : "projectId"
} ]
}
Example walkthrough
This section provides a walkthrough of the ListRegisteredModels.py
file.
Add the following function to filter the models by a given parameter.
ListRegisteredModels.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 models.
ListRegisteredModels.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 registered models for the given project by calling the
ListRegisteredModels
endpoint of theRegisteredModelService
.ListRegisteredModels.pydef list_registered_models(
mlops_client: mlops.Client,
list_model_request: mlops.StorageListRegisteredModelsRequest
):
return mlops_client.storage.registered_model.list_registered_models(list_model_request)Filter the models by the
REGISTERED_MODEL_NAME
and sort them by thelast_modified_time
.ListRegisteredModels.pydef get_registered_model_list_request(project_id):
return mlops.StorageListRegisteredModelsRequest(
project_id=project_id,
paging=mlops.StoragePagingRequest(),
filter=_get_filter_by("name", REGISTERED_MODEL_NAME),
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
name
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_registered_models
function with thePROJECT_ID
constant defined in the beginning of the script.ListRegisteredModels.pylist_model_response: mlops.StorageListRegisteredModelsResponse = list_registered_models(
mlops_client,
get_registered_model_list_request(
project_id=PROJECT_ID
)
)
print(list_model_response)
- Submit and view feedback for this page
- Send feedback about H2O MLOps to cloud-feedback@h2o.ai