| 
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 | import reactor.core.scheduler.Schedulers;  | 
 | 
59 | 52 | import org.springframework.ai.chat.metadata.EmptyUsage;  | 
60 | 53 | import org.springframework.ai.chat.metadata.Usage;  | 
61 | 54 | import org.springframework.ai.chat.metadata.UsageUtils;  | 
62 |  | -import org.springframework.ai.chat.model.AbstractToolCallSupport;  | 
63 | 55 | import org.springframework.ai.chat.model.ChatModel;  | 
64 | 56 | import org.springframework.ai.chat.model.ChatResponse;  | 
65 | 57 | import org.springframework.ai.chat.model.Generation;  | 
 | 
72 | 64 | import org.springframework.ai.chat.prompt.Prompt;  | 
73 | 65 | import org.springframework.ai.model.Media;  | 
74 | 66 | import org.springframework.ai.model.ModelOptionsUtils;  | 
75 |  | -import org.springframework.ai.model.function.FunctionCallback;  | 
76 |  | -import org.springframework.ai.model.function.FunctionCallbackResolver;  | 
77 |  | -import org.springframework.ai.model.function.FunctionCallingOptions;  | 
 | 67 | +import org.springframework.ai.model.tool.ToolCallingChatOptions;  | 
 | 68 | +import org.springframework.ai.model.tool.ToolCallingManager;  | 
 | 69 | +import org.springframework.ai.model.tool.ToolExecutionResult;  | 
78 | 70 | import org.springframework.ai.retry.RetryUtils;  | 
 | 71 | +import org.springframework.ai.tool.definition.ToolDefinition;  | 
 | 72 | +import org.springframework.ai.util.json.JsonParser;  | 
79 | 73 | import org.springframework.http.ResponseEntity;  | 
80 | 74 | import org.springframework.retry.support.RetryTemplate;  | 
81 | 75 | import org.springframework.util.Assert;  | 
 | 
