Skip to content
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 @@ -78,6 +78,10 @@ public AIAssistant createWithPrompt(

SystemMessage systemPrompt = systemPromptProvider.getSystemPrompt(systemPrompts);
aiAssistant.setSystemPrompt(systemPrompt);
String locale = assistantConfig.getLanguage();
if (locale != null) {
aiAssistant.setSystemPrompt(systemPromptProvider.getLanguagePrompt(locale));
}
return aiAssistant;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ public class AIAssistantConfig implements AIAssistantConfigProvider {
*/
private final Map<String, String> credentials;

private final String language;
/**
* Platform extra configs are put here
*/
private final Map<String, String> configs;

private AIAssistantConfig(String model, Map<String, String> credentials, Map<String, String> configMap) {
private AIAssistantConfig(
String model, Map<String, String> credentials, String language, Map<String, String> configMap) {
this.model = model;
this.credentials = credentials;
this.language = language;
this.configs = configMap;
}

Expand All @@ -65,8 +68,14 @@ public Map<String, String> getConfigs() {
return configs;
}

@Override
public String getLanguage() {
return language;
}

public static class Builder {
private String model;
private String language;

private final Map<String, String> credentials = new HashMap<>();

Expand All @@ -89,6 +98,11 @@ public Builder addCredentials(Map<String, String> credentialMap) {
return this;
}

public Builder setLanguage(String language) {
this.language = language;
return this;
}

public Builder addConfig(String key, String value) {
configs.put(key, value);
return this;
Expand All @@ -100,7 +114,7 @@ public Builder addConfigs(Map<String, String> configMap) {
}

public AIAssistantConfig build() {
return new AIAssistantConfig(model, credentials, configs);
return new AIAssistantConfig(model, credentials, language, configs);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Objects;

@Slf4j
public class LocSystemPromptProvider implements SystemPromptProvider {

private static final String SYSTEM_PROMPT_PATH = "src/main/resources/";

@Override
public SystemMessage getSystemPrompt(SystemPrompt systemPrompt) {
if (systemPrompt == SystemPrompt.DEFAULT_PROMPT) {
Expand All @@ -50,17 +49,38 @@ public SystemMessage getSystemPrompt() {
return getSystemPrompt(SystemPrompt.DEFAULT_PROMPT);
}

private SystemMessage loadPromptFromFile(String fileName) {
final String filePath = SYSTEM_PROMPT_PATH + fileName + ".st";
private String loadTextFromFile(String fileName) {
try {
File file = ResourceUtils.getFile(filePath);
String text = Files.readString(file.toPath(), StandardCharsets.UTF_8);
return SystemMessage.from(text);
} catch (IOException e) {
String fullPath = Objects.requireNonNull(
this.getClass().getClassLoader().getResource(fileName))
.toString();
File file = ResourceUtils.getFile(fullPath);
return Files.readString(file.toPath(), StandardCharsets.UTF_8);
} catch (IOException | NullPointerException e) {
log.error(
"Exception occurred while loading SystemPrompt from local. Here is some information:{}",
e.getMessage());
return null;
}
}

private SystemMessage loadPromptFromFile(String fileName) {
final String filePath = fileName + ".st";
String text = loadTextFromFile(filePath);
if (text == null) {
return SystemMessage.from("You are a helpful assistant.");
} else {
return SystemMessage.from(text);
}
}

public SystemMessage getLanguagePrompt(String locale) {
final String filePath = SystemPrompt.LANGUAGE_PROMPT.getValue() + '-' + locale + ".st";
String text = loadTextFromFile(filePath);
if (text == null) {
return SystemMessage.from("Answer in " + locale);
} else {
return SystemMessage.from(text);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Please answer the question in English:
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
请使用中文回答问题:
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public interface AIAssistantConfigProvider {
Map<String, String> getCredentials();

Map<String, String> getConfigs();

String getLanguage();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface SystemPromptProvider {

// return default system prompt
SystemMessage getSystemPrompt();

SystemMessage getLanguagePrompt(String locale);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;

public interface AIChatService {

List<PlatformVO> platforms();

List<PlatformAuthorizedVO> authorizedPlatforms();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.apache.bigtop.manager.server.service.AIChatService;

import org.jetbrains.annotations.NotNull;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

Expand Down Expand Up @@ -91,6 +92,7 @@ public AIAssistantFactory getAiAssistantFactory() {
private AIAssistantConfig getAIAssistantConfig(PlatformAuthorizedDTO platformAuthorizedDTO) {
return AIAssistantConfig.builder()
.setModel(platformAuthorizedDTO.getModel())
.setLanguage(LocaleContextHolder.getLocale().toString())
.addCredentials(platformAuthorizedDTO.getCredentials())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ CREATE TABLE `llm_chat_message`
`sender` VARCHAR(50) NOT NULL,
`create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
`update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`create_by` BIGINT DEFAULT NULL,
`update_by` BIGINT DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_thread_id` (`thread_id`),
KEY `idx_user_id` (`user_id`)
Expand Down