


aiplatformb.PredictRequest.Instances requires type *structpb.Value (GCP golang client library; aiplatform)
php Editor Apple AI Platform B is a powerful prediction request instance, which requires parameters of type structpb.Value. This is a Go language client library for GCP (Google Cloud Platform), specially designed for aiplatform. It provides users with convenient prediction functions to quickly make model predictions during the development process. By using this library, users can easily integrate AI technology into their applications and obtain accurate and efficient prediction results.
Question content
I'm trying to access my Vertex AI endpoint from a Golang web application (the web server/application is running on a cloud run build). The web application has a form where I am submitting details, my question is, how can I get the structure received from the web application and convert it to the type accepted in the Instances field of the aiplatformb.PredictRequest structure?
type Submission struct { MonthlyIncome int Age int Passport int } var Details = Submission{} Ctx := context.Background() C, err := aiplatform.NewPredictionClient(Ctx) if err != nil { log.Fatalf("Error 1: %v", err) } defer C.Close() reqs := &aiplatformpb.PredictRequest{ Endpoint: "{{my endpoint that is formatted correctly}", Instances: []*structpb.Value{},
I tried accessing this endpoint externally using Postman and the request below confirms the endpoint is up and running. These values are the values submitted by the details
{ "instances": [ [ 29823, 43.5, 1 ] ] }
Workaround
After multiple attempts using the client library and consulting the documentation, the .Predict() method [acting on a pointer to a PredictionClient type] does not allow you to specify a vertex AI model endpoint architecture. So the solution is to send the request through the .RawPredict() method, so the serialized JSON (structpb) request will only work if the schema implemented by the golang GCP client library matches your deployed model. Here is the GCP documentation for PredictionClient:
The following are the libraries required to form and use the RawPredict() method:
import ( "context" "fmt" "log" "reflect" "strconv" aiplatform "cloud.google.com/go/aiplatform/apiv1" "cloud.google.com/go/aiplatform/apiv1/aiplatformpb" "google.golang.org/api/option" "google.golang.org/genproto/googleapis/api/httpbody" )
This is the code:
// Get the form values from the web applicaiton income, _ := strconv.Atoi(r.FormValue("MonthlyIncome")) age, _ := strconv.Atoi(r.FormValue("Age")) passport, _ := strconv.Atoi(r.FormValue("Passport")) //create our struct from the form values Details = Submission{ MonthlyIncome: income, Age: age, Passport: passport, } v := reflect.ValueOf(Details) body = "" for i := 0; i < v.NumField(); i++ { body = body + fmt.Sprintf("%v", v.Field(i).Interface()) + "," } if last := len(body) - 1; last >= 0 && body[last] == ',' { body = body[:last] } Requestb = pre + body + post log.Println("The request string was:", Requestb) // structure the body of the raw request Raw := &httpbody.HttpBody{} Raw.Data = []byte(Requestb) // indentify the post request using the raw body and the endpoint reqs := &aiplatformpb.RawPredictRequest{ // Note GCP Project ID, Region, and endpoint ID Endpoint: "projects/<PROJECT-HERE>/locations/<REGDION-HERE>/endpoints/<ENDPOINT-ID-HERE>", HttpBody: Raw, } // CTX gets the credentials of the application service account - NOTE THE REGION Ctx := context.Background() C, err := aiplatform.NewPredictionClient(Ctx, option.WithEndpoint("<REGION-HERE>-aiplatform.googleapis.com:443")) if err != nil { log.Println("Error 1, connectrion:", err) } defer C.Close() // gets the response using the credentials of the application service account resp, err := C.RawPredict(Ctx, reqs) if err != nil { log.Fatalf("Error 2, response: %v", err) } log.Println(resp) RespString := fmt.Sprintf("%+v", resp) log.Println("The Response String was:", resp)
The above is the detailed content of aiplatformb.PredictRequest.Instances requires type *structpb.Value (GCP golang client library; aiplatform). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

When using sql.Open, why doesn’t the DSN report an error? In Go language, sql.Open...
