Search external registry
This example demonstrates how you can use the MLOps Python client to search registered models of the external model registry using the ExternalRegisteredModelService
of the MLOps API.
- Ensure that the external model registry is configured.
- 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 | Value | Description |
---|---|---|
MLOPS_API_URL | Usually: https://api.mlops.my.domain | Defines the URL for the MLOps Gateway component. |
TOKEN_ENDPOINT_URL | https://mlops.keycloak.domain/auth/realms/[fill-in-realm-name]/protocol/openid-connect/token | Defines the token endpoint URL of the Identity Provider. This uses Keycloak as the Identity Provider. Keycloak Realm should be provided. |
REFRESH_TOKEN | <your-refresh-token> | Defines the user's refresh token. |
CLIENT_ID | <your-client-id> | Sets the client id for authentication. This is the client you will be using to connect to MLOps. |
CLIENT_SECRET | <your-client-secret> | Sets the client secret. |
SEARCH_BY | tag or name | The API supports search by tag or name. To search models by tag, set the SEARCH_BY field as tag . To search by name, set it as name . Currently, only one clause at a time, either name or tag, is supported. |
SEARCH_VALUE | <key>=<value> or <name> | If the SEARCH_BY field is tag , set the <key>=<value> pair of that tag here. This fetches any model that has the given <key>=<value> combination. If the SEARCH_BY field is name , set the name of the search field here. This performs a LIKE %<FILTER_VALUE>% operation. |
Using search criteria is optional. If the search criteria are not defined, the search will fetch all models in the external model registry, optimized by the default paging and sorting criteria.
The following steps demonstrate how you can use the MLOps Python client to search registered models of the external model registry.
Change the values of the following constants in your
SearchExternalRegistry.py
file as given in the preceding data table.SearchExternalRegistry.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>"
SEARCH_BY = "tag" or "name"
SEARCH_VALUE = "<key>=<value>" or "<name>"
Run the
SearchExternalRegistry.py
file.python3 SearchExternalRegistry.py
This provides all the registered models in the external model registry that satisfy the search criteria you've specified. If no search criteria are specified, the search will fetch all models in the external model registry.
{
"externalRegisteredModels" : [ {
"sourceKey" : "sourceKey",
"updatedTime" : "2000-01-23T04:56:07.000+00:00",
"sourceCreatedBy" : "sourceCreatedBy",
"createdBy" : "createdBy",
"name" : "name",
"description" : "description",
"project" : {
"displayName" : "displayName",
"id" : "id",
"ownerId" : "ownerId"
},
"createdTime" : "2000-01-23T04:56:07.000+00:00",
"id" : "id",
"tags" : [ {
"value" : "value",
"key" : "key"
}]
}
"paging" : {
"nextPageToken" : "nextPageToken"
}
}
Example walkthrough
This section provides a walkthrough of the SearchExternalRegistry.py
file.
Add the following function to filter the models.
SearchExternalRegistry.pydef _get_filter_by(type, value):
return mlops.ModelregistryFilterRequest(
query=mlops.ModelregistryQuery(
clause=[
mlops.ModelregistryClause(
property_constraint=[
mlops.ModelregistryPropertyConstraint(
_property=mlops.ModelregistryProperty(
field=type
),
operator=mlops.ModelregistryOperator.EQUAL_TO,
value=mlops.ModelregistryValue(
string_value=value
),
)
]
)
]
)
)Add the following function to sort the models.
SearchExternalRegistry.pydef _get_sort_by(field, order=mlops.ModelregistryOrder.DESCENDING):
return mlops.ModelregistrySortingRequest(
_property=[
mlops.ModelregistrySortProperty(
_property=mlops.ModelregistryProperty(
field=field
),
order=order,
)
]
)Search the models in the external model registry by calling the
SearchRegisteredModel
endpoint of theExternalRegisteredModelService
.SearchExternalRegistry.pydef search_external_registry(
mlops_client: mlops.Client, search_request: mlops.ModelregistrySearchRegisteredModelRequest
):
return mlops_client.external_registry.external_registered_model_service.search_registered_model(search_request)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
search_external_registry
function with the paging, filtering and sorting criteria.SearchExternalRegistry.pyresponse = search_external_registry(mlops_client, mlops.ModelregistrySearchRegisteredModelRequest(
paging = mlops.ModelregistryPagingRequest(),
filter = _get_filter_by(SEARCH_BY, SEARCH_VALUE),
sorting = _get_sort_by("lastUpdateTimestampMs")
)
)
print(response)TipIf no search criteria are defined at the beginning of the script, ensure that the
filter = _get_filter_by(SEARCH_BY, SEARCH_VALUE)
line is removed.
- Submit and view feedback for this page
- Send feedback about H2O MLOps to cloud-feedback@h2o.ai