package com.kchandrakant;

import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;

public class ServiceWithTools {

    static class Calculator {

        @Tool("Calculates the length of a string")
        int stringLength(String s) {
            return s.length();
        }

        @Tool("Calculates the sum of two numbers")
        int add(int a, int b) {
            return a + b;
        }

        @Tool("Calculates the square root of a number")
        double sqrt(int x) {
            return Math.sqrt(x);
        }
    }

    interface Assistant {

        String chat(String userMessage);
    }

    public static void main(String[] args) {

        Assistant assistant = AiServices.builder(Assistant.class)
                .chatLanguageModel(OpenAiChatModel.withApiKey(ApiKeys.OPENAI_API_KEY))
                .tools(new Calculator())
                .chatMemory(MessageWindowChatMemory.withMaxMessages(10))
                .build();

        //String question = "What is the square root of the sum of the numbers of letters in the words \"hello\" and \"world\"?";

        String question = "What is the sum of the numbers of letters in the words \"language\" and \"model\"?";

        String answer = assistant.chat(question);

        System.out.println(answer);
        // The square root of the sum of the number of letters in the words "hello" and "world" is approximately 3.162.
    }
}

dev.langchain4j.service.AiServices.builder
[((824, 1085), 'dev.langchain4j.service.AiServices.builder'), ((824, 1060), 'dev.langchain4j.service.AiServices.builder'), ((824, 987), 'dev.langchain4j.service.AiServices.builder'), ((824, 946), 'dev.langchain4j.service.AiServices.builder')]