All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fi.evolver.ai.vaadin.util.ChatUtils Maven / Gradle / Ivy

The newest version!
package fi.evolver.ai.vaadin.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;

import fi.evolver.ai.spring.Model;
import fi.evolver.ai.spring.chat.ChatApi;
import fi.evolver.ai.spring.chat.prompt.ChatPrompt;
import fi.evolver.ai.spring.chat.prompt.Message;
import fi.evolver.ai.vaadin.entity.Chat;
import fi.evolver.ai.vaadin.entity.ChatMessage;
import fi.evolver.utils.DateUtils;
import fi.evolver.utils.NullSafetyUtils;

public class ChatUtils {
	private static final Parser PARSER = Parser.builder().build();
	private static final HtmlRenderer RENDERER = HtmlRenderer.builder().build();

	public enum CommandType {
		DEBUG,
		DEBUG_FULL,
		LOAD,
		EDIT_PROMPT,
		CONFIGURE
	}

	private ChatUtils() { /* Utility class */ }

	public static Instant convertToInstantFi(LocalDateTime ldt) {
		return ldt.atZone(DateUtils.ZONE_HELSINKI).toInstant();
	}

	public static Instant currentTimeHelsinki() {
		return convertToInstantFi(LocalDateTime.now());
	}

	public static String convertToHtml(String original) {
		return "
%s
".formatted(RENDERER.render(PARSER.parse(original))) .replaceAll("href=", "router-ignore=\"true\" href=") .replaceAll("

", "

"); } public static String inferUsername(Chat chat, ChatMessage message) { return switch (message.getRole()) { case ASSISTANT -> "AI"; case USER -> NullSafetyUtils.denull(chat.getDisplayName(), "User"); default -> "Unknown"; }; } public static Optional parseCommand(String text) { return text.startsWith("/") ? Arrays.asList(CommandType.values()).stream() .filter(c -> c.name().equalsIgnoreCase(text.substring(1))) .findFirst() : Optional.empty(); } public static ChatPrompt createSummaryPrompt(Model model, Optional provider, String userMessage, String assistantMessage) { ChatPrompt.Builder builder = ChatPrompt.builder(model) .add(Message.system("You are a summarization assistant that creates titles for chats. Your task is to summarize the following text in exactly 15 words. Make sure the summary is in the same language as the input text. \n Text: %s \n Summary:".formatted(userMessage))) .add(Message.user(userMessage)) .add(Message.assistant(assistantMessage)); provider.ifPresent(p -> builder.setParameter("provider", p)); return builder.build(); } public static ChatPrompt addHistoryToPrompt(ChatPrompt basePrompt, List chatMessages) { return addHistoryToPrompt(basePrompt, chatMessages, basePrompt.getIntProperty("history_count").orElse(3)); } public static ChatPrompt addHistoryToPrompt(ChatPrompt basePrompt, List chatMessages, int count) { ChatPrompt.Builder result = basePrompt.builder(); result.addAll(chatMessages.subList(Math.max(chatMessages.size() - count, 0), chatMessages.size())); return result.build(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy