Use google cloud aiplatform with golang

Issue

I have a vertex AI modele deployed on an endpoint and want to do some prediction from my app in golang.

To do this I create code inspired by this exemple : https://cloud.google.com/go/docs/reference/cloud.google.com/go/aiplatform/latest/apiv1?hl=en

const file = "MY_BASE64_IMAGE"

func main() {

    ctx := context.Background()

    c, err := aiplatform.NewPredictionClient(cox)
    if err != nil {
        log.Printf("QueryVertex NewPredictionClient - Err:%s", err)
    }
    defer c.Close()

    parameters, err := structpb.NewValue(map[string]interface{}{
        "confidenceThreshold": 0.2,
        "maxPredictions":      5,
    })
    if err != nil {
        log.Printf("QueryVertex structpb.NewValue parameters - Err:%s", err)
    }

    instance, err := structpb.NewValue(map[string]interface{}{
        "content": file,
    })
    if err != nil {
        log.Printf("QueryVertex structpb.NewValue instance - Err:%s", err)
    }

    reqP := &aiplatformpb.PredictRequest{
        Endpoint:   "projects/PROJECT_ID/locations/LOCATION_ID/endpoints/ENDPOINT_ID",
        Instances:  []*structpb.Value{instance},
        Parameters: parameters,
    }

    resp, err := c.Predict(cox, reqP)
    if err != nil {
        log.Printf("QueryVertex Predict - Err:%s", err)
    }

    log.Printf("QueryVertex Res:%+v", resp)
}

I put the path to my service account json file on GOOGLE_APPLICATION_CREDENTIALS environment variable.
But when I’m run my test app I obtain this error message :

QueryVertex Predict - Err:rpc error: code = Unimplemented desc = unexpected HTTP status code received from server: 404 (Not Found); transport: received unexpected content-type "text/html; charset=UTF-8"
QueryVertex Res:<nil>

Solution

As @DazWilkin suggested, configure the client option to specify the specific regional endpoint with a port 443:

option.WithEndpoint("<region>-aiplatform.googleapis.com:443")

Try like below:

func main() {
 
   ctx := context.Background()
   c, err := aiplatform.NewPredictionClient(
       ctx,
       option.WithEndpoint("<region>-aiplatform.googleapis.com:443"),
   )
   if err != nil {
       log.Printf("QueryVertex NewPredictionClient - Err:%s", err)
   }
   defer c.Close()
       .
       .

Answered By – Vishal K

Answer Checked By – Marilyn (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.