Skip to content

Commit 091fca2

Browse files
committed
refactor(client): rename overloaded tools methods in prompt builder
Renames the ambiguous overloaded `tools` methods in `ChatClient.PromptRequestSpec` to `toolNames` and `toolCallbacks` respectively. This improves clarity and prevents potential issues with method dispatching based on argument types. Updates relevant code examples and adds documentation to upgrade notes. Signed-off-by: Mark Pollack <mark.pollack@broadcom.com>
1 parent ec95eeb commit 091fca2

File tree

23 files changed

+123
-116
lines changed

23 files changed

+123
-116
lines changed

auto-configurations/models/spring-ai-autoconfigure-model-openai/src/test/java/org/springframework/ai/model/openai/autoconfigure/tool/FunctionCallbackInPrompt2IT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void functionCallTest() {
6060

6161
String content = ChatClient.builder(chatModel).build().prompt()
6262
.user("What's the weather like in San Francisco, Tokyo, and Paris?")
63-
.tools(FunctionToolCallback
63+
.toolCallbacks(FunctionToolCallback
6464
.builder("CurrentWeatherService", new MockWeatherService())
6565
.description("Get the weather in location")
6666
.inputType(MockWeatherService.Request.class)
@@ -88,7 +88,7 @@ record LightInfo(String roomName, boolean isOn) {
8888
// @formatter:off
8989
String content = ChatClient.builder(chatModel).build().prompt()
9090
.user("Turn the light on in the kitchen and in the living room!")
91-
.tools(FunctionToolCallback
91+
.toolCallbacks(FunctionToolCallback
9292
.builder("turnLight", (LightInfo lightInfo) -> {
9393
logger.info("Turning light to [" + lightInfo.isOn + "] in " + lightInfo.roomName());
9494
state.put(lightInfo.roomName(), lightInfo.isOn());
@@ -114,7 +114,7 @@ void functionCallTest2() {
114114
// @formatter:off
115115
String content = ChatClient.builder(chatModel).build().prompt()
116116
.user("What's the weather like in Amsterdam?")
117-
.tools(FunctionToolCallback
117+
.toolCallbacks(FunctionToolCallback
118118
.builder("CurrentWeatherService", input -> "18 degrees Celsius")
119119
.description("Get the weather in location")
120120
.inputType(MockWeatherService.Request.class)
@@ -138,7 +138,7 @@ void streamingFunctionCallTest() {
138138
// @formatter:off
139139
String content = ChatClient.builder(chatModel).build().prompt()
140140
.user("What's the weather like in San Francisco, Tokyo, and Paris?")
141-
.tools(FunctionToolCallback
141+
.toolCallbacks(FunctionToolCallback
142142
.builder("CurrentWeatherService", new MockWeatherService())
143143
.description("Get the weather in location")
144144
.inputType(MockWeatherService.Request.class)

auto-configurations/models/spring-ai-autoconfigure-model-openai/src/test/java/org/springframework/ai/model/openai/autoconfigure/tool/FunctionCallbackWithPlainFunctionBeanIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void functionCallWithDirectBiFunction() {
174174
ChatClient chatClient = ChatClient.builder(chatModel).build();
175175

176176
String content = chatClient.prompt("What's the weather like in San Francisco, Tokyo, and Paris?")
177-
.tools("weatherFunctionWithContext")
177+
.toolNames("weatherFunctionWithContext")
178178
.toolContext(Map.of("sessionId", "123"))
179179
.call()
180180
.content();
@@ -206,7 +206,7 @@ void functionCallWithBiFunctionClass() {
206206
ChatClient chatClient = ChatClient.builder(chatModel).build();
207207

208208
String content = chatClient.prompt("What's the weather like in San Francisco, Tokyo, and Paris?")
209-
.tools("weatherFunctionWithClassBiFunction")
209+
.toolNames("weatherFunctionWithClassBiFunction")
210210
.toolContext(Map.of("sessionId", "123"))
211211
.call()
212212
.content();

auto-configurations/models/spring-ai-autoconfigure-model-openai/src/test/java/org/springframework/ai/model/openai/autoconfigure/tool/OpenAiFunctionCallback2IT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void functionCallTest() {
5555

5656
// @formatter:off
5757
ChatClient chatClient = ChatClient.builder(chatModel)
58-
.defaultTools("WeatherInfo")
58+
.defaultToolNames("WeatherInfo")
5959
.defaultUser(u -> u.text("What's the weather like in {cities}?"))
6060
.build();
6161

@@ -78,7 +78,7 @@ void streamFunctionCallTest() {
7878

7979
// @formatter:off
8080
String content = ChatClient.builder(chatModel).build().prompt()
81-
.tools("WeatherInfo")
81+
.toolNames("WeatherInfo")
8282
.user("What's the weather like in San Francisco, Tokyo, and Paris?")
8383
.stream().content()
8484
.collectList().block().stream().collect(Collectors.joining());

models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/client/AnthropicChatClientIT.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Map;
2424
import java.util.stream.Collectors;
2525

26-
import org.junit.jupiter.api.Disabled;
2726
import org.junit.jupiter.api.Test;
2827
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
2928
import org.junit.jupiter.params.ParameterizedTest;
@@ -213,7 +212,7 @@ void functionCallTest() {
213212
// @formatter:off
214213
String response = ChatClient.create(this.chatModel).prompt()
215214
.user("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius.")
216-
.tools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
215+
.toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
217216
.inputType(MockWeatherService.Request.class)
218217
.build())
219218
.call()
@@ -231,7 +230,7 @@ void functionCallWithGeneratedDescription() {
231230
// @formatter:off
232231
String response = ChatClient.create(this.chatModel).prompt()
233232
.user("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius.")
234-
.tools(FunctionToolCallback.builder("getCurrentWeatherInLocation", new MockWeatherService())
233+
.toolCallbacks(FunctionToolCallback.builder("getCurrentWeatherInLocation", new MockWeatherService())
235234
.inputType(MockWeatherService.Request.class)
236235
.build())
237236
.call()
@@ -248,7 +247,7 @@ void defaultFunctionCallTest() {
248247

249248
// @formatter:off
250249
String response = ChatClient.builder(this.chatModel)
251-
.defaultTools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
250+
.defaultToolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
252251
.description("Get the weather in location")
253252
.inputType(MockWeatherService.Request.class)
254253
.build())
@@ -270,7 +269,7 @@ void streamFunctionCallTest() {
270269
// @formatter:off
271270
Flux<String> response = ChatClient.create(this.chatModel).prompt()
272271
.user("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius.")
273-
.tools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
272+
.toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
274273
.description("Get the weather in location")
275274
.inputType(MockWeatherService.Request.class)
276275
.build())

models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/client/AnthropicChatClientMethodInvokingFunctionCallbackIT.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void methodGetWeatherGeneratedDescription() {
6767

6868
String response = ChatClient.create(this.chatModel).prompt()
6969
.user("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius.")
70-
.tools(MethodToolCallback.builder()
70+
.toolCallbacks(MethodToolCallback.builder()
7171
.toolDefinition(ToolDefinition.builder(toolMethod).build())
7272
.toolMethod(toolMethod)
7373
.build())
@@ -89,7 +89,7 @@ void methodGetWeatherStatic() {
8989

9090
String response = ChatClient.create(this.chatModel).prompt()
9191
.user("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius.")
92-
.tools(MethodToolCallback.builder()
92+
.toolCallbacks(MethodToolCallback.builder()
9393
.toolDefinition(ToolDefinition.builder(toolMethod)
9494
.description("Get the weather in location")
9595
.build())
@@ -116,7 +116,7 @@ void methodTurnLightNoResponse() {
116116

117117
String response = ChatClient.create(this.chatModel).prompt()
118118
.user("Turn light on in the living room.")
119-
.tools(MethodToolCallback.builder()
119+
.toolCallbacks(MethodToolCallback.builder()
120120
.toolDefinition(ToolDefinition.builder(turnLightMethod)
121121
.description("Turn light on in the living room.")
122122
.build())
@@ -144,7 +144,7 @@ void methodGetWeatherNonStatic() {
144144

145145
String response = ChatClient.create(this.chatModel).prompt()
146146
.user("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius.")
147-
.tools(MethodToolCallback.builder()
147+
.toolCallbacks(MethodToolCallback.builder()
148148
.toolDefinition(ToolDefinition.builder(toolMethod)
149149
.description("Get the weather in location")
150150
.build())
@@ -171,7 +171,7 @@ void methodGetWeatherToolContext() {
171171

172172
String response = ChatClient.create(this.chatModel).prompt()
173173
.user("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius.")
174-
.tools(MethodToolCallback.builder()
174+
.toolCallbacks(MethodToolCallback.builder()
175175
.toolDefinition(ToolDefinition.builder(toolMethod)
176176
.description("Get the weather in location")
177177
.build())
@@ -202,7 +202,7 @@ void methodGetWeatherWithContextMethodButMissingContext() {
202202

203203
assertThatThrownBy(() -> ChatClient.create(this.chatModel).prompt()
204204
.user("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius.")
205-
.tools(MethodToolCallback.builder()
205+
.toolCallbacks(MethodToolCallback.builder()
206206
.toolDefinition(ToolDefinition.builder(toolMethod)
207207
.description("Get the weather in location")
208208
.build())
@@ -227,7 +227,7 @@ void methodNoParameters() {
227227

228228
String response = ChatClient.create(this.chatModel).prompt()
229229
.user("Turn light on in the living room.")
230-
.tools(MethodToolCallback.builder()
230+
.toolCallbacks(MethodToolCallback.builder()
231231
.toolMethod(toolMethod)
232232
.toolDefinition(ToolDefinition.builder(toolMethod)
233233
.description("Can turn lights on in the Living Room")

models/spring-ai-bedrock-converse/src/test/java/org/springframework/ai/bedrock/converse/BedrockConverseChatClientIT.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void functionCallTest() {
212212
// @formatter:off
213213
String response = ChatClient.create(this.chatModel)
214214
.prompt("What's the weather like in San Francisco, Tokyo, and Paris? Return the temperature in Celsius.")
215-
.tools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
215+
.toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
216216
.description("Get the weather in location")
217217
.inputType(MockWeatherService.Request.class)
218218
.build())
@@ -231,7 +231,7 @@ void functionCallWithUsageMetadataTest() {
231231
// @formatter:off
232232
ChatResponse response = ChatClient.create(this.chatModel)
233233
.prompt("What's the weather like in San Francisco, Tokyo, and Paris? Return the temperature in Celsius.")
234-
.tools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
234+
.toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
235235
.description("Get the weather in location")
236236
.inputType(MockWeatherService.Request.class)
237237
.build())
@@ -265,7 +265,7 @@ void functionCallWithAdvisorTest() {
265265
// @formatter:off
266266
String response = ChatClient.create(this.chatModel)
267267
.prompt("What's the weather like in San Francisco, Tokyo, and Paris? Return the temperature in Celsius.")
268-
.tools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
268+
.toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
269269
.description("Get the weather in location")
270270
.inputType(MockWeatherService.Request.class)
271271
.build())
@@ -284,7 +284,7 @@ void defaultFunctionCallTest() {
284284

285285
// @formatter:off
286286
String response = ChatClient.builder(this.chatModel)
287-
.defaultTools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
287+
.defaultToolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
288288
.description("Get the weather in location")
289289
.inputType(MockWeatherService.Request.class)
290290
.build())
@@ -306,7 +306,7 @@ void streamFunctionCallTest() {
306306
// @formatter:off
307307
Flux<ChatResponse> response = ChatClient.create(this.chatModel).prompt()
308308
.user("What's the weather like in San Francisco, Tokyo, and Paris? Return the temperature in Celsius.")
309-
.tools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
309+
.toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
310310
.description("Get the weather in location")
311311
.inputType(MockWeatherService.Request.class)
312312
.build())
@@ -347,7 +347,7 @@ void singularStreamFunctionCallTest() {
347347
// @formatter:off
348348
Flux<String> response = ChatClient.create(this.chatModel).prompt()
349349
.user("What's the weather like in Paris? Return the temperature in Celsius.")
350-
.tools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
350+
.toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
351351
.description("Get the weather in location")
352352
.inputType(MockWeatherService.Request.class)
353353
.build())

models/spring-ai-bedrock-converse/src/test/java/org/springframework/ai/bedrock/converse/client/BedrockNovaChatClientIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void functionCallTest() {
141141
// @formatter:off
142142
String response = ChatClient.create(this.chatModel).prompt()
143143
.user("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius.")
144-
.tools(FunctionToolCallback.builder("getCurrentWeather", (WeatherRequest request) -> {
144+
.toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", (WeatherRequest request) -> {
145145
if (request.location().contains("Paris")) {
146146
return new WeatherResponse(15, request.unit());
147147
}

models/spring-ai-mistral-ai/src/test/java/org/springframework/ai/mistralai/MistralAiChatClientIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ void functionCallTest() {
228228
String response = ChatClient.create(this.chatModel).prompt()
229229
.options(MistralAiChatOptions.builder().model(MistralAiApi.ChatModel.SMALL).toolChoice(ToolChoice.AUTO).build())
230230
.user(u -> u.text("What's the weather like in San Francisco, Tokyo, and Paris? Use parallel function calling if required. Response should be in Celsius."))
231-
.tools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
231+
.toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
232232
.description("Get the weather in location")
233233
.inputType(MockWeatherService.Request.class)
234234
.build())
@@ -249,7 +249,7 @@ void defaultFunctionCallTest() {
249249
// @formatter:off
250250
String response = ChatClient.builder(this.chatModel)
251251
.defaultOptions(MistralAiChatOptions.builder().model(MistralAiApi.ChatModel.SMALL).build())
252-
.defaultTools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
252+
.defaultToolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
253253
.description("Get the weather in location")
254254
.inputType(MockWeatherService.Request.class)
255255
.build())
@@ -272,7 +272,7 @@ void streamFunctionCallTest() {
272272
Flux<String> response = ChatClient.create(this.chatModel).prompt()
273273
.options(MistralAiChatOptions.builder().model(MistralAiApi.ChatModel.SMALL).build())
274274
.user("What's the weather like in San Francisco, Tokyo, and Paris? Use parallel function calling if required. Response should be in Celsius.")
275-
.tools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
275+
.toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
276276
.description("Get the weather in location")
277277
.inputType(MockWeatherService.Request.class)
278278
.build())

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/OpenAiChatModelFunctionCallingIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void functionCallSupplier() {
6969
// @formatter:off
7070
String response = ChatClient.create(this.chatModel).prompt()
7171
.user("Turn the light on in the living room")
72-
.tools(FunctionToolCallback.builder("turnsLightOnInTheLivingRoom", () -> state.put("Light", "ON"))
72+
.toolCallbacks(FunctionToolCallback.builder("turnsLightOnInTheLivingRoom", () -> state.put("Light", "ON"))
7373
.build())
7474
.call()
7575
.content();

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/OpenAiPaymentTransactionIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class OpenAiPaymentTransactionIT {
8282
public void transactionPaymentStatuses(String functionName) {
8383
List<TransactionStatusResponse> content = this.chatClient.prompt()
8484
.advisors(new LoggingAdvisor())
85-
.tools(functionName)
85+
.toolNames(functionName)
8686
.user("""
8787
What is the status of my payment transactions 001, 002 and 003?
8888
""")
@@ -113,7 +113,7 @@ public void streamingPaymentStatuses(String functionName) {
113113

114114
Flux<String> flux = this.chatClient.prompt()
115115
.advisors(new LoggingAdvisor())
116-
.tools(functionName)
116+
.toolNames(functionName)
117117
.user(u -> u.text("""
118118
What is the status of my payment transactions 001, 002 and 003?
119119

0 commit comments

Comments
 (0)