From 2a000bd9be144bff341fcb1546e823a51b125102 Mon Sep 17 00:00:00 2001 From: "Dr. Hans-Peter Stoerr" Date: Wed, 5 Jun 2024 14:37:09 +0200 Subject: [PATCH 1/3] add ca configuration for using "high intelligence model" in translation, as well as add a switch to the poc ui app --- .../autotranslate/AutoTranslateCaConfig.java | 8 ++++++ .../autotranslate/AutoTranslateListModel.java | 6 +++++ .../AutoTranslateServiceImpl.java | 2 +- .../rollout/AutoTranslateLiveActionImpl.java | 5 ++++ .../AutoTranslateWorkflowProcess.java | 6 +++++ .../components/autotranslate/list/list.html | 14 ++++++++--- .../base/service/chat/GPTConfiguration.java | 25 ++++++++++++++++--- 7 files changed, 58 insertions(+), 8 deletions(-) diff --git a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateCaConfig.java b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateCaConfig.java index 1eeb6fc21..3ef1ae1b3 100644 --- a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateCaConfig.java +++ b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateCaConfig.java @@ -9,7 +9,15 @@ // is also added to Sling-ContextAware-Configuration-Classes bnd header in pom.xml public @interface AutoTranslateCaConfig { + enum RequiredModel { + HIGH_INTELLIGENCE_MODEL, + STANDARD_MODEL + } + @Property(label = "Additional Instructions", description = "Additional instructions for the automatic translation.") String additionalInstructions(); + @Property(label = "Model", description = "If set, this model will be used for translation. If not set, a default will be used.") + RequiredModel model(); + } diff --git a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateListModel.java b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateListModel.java index 230a2db71..6b7b0b844 100644 --- a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateListModel.java +++ b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateListModel.java @@ -60,6 +60,12 @@ public AutoTranslateService.TranslationRun createRun() throws LoginException, Pe String additionalInstructions = request.getParameter("additionalInstructions"); boolean breakInheritance = request.getParameter("breakInheritance") != null; GPTConfiguration configuration = configurationService.getGPTConfiguration(request.getResourceResolver(), path); + String translationmodel = request.getParameter("translationmodel"); + if ("standard".equals(translationmodel)) { + configuration = GPTConfiguration.STANDARD_INTELLIGENCE.merge(configuration); + } else if ("highintelligence".equals(translationmodel)) { + configuration = GPTConfiguration.HIGH_INTELLIGENCE.merge(configuration); + } AutoTranslateService.TranslationParameters parms = new AutoTranslateService.TranslationParameters(); parms.recursive = recursive; parms.translateWhenChanged = changed; diff --git a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateServiceImpl.java b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateServiceImpl.java index 5a0b58081..644487a91 100644 --- a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateServiceImpl.java +++ b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateServiceImpl.java @@ -222,7 +222,7 @@ public void execute(ResourceResolver callResourceResolver) { mergedConfiguration = new GPTConfiguration(null, null, null, translationParameters.additionalInstructions).merge(configuration); } - if (autoTranslateConfigService.isUseHighIntelligenceModel()) { + if (autoTranslateConfigService.isUseHighIntelligenceModel() && mergedConfiguration.isHighIntelligenceNeeded() == null) { mergedConfiguration = GPTConfiguration.HIGH_INTELLIGENCE.merge(mergedConfiguration); } for (TranslationPageImpl page : translatedPages) { diff --git a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/rollout/AutoTranslateLiveActionImpl.java b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/rollout/AutoTranslateLiveActionImpl.java index ba0fc4d31..c0b4e7b80 100644 --- a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/rollout/AutoTranslateLiveActionImpl.java +++ b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/rollout/AutoTranslateLiveActionImpl.java @@ -72,6 +72,11 @@ protected void doExecute(Resource source, Resource target, LiveRelationship live ConfigurationBuilder confBuilder = Objects.requireNonNull(target.adaptTo(ConfigurationBuilder.class)); AutoTranslateCaConfig autoTranslateCaConfig = confBuilder.as(AutoTranslateCaConfig.class); parms.additionalInstructions = autoTranslateCaConfig.additionalInstructions(); + if (autoTranslateCaConfig != null && autoTranslateCaConfig.model() == AutoTranslateCaConfig.RequiredModel.HIGH_INTELLIGENCE_MODEL) { + config = GPTConfiguration.HIGH_INTELLIGENCE.merge(config, true); + } else if (autoTranslateCaConfig != null && autoTranslateCaConfig.model() == AutoTranslateCaConfig.RequiredModel.STANDARD_MODEL) { + config = GPTConfiguration.STANDARD_INTELLIGENCE.merge(config, true); + } // parms.translateWhenChanged probably only makes sense when differential translation is integrated. try { boolean duringLiveCopyCreation = liveRelationship.getStatus() == null || liveRelationship.getStatus().getLastRolledOut() == null; diff --git a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/workflow/AutoTranslateWorkflowProcess.java b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/workflow/AutoTranslateWorkflowProcess.java index ccb0be1a3..722f8da64 100644 --- a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/workflow/AutoTranslateWorkflowProcess.java +++ b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/workflow/AutoTranslateWorkflowProcess.java @@ -143,6 +143,12 @@ protected void translate(@Nonnull Resource resource, String processArguments) th try { GPTConfiguration config = configurationService.getGPTConfiguration(contentResource.getResourceResolver(), contentResource.getPath()); + if (autoTranslateCaConfig != null && autoTranslateCaConfig.model() == AutoTranslateCaConfig.RequiredModel.HIGH_INTELLIGENCE_MODEL) { + config = GPTConfiguration.HIGH_INTELLIGENCE.merge(config, true); + } else if (autoTranslateCaConfig != null && autoTranslateCaConfig.model() == AutoTranslateCaConfig.RequiredModel.STANDARD_MODEL) { + config = GPTConfiguration.STANDARD_INTELLIGENCE.merge(config, true); + } + if (parms.additionalInstructions != null) { config = GPTConfiguration.merge(config, new GPTConfiguration(null, null, null, parms.additionalInstructions)); diff --git a/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate/list/list.html b/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate/list/list.html index ae9a71248..bc42f946a 100644 --- a/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate/list/list.html +++ b/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate/list/list.html @@ -83,9 +83,17 @@ name="additionalInstructions" value="" labelledby="label-vertical-textarea-1"> - diff --git a/backend/base/src/main/java/com/composum/ai/backend/base/service/chat/GPTConfiguration.java b/backend/base/src/main/java/com/composum/ai/backend/base/service/chat/GPTConfiguration.java index de44b6913..bdbcdc288 100644 --- a/backend/base/src/main/java/com/composum/ai/backend/base/service/chat/GPTConfiguration.java +++ b/backend/base/src/main/java/com/composum/ai/backend/base/service/chat/GPTConfiguration.java @@ -119,22 +119,35 @@ public Boolean isHighIntelligenceNeeded() { /** * Creates a configuration that joins the values. * - * @throws IllegalArgumentException if values conflict + * @throws IllegalArgumentException if values conflict - that's checked for apiKey and organizationId and answerType. */ public GPTConfiguration merge(@Nullable GPTConfiguration other) throws IllegalArgumentException { + return merge(other, false); + } + + /** + * Creates a configuration that joins the values. + * + * @param override if true, values set in this configuration will override values set in the other configuration. + * Otherwise, a conflict will throw an exception. + * @param other the other configuration to merge with (optional) + * @throws IllegalArgumentException if values conflict + * @return a new configuration + */ + public GPTConfiguration merge(@Nullable GPTConfiguration other, boolean override) throws IllegalArgumentException { if (other == null) { return this; } String apiKey = this.apiKey != null ? this.apiKey : other.apiKey; - if (this.apiKey != null && other.apiKey != null && !this.apiKey.equals(other.apiKey)) { + if (!override && this.apiKey != null && other.apiKey != null && !this.apiKey.equals(other.apiKey)) { throw new IllegalArgumentException("Cannot merge conflicting API keys: " + this.apiKey + " vs. " + other.apiKey); } String organizationId = this.organizationId != null ? this.organizationId : other.organizationId; - if (this.organizationId != null && other.organizationId != null && !this.organizationId.equals(other.organizationId)) { + if (!override && this.organizationId != null && other.organizationId != null && !this.organizationId.equals(other.organizationId)) { throw new IllegalArgumentException("Cannot merge conflicting organization ids: " + this.organizationId + " vs. " + other.organizationId); } AnswerType answerType = this.answerType != null ? this.answerType : other.answerType; - if (this.answerType != null && other.answerType != null && !this.answerType.equals(other.answerType)) { + if (!override && this.answerType != null && other.answerType != null && !this.answerType.equals(other.answerType)) { throw new IllegalArgumentException("Cannot merge conflicting answer types: " + this.answerType + " vs. " + other.answerType); } String additionalInstructions = this.additionalInstructions == null ? other.additionalInstructions : @@ -193,6 +206,10 @@ public String toString() { * Requests slower and more expensive "high intelligence" model - use sparingly. */ public static final GPTConfiguration HIGH_INTELLIGENCE = new GPTConfiguration(null, null, null, null, null, true); + /** + * Requests faster and less expensive "normal intelligence" model. + */ + public static final GPTConfiguration STANDARD_INTELLIGENCE = new GPTConfiguration(null, null, null, null, null, false); @Override public boolean equals(Object o) { From 566fb61adbd8c683a97038cb4365008eaa62a20d Mon Sep 17 00:00:00 2001 From: "Dr. Hans-Peter Stoerr" Date: Wed, 5 Jun 2024 14:37:42 +0200 Subject: [PATCH 2/3] display errors when triggering a translation from poc ui --- .../components/autotranslate/list/create.jsp | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate/list/create.jsp b/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate/list/create.jsp index b84dbb262..3c6f42a13 100644 --- a/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate/list/create.jsp +++ b/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate/list/create.jsp @@ -3,8 +3,41 @@ <%@page session="false" pageEncoding="UTF-8" %> <%-- Redirects to page displaying the run --%> <% - SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request; - AutoTranslateListModel model = slingRequest.adaptTo(AutoTranslateListModel.class); - String id = model.createRun().id; - response.sendRedirect("run.html/" + id); + try { + SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request; + AutoTranslateListModel model = slingRequest.adaptTo(AutoTranslateListModel.class); + String id = model.createRun().id; + response.sendRedirect("run.html/" + id); + } catch (Exception e) { +%> + + + + + Error during translation + + + +
+
+
+
+
+

