Skip to main content
Version: v0.61.1

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.

Before you begin
ConstantValueDescription
MLOPS_API_URLUsually: https://api.mlops.my.domainDefines 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_BYtag or nameThe 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.
Note

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.

  1. Download the SearchExternalRegistry.py file.

  2. 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>"
  1. Run the SearchExternalRegistry.py file.

    python3 SearchExternalRegistry.py
  2. 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.

  1. Add the following function to filter the models.

    SearchExternalRegistry.py
    def _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
    ),
    )
    ]
    )
    ]
    )
    )
  2. Add the following function to sort the models.

    SearchExternalRegistry.py
    def _get_sort_by(field, order=mlops.ModelregistryOrder.DESCENDING):
    return mlops.ModelregistrySortingRequest(
    _property=[
    mlops.ModelregistrySortProperty(
    _property=mlops.ModelregistryProperty(
    field=field
    ),
    order=order,
    )
    ]
    )
  3. Search the models in the external model registry by calling the SearchRegisteredModel endpoint of the ExternalRegisteredModelService.

    SearchExternalRegistry.py
    def 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)
  4. In the main function, set up the token provider using an existing refresh token and client secret, and then set up the MLOps client.

  5. Call the search_external_registry function with the paging, filtering and sorting criteria.

    SearchExternalRegistry.py
    response = 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)
    Tip

    If 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.


Feedback