Google Cloud Translation API AdaptiveMtTranslateRequest传多字符串报错咨询
Google Cloud Translation API AdaptiveMtTranslateRequest 批量翻译问题
问题重现
尝试使用AdaptiveMtTranslateRequest接口批量翻译多句文本,按照官方文档传入字符串列表,但收到400错误:
{ "error": { "code": 400, "message": "Request should not have more than 1 entries", "status": "INVALID_ARGUMENT" } }
使用的Java代码如下:
/** Translates using AdaptiveMt. */ private static void adaptiveMtTranslate( TranslationServiceClient translationServiceClient, String projectId, String location, String datasetId, List<String> content) { String adaptiveMtDatasetName = String.format("projects/%s/locations/%s/adaptiveMtDatasets/%s", projectId, location, datasetId); AdaptiveMtTranslateRequest request = AdaptiveMtTranslateRequest.newBuilder() .setParent(LocationName.of(projectId, location).toString()) .setDataset(adaptiveMtDatasetName) .addAllContent(content) .build(); AdaptiveMtTranslateResponse response = translationServiceClient.adaptiveMtTranslate(request); for (AdaptiveMtTranslation translation : response.getTranslationsList()) { System.out.println(translation.getTranslatedText()); } }
原因分析
目前Google Cloud Translation API的AdaptiveMtTranslate接口实际不支持单请求传入多个文本条目,尽管API定义和客户端库文档标注content为可重复字段,但服务端存在严格的1条限制,属于文档与实际实现的不一致。
批量翻译替代方案
1. 异步批量翻译(Batch Translation)
适合处理大量文本或完整文档,提交任务后后台异步处理,完成后可获取翻译结果。示例代码:
private static void batchTranslateText(TranslationServiceClient client, String projectId, String location, String datasetId) { String sourceLanguageCode = "en"; String targetLanguageCode = "zh"; // 输入文件路径(需存储在GCS) GcsSource gcsSource = GcsSource.newBuilder().setInputUri("gs://your-bucket/input.txt").build(); InputConfig inputConfig = InputConfig.newBuilder() .setGcsSource(gcsSource) .setMimeType("text/plain") .build(); // 输出文件存储路径(GCS) GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix("gs://your-bucket/output/").build(); OutputConfig outputConfig = OutputConfig.newBuilder() .setGcsDestination(gcsDestination) .build(); // 构建批量翻译请求 BatchTranslateTextRequest request = BatchTranslateTextRequest.newBuilder() .setParent(LocationName.of(projectId, location).toString()) .addSourceLanguageCodes(sourceLanguageCode) .addTargetLanguageCodes(targetLanguageCode) .addInputConfigs(inputConfig) .setOutputConfig(outputConfig) // 绑定Adaptive MT数据集 .setAdaptiveMtDataset(String.format("projects/%s/locations/%s/adaptiveMtDatasets/%s", projectId, location, datasetId)) .build(); // 提交异步任务并等待完成 OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future = client.batchTranslateTextAsync(request); System.out.println("批量翻译任务已提交,等待处理完成..."); try { BatchTranslateResponse response = future.get(); System.out.println("批量翻译完成,结果存储路径:" + response.getOutputConfig().getGcsDestination().getOutputUriPrefix()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } }
2. 客户端并发请求
对多个文本条目发起并发请求,提升处理效率(注意控制请求速率,避免触发API限流)。示例代码(使用CompletableFuture):
private static void concurrentAdaptiveMtTranslate(TranslationServiceClient client, String projectId, String location, String datasetId, List<String> content) { String parent = LocationName.of(projectId, location).toString(); String dataset = String.format("projects/%s/locations/%s/adaptiveMtDatasets/%s", projectId, location, datasetId); // 构建所有文本的异步翻译任务 List<CompletableFuture<String>> futures = content.stream() .map(text -> CompletableFuture.supplyAsync(() -> { AdaptiveMtTranslateRequest request = AdaptiveMtTranslateRequest.newBuilder() .setParent(parent) .setDataset(dataset) .addContent(text) .build(); AdaptiveMtTranslateResponse response = client.adaptiveMtTranslate(request); return response.getTranslationsList().get(0).getTranslatedText(); })) .collect(Collectors.toList()); // 等待所有任务完成并收集结果 List<String> translations = futures.stream() .map(CompletableFuture::join) .collect(Collectors.toList()); translations.forEach(System.out::println); }
内容的提问来源于stack exchange,提问作者Random Human
相关产品推荐
相关产品推荐

