在多次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)