Skip to content
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

Make automatic translation model switchable #95

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ public void execute(ResourceResolver callResourceResolver) {
mergedConfiguration = new GPTConfiguration(null, null, null,
translationParameters.additionalInstructions).merge(configuration);
}
if (autoTranslateConfigService.isUseHighIntelligenceModel()) {
if (autoTranslateConfigService.isUseHighIntelligenceModel() &&
(mergedConfiguration == null || mergedConfiguration.isHighIntelligenceNeeded() == null)) {
mergedConfiguration = GPTConfiguration.HIGH_INTELLIGENCE.merge(mergedConfiguration);
}
for (TranslationPageImpl page : translatedPages) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Error during translation</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body class="coral--light foundation-layout-util-maximized-alt">
<div class="foundation-layout-panel">
<div class="foundation-layout-panel-bodywrapper spectrum-ready">
<div class="foundation-layout-panel-body">
<div class="foundation-layout-panel-content foundation-layout-form foundation-layout-form-mode-edit">
<div class="cq-dialog-content-page">
<p>Error: <%= e.toString() %>
</p>
<pre>
<%
out.flush();
e.printStackTrace(response.getWriter());
%>
</pre>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<%
}
%>
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,17 @@
name="additionalInstructions" value=""
labelledby="label-vertical-textarea-1"></textarea>
</div>
<button type="submit" class="coral-Button coral-Button--primary">Start
Translation
Process
<div class="coral-Form-fieldwrapper">
<label class="coral-Form-fieldlabel" for="selectmodel">Model</label>
<coral-select class="coral-Form-field _coral-Dropdown" name="translationmodel"
placeholder="" variant="default">
<coral-select-item value="default" selected="">default</coral-select-item>
<coral-select-item value="standard">Standard</coral-select-item>
<coral-select-item value="highintelligence">High</coral-select-item>
</coral-select>
</div>
<button type="submit" class="coral-Button coral-Button--primary">
Start Translation Process
</button>
</div>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down Expand Up @@ -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) {
Expand Down
Loading