How to delete Models deployed to an endpoint on Unified AI Platform using Python?

Question:

I have successfully created an endpoint on Unified Cloud AI Platform and have deployed two Models to it – Model B and Model C with 20% and 80% of the traffic respectively. I had a third ModelModel A that I had deployed earlier and which now gets 0% traffic.

Now, on the Cloud Console (the UI) I get an option to Undeploy this Model A and I can successfully do so. However I am unable to figure out how to do this using the Python Client API.

The documentation provided over here is inadequate to understand how we could do this. Any help will be appreciated.

Asked By: Jash Shah

||

Answers:

The documentation for AI Platform Unified does not yet have an example on how to undeploy a model in an endpoint. You can refer to AI platform unified rpc reference on the available services for now. Here is the code for that:

NOTE: Don’t forget to update the values for end_point (endpoint ID),project (project ID), model_id before running the code.

from google.cloud import aiplatform
from google.cloud import aiplatform_v1


def undeploy_model_in_endpoint(
    end_point: str,
    project: str,
    model_id: str,
    location: str = "us-central1",
    api_endpoint: str = "us-central1-aiplatform.googleapis.com",
    timeout: int = 7200,
):
    # The AI Platform services require regional API endpoints.
    client_options = {"api_endpoint": api_endpoint}
    # Initialize client that will be used to create and send requests.
    # This client only needs to be created once, and can be reused for multiple requests.
    client = aiplatform.gapic.EndpointServiceClient(client_options=client_options)
    client_model = aiplatform_v1.services.model_service.ModelServiceClient(client_options=client_options)

    # Get deployed_model_id
    model_name = f'projects/{project}/locations/{location}/models/{model_id}'
    model_request = aiplatform_v1.types.GetModelRequest(name=model_name)
    model_info = client_model.get_model(request=model_request)
    deployed_models_info = model_info.deployed_models
    deployed_model_id=model_info.deployed_models[0].deployed_model_id

    name=f'projects/{project}/locations/{location}/endpoints/{end_point}'

    undeploy_request = aiplatform_v1.types.UndeployModelRequest(endpoint=name,deployed_model_id=deployed_model_id)
    client.undeploy_model(request=undeploy_request)

undeploy_model_in_endpoint(end_point='your-endpoint-id',project='your-project-id',model_id='your-model-id')
Answered By: Ricco D

One complement for @Ricco D’s answer is after undeploying it, if you wish to delete the endpoint, you would need to provide the Endpoint class aiplatform.Endpoint and then use the method delete() from this class to delete the endpoint. You can explore that by inspecting the object itself in Python:

endpoints = aiplatform.Endpoint.list()
# Taking just the first endpoint and checking its attributes and methods
print(dir(endpoints[0]))

One way to get a list of those endpoint classes is by listing all the endpoints in your project (this approach doesn’t need the project to be declared or anything, it makes use of the Application Default Credentials (ADC) be it in the Cloud or not:

from google.cloud import aiplatform
from google.cloud import aiplatform_v1
endpoints = aiplatform.Endpoint.list()
for endpoint in endpoints:
    # You need to make sure the model is undeployed before deleting it
    # Some logic to get information about undeployed model has to be done
    endpoint.delete()

Hope this helps too!

Answered By: Henrique Poleselo