使用 google 云 aiplatform 与 golang

Use google cloud aiplatform with golang

我在一个端点上部署了一个顶点 AI 模型,想从我的 golang 应用程序中做一些预测。

为此,我创建了受此示例启发的代码:https://cloud.google.com/go/docs/reference/cloud.google.com/go/aiplatform/latest/apiv1?hl=en

const file = "MY_BASE64_IMAGE"

func main() {

    ctx := context.Background()

    c, err := aiplatform.NewPredictionClient(cox)
    if err != nil {
        log.Printf("QueryVertex NewPredictionClient - Err:%s", err)
    }
    defer c.Close()

    parameters, err := structpb.NewValue(map[string]interface{}{
        "confidenceThreshold": 0.2,
        "maxPredictions":      5,
    })
    if err != nil {
        log.Printf("QueryVertex structpb.NewValue parameters - Err:%s", err)
    }

    instance, err := structpb.NewValue(map[string]interface{}{
        "content": file,
    })
    if err != nil {
        log.Printf("QueryVertex structpb.NewValue instance - Err:%s", err)
    }

    reqP := &aiplatformpb.PredictRequest{
        Endpoint:   "projects/PROJECT_ID/locations/LOCATION_ID/endpoints/ENDPOINT_ID",
        Instances:  []*structpb.Value{instance},
        Parameters: parameters,
    }

    resp, err := c.Predict(cox, reqP)
    if err != nil {
        log.Printf("QueryVertex Predict - Err:%s", err)
    }

    log.Printf("QueryVertex Res:%+v", resp)
}

我将我的服务帐户 json 文件的路径放在 GOOGLE_APPLICATION_CREDENTIALS 环境变量中。 但是当我 运行 我的测试应用程序时,我收到此错误消息:

QueryVertex Predict - Err:rpc error: code = Unimplemented desc = unexpected HTTP status code received from server: 404 (Not Found); transport: received unexpected content-type "text/html; charset=UTF-8"
QueryVertex Res:<nil>

我不熟悉 Google 的(Vertex?)AI 平台,无法验证这个假设,但 API 似乎使用 location-specific endpoints .

您能否尝试配置客户端的 ClientOption 以指定特定的区域端点,即:

url := fmt.Sprintf("https://%s-aiplatform.googleapis.com", location)
opts := []option.ClientOption{
    option.WithEndpoint(url),
}

并且:

package main

import (
    "context"
    "fmt"
    "log"
    "os"

    aiplatform "cloud.google.com/go/aiplatform/apiv1"

    "google.golang.org/api/option"
    aiplatformpb "google.golang.org/genproto/googleapis/cloud/aiplatform/v1"
    "google.golang.org/protobuf/types/known/structpb"
)

const file = "MY_BASE64_IMAGE"

func main() {
    // Values from the environment
    project := os.Getenv("PROJECT")
    location := os.Getenv("LOCATION")
    endpoint := os.Getenv("ENDPOINT")

    ctx := context.Background()

    // Configure the client with a region-specific endpoint
    url := fmt.Sprintf("https://%s-aiplatform.googleapis.com", location)
    opts := []option.ClientOption{
        option.WithEndpoint(url),
    }

    c, err := aiplatform.NewPredictionClient(ctx, opts...)
    if err != nil {
        log.Fatal(err)
    }
    defer c.Close()

    parameters, err := structpb.NewValue(map[string]interface{}{
        "confidenceThreshold": 0.2,
        "maxPredictions":      5,
    })
    if err != nil {
        log.Fatal(err)
    }

    instance, err := structpb.NewValue(map[string]interface{}{
        "content": file,
    })
    if err != nil {
        log.Printf("QueryVertex structpb.NewValue instance - Err:%s", err)
    }

    rqst := &aiplatformpb.PredictRequest{
        Endpoint: fmt.Sprintf("projects/%s/locations/%s/endpoints/%s",
            project,
            location,
            endpoint,
        ),
        Instances: []*structpb.Value{
            instance,
        },
        Parameters: parameters,
    }

    resp, err := c.Predict(ctx, rqst)
    if err != nil {
        log.Fatal(err)
    }

    log.Printf("QueryVertex Res:%+v", resp)
}

正如@DazWilkin 所建议的那样,配置客户端选项以指定具有端口 443 的特定 regional endpoint

option.WithEndpoint("<region>-aiplatform.googleapis.com:443")

尝试如下:

func main() {
 
   ctx := context.Background()
   c, err := aiplatform.NewPredictionClient(
       ctx,
       option.WithEndpoint("<region>-aiplatform.googleapis.com:443"),
   )
   if err != nil {
       log.Printf("QueryVertex NewPredictionClient - Err:%s", err)
   }
   defer c.Close()
       .
       .