我正在try 从Golang Web应用程序(Web服务器/应用程序运行在云Run+Build上)访问我的Vertex AI端点.Web应用程序有一个我要登录到详细信息提交中的表单,我的问题是,我如何获取从Web应用程序接收的 struct ,并将其转换为aiPlatformb.PredicRequeststruct的实例字段中接受的类型?

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

我try 使用POSTMAN从外部访问此端点,下面的请求确认该端点已启动并正在运行.这些值是提交的详细信息的值

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

推荐答案

在多次try 使用客户端库和参考文档之后,.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)

Go相关问答推荐

区分Terminal和Hook Zerolog Go中的错误级别日志(log)输出

Golang html/模板&需要错误数量的参数1在模板中使用';调用';获得0&q;

Golang Viper:如果第一个字段不存在,如何从另一个字段获取值

埃拉托塞尼筛:加快交叉关闭倍数步骤

如何配置vscode以在Go中显示不必要的(过度指定的)泛型?

go中跨域自定义验证的问题

go-chi: 接受带有反斜杠的 url 路径参数

nixOS 上的 Nginx 反向代理在try 加载 css/js 时返回 404

在 GoLang 中对自定义 struct 体数组进行排序

如何将Golang测试用例的测试覆盖率值与特定阈值进行比较

如何在 Go 中将 int 转换为包含 complex128 的泛型类型?

Go Colly 如何找到请求的元素?

获取切片元素的地址是否意味着 Go 中元素的副本?

从 os.stdout 读取

如何编写一个以字符串或错误为参数的通用函数?

有没有办法将 yaml node 添加到 golang 中现有的 yaml 文档中?

使用 delve 在容器中调试 Golang:container_linux.go:380:启动容器进程导致:exec:/dlv:stat /dlv:没有这样的文件或目录

测试包外文件时的 Golang 测试覆盖率

有没有办法在golang中映射一组对象?

在 Go 泛型中,如何对联合约束中的类型使用通用方法?