|
|
@@ -1,5 +1,12 @@
|
|
|
package es.uv.saic.service;
|
|
|
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.jsoup.Jsoup;
|
|
|
+import org.jsoup.nodes.Document;
|
|
|
+import org.jsoup.nodes.Element;
|
|
|
import org.springframework.ai.chat.client.ChatClient;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@@ -7,15 +14,37 @@ import org.springframework.stereotype.Service;
|
|
|
public class EnhancementService {
|
|
|
|
|
|
private final ChatClient chatClient;
|
|
|
+ private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
public EnhancementService(ChatClient.Builder chatClientBuilder) {
|
|
|
this.chatClient = chatClientBuilder.build();
|
|
|
}
|
|
|
|
|
|
+ @SneakyThrows
|
|
|
public String ask(String message) {
|
|
|
return chatClient.prompt()
|
|
|
- .user(message)
|
|
|
+ .user(normalizeMessage(message))
|
|
|
.call()
|
|
|
.content();
|
|
|
}
|
|
|
+
|
|
|
+ private String normalizeMessage(String message) throws JsonProcessingException {
|
|
|
+ String cleanHtml = removeHtmlAttributes(message);
|
|
|
+
|
|
|
+ return objectMapper.writeValueAsString(cleanHtml);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String removeHtmlAttributes(String html) {
|
|
|
+ if (StringUtils.isEmpty(html)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ Document doc = Jsoup.parse(html);
|
|
|
+
|
|
|
+ for (Element el : doc.getAllElements()) {
|
|
|
+ el.clearAttributes();
|
|
|
+ }
|
|
|
+
|
|
|
+ return doc.body().html();
|
|
|
+ }
|
|
|
}
|