Search code examples
gogrpcgcloudgoogle-cloud-vertex-aigoogle-ai-platform

Google Cloud Vertex AI with Golang: rpc error: code = Unimplemented desc = unexpected HTTP status code received from server: 404 (Not Found)


I have a Vertex AI model deployed on an endpoint and want to do some prediction from my app in Golang.

To do this I create code inspired by this example : 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 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()
           .
           .