94 | 88 |  * @author Alexandros Pappas  | 
95 | 89 |  * @since 1.0.0  | 
96 | 90 |  */  | 
97 |  | -public class AnthropicChatModel extends AbstractToolCallSupport implements ChatModel {  | 
 | 91 | +public class AnthropicChatModel implements ChatModel {  | 
98 | 92 | 
 
  | 
99 | 93 | 	public static final String DEFAULT_MODEL_NAME = AnthropicApi.ChatModel.CLAUDE_3_7_SONNET.getValue();  | 
100 | 94 | 
 
  | 
@@ -135,111 +129,9 @@ public class AnthropicChatModel extends AbstractToolCallSupport implements ChatM  | 
135 | 129 | 	 */  | 
136 | 130 | 	private ChatModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION;  | 
137 | 131 | 
 
  | 
138 |  | -	/**  | 
139 |  | -	 * Construct a new {@link AnthropicChatModel} instance.  | 
140 |  | -	 * @param anthropicApi the lower-level API for the Anthropic service.  | 
141 |  | -	 * @deprecated Use {@link AnthropicChatModel.Builder}.  | 
142 |  | -	 */  | 
143 |  | -	@Deprecated  | 
144 |  | -	public AnthropicChatModel(AnthropicApi anthropicApi) {  | 
145 |  | -		this(anthropicApi,  | 
146 |  | -				AnthropicChatOptions.builder()  | 
147 |  | -					.model(DEFAULT_MODEL_NAME)  | 
148 |  | -					.maxTokens(DEFAULT_MAX_TOKENS)  | 
149 |  | -					.temperature(DEFAULT_TEMPERATURE)  | 
150 |  | -					.build());  | 
151 |  | -	}  | 
152 |  | - | 
153 |  | -	/**  | 
154 |  | -	 * Construct a new {@link AnthropicChatModel} instance.  | 
155 |  | -	 * @param anthropicApi the lower-level API for the Anthropic service.  | 
156 |  | -	 * @param defaultOptions the default options used for the chat completion requests.  | 
157 |  | -	 * @deprecated Use {@link AnthropicChatModel.Builder}.  | 
158 |  | -	 */  | 
159 |  | -	@Deprecated  | 
160 |  | -	public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaultOptions) {  | 
161 |  | -		this(anthropicApi, defaultOptions, RetryUtils.DEFAULT_RETRY_TEMPLATE);  | 
162 |  | -	}  | 
163 |  | - | 
164 |  | -	/**  | 
165 |  | -	 * Construct a new {@link AnthropicChatModel} instance.  | 
166 |  | -	 * @param anthropicApi the lower-level API for the Anthropic service.  | 
167 |  | -	 * @param defaultOptions the default options used for the chat completion requests.  | 
168 |  | -	 * @param retryTemplate the retry template used to retry the Anthropic API calls.  | 
169 |  | -	 * @deprecated Use {@link AnthropicChatModel.Builder}.  | 
170 |  | -	 */  | 
171 |  | -	@Deprecated  | 
172 |  | -	public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaultOptions,  | 
173 |  | -			RetryTemplate retryTemplate) {  | 
174 |  | -		this(anthropicApi, defaultOptions, retryTemplate, null);  | 
175 |  | -	}  | 
176 |  | - | 
177 |  | -	/**  | 
178 |  | -	 * Construct a new {@link AnthropicChatModel} instance.  | 
179 |  | -	 * @param anthropicApi the lower-level API for the Anthropic service.  | 
180 |  | -	 * @param defaultOptions the default options used for the chat completion requests.  | 
181 |  | -	 * @param retryTemplate the retry template used to retry the Anthropic API calls.  | 
182 |  | -	 * @param functionCallbackResolver the function callback resolver used to resolve the  | 
183 |  | -	 * function by its name.  | 
184 |  | -	 * @deprecated Use {@link AnthropicChatModel.Builder}.  | 
185 |  | -	 */  | 
186 |  | -	@Deprecated  | 
187 |  | -	public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaultOptions,  | 
188 |  | -			RetryTemplate retryTemplate, FunctionCallbackResolver functionCallbackResolver) {  | 
189 |  | -		this(anthropicApi, defaultOptions, retryTemplate, functionCallbackResolver, List.of());  | 
190 |  | -	}  | 
191 |  | - | 
192 |  | -	/**  | 
193 |  | -	 * Construct a new {@link AnthropicChatModel} instance.  | 
194 |  | -	 * @param anthropicApi the lower-level API for the Anthropic service.  | 
195 |  | -	 * @param defaultOptions the default options used for the chat completion requests.  | 
196 |  | -	 * @param retryTemplate the retry template used to retry the Anthropic API calls.  | 
197 |  | -	 * @param functionCallbackResolver the function callback resolver used to resolve the  | 
198 |  | -	 * function by its name.  | 
199 |  | -	 * @param toolFunctionCallbacks the tool function callbacks used to handle the tool  | 
200 |  | -	 * calls.  | 
201 |  | -	 * @deprecated Use {@link AnthropicChatModel.Builder}.  | 
202 |  | -	 */  | 
203 |  | -	@Deprecated  | 
204 |  | -	public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaultOptions,  | 
205 |  | -			RetryTemplate retryTemplate, FunctionCallbackResolver functionCallbackResolver,  | 
206 |  | -			List<FunctionCallback> toolFunctionCallbacks) {  | 
207 |  | -		this(anthropicApi, defaultOptions, retryTemplate, functionCallbackResolver, toolFunctionCallbacks,  | 
208 |  | -				ObservationRegistry.NOOP);  | 
209 |  | -	}  | 
210 |  | - | 
211 |  | -	/**  | 
212 |  | -	 * Construct a new {@link AnthropicChatModel} instance.  | 
213 |  | -	 * @param anthropicApi the lower-level API for the Anthropic service.  | 
214 |  | -	 * @param defaultOptions the default options used for the chat completion requests.  | 
215 |  | -	 * @param retryTemplate the retry template used to retry the Anthropic API calls.  | 
216 |  | -	 * @param functionCallbackResolver the function callback resolver used to resolve the  | 
217 |  | -	 * function by its name.  | 
218 |  | -	 * @param toolFunctionCallbacks the tool function callbacks used to handle the tool  | 
219 |  | -	 * calls.  | 
220 |  | -	 * @deprecated Use {@link AnthropicChatModel.Builder}.  | 
221 |  | -	 */  | 
222 |  | -	@Deprecated  | 
223 |  | -	public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaultOptions,  | 
224 |  | -			RetryTemplate retryTemplate, @Nullable FunctionCallbackResolver functionCallbackResolver,  | 
225 |  | -			@Nullable List<FunctionCallback> toolFunctionCallbacks, ObservationRegistry observationRegistry) {  | 
226 |  | -		this(anthropicApi, defaultOptions,  | 
227 |  | -				LegacyToolCallingManager.builder()  | 
228 |  | -					.functionCallbackResolver(functionCallbackResolver)  | 
229 |  | -					.functionCallbacks(toolFunctionCallbacks)  | 
230 |  | -					.build(),  | 
231 |  | -				retryTemplate, observationRegistry);  | 
232 |  | -		logger.warn("This constructor is deprecated and will be removed in the next milestone. "  | 
233 |  | -				+ "Please use the MistralAiChatModel.Builder or the new constructor accepting ToolCallingManager instead.");  | 
234 |  | -	}  | 
235 |  | - | 
236 | 132 | 	public AnthropicChatModel(AnthropicApi anthropicApi, AnthropicChatOptions defaultOptions,  | 
237 | 133 | 			ToolCallingManager toolCallingManager, RetryTemplate retryTemplate,  | 
238 | 134 | 			ObservationRegistry observationRegistry) {  | 
239 |  | -		// We do not pass the 'defaultOptions' to the AbstractToolSupport,  | 
240 |  | -		// because it modifies them. We are using ToolCallingManager instead,  | 
241 |  | -		// so we just pass empty options here.  | 
242 |  | -		super(null, AnthropicChatOptions.builder().build(), List.of());  | 
243 | 135 | 
 
  | 
244 | 136 | 		Assert.notNull(anthropicApi, "anthropicApi cannot be null");  | 
245 | 137 | 		Assert.notNull(defaultOptions, "defaultOptions cannot be null");  | 
@@ -488,10 +380,6 @@ Prompt buildRequestPrompt(Prompt prompt) {  | 
488 | 380 | 				runtimeOptions = ModelOptionsUtils.copyToTarget(toolCallingChatOptions, ToolCallingChatOptions.class,  | 
489 | 381 | 						AnthropicChatOptions.class);  | 
490 | 382 | 			}  | 
491 |  | -			else if (prompt.getOptions() instanceof FunctionCallingOptions functionCallingOptions) {  | 
492 |  | -				runtimeOptions = ModelOptionsUtils.copyToTarget(functionCallingOptions, FunctionCallingOptions.class,  | 
493 |  | -						AnthropicChatOptions.class);  | 
494 |  | -			}  | 
495 | 383 | 			else {  | 
496 | 384 | 				runtimeOptions = ModelOptionsUtils.copyToTarget(prompt.getOptions(), ChatOptions.class,  | 
497 | 385 | 						AnthropicChatOptions.class);  | 
@@ -648,10 +536,6 @@ public static final class Builder {  | 
648 | 536 | 
 
  | 
649 | 537 | 		private RetryTemplate retryTemplate = RetryUtils.DEFAULT_RETRY_TEMPLATE;  | 
650 | 538 | 
 
  | 
651 |  | -		private FunctionCallbackResolver functionCallbackResolver;  | 
652 |  | - | 
653 |  | -		private List<FunctionCallback> toolCallbacks;  | 
654 |  | - | 
655 | 539 | 		private ToolCallingManager toolCallingManager;  | 
656 | 540 | 
 
  | 
657 | 541 | 		private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;  | 
@@ -679,41 +563,16 @@ public Builder toolCallingManager(ToolCallingManager toolCallingManager) {  | 
679 | 563 | 			return this;  | 
680 | 564 | 		}  | 
681 | 565 | 
 
  | 
682 |  | -		@Deprecated  | 
683 |  | -		public Builder functionCallbackResolver(FunctionCallbackResolver functionCallbackResolver) {  | 
684 |  | -			this.functionCallbackResolver = functionCallbackResolver;  | 
685 |  | -			return this;  | 
686 |  | -		}  | 
687 |  | - | 
688 |  | -		@Deprecated  | 
689 |  | -		public Builder toolCallbacks(List<FunctionCallback> toolCallbacks) {  | 
690 |  | -			this.toolCallbacks = toolCallbacks;  | 
691 |  | -			return this;  | 
692 |  | -		}  | 
693 |  | - | 
694 | 566 | 		public Builder observationRegistry(ObservationRegistry observationRegistry) {  | 
695 | 567 | 			this.observationRegistry = observationRegistry;  | 
696 | 568 | 			return this;  | 
697 | 569 | 		}  | 
698 | 570 | 
 
  | 
699 | 571 | 		public AnthropicChatModel build() {  | 
700 | 572 | 			if (toolCallingManager != null) {  | 
701 |  | -				Assert.isNull(functionCallbackResolver,  | 
702 |  | -						"functionCallbackResolver cannot be set when toolCallingManager is set");  | 
703 |  | -				Assert.isNull(toolCallbacks, "toolCallbacks cannot be set when toolCallingManager is set");  | 
704 |  | - | 
705 | 573 | 				return new AnthropicChatModel(anthropicApi, defaultOptions, toolCallingManager, retryTemplate,  | 
706 | 574 | 						observationRegistry);  | 
707 | 575 | 			}  | 
708 |  | -			if (functionCallbackResolver != null) {  | 
709 |  | -				Assert.isNull(toolCallingManager,  | 
710 |  | -						"toolCallingManager cannot be set when functionCallbackResolver is set");  | 
711 |  | -				List<FunctionCallback> toolCallbacks = this.toolCallbacks != null ? this.toolCallbacks : List.of();  | 
712 |  | - | 
713 |  | -				return new AnthropicChatModel(anthropicApi, defaultOptions, retryTemplate, functionCallbackResolver,  | 
714 |  | -						toolCallbacks, observationRegistry);  | 
715 |  | -			}  | 
716 |  | - | 
717 | 576 | 			return new AnthropicChatModel(anthropicApi, defaultOptions, DEFAULT_TOOL_CALLING_MANAGER, retryTemplate,  | 
718 | 577 | 					observationRegistry);  | 
719 | 578 | 		}  | 
 | 
0 commit comments