Skip to content

Commit cd0efe5

Browse files
committed
Add funcs to tools migration guide
1 parent d992600 commit cd0efe5

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
# Migrating from FunctionCallback to ToolCallback API
2+
3+
This guide helps you migrate from the deprecated FunctionCallback API to the new ToolCallback API in Spring AI.
4+
5+
## Overview of Changes
6+
7+
The Spring AI project is moving from "functions" to "tools" terminology to better align with industry standards. This involves several API changes while maintaining backward compatibility through deprecated methods.
8+
9+
## Key Changes
10+
11+
1. `FunctionCallback``ToolCallback`
12+
2. `FunctionCallback.builder().functions()``FunctionToolCallback.builder()`
13+
3. `FunctionCallback.builder().method()``MethodToolCallback.builder()`
14+
4. `FunctionCallingOptions``ToolCallingChatOptions`
15+
5. Method names from `functions()``tools()`
16+
17+
## Migration Examples
18+
19+
### 1. Basic Function Callback
20+
21+
Before:
22+
```java
23+
FunctionCallback.builder()
24+
.function("getCurrentWeather", new MockWeatherService())
25+
.description("Get the weather in location")
26+
.inputType(MockWeatherService.Request.class)
27+
.build()
28+
```
29+
30+
After:
31+
```java
32+
FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
33+
.description("Get the weather in location")
34+
.inputType(MockWeatherService.Request.class)
35+
.build()
36+
```
37+
38+
### 2. ChatClient Usage
39+
40+
Before:
41+
```java
42+
String response = ChatClient.create(chatModel)
43+
.prompt()
44+
.user("What's the weather like in San Francisco?")
45+
.functions(FunctionCallback.builder()
46+
.function("getCurrentWeather", new MockWeatherService())
47+
.description("Get the weather in location")
48+
.inputType(MockWeatherService.Request.class)
49+
.build())
50+
.call()
51+
.content();
52+
```
53+
54+
After:
55+
```java
56+
String response = ChatClient.create(chatModel)
57+
.prompt()
58+
.user("What's the weather like in San Francisco?")
59+
.tools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
60+
.description("Get the weather in location")
61+
.inputType(MockWeatherService.Request.class)
62+
.build())
63+
.call()
64+
.content();
65+
```
66+
67+
### 3. Method-Based Function Callbacks
68+
69+
Before:
70+
```java
71+
FunctionCallback.builder()
72+
.method("getWeatherInLocation", String.class, Unit.class)
73+
.description("Get the weather in location")
74+
.targetClass(TestFunctionClass.class)
75+
.build()
76+
```
77+
78+
After:
79+
```java
80+
var toolMethod = ReflectionUtils.findMethod(
81+
TestFunctionClass.class, "getWeatherInLocation", String.class, Unit.class);
82+
83+
MethodToolCallback.builder()
84+
.toolDefinition(ToolDefinition.builder(toolMethod)
85+
.description("Get the weather in location")
86+
.build())
87+
.toolMethod(toolMethod)
88+
.build()
89+
```
90+
91+
### 4. Options Configuration
92+
93+
Before:
94+
```java
95+
FunctionCallingOptions.builder()
96+
.model(modelName)
97+
.function("weatherFunction")
98+
.build()
99+
```
100+
101+
After:
102+
```java
103+
ToolCallingChatOptions.builder()
104+
.model(modelName)
105+
.tools("weatherFunction")
106+
.build()
107+
```
108+
109+
### 5. Default Functions in ChatClient Builder
110+
111+
Before:
112+
```java
113+
ChatClient.builder(chatModel)
114+
.defaultFunctions(FunctionCallback.builder()
115+
.function("getCurrentWeather", new MockWeatherService())
116+
.description("Get the weather in location")
117+
.inputType(MockWeatherService.Request.class)
118+
.build())
119+
.build()
120+
```
121+
122+
After:
123+
```java
124+
ChatClient.builder(chatModel)
125+
.defaultTools(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService())
126+
.description("Get the weather in location")
127+
.inputType(MockWeatherService.Request.class)
128+
.build())
129+
.build()
130+
```
131+
132+
### 6. Spring Bean Configuration
133+
134+
Before:
135+
```java
136+
@Bean
137+
public FunctionCallback weatherFunctionInfo() {
138+
return FunctionCallback.builder()
139+
.function("WeatherInfo", new MockWeatherService())
140+
.description("Get the current weather")
141+
.inputType(MockWeatherService.Request.class)
142+
.build();
143+
}
144+
```
145+
146+
After:
147+
```java
148+
@Bean
149+
public ToolCallback weatherFunctionInfo() {
150+
return FunctionToolCallback.builder("WeatherInfo", new MockWeatherService())
151+
.description("Get the current weather")
152+
.inputType(MockWeatherService.Request.class)
153+
.build();
154+
}
155+
```
156+
157+
## Breaking Changes
158+
159+
1. The `method()` configuration in function callbacks has been replaced with a more explicit method tool configuration using `ToolDefinition` and `MethodToolCallback`.
160+
161+
2. When using method-based callbacks, you now need to explicitly find the method using `ReflectionUtils` and provide it to the builder.
162+
163+
3. For non-static methods, you must now provide both the method and the target object:
164+
```java
165+
MethodToolCallback.builder()
166+
.toolDefinition(ToolDefinition.builder(toolMethod)
167+
.description("Description")
168+
.build())
169+
.toolMethod(toolMethod)
170+
.toolObject(targetObject)
171+
.build()
172+
```
173+
174+
## Deprecated Methods
175+
176+
The following methods are deprecated and will be removed in a future release:
177+
178+
- `ChatClient.Builder.defaultFunctions(String...)`
179+
- `ChatClient.Builder.defaultFunctions(FunctionCallback...)`
180+
- `ChatClient.RequestSpec.functions()`
181+
182+
Use their `tools` counterparts instead.
183+
184+
## Additional Notes
185+
186+
1. The new API provides better separation between tool definition and implementation.
187+
2. Tool definitions can be reused across different implementations.
188+
3. The builder pattern has been simplified for common use cases.
189+
4. Better support for method-based tools with improved error handling.
190+
191+
## Timeline
192+
193+
The deprecated methods will be maintained for backward compatibility in the current major version but will be removed in the next major release. It's recommended to migrate to the new API as soon as possible.

0 commit comments

Comments
 (0)