You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Vertex AI预测请求失败:AutoML迁移后的连接问题

Vertex AI 预测请求连接错误排查(从AutoML迁移)

问题背景

已完成从Google Cloud Language AutoML到Vertex AI的迁移准备:数据集上传至Vertex AI、模型训练完成,Cloud Console中验证预测功能正常,端点创建成功,收到官方通知:

Hello Vertex AI Customer,

Vertex AI finished creating endpoint "{Endpoint Name}".
Additional Details:
Operation State: Succeeded
Resource Name:
projects/{Project ID}/locations/us-central1/endpoints/{Endpoint ID}

使用与原AutoML预测相同的服务账号,新代码执行时出现连接错误。

旧AutoML工作代码

string authjson = @"
{
    ""type"": ""service_account"",
    ""project_id"": ""{projectname}"",
    ""private_key_id""
    ... etc..
}

GoogleCredential credential = GoogleCredential.FromJson(authjson).CreateScoped(PredictionServiceClient.DefaultScopes);

var client = new PredictionServiceClientBuilder { JsonCredentials = authjson }.Build();

var predictionrequest = new PredictRequest()
{
    ModelName = new ModelName("{Project ID}", "us-central1", "{Model ID}"),
    Payload = new ExamplePayload
    {
        TextSnippet = new TextSnippet
        {
            Content = content_input,
            MimeType = "text/plain"
        },
    },

};

var response = client.Predict(predictionrequest);

新Vertex AI代码及错误信息

新代码

string projectID = "1234";
string endpointID = "5678";
string string_input_for_prediction = "blah blah blah";

string authjson = @"
{
    ""type"": ""service_account"",
    ... etc..
}

GoogleCredential credential = GoogleCredential.FromJson(authjson).CreateScoped(PredictionServiceClient.DefaultScopes);

PredictionServiceClientBuilder ClientBuilder = new PredictionServiceClientBuilder
{
    Credential = credential,
    Endpoint = "projects/" + projectID + "/locations/us-central1/endpoints/" + endpointID;
};

PredictionServiceClient client = ClientBuilder.Build();

var structVal = Google.Protobuf.WellKnownTypes.Value.ForStruct(new Struct
{
    Fields =
    {
    ["mimeType"] = Google.Protobuf.WellKnownTypes.Value.ForString("text/plain")
    ,
    ["content"] = Google.Protobuf.WellKnownTypes.Value.ForString(string_input_for_prediction)
    }
 });

PredictRequest predictionrequest = new PredictRequest()
{
    Instances = { structVal }
};

PredictResponse response = client.Predict(predictionrequest);

错误信息

Grpc.Core.RpcException
  HResult=0x80131500
  Message=Status(StatusCode="Unavailable", Detail="Error connecting to subchannel.", DebugException="System.Net.Sockets.SocketException: No such host is known.")
  Source=Grpc.Net.Client

修正方案及代码

错误根源在于端点地址配置错误以及请求未指定目标端点资源,以下是修正后的完整代码:

string projectID = "1234";
string endpointID = "5678";
string location = "us-central1";
string string_input_for_prediction = "blah blah blah";

string authjson = @"
{
    ""type"": ""service_account"",
    // 补充完整的服务账号凭证内容
}";

// 指定正确的权限范围
GoogleCredential credential = GoogleCredential.FromJson(authjson)
    .CreateScoped(new[] { "https://www.googleapis.com/auth/cloud-platform" });

PredictionServiceClientBuilder ClientBuilder = new PredictionServiceClientBuilder
{
    Credential = credential,
    // 设置Vertex AI区域API服务地址,而非端点资源ID
    Endpoint = $"{location}-aiplatform.googleapis.com:443"
};

PredictionServiceClient client = ClientBuilder.Build();

// 构建符合模型预期的输入结构
var structVal = Google.Protobuf.WellKnownTypes.Value.ForStruct(new Google.Protobuf.WellKnownTypes.Struct
{
    Fields =
    {
        ["content"] = Google.Protobuf.WellKnownTypes.Value.ForString(string_input_for_prediction),
        ["mimeType"] = Google.Protobuf.WellKnownTypes.Value.ForString("text/plain")
    }
});

PredictRequest predictionrequest = new PredictRequest()
{
    // 通过EndpointName类生成正确的端点资源名称,避免拼写错误
    EndpointName = EndpointName.FromProjectLocationEndpoint(projectID, location, endpointID),
    Instances = { structVal }
};

PredictResponse response = client.Predict(predictionrequest);

关键修正点

  1. 修正Endpoint属性值:PredictionServiceClientBuilder.Endpoint需要设置的是Vertex AI的区域API服务地址,格式为{region}-aiplatform.googleapis.com:443,而非端点的资源ID。
  2. 添加EndpointName指定:PredictRequest必须明确指定目标端点的完整资源名称,使用EndpointName.FromProjectLocationEndpoint方法可以自动生成正确格式的名称,避免手动拼接出错。
  3. 确认权限范围:确保服务账号的权限范围包含https://www.googleapis.com/auth/cloud-platform,覆盖Vertex AI的操作权限。

内容的提问来源于stack exchange,提问作者Mike Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 04:52:33