defaultTools() is a ChatClient builder method that registers tool objects for every prompt sent through the resulting client. Spring injects the tool, and annotated methods become available to the LLM. section 6.1.1
Use defaultTools() when tools should be consistently available:
- Inject a Spring-managed tool such as TimeTools or GameTools into the ChatClient configuration. section 6.1.1 section 6.2.3
- Pass the tool to defaultTools() before calling build(). Any @Tool-annotated methods are then available in client prompts. section 6.2.3
- Choose tools() instead when availability should be limited to an individual prompt. section 6.2.3
Listing 6.3 Applying tools when sending a prompt
package com.example.simpletools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GetTimeController {
private static final String CURRENT_TIME_TEMPLATE =
"What is the current time in {city}?";
private final ChatClient chatClient;
public GetTimeController(ChatClient.Builder chatClientBuilder,
TimeTools timeTools) { #1
this.chatClient = chatClientBuilder
.defaultTools(timeTools) #2
.build();
}
@GetMapping(path="/time", params = "city")
public String getTime(@RequestParam("city") String city) {
return chatClient.prompt()
.user(userSpec -> {
userSpec
.text(CURRENT_TIME_TEMPLATE)
.param("city", city);
})
.call()
.content();
}
}
Builds a ChatClient with TimeTools registered as a default tool. section 6.1.1
Current-time tool
A controller injects TimeTools, registers it with defaultTools(), and sends a city-based prompt. The LLM can then call getCurrentTime and return the specified local time. section 6.1.1
