Skip to content

Commit

Permalink
Added CLI command to add/remove locale translation prompt overrides (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
maallen authored Nov 11, 2024
1 parent 8b9fb5a commit c446d2d
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.box.l10n.mojito.cli.command;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.box.l10n.mojito.rest.client.AIServiceClient;
import com.box.l10n.mojito.rest.entity.AITranslationLocalePromptOverridesRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@Scope("prototype")
@Parameters(
commandNames = {"ai-repository-locale-prompt-override"},
commandDescription =
"Create/Update/Delete locale translation AI prompt overrides for a given repository")
public class AIRepositoryLocaleOverrideCommand extends Command {

static Logger logger = LoggerFactory.getLogger(CreateAIPromptCommand.class);

@Autowired AIServiceClient aiServiceClient;

@Parameter(
names = {"--repository-name", "-r"},
required = true,
description = "Repository name")
String repository;

@Parameter(
names = {"--ai-prompt-id", "-aid"},
required = true,
description = "AI prompt id")
Long aiPromptId;

@Parameter(
names = {"--locales", "-l"},
required = true,
description = "Locale BCP-47 tags provided in a comma separated list")
String locales;

@Parameter(
names = {"--disabled"},
required = false,
description =
"Indicates if the locales are disabled for AI translation. Setting to false means AI translation will be skipped for the relevant locales. Default is false")
boolean disabled = false;

@Parameter(
names = {"--delete"},
required = false,
description = "Delete the AI prompt overrides for the given locales")
boolean isDelete = false;

@Override
protected void execute() throws CommandException {
AITranslationLocalePromptOverridesRequest aiTranslationLocalePromptOverridesRequest =
new AITranslationLocalePromptOverridesRequest(
repository, StringUtils.commaDelimitedListToSet(locales), aiPromptId, disabled);

if (isDelete) {
aiServiceClient.deleteRepositoryLocalePromptOverrides(
aiTranslationLocalePromptOverridesRequest);
} else {
aiServiceClient.createOrUpdateRepositoryLocalePromptOverrides(
aiTranslationLocalePromptOverridesRequest);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import com.box.l10n.mojito.rest.entity.AICheckResponse;
import com.box.l10n.mojito.rest.entity.AIPromptContextMessageCreateRequest;
import com.box.l10n.mojito.rest.entity.AIPromptCreateRequest;
import com.box.l10n.mojito.rest.entity.AITranslationLocalePromptOverridesRequest;
import com.box.l10n.mojito.rest.entity.OpenAIPrompt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

@Component
Expand Down Expand Up @@ -64,4 +68,25 @@ public void addPromptToRepository(Long promptId, String repositoryName, String p
null,
Void.class);
}

public void createOrUpdateRepositoryLocalePromptOverrides(
AITranslationLocalePromptOverridesRequest aiTranslationLocalePromptOverridesRequest) {
logger.debug("Received request to create or update repository locale prompt overrides");
authenticatedRestTemplate.postForObject(
getBasePathForEntity() + "/prompts/translation/locale/overrides",
aiTranslationLocalePromptOverridesRequest,
Void.class);
}

public void deleteRepositoryLocalePromptOverrides(
AITranslationLocalePromptOverridesRequest aiTranslationLocalePromptOverridesRequest) {
logger.debug("Received request to delete repository locale prompt overrides");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<AITranslationLocalePromptOverridesRequest> entity =
new HttpEntity<>(aiTranslationLocalePromptOverridesRequest, headers);
authenticatedRestTemplate.deleteForObject(
getBasePathForEntity() + "/prompts/translation/locale/overrides", entity, Void.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.box.l10n.mojito.rest.entity;

import java.util.Set;

public class AITranslationLocalePromptOverridesRequest {

private String repositoryName;
private Set<String> locales;
private Long aiPromptId;
private boolean disabled;

public AITranslationLocalePromptOverridesRequest(
String repositoryName, Set<String> locales, Long aiPromptId, boolean disabled) {
this.repositoryName = repositoryName;
this.locales = locales;
this.aiPromptId = aiPromptId;
this.disabled = disabled;
}

public String getRepositoryName() {
return repositoryName;
}

public void setRepositoryName(String repositoryName) {
this.repositoryName = repositoryName;
}

public Set<String> getLocales() {
return locales;
}

public void setLocales(Set<String> locales) {
this.locales = locales;
}

public Long getAiPromptId() {
return aiPromptId;
}

public void setAiPromptId(Long aiPromptId) {
this.aiPromptId = aiPromptId;
}

public boolean isDisabled() {
return disabled;
}

public void setDisabled(boolean disabled) {
this.disabled = disabled;
}
}
29 changes: 29 additions & 0 deletions webapp/src/main/java/com/box/l10n/mojito/rest/ai/AIPromptWS.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -91,6 +93,33 @@ public void deletePromptMessage(@PathVariable("context_message_id") Long context
promptService.deletePromptContextMessage(contextMessageId);
}

@RequestMapping(
value = "/api/ai/prompts/translation/locale/overrides",
method = RequestMethod.POST)
@Timed("AIWS.createOrUpdateRepositoryLocalePromptOverrides")
public ResponseEntity<String> createOrUpdateRepositoryLocalePromptOverrides(
@RequestBody AITranslationLocalePromptOverridesRequest request) {
logger.debug("Received request to create or update repository locale prompt overrides");
promptService.createOrUpdateRepositoryLocaleTranslationPromptOverrides(
request.getRepositoryName(),
request.getLocales(),
request.getAiPromptId(),
request.isDisabled());
return new ResponseEntity<>(HttpStatus.CREATED);
}

@RequestMapping(
value = "/api/ai/prompts/translation/locale/overrides",
method = RequestMethod.DELETE)
@Timed("AIWS.deleteRepositoryLocalePromptOverrides")
public ResponseEntity<String> deleteRepositoryLocalePromptOverrides(
@RequestBody AITranslationLocalePromptOverridesRequest request) {
logger.debug("Received request to delete repository locale prompt overrides");
promptService.deleteRepositoryLocaleTranslationPromptOverride(
request.getRepositoryName(), request.getLocales());
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

private static AIPrompt buildOpenAIPromptDTO(com.box.l10n.mojito.entity.AIPrompt prompt) {
AIPrompt AIPrompt = new AIPrompt();
AIPrompt.setSystemPrompt(prompt.getSystemPrompt());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.box.l10n.mojito.rest.ai;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.Set;

@JsonIgnoreProperties(ignoreUnknown = true)
public class AITranslationLocalePromptOverridesRequest {

private String repositoryName;
private Set<String> locales;
private Long aiPromptId;
private boolean disabled;

public String getRepositoryName() {
return repositoryName;
}

public void setRepositoryName(String repositoryName) {
this.repositoryName = repositoryName;
}

public Set<String> getLocales() {
return locales;
}

public void setLocales(Set<String> locales) {
this.locales = locales;
}

public Long getAiPromptId() {
return aiPromptId;
}

public void setAiPromptId(Long aiPromptId) {
this.aiPromptId = aiPromptId;
}

public boolean isDisabled() {
return disabled;
}

public void setDisabled(boolean disabled) {
this.disabled = disabled;
}
}
Loading

0 comments on commit c446d2d

Please sign in to comment.