diff --git a/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/AiServiceWithExplicitTools.java b/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/AiServiceWithExplicitTools.java new file mode 100644 index 00000000..d9e17802 --- /dev/null +++ b/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/AiServiceWithExplicitTools.java @@ -0,0 +1,11 @@ +package dev.langchain4j.service.spring.mode.explicit.tools; + +import dev.langchain4j.service.spring.AiService; + +import static dev.langchain4j.service.spring.AiServiceWiringMode.EXPLICIT; + +@AiService(wiringMode = EXPLICIT, chatModel = "openAiChatModel", tools = {"tools1"}) +public interface AiServiceWithExplicitTools { + + String chat(String userMessage); +} diff --git a/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/AiServiceWithExplicitToolsApplication.java b/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/AiServiceWithExplicitToolsApplication.java new file mode 100644 index 00000000..5905e85a --- /dev/null +++ b/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/AiServiceWithExplicitToolsApplication.java @@ -0,0 +1,12 @@ +package dev.langchain4j.service.spring.mode.explicit.tools; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +class AiServiceWithExplicitToolsApplication { + + public static void main(String[] args) { + SpringApplication.run(AiServiceWithExplicitToolsApplication.class, args); + } +} diff --git a/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/AiServiceWithExplicitToolsIT.java b/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/AiServiceWithExplicitToolsIT.java new file mode 100644 index 00000000..c2e987df --- /dev/null +++ b/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/AiServiceWithExplicitToolsIT.java @@ -0,0 +1,36 @@ +package dev.langchain4j.service.spring.mode.explicit.tools; + +import dev.langchain4j.service.spring.AiServicesAutoConfig; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static dev.langchain4j.service.spring.mode.explicit.tools.Tools1.TOOL_1_TEMPERATURE; +import static dev.langchain4j.service.spring.mode.explicit.tools.Tools2.TOOL_2_TEMPERATURE; +import static org.assertj.core.api.Assertions.assertThat; + +class AiServiceWithExplicitToolsIT { + + ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class)); + + @Test + void should_create_AI_service_with_explicit_tools() { + contextRunner + .withPropertyValues( + "langchain4j.open-ai.chat-model.api-key=" + System.getenv("OPENAI_API_KEY"), + "langchain4j.open-ai.chat-model.max-tokens=100", + "langchain4j.open-ai.chat-model.temperature=0.0") + .withUserConfiguration(AiServiceWithExplicitToolsApplication.class) + .run(context -> { + + // given + AiServiceWithExplicitTools aiService = context.getBean(AiServiceWithExplicitTools.class); + + // when + String answer = aiService.chat("What is the temperature?"); + + // then + assertThat(answer).contains(TOOL_1_TEMPERATURE).doesNotContain(TOOL_2_TEMPERATURE); + }); + } +} \ No newline at end of file diff --git a/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/Tools1.java b/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/Tools1.java new file mode 100644 index 00000000..21667338 --- /dev/null +++ b/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/Tools1.java @@ -0,0 +1,15 @@ +package dev.langchain4j.service.spring.mode.explicit.tools; + +import dev.langchain4j.agent.tool.Tool; +import org.springframework.stereotype.Component; + +@Component +class Tools1 { + + public static final String TOOL_1_TEMPERATURE = "6"; + + @Tool + String getCurrentTemperature() { + return TOOL_1_TEMPERATURE; + } +} diff --git a/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/Tools2.java b/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/Tools2.java new file mode 100644 index 00000000..310848d6 --- /dev/null +++ b/langchain4j-spring-boot-starter/src/test/java/dev/langchain4j/service/spring/mode/explicit/tools/Tools2.java @@ -0,0 +1,15 @@ +package dev.langchain4j.service.spring.mode.explicit.tools; + +import dev.langchain4j.agent.tool.Tool; +import org.springframework.stereotype.Component; + +@Component +class Tools2 { + + public static final String TOOL_2_TEMPERATURE = "9"; + + @Tool + String getCurrentTemperature() { + return TOOL_2_TEMPERATURE; + } +}