【问题标题】:Hot to serialize my payload to the PredictionServiceClient class?很想将我的有效负载序列化到 PredictionServiceClient 类?
【发布时间】:2021-11-16 15:33:48
【问题描述】:

我得到了一个基于 GCP 中的 AutoML 表的训练和部署模型。

它在 PowerShell 中回答以下请求:

$env:GOOGLE_APPLICATION_CREDENTIALS="[MY-SERVICE-ACCOUNT-KEY-FILE].json"    
$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile [MY-REQUEST-FILE].json `
-Uri "https://eu-automl.googleapis.com/v1beta1/projects/[MY-PROJECT-ID]/locations/eu/models/[MY-MODEL-ID]:predict" | Select-Object -Expand Content

对应回复:

{
  "payload": [
    {
      "tables": {
        "score": 0.9930415,
        "value": "Low"
      }
    },
    {
      "tables": {
        "score": 0.006626979,
        "value": "Medium"
      }
    },
    {
      "tables": {
        "score": 0.0003314384,
        "value": "High"
      }
    }
  ]
}

一切都好。现在我想按照文档here 使用 Google.Cloud.AutoML.V1 .NET 库从 C# 进行调用。

<PackageReference Include="Google.Cloud.AutoML.V1" Version="2.3.0" />

    // Create client
    PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create();
    // Initialize request argument(s)
    ModelName name = ModelName.FromProjectLocationModel([MY-PROJECT-ID], "eu", [MY-MODEL-ID]);
    ExamplePayload payload = new ExamplePayload();
    IDictionary<string, string> @params = new Dictionary<string, string> { { "feature_importance", "true" }, };
    // Make the request
    PredictResponse response = predictionServiceClient.Predict(name, payload, @params);

终端:

Unhandled Exception: Grpc.Core.RpcException: Status(StatusCode="InvalidArgument", Detail="List of found errors: 1.Field: name; Message: The provided location ID doesn't match the endpoint. For automl.googleapis.com, the valid location ID is `us-central1`. For eu-automl.googleapis.com, the valid location ID is `eu`.      2.Field: payload.payload; Message: Required field not set.        ", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {"created":"@1632383996.440000000","description":"Error received from peer ipv4:216.58.211.10:443","file":"..\..\..\src\core\lib\surface\call.cc","file_line":1067,"grpc_message":"List of found errors:\t1.Field: name; Message: The provided location ID doesn't match the endpoint. For automl.googleapis.com, the valid location ID is `us-central1`. For eu-automl.googleapis.com, the valid location ID is `eu`.\t2.Field: payload.payload; Message: Required field not set.\t","grpc_status":3}")
   at Grpc.Core.Internal.AsyncCall`2.UnaryCall(TRequest msg) in /var/local/git/grpc/src/csharp/Grpc.Core/Internal/AsyncCall.cs:line 78
   at Grpc.Core.DefaultCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) in /var/local/git/grpc/src/csharp/Grpc.Core/DefaultCallInvoker.cs:line 46
   at Grpc.Core.Interceptors.InterceptingCallInvoker.<BlockingUnaryCall>b__3_0[TRequest,TResponse](TRequest req, ClientInterceptorContext`2 ctx) in /var/local/git/grpc/src/csharp/Grpc.Core.Api/Interceptors/InterceptingCallInvoker.cs:line 51
   at Grpc.Core.ClientBase.ClientBaseConfiguration.ClientBaseConfigurationInterceptor.BlockingUnaryCall[TRequest,TResponse](TRequest request, ClientInterceptorContext`2 context, BlockingUnaryCallContinuation`2 continuation) in /var/local/git/grpc/src/csharp/Grpc.Core.Api/ClientBase.cs:line 174
   at Grpc.Core.Interceptors.InterceptingCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) in /var/local/git/grpc/src/csharp/Grpc.Core.Api/Interceptors/InterceptingCallInvoker.cs:line 48
   at Google.Cloud.AutoML.V1.PredictionService.PredictionServiceClient.Predict(PredictRequest request, CallOptions options) in /_/apis/Google.Cloud.AutoML.V1/Google.Cloud.AutoML.V1/PredictionServiceGrpc.g.cs:line 281
   at Google.Api.Gax.Grpc.ApiCall.GrpcCallAdapter`2.CallSync(TRequest request, CallSettings callSettings) in /_/Google.Api.Gax.Grpc/ApiCall.cs:line 92
   at Google.Api.Gax.Grpc.ApiCallRetryExtensions.<>c__DisplayClass1_0`2.<WithRetry>b__0(TRequest request, CallSettings callSettings) in /_/Google.Api.Gax.Grpc/ApiCallRetryExtensions.cs:line 78
   at Predict.Program.predict1(String project, String location, String model) in 

知道如何进行吗?

【问题讨论】:

    标签: c# .net google-cloud-platform google-cloud-automl


    【解决方案1】:

    我在使用 Java 实现时收到了相同的错误消息。但是当如下所示覆盖默认端点时,它可以工作。

    PredictionServiceSettings settings =
                PredictionServiceSettings.newBuilder()
                    .setEndpoint("eu-automl.googleapis.com:443")
                    .build();
    
    try (PredictionServiceClient client = PredictionServiceClient.create(settings)) { ...
    

    很遗憾,.NET 库没有提供将设置作为参数的 Create 方法。

    【讨论】:

    【解决方案2】:

    官方文档指出当前用于 .NET (C#) 客户端库 the only supported location_id for autoML endpoints is us-central1,而您正在尝试使用 eu 端点。

    我已经打开feature request 询问将来是否会启用其他位置。

    关于第二个错误,我认为这是由于第一个错误,因为您完全按照官方文档的示例代码,没有为有效负载构造函数提供任何额外的参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 2018-01-08
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多