Skip to content

Commit

Permalink
feat(FgForrest#3): add chatgpt translator a configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
Stepan Kamenik committed Mar 10, 2024
1 parent 58ea24c commit f456970
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Optional.ofNullable;

/**
* I apologize in advance for the lack of documentation in this code.
* I had every intention of providing clear and concise explanations
Expand Down Expand Up @@ -43,13 +45,16 @@ public List<String> translate(@Nullable String defaultLang, @NotNull List<String
if (lang.equals("en")) {
lang = "en-GB";
}

String contextMessage = System.getProperty("babylon.deepl.context");
try {
return service.translateText(
original,
defaultLang,
lang,
new TextTranslationOptions()
.setContext("You are eshop translator. Do not translate text from other languages then source lang, or technical texts."))
.setContext(ofNullable(contextMessage)
.orElse("You are eshop translator. Do not translate text from other languages then source lang, or technical texts.")))
.stream()
.map(TextResult::getText)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ public List<String> translate(@Nullable String defaultLang, @NotNull List<String
lang);


String joined = String.join("~", original);
List<ChatMessage> messages = Arrays.asList(
new ChatMessage("system", formattedSystemMessage),
new ChatMessage("user", String.join("~", original))
new ChatMessage("user", joined)
);

ChatCompletionResult chatCompletion;
Expand All @@ -81,7 +82,10 @@ public List<String> translate(@Nullable String defaultLang, @NotNull List<String
}
throw e;
}
return Arrays.stream(chatCompletion.getChoices().get(0).getMessage().getContent().split("~")).collect(Collectors.toList());
String result = chatCompletion.getChoices().get(0).getMessage().getContent();
List<String> output = Arrays.stream(result.split("~")).collect(Collectors.toList());
Assert.isTrue(output.size() == original.size(), "Size not equal " + joined + " " + result);
return output;
}

@Override
Expand Down

0 comments on commit f456970

Please sign in to comment.