6

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>
tom
  • 21,844
  • 6
  • 43
  • 36
garrym
  • 61
  • 4
  • Hello @garrym. Can you let me know if your issue is resolved? – Vishal K May 14 '22 at 18:04
  • If my answer addressed your question, consider upvoting and accepting it. If not, let me know so that the answer can be improved. Accepting an answer will help the community members with their research as well :) – Vishal K May 15 '22 at 20:58
  • Did you figure it out? – humble May 17 '22 at 15:52

2 Answers2

8

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()
       .
       .
Vishal K
  • 1,368
  • 1
  • 7
  • 15
  • did not work for me. I get "request contains an invalid endpoint" – humble May 17 '22 at 15:47
  • Sorry for the delay, I tried your last exemple and we make a progress. Now the error is the following : `rpc error: code = InvalidArgument desc = Invalid instances: struct_value {}` I will make some tests to find where this come from. – garrym May 17 '22 at 19:24
  • Ok it works, the error come from some comments I have added during the test. Thanks you @DazWilkin and Vishal K – garrym May 17 '22 at 19:30
  • This answer helped me solve the same issue with Python. – havryliuk Jan 17 '23 at 09:58
  • Thank you very much! You've saved up my time a lot! – leonkong Mar 24 '23 at 06:47
0

Try to do something like this

[...]
url := fmt.Sprintf("%s-aiplatform.googleapis.com:443", location)
[..]

tcz
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 02 '22 at 12:13