Error: <%= e.toString() %> +

+
+                        <%
+                            out.flush();
+                            e.printStackTrace(response.getWriter());
+                        %>
+                    
+
+
+
+
+
+ + +<% + } %> From 22f3fdf26cc0036eab87da223a09ea960708e644 Mon Sep 17 00:00:00 2001 From: "Dr. Hans-Peter Stoerr" Date: Wed, 5 Jun 2024 17:03:37 +0200 Subject: [PATCH 3/3] npe fix --- .../aem/core/impl/autotranslate/AutoTranslateServiceImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateServiceImpl.java b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateServiceImpl.java index 644487a91..7daff1985 100644 --- a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateServiceImpl.java +++ b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateServiceImpl.java @@ -222,7 +222,8 @@ public void execute(ResourceResolver callResourceResolver) { mergedConfiguration = new GPTConfiguration(null, null, null, translationParameters.additionalInstructions).merge(configuration); } - if (autoTranslateConfigService.isUseHighIntelligenceModel() && mergedConfiguration.isHighIntelligenceNeeded() == null) { + if (autoTranslateConfigService.isUseHighIntelligenceModel() && + (mergedConfiguration == null || mergedConfiguration.isHighIntelligenceNeeded() == null)) { mergedConfiguration = GPTConfiguration.HIGH_INTELLIGENCE.merge(mergedConfiguration); } for (TranslationPageImpl page : translatedPages) {