generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 16
feat: support partial translation via apply_to array #768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
n-o-u-r-h-a-n
wants to merge
21
commits into
main
Choose a base branch
from
support-partial-translation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9614895
full implementation
n-o-u-r-h-a-n 049ef67
Merge branch 'main' into support-partial-translation
n-o-u-r-h-a-n 3aec367
release notes
n-o-u-r-h-a-n a429f37
Update sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/servic…
n-o-u-r-h-a-n 46230db
Formatting
bot-sdk-js fdf7a5e
reverting
n-o-u-r-h-a-n 5b8e536
Formatting
bot-sdk-js 9a5a200
refactoring the new class name
n-o-u-r-h-a-n 8d61683
Merge remote-tracking branch 'origin/support-partial-translation' int…
n-o-u-r-h-a-n d3cf79d
Formatting
bot-sdk-js dc21aea
reverting a comment
n-o-u-r-h-a-n 212e506
Merge remote-tracking branch 'origin/support-partial-translation' int…
n-o-u-r-h-a-n 0ecab09
Merge branch 'main' into support-partial-translation
n-o-u-r-h-a-n 0fb1bc3
fixed test + reduced methods
n-o-u-r-h-a-n 08e8d82
Merge remote-tracking branch 'origin/support-partial-translation' int…
n-o-u-r-h-a-n 20b4c27
Formatting
bot-sdk-js 7918494
further method reduction
n-o-u-r-h-a-n f2ed08d
Merge remote-tracking branch 'origin/support-partial-translation' int…
n-o-u-r-h-a-n 2d3ef5b
Formatting
bot-sdk-js e6ebe2f
updating constructors
n-o-u-r-h-a-n bb9f590
Merge remote-tracking branch 'origin/support-partial-translation' int…
n-o-u-r-h-a-n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
orchestration/src/main/java/com/sap/ai/sdk/orchestration/ApplyTo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package com.sap.ai.sdk.orchestration; | ||
|
|
||
| import static com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationApplyToSelector.CategoryEnum.PLACEHOLDERS; | ||
| import static com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationApplyToSelector.CategoryEnum.TEMPLATE_ROLES; | ||
|
|
||
| import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationApplyToSelector; | ||
| import java.util.Objects; | ||
| import java.util.stream.Stream; | ||
| import javax.annotation.Nonnull; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| /** | ||
| * Convenience builder for {@link SAPDocumentTranslationApplyToSelector}. | ||
| * | ||
| * <p>This avoids passing raw strings for template roles and keeps sample-code readable. | ||
| */ | ||
| public final class ApplyTo { | ||
| /** | ||
| * Supported values for {@code items[]} when {@code category=template_roles}. | ||
| * | ||
| * <p>These map to the roles used in prompt templates. | ||
| */ | ||
| @RequiredArgsConstructor | ||
| public enum TemplateRole { | ||
| /** Template role for user messages. */ | ||
| USER("user"), | ||
|
|
||
| /** Template role for system messages. */ | ||
| SYSTEM("system"), | ||
|
|
||
| /** Template role for assistant messages. */ | ||
| ASSISTANT("assistant"), | ||
|
|
||
| /** Template role for developer messages. */ | ||
| DEVELOPER("developer"), | ||
|
|
||
| /** Template role for tool messages. */ | ||
| TOOL("tool"); | ||
|
|
||
| @Getter private final String value; | ||
| } | ||
|
|
||
| /** | ||
| * Start an {@code apply_to} selector for placeholder names in {@code placeholder_values}. | ||
| * | ||
| * @param names The placeholder keys to translate. | ||
| * @return A selector with {@code category=placeholders} and the given items. | ||
| */ | ||
| @Nonnull | ||
| public static SAPDocumentTranslationApplyToSelector placeholders(@Nonnull final String... names) { | ||
| return SAPDocumentTranslationApplyToSelector.create().category(PLACEHOLDERS).items(names); | ||
| } | ||
|
|
||
| /** | ||
| * Start an {@code apply_to} selector for prompt template message roles. | ||
| * | ||
| * @param roles The template roles to translate. | ||
| * @return A selector with {@code category=template_roles} and the given items. | ||
| */ | ||
| @Nonnull | ||
| public static SAPDocumentTranslationApplyToSelector templateRoles( | ||
| @Nonnull final TemplateRole... roles) { | ||
| final var roleStrings = | ||
| Stream.of(roles).filter(Objects::nonNull).map(TemplateRole::getValue).toList(); | ||
|
|
||
| return SAPDocumentTranslationApplyToSelector.create() | ||
| .category(TEMPLATE_ROLES) | ||
| .items(roleStrings); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| package com.sap.ai.sdk.orchestration; | ||
|
|
||
| import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationApplyToSelector; | ||
| import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationInput; | ||
| import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationInputConfig; | ||
| import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationOutput; | ||
| import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationOutputConfig; | ||
| import com.sap.ai.sdk.orchestration.model.SAPDocumentTranslationOutputTargetLanguage; | ||
| import java.util.List; | ||
| import javax.annotation.Nonnull; | ||
| import lombok.AccessLevel; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
@@ -29,13 +31,21 @@ class Input implements TranslationConfig { | |
|
|
||
| @With String sourceLanguage; | ||
|
|
||
| Object ApplyTo; // Can be null | ||
| /** | ||
| * Optional selection(s) to translate. If empty or null, translation is applied to the whole | ||
| * user input. | ||
| */ | ||
| @With List<SAPDocumentTranslationApplyToSelector> applyTo; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather have this: .applyToPlaceholders("exam_type", "topic")
.applyToTemplateRoles(TemplateRole.ASSISTANT)than what we have currently: .withApplyTo(
List.of(
TranslationApplyToSelector.placeholders(List.of("exam_type", "topic")),
TranslationApplyToSelector.templateRoles(TemplateRole.ASSISTANT))) |
||
|
|
||
| @Nonnull | ||
| SAPDocumentTranslationInput createSAPDocumentTranslationInput() { | ||
| val translationType = SAPDocumentTranslationInput.TypeEnum.SAP_DOCUMENT_TRANSLATION; | ||
| val conf = | ||
| SAPDocumentTranslationInputConfig.create().targetLanguage(targetLanguage).applyTo(null); | ||
| final var conf = SAPDocumentTranslationInputConfig.create().targetLanguage(targetLanguage); | ||
|
|
||
| if (applyTo != null && !applyTo.isEmpty()) { | ||
| conf.applyTo(applyTo); | ||
| } | ||
|
|
||
| return SAPDocumentTranslationInput.create().type(translationType).config(conf); | ||
| } | ||
| } | ||
|
|
@@ -71,7 +81,6 @@ SAPDocumentTranslationOutput createSAPDocumentTranslationOutput() { | |
| */ | ||
| @Nonnull | ||
| static TranslationConfig.Input translateInputTo(@Nonnull final String targetLanguage) { | ||
|
|
||
| return new TranslationConfig.Input(targetLanguage, null, null); | ||
| } | ||
|
|
||
|
|
@@ -86,7 +95,6 @@ static TranslationConfig.Input translateInputTo(@Nonnull final String targetLang | |
| */ | ||
| @Nonnull | ||
| static TranslationConfig.Output translateOutputTo(@Nonnull final String targetLanguage) { | ||
|
|
||
| return new TranslationConfig.Output(targetLanguage, null); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mistake from your old PR:
sourceLanguage isn't being used and applied.
You should fix it and improve the unit test to make sure it is being used and applied.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used in OrchestrationService class and all other test classes.What do you mean exactly?