首页 > 后端开发 > Golang > 正文

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

王林
发布: 2024-02-10 15:00:09
转载
682人浏览过

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

uBrand Logo生成器
uBrand Logo生成器

uBrand Logo生成器是一款强大的AI智能LOGO设计工具。

uBrand Logo生成器 124
查看详情 uBrand Logo生成器

立即学习go语言免费学习笔记(深入)”;

以下是形成和使用 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中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号