Home > Backend Development > Golang > aiplatformb.PredictRequest.Instances requires type *structpb.Value (GCP golang client library; aiplatform)

aiplatformb.PredictRequest.Instances requires type *structpb.Value (GCP golang client library; aiplatform)

王林
Release: 2024-02-10 15:00:09
forward
532 people have browsed it

aiplatformb.PredictRequest.Instances 需要类型 *structpb.Value (GCP golang 客户端库;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{},
Copy after login

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
            ]
        ]
    }
Copy after login

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:

https ://cloud.google.com/go/docs/reference/cloud.google.com/go/aiplatform/1.0.0/apiv1#cloud_google_com_go_aiplatform_apiv1_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"
)
Copy after login

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)
Copy after login

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template