跳到主要内容

Spring AI

Easy LLM Tools provides integration with Spring AI.

To use this integration, add the Maven dependency to your project, replacing ${integration-spring-ai-version} with the latest version.

Latest version: Maven Central Version

Add Maven dependency
<dependency>
<groupId>com.javaaidev.easyllmtools</groupId>
<artifactId>integration-spring-ai</artifactId>
<version>${integration-spring-ai-version}</version>
</dependency>

The Spring AI integration library provides the class ToolsFunctionCallbackResolver to resolve tools, which is an implementation of Spring AI FunctionCallbackResolver. When creating FunctionCallbackResolver, DefaultFunctionCallbackResolver from Spring AI can be used as a fallback to resolve a tool.

Create ToolsFunctionCallbackResolver
@Bean
public ToolsFunctionCallbackResolver toolsFunctionCallbackResolver(
ApplicationContext applicationContext) {
DefaultFunctionCallbackResolver fallbackResolver = new DefaultFunctionCallbackResolver();
fallbackResolver.setApplicationContext(applicationContext);
return new ToolsFunctionCallbackResolver(fallbackResolver);
}

ToolsFunctionCallbackResolver finds all Tools in the application context.

Tools are created from factories and defined as Spring beans. So these tools can be resolved by ToolsFunctionCallbackResolver.

In the code below, the tool GetWeather is created using the factory GetWeatherFactory.

Create a tool and register as a Spring Bean
@Bean
public GetWeather getWeather() {
var config = new GetWeatherConfiguration();
config.setTemperatureUnit(TemperatureUnit.C);
return new GetWeatherFactory().create(config);
}

When sending requests to an LLM using Spring AI ChatClient, we can specify id of the tool to use by calling the functions method. GetWeather is used here as a sample tool.

Use a tool in ChatClient
@PostMapping("/getWeather")
public ChatOutput getWeather(@RequestBody ChatInput chatInput) {
var content = chatClient.prompt()
.user(chatInput.input())
.functions("GetWeather")
.call().content();
return new ChatOutput(content);
}

Below is the result of using this tool. Since the tool's implementation only returns fake weather data, the output of LLM is always the same.

Weather tool test