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
This commit is contained in:
Ilayaperumal Gopinathan
2024-12-19 16:43:15 +00:00
committed by Mark Pollack
parent a4acd8e4fa
commit bfc0b8285e
2 changed files with 21 additions and 16 deletions

View File

@@ -23,7 +23,15 @@ import java.util.List;
*/
public class DefaultChatOptionsBuilder<T extends DefaultChatOptionsBuilder<T>> implements ChatOptions.Builder<T> {
protected DefaultChatOptions options = new DefaultChatOptions();
protected DefaultChatOptions options;
public DefaultChatOptionsBuilder() {
this.options = new DefaultChatOptions();
}
protected DefaultChatOptionsBuilder(DefaultChatOptions options) {
this.options = options;
}
protected T self() {
return (T) this;

View File

@@ -36,62 +36,59 @@ public class DefaultFunctionCallingOptionsBuilder
extends DefaultChatOptionsBuilder<DefaultFunctionCallingOptionsBuilder>
implements FunctionCallingOptions.Builder<DefaultFunctionCallingOptionsBuilder> {
private final DefaultFunctionCallingOptions functionCallingOptions;
public DefaultFunctionCallingOptionsBuilder() {
this.functionCallingOptions = new DefaultFunctionCallingOptions();
// Set the options in the parent class to be the same instance
super.options = this.functionCallingOptions;
super(new DefaultFunctionCallingOptions());
}
public DefaultFunctionCallingOptionsBuilder functionCallbacks(List<FunctionCallback> functionCallbacks) {
this.functionCallingOptions.setFunctionCallbacks(functionCallbacks);
((FunctionCallingOptions) this.options).setFunctionCallbacks(functionCallbacks);
return self();
}
public DefaultFunctionCallingOptionsBuilder functionCallbacks(FunctionCallback... functionCallbacks) {
Assert.notNull(functionCallbacks, "FunctionCallbacks must not be null");
this.functionCallingOptions.setFunctionCallbacks(List.of(functionCallbacks));
((FunctionCallingOptions) this.options).setFunctionCallbacks(List.of(functionCallbacks));
return self();
}
public DefaultFunctionCallingOptionsBuilder functions(Set<String> functions) {
this.functionCallingOptions.setFunctions(functions);
((FunctionCallingOptions) this.options).setFunctions(functions);
return self();
}
public DefaultFunctionCallingOptionsBuilder function(String function) {
Assert.notNull(function, "Function must not be null");
var set = new HashSet<>(this.functionCallingOptions.getFunctions());
var set = new HashSet<>(((FunctionCallingOptions) this.options).getFunctions());
set.add(function);
this.functionCallingOptions.setFunctions(set);
((FunctionCallingOptions) this.options).setFunctions(set);
return self();
}
public DefaultFunctionCallingOptionsBuilder proxyToolCalls(Boolean proxyToolCalls) {
this.functionCallingOptions.setProxyToolCalls(proxyToolCalls);
((FunctionCallingOptions) this.options).setProxyToolCalls(proxyToolCalls);
return self();
}
public DefaultFunctionCallingOptionsBuilder toolContext(Map<String, Object> context) {
Assert.notNull(context, "Tool context must not be null");
Map<String, Object> newContext = new HashMap<>(this.functionCallingOptions.getToolContext());
Map<String, Object> newContext = new HashMap<>(((FunctionCallingOptions) this.options).getToolContext());
newContext.putAll(context);
this.functionCallingOptions.setToolContext(newContext);
((FunctionCallingOptions) this.options).setToolContext(newContext);
return self();
}
public DefaultFunctionCallingOptionsBuilder toolContext(String key, Object value) {
Assert.notNull(key, "Key must not be null");
Assert.notNull(value, "Value must not be null");
Map<String, Object> newContext = new HashMap<>(this.functionCallingOptions.getToolContext());
Map<String, Object> newContext = new HashMap<>(((FunctionCallingOptions) this.options).getToolContext());
newContext.put(key, value);
this.functionCallingOptions.setToolContext(newContext);
((FunctionCallingOptions) this.options).setToolContext(newContext);
return self();
}
public FunctionCallingOptions build() {
return this.functionCallingOptions;
return ((FunctionCallingOptions) this.options);
}
}