首页  >  文章  >  后端开发  >  aiplatformb.PredictRequest.Instances 需要类型 *structpb.Value (GCP golang 客户端库;aiplatform)

aiplatformb.PredictRequest.Instances 需要类型 *structpb.Value (GCP golang 客户端库;aiplatform)

王林
王林转载
2024-02-10 15:00:09497浏览

aiplatformb.PredictRequest.Instances 需要类型 *structpb.Value (GCP golang 客户端库;aiplatform)

php小编苹果AI平台B是一个功能强大的预测请求实例,它需要类型为structpb.Value的参数。这是一个GCP(谷歌云平台)的Go语言客户端库,特别为aiplatform设计。它为用户提供了便捷的预测功能,可以在开发过程中快速进行模型预测。通过使用这个库,用户可以轻松地将AI技术集成到他们的应用程序中,并获得准确、高效的预测结果。

问题内容

我正在尝试从 Golang Web 应用程序访问我的 Vertex AI 端点(Web 服务器/应用程序在云运行+构建上运行)。 Web 应用程序有一个表单,我正在提交详细信息,我的问题是,如何获取从 Web 应用程序接收到的结构并将其转换为 aiplatformb.PredictRequest 结构的 Instances 字段中接受的类型?

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{},

我尝试使用邮递员从外部访问此端点,下面的请求确认端点已启动并正在运行。这些值是详细信息提交的值

{
        "instances": [
            [
                29823,
                43.5,
                1
            ]
        ]
    }

解决方法

在多次尝试使用客户端库和咨询文档后,.Predict() 方法[作用于指向 PredictionClient 类型的指针]不允许您指定顶点 AI 模型端点的架构。因此,解决方案是通过 .RawPredict() 方法发送请求,因此只有当 golang GCP 客户端库实现的架构与您部署的模型匹配时,序列化 JSON (structpb) 请求才有效。以下是 PredictionClient 的 GCP 文档:

https ://cloud.google.com/go/docs/reference/cloud.google.com/go/aiplatform/1.0.0/apiv1#cloud_google_com_go_aiplatform_apiv1_PredictionClient

以下是形成和使用 RawPredict() 方法所需的库:

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"
)

这是代码:

// 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)

以上是aiplatformb.PredictRequest.Instances 需要类型 *structpb.Value (GCP golang 客户端库;aiplatform)的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除