Skip to content

Commit

Permalink
Add an option to AiTranslate only a specific list of text units
Browse files Browse the repository at this point in the history
  • Loading branch information
ja-openai committed Nov 22, 2024
1 parent 719ec38 commit 5b47da8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> textUnitIds;

@Parameter(
names = {"--use-batch"},
arity = 1,
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public record ProtoAiTranslateRequest(
String repositoryName,
List<String> targetBcp47tags,
int sourceTextMaxCountPerLocale,
List<Long> tmTextUnitIds,
boolean useBatch) {}

public record ProtoAiTranslateResponse(PollableTask pollableTask) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public ProtoAiTranslateResponse aiTranslate(
protoAiTranslateRequest.repositoryName(),
protoAiTranslateRequest.targetBcp47tags(),
protoAiTranslateRequest.sourceTextMaxCountPerLocale(),
protoAiTranslateRequest.tmTextUnitIds(),
protoAiTranslateRequest.useBatch()));

return new ProtoAiTranslateResponse(pollableFuture.getPollableTask());
Expand All @@ -53,6 +54,7 @@ public record ProtoAiTranslateRequest(
List<String> targetBcp47tags,
int sourceTextMaxCountPerLocale,
boolean useBatch,
List<Long> tmTextUnitIds,
boolean allLocales) {}

public record ProtoAiTranslateResponse(PollableTask pollableTask) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public record AiTranslateInput(
String repositoryName,
List<String> targetBcp47tags,
int sourceTextMaxCountPerLocale,
List<Long> tmTextUnitIds,
boolean useBatch) {}

public PollableFuture<Void> aiTranslateAsync(AiTranslateInput aiTranslateInput) {
Expand Down Expand Up @@ -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(
Expand All @@ -174,6 +175,7 @@ public void aiTranslateNoBatch(AiTranslateInput aiTranslateInput) {
Mono<Void> asyncProcessLocale(
RepositoryLocale repositoryLocale,
int sourceTextMaxCountPerLocale,
List<Long> tmTextUnitIds,
OpenAIClientPool openAIClientPool) {

Repository repository = repositoryLocale.getRepository();
Expand All @@ -187,7 +189,12 @@ Mono<Void> 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<TextUnitDTO> textUnitDTOS = textUnitSearcher.search(textUnitSearcherParameters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}
}

0 comments on commit 5b47da8

Please sign in to comment.