pythongoogle-cloud-platformgoogle-cloud-mlgoogle-ai-platform

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


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


Solution

  • 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')