From 5b47da8520a0758ba7023cbeabed9cbcc431b202 Mon Sep 17 00:00:00 2001 From: Jean Aurambault Date: Fri, 22 Nov 2024 15:28:50 -0800 Subject: [PATCH] Add an option to AiTranslate only a specific list of text units --- .../cli/command/RepositoryAiTranslationCommand.java | 9 ++++++++- .../rest/client/RepositoryAiTranslateClient.java | 1 + .../box/l10n/mojito/rest/textunit/AiTranslateWS.java | 2 ++ .../service/oaitranslate/AiTranslateService.java | 11 +++++++++-- .../service/oaitranslate/AiTranslateServiceTest.java | 4 ++-- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/RepositoryAiTranslationCommand.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/RepositoryAiTranslationCommand.java index 7986d5ab84..3117033d5f 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/RepositoryAiTranslationCommand.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/RepositoryAiTranslationCommand.java @@ -55,6 +55,13 @@ public class RepositoryAiTranslationCommand extends Command { + "sending too many strings to MT)") int sourceTextMaxCount = 100; + @Parameter( + names = {"--text-unit-ids"}, + arity = 1, + description = + "The list of TmTextUnitIds to translate") + List textUnitIds; + @Parameter( names = {"--use-batch"}, arity = 1, @@ -87,7 +94,7 @@ public void execute() throws CommandException { ProtoAiTranslateResponse protoAiTranslateResponse = repositoryAiTranslateClient.translateRepository( new RepositoryAiTranslateClient.ProtoAiTranslateRequest( - repositoryParam, locales, sourceTextMaxCount, useBatch)); + repositoryParam, locales, sourceTextMaxCount, textUnitIds, useBatch)); PollableTask pollableTask = protoAiTranslateResponse.pollableTask(); commandHelper.waitForPollableTask(pollableTask.getId()); diff --git a/restclient/src/main/java/com/box/l10n/mojito/rest/client/RepositoryAiTranslateClient.java b/restclient/src/main/java/com/box/l10n/mojito/rest/client/RepositoryAiTranslateClient.java index f0f5207675..27986eb8a4 100644 --- a/restclient/src/main/java/com/box/l10n/mojito/rest/client/RepositoryAiTranslateClient.java +++ b/restclient/src/main/java/com/box/l10n/mojito/rest/client/RepositoryAiTranslateClient.java @@ -32,6 +32,7 @@ public record ProtoAiTranslateRequest( String repositoryName, List targetBcp47tags, int sourceTextMaxCountPerLocale, + List tmTextUnitIds, boolean useBatch) {} public record ProtoAiTranslateResponse(PollableTask pollableTask) {} diff --git a/webapp/src/main/java/com/box/l10n/mojito/rest/textunit/AiTranslateWS.java b/webapp/src/main/java/com/box/l10n/mojito/rest/textunit/AiTranslateWS.java index 7f33de3211..6f600b0ed5 100644 --- a/webapp/src/main/java/com/box/l10n/mojito/rest/textunit/AiTranslateWS.java +++ b/webapp/src/main/java/com/box/l10n/mojito/rest/textunit/AiTranslateWS.java @@ -43,6 +43,7 @@ public ProtoAiTranslateResponse aiTranslate( protoAiTranslateRequest.repositoryName(), protoAiTranslateRequest.targetBcp47tags(), protoAiTranslateRequest.sourceTextMaxCountPerLocale(), + protoAiTranslateRequest.tmTextUnitIds(), protoAiTranslateRequest.useBatch())); return new ProtoAiTranslateResponse(pollableFuture.getPollableTask()); @@ -53,6 +54,7 @@ public record ProtoAiTranslateRequest( List targetBcp47tags, int sourceTextMaxCountPerLocale, boolean useBatch, + List tmTextUnitIds, boolean allLocales) {} public record ProtoAiTranslateResponse(PollableTask pollableTask) {} diff --git a/webapp/src/main/java/com/box/l10n/mojito/service/oaitranslate/AiTranslateService.java b/webapp/src/main/java/com/box/l10n/mojito/service/oaitranslate/AiTranslateService.java index 8d9b962398..983e55a66d 100644 --- a/webapp/src/main/java/com/box/l10n/mojito/service/oaitranslate/AiTranslateService.java +++ b/webapp/src/main/java/com/box/l10n/mojito/service/oaitranslate/AiTranslateService.java @@ -126,6 +126,7 @@ public record AiTranslateInput( String repositoryName, List targetBcp47tags, int sourceTextMaxCountPerLocale, + List tmTextUnitIds, boolean useBatch) {} public PollableFuture aiTranslateAsync(AiTranslateInput aiTranslateInput) { @@ -161,7 +162,7 @@ public void aiTranslateNoBatch(AiTranslateInput aiTranslateInput) { .flatMap( rl -> asyncProcessLocale( - rl, aiTranslateInput.sourceTextMaxCountPerLocale(), openAIClientPool), + rl, aiTranslateInput.sourceTextMaxCountPerLocale(), aiTranslateInput.tmTextUnitIds(), openAIClientPool), 10) .then() .doOnTerminate( @@ -174,6 +175,7 @@ public void aiTranslateNoBatch(AiTranslateInput aiTranslateInput) { Mono asyncProcessLocale( RepositoryLocale repositoryLocale, int sourceTextMaxCountPerLocale, + List tmTextUnitIds, OpenAIClientPool openAIClientPool) { Repository repository = repositoryLocale.getRepository(); @@ -187,7 +189,12 @@ Mono asyncProcessLocale( textUnitSearcherParameters.setRepositoryIds(repository.getId()); textUnitSearcherParameters.setStatusFilter(StatusFilter.FOR_TRANSLATION); textUnitSearcherParameters.setLocaleId(repositoryLocale.getLocale().getId()); - textUnitSearcherParameters.setLimit(sourceTextMaxCountPerLocale); + if (tmTextUnitIds != null) { + logger.debug("Using tmTextUnitIds: {} for ai translate repository: {}", tmTextUnitIds, repository.getName()); + textUnitSearcherParameters.setTmTextUnitIds(tmTextUnitIds); + } else { + textUnitSearcherParameters.setLimit(sourceTextMaxCountPerLocale); + } List textUnitDTOS = textUnitSearcher.search(textUnitSearcherParameters); diff --git a/webapp/src/test/java/com/box/l10n/mojito/service/oaitranslate/AiTranslateServiceTest.java b/webapp/src/test/java/com/box/l10n/mojito/service/oaitranslate/AiTranslateServiceTest.java index 1ef12ede94..afad21f8c3 100644 --- a/webapp/src/test/java/com/box/l10n/mojito/service/oaitranslate/AiTranslateServiceTest.java +++ b/webapp/src/test/java/com/box/l10n/mojito/service/oaitranslate/AiTranslateServiceTest.java @@ -33,7 +33,7 @@ public void aiTranslateBatch() throws ExecutionException, InterruptedException { aiTranslateService .aiTranslateAsync( new AiTranslateService.AiTranslateInput( - tmTestData.repository.getName(), null, 100, true)) + tmTestData.repository.getName(), null, 100, null, true)) .get(); } @@ -47,7 +47,7 @@ public void aiTranslateNoBatch() aiTranslateService .aiTranslateAsync( new AiTranslateService.AiTranslateInput( - tmTestData.repository.getName(), null, 100, false)) + tmTestData.repository.getName(), null, 100, null, false)) .get(); } }