|
|
|
|
@@ -1,91 +1,115 @@
|
|
|
|
|
package com.olympus.hermione.services;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.ai.chat.messages.Message;
|
|
|
|
|
import org.springframework.ai.chat.messages.SystemMessage;
|
|
|
|
|
import org.springframework.ai.chat.messages.UserMessage;
|
|
|
|
|
import org.springframework.ai.chat.model.ChatModel;
|
|
|
|
|
import org.springframework.ai.chat.model.Generation;
|
|
|
|
|
import org.springframework.ai.chat.prompt.Prompt;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import com.olympus.hermione.client.OlympusChatClient;
|
|
|
|
|
import com.olympus.hermione.client.AzureOpenApiChatClient;
|
|
|
|
|
import com.olympus.hermione.client.GoogleGeminiChatClient;
|
|
|
|
|
import com.olympus.hermione.client.OlympusChatClientResponse;
|
|
|
|
|
import com.olympus.hermione.dto.CanvasExecutionInput;
|
|
|
|
|
import com.olympus.hermione.dto.CanvasOutput;
|
|
|
|
|
import com.olympus.hermione.models.AiModel;
|
|
|
|
|
import com.olympus.hermione.repository.AiModelRepository;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class CanvasExecutionService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final ChatModel chatModel;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
public CanvasExecutionService(@Qualifier("openAiChatModel") ChatModel chatModel) {
|
|
|
|
|
this.chatModel = chatModel;
|
|
|
|
|
}
|
|
|
|
|
private AiModelRepository aiModelRepository;
|
|
|
|
|
|
|
|
|
|
private OlympusChatClient olympusChatClient = null;
|
|
|
|
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(CanvasExecutionService.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CanvasOutput askHermione(CanvasExecutionInput canvasExecutionInput){
|
|
|
|
|
CanvasOutput canvasOutput = new CanvasOutput();
|
|
|
|
|
String input = canvasExecutionInput.getInput();
|
|
|
|
|
if(input != null){
|
|
|
|
|
Message userMessage = new UserMessage(input);
|
|
|
|
|
Message systemMessage = new SystemMessage("Please answer the question accurately.");
|
|
|
|
|
Prompt prompt = new Prompt(List.of(userMessage,systemMessage));
|
|
|
|
|
List<Generation> response = chatModel.call(prompt).getResults();
|
|
|
|
|
|
|
|
|
|
canvasOutput.setStringOutput(response.get(0).getOutput().getText());
|
|
|
|
|
return canvasOutput;
|
|
|
|
|
} else{
|
|
|
|
|
logger.error("Input is not correct");
|
|
|
|
|
canvasOutput.setStringOutput(null);
|
|
|
|
|
public OlympusChatClient createCanvasClient(AiModelRepository aiModelRepository){
|
|
|
|
|
AiModel aiModel = aiModelRepository.findByIsCanvasDefault(true);
|
|
|
|
|
if (aiModel == null) {
|
|
|
|
|
logger.error("No default AI model found");
|
|
|
|
|
throw new IllegalArgumentException("No default AI model found");
|
|
|
|
|
}
|
|
|
|
|
return canvasOutput;
|
|
|
|
|
|
|
|
|
|
if (aiModel.getApiProvider().equals("AzureOpenAI")) {
|
|
|
|
|
olympusChatClient = new AzureOpenApiChatClient();
|
|
|
|
|
}
|
|
|
|
|
if (aiModel.getApiProvider().equals("GoogleGemini")) {
|
|
|
|
|
olympusChatClient = new GoogleGeminiChatClient();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
olympusChatClient.init(aiModel.getEndpoint(), aiModel.getApiKey(), aiModel.getMaxTokens());
|
|
|
|
|
|
|
|
|
|
return olympusChatClient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CanvasOutput askHermione(CanvasExecutionInput canvasExecutionInput){
|
|
|
|
|
try{
|
|
|
|
|
CanvasOutput canvasOutput = new CanvasOutput();
|
|
|
|
|
String input = canvasExecutionInput.getInput();
|
|
|
|
|
if(input != null){
|
|
|
|
|
olympusChatClient = createCanvasClient(aiModelRepository);
|
|
|
|
|
String userText = input;
|
|
|
|
|
String systemText = "Answer the following question using the language below using markdown: ";
|
|
|
|
|
OlympusChatClientResponse resp = olympusChatClient.getChatCompletion(systemText +"\n"+ userText);
|
|
|
|
|
|
|
|
|
|
canvasOutput.setStringOutput(resp.getContent());
|
|
|
|
|
return canvasOutput;
|
|
|
|
|
} else{
|
|
|
|
|
logger.error("Input is not correct");
|
|
|
|
|
canvasOutput.setStringOutput(null);
|
|
|
|
|
}
|
|
|
|
|
return canvasOutput;
|
|
|
|
|
} catch (Exception e){
|
|
|
|
|
logger.error("Error in askHermione: " + e.getMessage());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CanvasOutput rephraseText(CanvasExecutionInput canvasExecutionInput) {
|
|
|
|
|
CanvasOutput canvasOutput = new CanvasOutput();
|
|
|
|
|
String input = canvasExecutionInput.getInput();
|
|
|
|
|
if(input != null){
|
|
|
|
|
Message userMessage = new UserMessage(input);
|
|
|
|
|
Message systemMessage = new SystemMessage("Please rephrase the following text: ");
|
|
|
|
|
Prompt prompt = new Prompt(List.of(userMessage,systemMessage));
|
|
|
|
|
List<Generation> response = chatModel.call(prompt).getResults();
|
|
|
|
|
|
|
|
|
|
canvasOutput.setStringOutput(response.get(0).getOutput().getText());
|
|
|
|
|
try{
|
|
|
|
|
CanvasOutput canvasOutput = new CanvasOutput();
|
|
|
|
|
String input = canvasExecutionInput.getInput();
|
|
|
|
|
if(input != null){
|
|
|
|
|
olympusChatClient = createCanvasClient(aiModelRepository);
|
|
|
|
|
String userText = input;
|
|
|
|
|
String systemText = "Rephrase the following text using the language below using markdown: ";
|
|
|
|
|
OlympusChatClientResponse resp = olympusChatClient.getChatCompletion(systemText+"\n"+userText);
|
|
|
|
|
|
|
|
|
|
canvasOutput.setStringOutput(resp.getContent());
|
|
|
|
|
return canvasOutput;
|
|
|
|
|
} else{
|
|
|
|
|
logger.error("Input is not correct");
|
|
|
|
|
canvasOutput.setStringOutput(null);
|
|
|
|
|
}
|
|
|
|
|
return canvasOutput;
|
|
|
|
|
} else{
|
|
|
|
|
logger.error("Input is not correct");
|
|
|
|
|
canvasOutput.setStringOutput(null);
|
|
|
|
|
} catch (Exception e){
|
|
|
|
|
logger.error("Error in rephraseText: " + e.getMessage());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return canvasOutput;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CanvasOutput summarizeText(CanvasExecutionInput canvasExecutionInput) {
|
|
|
|
|
CanvasOutput canvasOutput = new CanvasOutput();
|
|
|
|
|
String input = canvasExecutionInput.getInput();
|
|
|
|
|
if(input != null){
|
|
|
|
|
Message userMessage = new UserMessage(input);
|
|
|
|
|
Message systemMessage = new SystemMessage("Please summarize the following text: ");
|
|
|
|
|
Prompt prompt = new Prompt(List.of(userMessage,systemMessage));
|
|
|
|
|
List<Generation> response = chatModel.call(prompt).getResults();
|
|
|
|
|
|
|
|
|
|
canvasOutput.setStringOutput(response.get(0).getOutput().getText());
|
|
|
|
|
try{
|
|
|
|
|
CanvasOutput canvasOutput = new CanvasOutput();
|
|
|
|
|
String input = canvasExecutionInput.getInput();
|
|
|
|
|
if(input != null){
|
|
|
|
|
olympusChatClient = createCanvasClient(aiModelRepository);
|
|
|
|
|
String userText = input;
|
|
|
|
|
String systemText = "Summarize the following text using the language below using markdown: ";
|
|
|
|
|
OlympusChatClientResponse resp = olympusChatClient.getChatCompletion(systemText+"\n"+userText);
|
|
|
|
|
|
|
|
|
|
canvasOutput.setStringOutput(resp.getContent());
|
|
|
|
|
return canvasOutput;
|
|
|
|
|
} else{
|
|
|
|
|
logger.error("Input is not correct");
|
|
|
|
|
canvasOutput.setStringOutput(null);
|
|
|
|
|
}
|
|
|
|
|
return canvasOutput;
|
|
|
|
|
} else{
|
|
|
|
|
logger.error("Input is not correct");
|
|
|
|
|
canvasOutput.setStringOutput(null);
|
|
|
|
|
} catch (Exception e){
|
|
|
|
|
logger.error("Error in summarizeText: " + e.getMessage());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return canvasOutput;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|