首頁 > 後端開發 > Golang > 主體

aiplatformb.PredictRequest.Instances 需要型別 *structpb.Value (GCP golang 用戶端函式庫;aiplatform)

王林
發布: 2024-02-10 15:00:09
轉載
497 人瀏覽過

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

php小編蘋果AI平台B是一個強大的預測請求實例,它需要類型為structpb.Value的參數。這是一個GCP(Google雲端平台)的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
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!