| 
30 | 30 | import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;  | 
31 | 31 | import org.slf4j.Logger;  | 
32 | 32 | 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;  | 
40 | 33 | import reactor.core.publisher.Flux;  | 
41 | 34 | import reactor.core.publisher.Mono;  | 
42 | 35 | 
 
  | 
 | 
58 | 51 | import org.springframework.ai.chat.metadata.EmptyUsage;  | 
59 | 52 | import org.springframework.ai.chat.metadata.Usage;  | 
60 | 53 | import org.springframework.ai.chat.metadata.UsageUtils;  | 
61 |  | -import org.springframework.ai.chat.model.AbstractToolCallSupport;  | 
62 | 54 | import org.springframework.ai.chat.model.ChatModel;  | 
63 | 55 | import org.springframework.ai.chat.model.ChatResponse;  | 
64 | 56 | import org.springframework.ai.chat.model.Generation;  | 
 | 
71 | 63 | import org.springframework.ai.chat.prompt.Prompt;  | 
72 | 64 | import org.springframework.ai.model.Media;  | 
73 | 65 | 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;  | 
77 | 69 | import org.springframework.ai.retry.RetryUtils;  | 
 | 70 | +import org.springframework.ai.tool.definition.ToolDefinition;  | 
 | 71 | +import org.springframework.ai.util.json.JsonParser;  | 
78 | 72 | import org.springframework.http.ResponseEntity;  | 
79 | 73 | import org.springframework.retry.support.RetryTemplate;  | 
80 | 74 | import org.springframework.util.Assert;  | 
 | 
93 | 87 |  * @author Alexandros Pappas  | 
94 | 88 |  * @since 1.0.0  | 
95 | 89 |  */  | 
96 |  | -public class AnthropicChatModel extends AbstractToolCallSupport implements ChatModel {  | 
 | 90 | +public class AnthropicChatModel implements ChatModel {  | 
97 | 91 | 
 
  | 
98 | 92 | 	public static final String DEFAULT_MODEL_NAME = AnthropicApi.ChatModel.CLAUDE_3_7_SONNET.getValue();  | 
99 | 93 | 
 
  | 
@@ -134,111 +128,9 @@ public class AnthropicChatModel extends AbstractToolCallSupport implements ChatM  | 
134 | 128 | 	 */  | 
135 | 129 | 	private ChatModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION;  | 
136 | 130 | 
 
  | 
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 |  | - | 
235 | 131 | 	public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaultOptions,  | 
236 | 132 | 			ToolCallingManager toolCallingManager, RetryTemplate retryTemplate,  | 
237 | 133 | 			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());  | 
242 | 134 | 
 
  | 
243 | 135 | 		Assert.notNull(anthropicApi, "anthropicApi cannot be null");  | 
244 | 136 | 		Assert.notNull(defaultOptions, "defaultOptions cannot be null");  | 
@@ -483,10 +375,6 @@ Prompt buildRequestPrompt(Prompt prompt) {  | 
483 | 375 | 				runtimeOptions = ModelOptionsUtils.copyToTarget(toolCallingChatOptions, ToolCallingChatOptions.class,  | 
484 | 376 | 						AnthropicChatOptions.class);  | 
485 | 377 | 			}  | 
486 |  | -			else if (prompt.getOptions() instanceof FunctionCallingOptions functionCallingOptions) {  | 
487 |  | -				runtimeOptions = ModelOptionsUtils.copyToTarget(functionCallingOptions, FunctionCallingOptions.class,  | 
488 |  | -						AnthropicChatOptions.class);  | 
489 |  | -			}  | 
490 | 378 | 			else {  | 
491 | 379 | 				runtimeOptions = ModelOptionsUtils.copyToTarget(prompt.getOptions(), ChatOptions.class,  | 
492 | 380 | 						AnthropicChatOptions.class);  | 
@@ -643,10 +531,6 @@ public static final class Builder {  | 
643 | 531 | 
 
  | 
644 | 532 | 		private RetryTemplate retryTemplate = RetryUtils.DEFAULT_RETRY_TEMPLATE;  | 
645 | 533 | 
 
  | 
646 |  | -		private FunctionCallbackResolver functionCallbackResolver;  | 
647 |  | - | 
648 |  | -		private List<FunctionCallback> toolCallbacks;  | 
649 |  | - | 
650 | 534 | 		private ToolCallingManager toolCallingManager;  | 
651 | 535 | 
 
  | 
652 | 536 | 		private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;  | 
@@ -674,41 +558,16 @@ public Builder toolCallingManager(ToolCallingManager toolCallingManager) {  | 
674 | 558 | 			return this;  | 
675 | 559 | 		}  | 
676 | 560 | 
 
  | 
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 |  | - | 
689 | 561 | 		public Builder observationRegistry(ObservationRegistry observationRegistry) {  | 
690 | 562 | 			this.observationRegistry = observationRegistry;  | 
691 | 563 | 			return this;  | 
692 | 564 | 		}  | 
693 | 565 | 
 
  | 
694 | 566 | 		public AnthropicChatModel build() {  | 
695 | 567 | 			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 |  | - | 
700 | 568 | 				return new AnthropicChatModel(anthropicApi, defaultOptions, toolCallingManager, retryTemplate,  | 
701 | 569 | 						observationRegistry);  | 
702 | 570 | 			}  | 
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 |  | - | 
712 | 571 | 			return new AnthropicChatModel(anthropicApi, defaultOptions, DEFAULT_TOOL_CALLING_MANAGER, retryTemplate,  | 
713 | 572 | 					observationRegistry);  | 
714 | 573 | 		}  | 
 | 
0 commit comments