package org.jugph;

import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.UrlDocumentLoader;
import dev.langchain4j.data.document.transformer.HtmlTextExtractor;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.UserMessage;

import static java.time.Duration.ofSeconds;

public class JavaNewsRetrieverToolsExample {
    static class JavaNewsRetriever {

        @Tool("Retrieves the latest java news. Limit to the 3 latest news")
        String retrieveJavaNews() {
            Document javaNews = UrlDocumentLoader.load("https://dev.java/news/");
            Document transformedJavaNews = new HtmlTextExtractor(".container", null, true)
                    .transform(javaNews);

            return transformedJavaNews.text().replaceAll("\n", " ");
        }
    }

    interface Assistant {
        String chat(String userMessage);
    }

    interface NewsPrettierAssistant {

        @UserMessage("Given a jumbled java news {{it}}, summarize each, and list down them in numerical format, latest to oldest. " +
                "Include details such as url and date announced.")
        String prettify(String userMessage);
    }

    public static void main(String[] args) {
        var model = OpenAiChatModel.builder()
                .apiKey(System.getenv("OPENAI_API_KEY"))
                .timeout(ofSeconds(120))
                .build();

        var assistant = AiServices.builder(Assistant.class)
                .chatLanguageModel(model)
                .tools(new JavaNewsRetriever())
                .chatMemory(MessageWindowChatMemory.withMaxMessages(10))
                .build();

        var newsPrettierAssistant = AiServices.builder(NewsPrettierAssistant.class)
                .chatLanguageModel(model)
                .build();

        var question = "What are latest java news?";

        var answer = assistant.chat(question);

        var prettiedAnswer = newsPrettierAssistant.prettify(answer);

        System.out.println("\n=================================\n"+prettiedAnswer);
    }
}

dev.langchain4j.service.AiServices.builder dev.langchain4j.model.openai.OpenAiChatModel.builder
[((1405, 1553), 'dev.langchain4j.model.openai.OpenAiChatModel.builder'), ((1405, 1528), 'dev.langchain4j.model.openai.OpenAiChatModel.builder'), ((1405, 1487), 'dev.langchain4j.model.openai.OpenAiChatModel.builder'), ((1580, 1803), 'dev.langchain4j.service.AiServices.builder'), ((1580, 1778), 'dev.langchain4j.service.AiServices.builder'), ((1580, 1705), 'dev.langchain4j.service.AiServices.builder'), ((1580, 1657), 'dev.langchain4j.service.AiServices.builder'), ((1842, 1956), 'dev.langchain4j.service.AiServices.builder'), ((1842, 1931), 'dev.langchain4j.service.AiServices.builder')]