Skip to content

Commit bfc0b82

Browse files
ilayaperumalgmarkpollack
authored andcommitted
Fix DefaultFunctionCallingOptionsBuilder inheritance
Removes duplicate state by reusing parent's options field for both function calling and chat options. This simplifies the inheritance structure and prevents potential state inconsistencies. - Remove functionCallingOptions field from DefaultFunctionCallingOptionsBuilder - Set parent class options with FunctionCallingOptions in child constructor - Add protected constructor in DefaultChatOptionsBuilder for inheritance - Initialize options through constructors instead of direct assignment
1 parent a4acd8e commit bfc0b82

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/DefaultChatOptionsBuilder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@
2323
*/
2424
public class DefaultChatOptionsBuilder<T extends DefaultChatOptionsBuilder<T>> implements ChatOptions.Builder<T> {
2525

26-
protected DefaultChatOptions options = new DefaultChatOptions();
26+
protected DefaultChatOptions options;
27+
28+
public DefaultChatOptionsBuilder() {
29+
this.options = new DefaultChatOptions();
30+
}
31+
32+
protected DefaultChatOptionsBuilder(DefaultChatOptions options) {
33+
this.options = options;
34+
}
2735

2836
protected T self() {
2937
return (T) this;

spring-ai-core/src/main/java/org/springframework/ai/model/function/DefaultFunctionCallingOptionsBuilder.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,62 +36,59 @@ public class DefaultFunctionCallingOptionsBuilder
3636
extends DefaultChatOptionsBuilder<DefaultFunctionCallingOptionsBuilder>
3737
implements FunctionCallingOptions.Builder<DefaultFunctionCallingOptionsBuilder> {
3838

39-
private final DefaultFunctionCallingOptions functionCallingOptions;
40-
4139
public DefaultFunctionCallingOptionsBuilder() {
42-
this.functionCallingOptions = new DefaultFunctionCallingOptions();
4340
// Set the options in the parent class to be the same instance
44-
super.options = this.functionCallingOptions;
41+
super(new DefaultFunctionCallingOptions());
4542
}
4643

4744
public DefaultFunctionCallingOptionsBuilder functionCallbacks(List<FunctionCallback> functionCallbacks) {
48-
this.functionCallingOptions.setFunctionCallbacks(functionCallbacks);
45+
((FunctionCallingOptions) this.options).setFunctionCallbacks(functionCallbacks);
4946
return self();
5047
}
5148

5249
public DefaultFunctionCallingOptionsBuilder functionCallbacks(FunctionCallback... functionCallbacks) {
5350
Assert.notNull(functionCallbacks, "FunctionCallbacks must not be null");
54-
this.functionCallingOptions.setFunctionCallbacks(List.of(functionCallbacks));
51+
((FunctionCallingOptions) this.options).setFunctionCallbacks(List.of(functionCallbacks));
5552
return self();
5653
}
5754

5855
public DefaultFunctionCallingOptionsBuilder functions(Set<String> functions) {
59-
this.functionCallingOptions.setFunctions(functions);
56+
((FunctionCallingOptions) this.options).setFunctions(functions);
6057
return self();
6158
}
6259

6360
public DefaultFunctionCallingOptionsBuilder function(String function) {
6461
Assert.notNull(function, "Function must not be null");
65-
var set = new HashSet<>(this.functionCallingOptions.getFunctions());
62+
var set = new HashSet<>(((FunctionCallingOptions) this.options).getFunctions());
6663
set.add(function);
67-
this.functionCallingOptions.setFunctions(set);
64+
((FunctionCallingOptions) this.options).setFunctions(set);
6865
return self();
6966
}
7067

7168
public DefaultFunctionCallingOptionsBuilder proxyToolCalls(Boolean proxyToolCalls) {
72-
this.functionCallingOptions.setProxyToolCalls(proxyToolCalls);
69+
((FunctionCallingOptions) this.options).setProxyToolCalls(proxyToolCalls);
7370
return self();
7471
}
7572

7673
public DefaultFunctionCallingOptionsBuilder toolContext(Map<String, Object> context) {
7774
Assert.notNull(context, "Tool context must not be null");
78-
Map<String, Object> newContext = new HashMap<>(this.functionCallingOptions.getToolContext());
75+
Map<String, Object> newContext = new HashMap<>(((FunctionCallingOptions) this.options).getToolContext());
7976
newContext.putAll(context);
80-
this.functionCallingOptions.setToolContext(newContext);
77+
((FunctionCallingOptions) this.options).setToolContext(newContext);
8178
return self();
8279
}
8380

8481
public DefaultFunctionCallingOptionsBuilder toolContext(String key, Object value) {
8582
Assert.notNull(key, "Key must not be null");
8683
Assert.notNull(value, "Value must not be null");
87-
Map<String, Object> newContext = new HashMap<>(this.functionCallingOptions.getToolContext());
84+
Map<String, Object> newContext = new HashMap<>(((FunctionCallingOptions) this.options).getToolContext());
8885
newContext.put(key, value);
89-
this.functionCallingOptions.setToolContext(newContext);
86+
((FunctionCallingOptions) this.options).setToolContext(newContext);
9087
return self();
9188
}
9289

9390
public FunctionCallingOptions build() {
94-
return this.functionCallingOptions;
91+
return ((FunctionCallingOptions) this.options);
9592
}
9693

9794
}

0 commit comments

Comments
 (0)