如何为.NET中Google PredictionServiceClient流式调用实现重试逻辑?
为Google.Cloud.AIPlatform.V1的PredictionServiceClient流式调用实现重试逻辑
需求
需要为Google.Cloud.AIPlatform.V1 NuGet包中的PredictionServiceClient实现重试逻辑,触发重试的场景包括:
- 请求超出配额限制(HTTP状态码429)
- 服务不可用(gRPC状态码Unavailable)
要求所有PredictionServiceClient的调用使用统一的重试和超时配置。
参考实现(Azure OpenAI)
同一应用中使用Azure OpenAI的Azure.AI.OpenAI包时,可在客户端初始化阶段直接配置重试参数,示例代码如下:
OpenAIClientOptions opts = new OpenAIClientOptions(); opts.Retry.MaxRetries = 2; opts.Retry.Delay = TimeSpan.FromSeconds(60); opts.Retry.NetworkTimeout = TimeSpan.FromSeconds(100); opts.Retry.Mode = (RetryMode)connDetails.AzureOpenAIRetryMode.Value; client = new OpenAIClient(new Uri("sampleUrl"), new AzureKeyCredential("sampleKey"), opts);
当前实现与报错
当前使用PredictionServiceClient进行流式内容生成的代码:
Google.Cloud.AIPlatform.V1.PredictionServiceClient.StreamGenerateContentStream response = myPredictionServiceClient.StreamGenerateContent(generateContentRequest);
尝试通过PredictionServiceSettings配置全局重试规则,但调用StreamGenerateContent时抛出错误:
PredictionServiceSettings settings = new PredictionServiceSettings { CallSettings = CallSettings.FromRetry(RetrySettings.FromExponentialBackoff( maxAttempts: 2, initialBackoff: TimeSpan.FromSeconds(1), maxBackoff: TimeSpan.FromSeconds(10), backoffMultiplier: 2, retryFilter: RetrySettings.FilterForStatusCodes(StatusCode.Unavailable) )).WithTimeout(TimeSpan.FromSeconds(100)) }; bardClient = await new PredictionServiceClientBuilder { Settings = settings, Endpoint = connDetails.AzureOpenAIResourceUrl }.BuildAsync();
错误详情:
HResult=0x80131509 Message=Retry not permitted for this operation type Source=Google.Api.Gax.Grpc StackTrace: at Google.Api.Gax.Grpc.CallSettingsExtensions.ValidateNoRetry(CallSettings callSettings) at Google.Api.Gax.Grpc.ApiServerStreamingCall.<>c__DisplayClass0_0`2.<Create>b__1(TRequest req, CallSettings cs) at Google.Cloud.AIPlatform.V1.PredictionServiceClientImpl.StreamGenerateContent(GenerateContentRequest request, CallSettings callSettings) in Google.Cloud.AIPlatform.V1\PredictionServiceClientImpl.cs:line 250
问题
如何为PredictionServiceClient的StreamGenerateContent流式调用实现符合要求的重试逻辑?需要示例代码或实现指引。
内容的提问来源于stack exchange,提问作者singh_v
相关产品推荐
相关产品推荐

