Connect to GCP Vertex AI endpoint in .Net AWS Lambda

Tags: vertex ai, .net core, gcp, aws, aws lambda, C#

Authentication

Application Default Credentials (ADC) is a strategy used by the Google authentication libraries to automatically find credentials based on the application environment.

ADC searches for credentials in the following locations:

  1. GOOGLE_APPLICATION_CREDENTIALS environment variable
  2. User credentials set up by using the Google Cloud CLI
  3. The attached service account, returned by the metadata server

GOOGLE_APPLICATION_CREDENTIALS environment variable example

	    //Authenticate to the service by using Service Account
            var options3 = new JsonSerializerOptions
            {
                Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
                WriteIndented = true
            };
            var json = JsonSerializer.Serialize(_appConfiguration.GcpAccessKeyConfiguration, options3);
            var guid = Guid.NewGuid();
            var temporaryDirectoryPath = Path.GetTempPath();
            var path = temporaryDirectoryPath + guid + ".json";
            
            await File.WriteAllTextAsync(path, json);
            
            Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);

Storing Configuration

The service account credentials can be stored in appsettings.json

{
...
  "ConnectionStrings": {
...
  },
...
"GcpAccessKey": {
    "type": "service_account",
    "project_id": "provide-the-value",
    "private_key_id": "provide-the-value",
    "private_key": "provide-the-value",
    "client_email": "provide-the-value",
    "client_id": "provide-the-value",
    "auth_uri": "provide-the-value",
    "token_uri": "provide-the-value",
    "auth_provider_x509_cert_url": "provide-the-value",
    "client_x509_cert_url": "provide-the-value",
    "universe_domain": "provide-the-value"
  }
...
}

Region Endpoint

If you get the following exception

"Grpc.Core.RpcException: 'Status(StatusCode="Unimplemented", Detail="Bad gRPC response. HTTP status code: 404")'"

For Vertex AI you must specify a region endpoint when calling your custom trained model.

const string regionEndpoint = "us-central1-aiplatform.googleapis.com:443";
try
{
    // Create client
    var serviceClientBuilder = new PredictionServiceClientBuilder
    {
        Endpoint = regionEndpoint
    };
    var predictionServiceClient = await serviceClientBuilder.BuildAsync();
    ...

}


Sample request JSON body

The sample request JSON body for Vertex AI deployed model looks like this

 
{
  "instances": [{
    "content": "YOUR_IMAGE_BYTES"
  }],
  "parameters": {
    "confidenceThreshold": 0.5,
    "maxPredictions": 5
  }
}

Sample PredictRequest for above JSON body

In the .net application this may transfer to this

                var request = new PredictRequest
                {
                    EndpointAsEndpointName = EndpointName.FromProjectLocationEndpoint("provide-the-value", "us-central1", "provide-the-value"),
                    Instances = 
                    {
                        new Value
                        {
                            StructValue = new Struct
                            {
                                Fields =
                                {
                                    new MapField
                                    {
                                        { "content", Value.ForString(base64StringValue)},
                                        { "mimeType",  Value.ForString("image/jpeg") }
                                    }
                                }
                            }
                        }
                    },
                    Parameters = new Value
                    {
                        StructValue = new Struct
                        {
                            Fields =
                            {
                                new MapField
                                {
                                    { "confidenceThreshold", Value.ForNumber(0.5) },
                                    { "maxPredictions", Value.ForNumber(10) }
                                }
                            }
                        }
                    },
                };
                // Make the request
                var response = await predictionServiceClient.PredictAsync(request);

Note the image must by base 64 encoded before passing to Vertax AI

Source:

  1. How Application Default Credentials works  |  Authentication  |  Google Cloud
  2. Get online predictions from a custom trained model
  3. connect vertex ai endpoint through .Net | SO
  4. Sending http request Google Vertex AI end point | SO