Skip to content

Commit 92eac5f

Browse files
committed
Models updates
- Remove AbstractToolCallSupport from the models which use ToolCallingManager - Remove deprecated constructors and their usage - Remove FunctionCallbackResolver and FunctionCallbacks usage in the models Signed-off-by: Ilayaperumal Gopinathan <[email protected]>
1 parent 4338328 commit 92eac5f

File tree

23 files changed

+60
-1037
lines changed

23 files changed

+60
-1037
lines changed

models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/AnthropicChatModel.java

Lines changed: 6 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@
3030
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
3131
import org.slf4j.Logger;
3232
import org.slf4j.LoggerFactory;
33-
import org.springframework.ai.model.tool.LegacyToolCallingManager;
34-
import org.springframework.ai.model.tool.ToolCallingChatOptions;
35-
import org.springframework.ai.model.tool.ToolCallingManager;
36-
import org.springframework.ai.model.tool.ToolExecutionResult;
37-
import org.springframework.ai.tool.definition.ToolDefinition;
38-
import org.springframework.ai.util.json.JsonParser;
39-
import org.springframework.lang.Nullable;
4033
import reactor.core.publisher.Flux;
4134
import reactor.core.publisher.Mono;
4235

@@ -58,7 +51,6 @@
5851
import org.springframework.ai.chat.metadata.EmptyUsage;
5952
import org.springframework.ai.chat.metadata.Usage;
6053
import org.springframework.ai.chat.metadata.UsageUtils;
61-
import org.springframework.ai.chat.model.AbstractToolCallSupport;
6254
import org.springframework.ai.chat.model.ChatModel;
6355
import org.springframework.ai.chat.model.ChatResponse;
6456
import org.springframework.ai.chat.model.Generation;
@@ -71,10 +63,12 @@
7163
import org.springframework.ai.chat.prompt.Prompt;
7264
import org.springframework.ai.model.Media;
7365
import org.springframework.ai.model.ModelOptionsUtils;
74-
import org.springframework.ai.model.function.FunctionCallback;
75-
import org.springframework.ai.model.function.FunctionCallbackResolver;
76-
import org.springframework.ai.model.function.FunctionCallingOptions;
66+
import org.springframework.ai.model.tool.ToolCallingChatOptions;
67+
import org.springframework.ai.model.tool.ToolCallingManager;
68+
import org.springframework.ai.model.tool.ToolExecutionResult;
7769
import org.springframework.ai.retry.RetryUtils;
70+
import org.springframework.ai.tool.definition.ToolDefinition;
71+
import org.springframework.ai.util.json.JsonParser;
7872
import org.springframework.http.ResponseEntity;
7973
import org.springframework.retry.support.RetryTemplate;
8074
import org.springframework.util.Assert;
@@ -93,7 +87,7 @@
9387
* @author Alexandros Pappas
9488
* @since 1.0.0
9589
*/
96-
public class AnthropicChatModel extends AbstractToolCallSupport implements ChatModel {
90+
public class AnthropicChatModel implements ChatModel {
9791

9892
public static final String DEFAULT_MODEL_NAME = AnthropicApi.ChatModel.CLAUDE_3_7_SONNET.getValue();
9993

@@ -134,111 +128,9 @@ public class AnthropicChatModel extends AbstractToolCallSupport implements ChatM
134128
*/
135129
private ChatModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION;
136130

137-
/**
138-
* Construct a new {@link AnthropicChatModel} instance.
139-
* @param anthropicApi the lower-level API for the Anthropic service.
140-
* @deprecated Use {@link AnthropicChatModel.Builder}.
141-
*/
142-
@Deprecated
143-
public AnthropicChatModel(AnthropicApi anthropicApi) {
144-
this(anthropicApi,
145-
AnthropicChatOptions.builder()
146-
.model(DEFAULT_MODEL_NAME)
147-
.maxTokens(DEFAULT_MAX_TOKENS)
148-
.temperature(DEFAULT_TEMPERATURE)
149-
.build());
150-
}
151-
152-
/**
153-
* Construct a new {@link AnthropicChatModel} instance.
154-
* @param anthropicApi the lower-level API for the Anthropic service.
155-
* @param defaultOptions the default options used for the chat completion requests.
156-
* @deprecated Use {@link AnthropicChatModel.Builder}.
157-
*/
158-
@Deprecated
159-
public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaultOptions) {
160-
this(anthropicApi, defaultOptions, RetryUtils.DEFAULT_RETRY_TEMPLATE);
161-
}
162-
163-
/**
164-
* Construct a new {@link AnthropicChatModel} instance.
165-
* @param anthropicApi the lower-level API for the Anthropic service.
166-
* @param defaultOptions the default options used for the chat completion requests.
167-
* @param retryTemplate the retry template used to retry the Anthropic API calls.
168-
* @deprecated Use {@link AnthropicChatModel.Builder}.
169-
*/
170-
@Deprecated
171-
public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaultOptions,
172-
RetryTemplate retryTemplate) {
173-
this(anthropicApi, defaultOptions, retryTemplate, null);
174-
}
175-
176-
/**
177-
* Construct a new {@link AnthropicChatModel} instance.
178-
* @param anthropicApi the lower-level API for the Anthropic service.
179-
* @param defaultOptions the default options used for the chat completion requests.
180-
* @param retryTemplate the retry template used to retry the Anthropic API calls.
181-
* @param functionCallbackResolver the function callback resolver used to resolve the
182-
* function by its name.
183-
* @deprecated Use {@link AnthropicChatModel.Builder}.
184-
*/
185-
@Deprecated
186-
public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaultOptions,
187-
RetryTemplate retryTemplate, FunctionCallbackResolver functionCallbackResolver) {
188-
this(anthropicApi, defaultOptions, retryTemplate, functionCallbackResolver, List.of());
189-
}
190-
191-
/**
192-
* Construct a new {@link AnthropicChatModel} instance.
193-
* @param anthropicApi the lower-level API for the Anthropic service.
194-
* @param defaultOptions the default options used for the chat completion requests.
195-
* @param retryTemplate the retry template used to retry the Anthropic API calls.
196-
* @param functionCallbackResolver the function callback resolver used to resolve the
197-
* function by its name.
198-
* @param toolFunctionCallbacks the tool function callbacks used to handle the tool
199-
* calls.
200-
* @deprecated Use {@link AnthropicChatModel.Builder}.
201-
*/
202-
@Deprecated
203-
public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaultOptions,
204-
RetryTemplate retryTemplate, FunctionCallbackResolver functionCallbackResolver,
205-
List<FunctionCallback> toolFunctionCallbacks) {
206-
this(anthropicApi, defaultOptions, retryTemplate, functionCallbackResolver, toolFunctionCallbacks,
207-
ObservationRegistry.NOOP);
208-
}
209-
210-
/**
211-
* Construct a new {@link AnthropicChatModel} instance.
212-
* @param anthropicApi the lower-level API for the Anthropic service.
213-
* @param defaultOptions the default options used for the chat completion requests.
214-
* @param retryTemplate the retry template used to retry the Anthropic API calls.
215-
* @param functionCallbackResolver the function callback resolver used to resolve the
216-
* function by its name.
217-
* @param toolFunctionCallbacks the tool function callbacks used to handle the tool
218-
* calls.
219-
* @deprecated Use {@link AnthropicChatModel.Builder}.
220-
*/
221-
@Deprecated
222-
public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaultOptions,
223-
RetryTemplate retryTemplate, @Nullable FunctionCallbackResolver functionCallbackResolver,
224-
@Nullable List<FunctionCallback> toolFunctionCallbacks, ObservationRegistry observationRegistry) {
225-
this(anthropicApi, defaultOptions,
226-
LegacyToolCallingManager.builder()
227-
.functionCallbackResolver(functionCallbackResolver)
228-
.functionCallbacks(toolFunctionCallbacks)
229-
.build(),
230-
retryTemplate, observationRegistry);
231-
logger.warn("This constructor is deprecated and will be removed in the next milestone. "
232-
+ "Please use the MistralAiChatModel.Builder or the new constructor accepting ToolCallingManager instead.");
233-
}
234-
235131
public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaultOptions,
236132
ToolCallingManager toolCallingManager, RetryTemplate retryTemplate,
237133
ObservationRegistry observationRegistry) {
238-
// We do not pass the 'defaultOptions' to the AbstractToolSupport,
239-
// because it modifies them. We are using ToolCallingManager instead,
240-
// so we just pass empty options here.
241-
super(null, AnthropicChatOptions.builder().build(), List.of());
242134

243135
Assert.notNull(anthropicApi, "anthropicApi cannot be null");
244136
Assert.notNull(defaultOptions, "defaultOptions cannot be null");
@@ -483,10 +375,6 @@ Prompt buildRequestPrompt(Prompt prompt) {
483375
runtimeOptions = ModelOptionsUtils.copyToTarget(toolCallingChatOptions, ToolCallingChatOptions.class,
484376
AnthropicChatOptions.class);
485377
}
486-
else if (prompt.getOptions() instanceof FunctionCallingOptions functionCallingOptions) {
487-
runtimeOptions = ModelOptionsUtils.copyToTarget(functionCallingOptions, FunctionCallingOptions.class,
488-
AnthropicChatOptions.class);
489-
}
490378
else {
491379
runtimeOptions = ModelOptionsUtils.copyToTarget(prompt.getOptions(), ChatOptions.class,
492380
AnthropicChatOptions.class);
@@ -643,10 +531,6 @@ public static final class Builder {
643531

644532
private RetryTemplate retryTemplate = RetryUtils.DEFAULT_RETRY_TEMPLATE;
645533

646-
private FunctionCallbackResolver functionCallbackResolver;
647-
648-
private List<FunctionCallback> toolCallbacks;
649-
650534
private ToolCallingManager toolCallingManager;
651535

652536
private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;
@@ -674,41 +558,16 @@ public Builder toolCallingManager(ToolCallingManager toolCallingManager) {
674558
return this;
675559
}
676560

677-
@Deprecated
678-
public Builder functionCallbackResolver(FunctionCallbackResolver functionCallbackResolver) {
679-
this.functionCallbackResolver = functionCallbackResolver;
680-
return this;
681-
}
682-
683-
@Deprecated
684-
public Builder toolCallbacks(List<FunctionCallback> toolCallbacks) {
685-
this.toolCallbacks = toolCallbacks;
686-
return this;
687-
}
688-
689561
public Builder observationRegistry(ObservationRegistry observationRegistry) {
690562
this.observationRegistry = observationRegistry;
691563
return this;
692564
}
693565

694566
public AnthropicChatModel build() {
695567
if (toolCallingManager != null) {
696-
Assert.isNull(functionCallbackResolver,
697-
"functionCallbackResolver cannot be set when toolCallingManager is set");
698-
Assert.isNull(toolCallbacks, "toolCallbacks cannot be set when toolCallingManager is set");
699-
700568
return new AnthropicChatModel(anthropicApi, defaultOptions, toolCallingManager, retryTemplate,
701569
observationRegistry);
702570
}
703-
if (functionCallbackResolver != null) {
704-
Assert.isNull(toolCallingManager,
705-
"toolCallingManager cannot be set when functionCallbackResolver is set");
706-
List<FunctionCallback> toolCallbacks = this.toolCallbacks != null ? this.toolCallbacks : List.of();
707-
708-
return new AnthropicChatModel(anthropicApi, defaultOptions, retryTemplate, functionCallbackResolver,
709-
toolCallbacks, observationRegistry);
710-
}
711-
712571
return new AnthropicChatModel(anthropicApi, defaultOptions, DEFAULT_TOOL_CALLING_MANAGER, retryTemplate,
713572
observationRegistry);
714573
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.springframework.ai.chat.observation.DefaultChatModelObservationConvention;
3535
import org.springframework.ai.chat.prompt.Prompt;
3636
import org.springframework.ai.model.function.DefaultFunctionCallbackResolver;
37+
import org.springframework.ai.model.tool.DefaultToolCallingManager;
38+
import org.springframework.ai.model.tool.ToolCallingManager;
3739
import org.springframework.ai.observation.conventions.AiOperationType;
3840
import org.springframework.ai.observation.conventions.AiProvider;
3941
import org.springframework.beans.factory.annotation.Autowired;
@@ -171,8 +173,7 @@ public AnthropicApi anthropicApi() {
171173
public AnthropicChatModel anthropicChatModel(AnthropicApi anthropicApi,
172174
TestObservationRegistry observationRegistry) {
173175
return new AnthropicChatModel(anthropicApi, AnthropicChatOptions.builder().build(),
174-
RetryTemplate.defaultInstance(), new DefaultFunctionCallbackResolver(), List.of(),
175-
observationRegistry);
176+
ToolCallingManager.builder().build(), RetryTemplate.defaultInstance(), observationRegistry);
176177
}
177178

178179
}

models/spring-ai-azure-openai/src/main/java/org/springframework/ai/azure/openai/AzureOpenAiChatModel.java

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
import org.springframework.ai.chat.metadata.PromptMetadata.PromptFilterMetadata;
7373
import org.springframework.ai.chat.metadata.Usage;
7474
import org.springframework.ai.chat.metadata.UsageUtils;
75-
import org.springframework.ai.chat.model.AbstractToolCallSupport;
7675
import org.springframework.ai.chat.model.ChatModel;
7776
import org.springframework.ai.chat.model.ChatResponse;
7877
import org.springframework.ai.chat.model.Generation;
@@ -85,16 +84,12 @@
8584
import org.springframework.ai.chat.prompt.Prompt;
8685
import org.springframework.ai.model.Media;
8786
import org.springframework.ai.model.ModelOptionsUtils;
88-
import org.springframework.ai.model.function.FunctionCallback;
89-
import org.springframework.ai.model.function.FunctionCallbackResolver;
9087
import org.springframework.ai.model.function.FunctionCallingOptions;
91-
import org.springframework.ai.model.tool.LegacyToolCallingManager;
9288
import org.springframework.ai.model.tool.ToolCallingChatOptions;
9389
import org.springframework.ai.model.tool.ToolCallingManager;
9490
import org.springframework.ai.model.tool.ToolExecutionResult;
9591
import org.springframework.ai.observation.conventions.AiProvider;
9692
import org.springframework.ai.tool.definition.ToolDefinition;
97-
import org.springframework.lang.Nullable;
9893
import org.springframework.util.Assert;
9994
import org.springframework.util.CollectionUtils;
10095

@@ -119,7 +114,7 @@
119114
* @see com.azure.ai.openai.OpenAIClient
120115
* @since 1.0.0
121116
*/
122-
public class AzureOpenAiChatModel extends AbstractToolCallSupport implements ChatModel {
117+
public class AzureOpenAiChatModel implements ChatModel {
123118

124119
private static final Logger logger = LoggerFactory.getLogger(AzureOpenAiChatModel.class);
125120

@@ -163,10 +158,6 @@ public class AzureOpenAiChatModel extends AbstractToolCallSupport implements Cha
163158

164159
public AzureOpenAiChatModel(OpenAIClientBuilder openAIClientBuilder, AzureOpenAiChatOptions defaultOptions,
165160
ToolCallingManager toolCallingManager, ObservationRegistry observationRegistry) {
166-
// We do not pass the 'defaultOptions' to the AbstractToolSupport,
167-
// because it modifies them. We are using ToolCallingManager instead,
168-
// so we just pass empty options here.
169-
super(null, AzureOpenAiChatOptions.builder().build(), List.of());
170161
Assert.notNull(openAIClientBuilder, "com.azure.ai.openai.OpenAIClient must not be null");
171162
Assert.notNull(defaultOptions, "defaultOptions cannot be null");
172163
Assert.notNull(toolCallingManager, "toolCallingManager cannot be null");
@@ -488,10 +479,6 @@ ChatCompletionsOptions toAzureChatCompletionsOptions(Prompt prompt) {
488479

489480
if (prompt.getOptions() != null) {
490481
AzureOpenAiChatOptions updatedRuntimeOptions;
491-
if (prompt.getOptions() instanceof FunctionCallingOptions functionCallingOptions) {
492-
updatedRuntimeOptions = ModelOptionsUtils.copyToTarget(functionCallingOptions,
493-
FunctionCallingOptions.class, AzureOpenAiChatOptions.class);
494-
}
495482
if (prompt.getOptions() instanceof ToolCallingChatOptions toolCallingChatOptions) {
496483
updatedRuntimeOptions = ModelOptionsUtils.copyToTarget(toolCallingChatOptions,
497484
ToolCallingChatOptions.class, AzureOpenAiChatOptions.class);
@@ -622,10 +609,6 @@ Prompt buildRequestPrompt(Prompt prompt) {
622609
runtimeOptions = ModelOptionsUtils.copyToTarget(toolCallingChatOptions, ToolCallingChatOptions.class,
623610
AzureOpenAiChatOptions.class);
624611
}
625-
else if (prompt.getOptions() instanceof FunctionCallingOptions functionCallingOptions) {
626-
runtimeOptions = ModelOptionsUtils.copyToTarget(functionCallingOptions, FunctionCallingOptions.class,
627-
AzureOpenAiChatOptions.class);
628-
}
629612
else {
630613
runtimeOptions = ModelOptionsUtils.copyToTarget(prompt.getOptions(), ChatOptions.class,
631614
AzureOpenAiChatOptions.class);
@@ -932,10 +915,6 @@ public static class Builder {
932915

933916
private ToolCallingManager toolCallingManager;
934917

935-
private FunctionCallbackResolver functionCallbackResolver;
936-
937-
private List<FunctionCallback> toolFunctionCallbacks;
938-
939918
private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;
940919

941920
private Builder() {
@@ -956,48 +935,16 @@ public Builder toolCallingManager(ToolCallingManager toolCallingManager) {
956935
return this;
957936
}
958937

959-
@Deprecated
960-
public Builder functionCallbackResolver(FunctionCallbackResolver functionCallbackResolver) {
961-
this.functionCallbackResolver = functionCallbackResolver;
962-
return this;
963-
}
964-
965-
@Deprecated
966-
public Builder toolFunctionCallbacks(List<FunctionCallback> toolFunctionCallbacks) {
967-
this.toolFunctionCallbacks = toolFunctionCallbacks;
968-
return this;
969-
}
970-
971938
public Builder observationRegistry(ObservationRegistry observationRegistry) {
972939
this.observationRegistry = observationRegistry;
973940
return this;
974941
}
975942

976943
public AzureOpenAiChatModel build() {
977944
if (toolCallingManager != null) {
978-
Assert.isNull(functionCallbackResolver,
979-
"functionCallbackResolver cannot be set when toolCallingManager is set");
980-
Assert.isNull(toolFunctionCallbacks,
981-
"toolFunctionCallbacks cannot be set when toolCallingManager is set");
982-
983945
return new AzureOpenAiChatModel(openAIClientBuilder, defaultOptions, toolCallingManager,
984946
observationRegistry);
985947
}
986-
987-
if (functionCallbackResolver != null) {
988-
Assert.isNull(toolCallingManager,
989-
"toolCallingManager cannot be set when functionCallbackResolver is set");
990-
List<FunctionCallback> toolCallbacks = this.toolFunctionCallbacks != null ? this.toolFunctionCallbacks
991-
: List.of();
992-
993-
return new Builder().openAIClientBuilder(openAIClientBuilder)
994-
.defaultOptions(defaultOptions)
995-
.functionCallbackResolver(functionCallbackResolver)
996-
.toolFunctionCallbacks(toolCallbacks)
997-
.observationRegistry(observationRegistry)
998-
.build();
999-
}
1000-
1001948
return new AzureOpenAiChatModel(openAIClientBuilder, defaultOptions, DEFAULT_TOOL_CALLING_MANAGER,
1002949
observationRegistry);
1003950
}

0 commit comments

Comments
 (0)