From 46be8987d6bc385bf74b9296aa4308c7a8658d2f Mon Sep 17 00:00:00 2001 From: Ilayaperumal Gopinathan Date: Mon, 5 May 2025 00:00:06 +0100 Subject: [PATCH] Remove deprecations from OllamApi and AnthropicApi - Remove deprecated constructors from OllamaApi and AnthropicApi - Modify AnthropicApi's public constructor to be private so that only the builder method can use it to construct - Update OllamApiAutoConfiguration and AnthropicChatAutoconfiguration to use the builder methods to construct the respective Apis - Fix other constructor usages to builder methods Signed-off-by: Ilayaperumal Gopinathan --- .../AnthropicChatAutoConfiguration.java | 15 +++++--- .../AnthropicPropertiesTests.java | 5 ++- .../OllamaApiAutoConfiguration.java | 8 ++-- .../ollama/autoconfigure/BaseOllamaIT.java | 2 +- .../ai/anthropic/api/AnthropicApi.java | 38 +------------------ .../ai/anthropic/api/AnthropicApiIT.java | 2 +- .../converse/BedrockConverseChatClientIT.java | 1 - .../ai/ollama/api/OllamaApi.java | 34 +---------------- .../OpenSearchVectorStoreWithOllamaIT.java | 4 +- 9 files changed, 25 insertions(+), 84 deletions(-) diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/main/java/org/springframework/ai/model/anthropic/autoconfigure/AnthropicChatAutoConfiguration.java b/auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/main/java/org/springframework/ai/model/anthropic/autoconfigure/AnthropicChatAutoConfiguration.java index e1b475e45..760f87afc 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/main/java/org/springframework/ai/model/anthropic/autoconfigure/AnthropicChatAutoConfiguration.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/main/java/org/springframework/ai/model/anthropic/autoconfigure/AnthropicChatAutoConfiguration.java @@ -67,11 +67,16 @@ public class AnthropicChatAutoConfiguration { ObjectProvider restClientBuilderProvider, ObjectProvider webClientBuilderProvider, ResponseErrorHandler responseErrorHandler) { - return new AnthropicApi(connectionProperties.getBaseUrl(), connectionProperties.getCompletionsPath(), - connectionProperties.getApiKey(), connectionProperties.getVersion(), - restClientBuilderProvider.getIfAvailable(RestClient::builder), - webClientBuilderProvider.getIfAvailable(WebClient::builder), responseErrorHandler, - connectionProperties.getBetaVersion()); + return AnthropicApi.builder() + .baseUrl(connectionProperties.getBaseUrl()) + .completionsPath(connectionProperties.getCompletionsPath()) + .apiKey(connectionProperties.getApiKey()) + .anthropicVersion(connectionProperties.getVersion()) + .restClientBuilder(restClientBuilderProvider.getIfAvailable(RestClient::builder)) + .webClientBuilder(webClientBuilderProvider.getIfAvailable(WebClient::builder)) + .responseErrorHandler(responseErrorHandler) + .anthropicBetaFeatures(connectionProperties.getBetaVersion()) + .build(); } @Bean diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/test/java/org/springframework/ai/model/anthropic/autoconfigure/AnthropicPropertiesTests.java b/auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/test/java/org/springframework/ai/model/anthropic/autoconfigure/AnthropicPropertiesTests.java index 79867bde5..5d4f5804d 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/test/java/org/springframework/ai/model/anthropic/autoconfigure/AnthropicPropertiesTests.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/test/java/org/springframework/ai/model/anthropic/autoconfigure/AnthropicPropertiesTests.java @@ -102,7 +102,7 @@ public class AnthropicPropertiesTests { public void chatCompletionDisabled() { // It is enabled by default - new ApplicationContextRunner() + new ApplicationContextRunner().withPropertyValues("spring.ai.anthropic.api-key=API_KEY") .withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class, RestClientAutoConfiguration.class, AnthropicChatAutoConfiguration.class)) .run(context -> { @@ -111,7 +111,8 @@ public class AnthropicPropertiesTests { }); // Explicitly enable the chat auto-configuration. - new ApplicationContextRunner().withPropertyValues("spring.ai.model.chat=anthropic") + new ApplicationContextRunner() + .withPropertyValues("spring.ai.anthropic.api-key=API_KEY", "spring.ai.model.chat=anthropic") .withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class, RestClientAutoConfiguration.class, AnthropicChatAutoConfiguration.class)) .run(context -> { diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-ollama/src/main/java/org/springframework/ai/model/ollama/autoconfigure/OllamaApiAutoConfiguration.java b/auto-configurations/models/spring-ai-autoconfigure-model-ollama/src/main/java/org/springframework/ai/model/ollama/autoconfigure/OllamaApiAutoConfiguration.java index cfbd820e8..dcdd8c2fb 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-ollama/src/main/java/org/springframework/ai/model/ollama/autoconfigure/OllamaApiAutoConfiguration.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-ollama/src/main/java/org/springframework/ai/model/ollama/autoconfigure/OllamaApiAutoConfiguration.java @@ -51,9 +51,11 @@ public class OllamaApiAutoConfiguration { public OllamaApi ollamaApi(OllamaConnectionDetails connectionDetails, ObjectProvider restClientBuilderProvider, ObjectProvider webClientBuilderProvider) { - return new OllamaApi(connectionDetails.getBaseUrl(), - restClientBuilderProvider.getIfAvailable(RestClient::builder), - webClientBuilderProvider.getIfAvailable(WebClient::builder)); + return OllamaApi.builder() + .baseUrl(connectionDetails.getBaseUrl()) + .restClientBuilder(restClientBuilderProvider.getIfAvailable(RestClient::builder)) + .webClientBuilder(webClientBuilderProvider.getIfAvailable(WebClient::builder)) + .build(); } static class PropertiesOllamaConnectionDetails implements OllamaConnectionDetails { diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-ollama/src/test/java/org/springframework/ai/model/ollama/autoconfigure/BaseOllamaIT.java b/auto-configurations/models/spring-ai-autoconfigure-model-ollama/src/test/java/org/springframework/ai/model/ollama/autoconfigure/BaseOllamaIT.java index b57f2ccab..f433011e5 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-ollama/src/test/java/org/springframework/ai/model/ollama/autoconfigure/BaseOllamaIT.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-ollama/src/test/java/org/springframework/ai/model/ollama/autoconfigure/BaseOllamaIT.java @@ -93,7 +93,7 @@ public abstract class BaseOllamaIT { public static OllamaApi buildOllamaApiWithModel(final String model) { final String baseUrl = SKIP_CONTAINER_CREATION ? OLLAMA_LOCAL_URL : ollamaContainer.getEndpoint(); - final OllamaApi api = new OllamaApi(baseUrl); + final OllamaApi api = OllamaApi.builder().baseUrl(baseUrl).build(); ensureModelIsPresent(api, model); return api; } diff --git a/models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/api/AnthropicApi.java b/models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/api/AnthropicApi.java index d9431a372..a5aa780a0 100644 --- a/models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/api/AnthropicApi.java +++ b/models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/api/AnthropicApi.java @@ -95,42 +95,6 @@ public class AnthropicApi { private final WebClient webClient; - /** - * Create a new client api with DEFAULT_BASE_URL - * @param anthropicApiKey Anthropic api Key. - */ - @Deprecated(since = "1.0.0.M8") - public AnthropicApi(String anthropicApiKey) { - this(DEFAULT_BASE_URL, anthropicApiKey); - } - - /** - * Create a new client api. - * @param baseUrl api base URL. - * @param anthropicApiKey Anthropic api Key. - */ - @Deprecated(since = "1.0.0.M8") - public AnthropicApi(String baseUrl, String anthropicApiKey) { - this(baseUrl, anthropicApiKey, DEFAULT_ANTHROPIC_VERSION, RestClient.builder(), WebClient.builder(), - RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER); - } - - /** - * Create a new client api. - * @param baseUrl api base URL. - * @param anthropicApiKey Anthropic api Key. - * @param restClientBuilder RestClient builder. - * @param webClientBuilder WebClient builder. - * @param responseErrorHandler Response error handler. - */ - @Deprecated(since = "1.0.0.M8") - public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVersion, - RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder, - ResponseErrorHandler responseErrorHandler) { - this(baseUrl, DEFAULT_MESSAGE_COMPLETIONS_PATH, anthropicApiKey, anthropicVersion, restClientBuilder, - webClientBuilder, responseErrorHandler, DEFAULT_ANTHROPIC_BETA_VERSION); - } - /** * Create a new client api. * @param baseUrl api base URL. @@ -142,7 +106,7 @@ public class AnthropicApi { * @param responseErrorHandler Response error handler. * @param anthropicBetaFeatures Anthropic beta features. */ - public AnthropicApi(String baseUrl, String completionsPath, String anthropicApiKey, String anthropicVersion, + private AnthropicApi(String baseUrl, String completionsPath, String anthropicApiKey, String anthropicVersion, RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder, ResponseErrorHandler responseErrorHandler, String anthropicBetaFeatures) { diff --git a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/AnthropicApiIT.java b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/AnthropicApiIT.java index efa5eda2b..67f0b61ed 100644 --- a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/AnthropicApiIT.java +++ b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/AnthropicApiIT.java @@ -110,7 +110,7 @@ public class AnthropicApiIT { void chatCompletionStreamError() { AnthropicMessage chatCompletionMessage = new AnthropicMessage(List.of(new ContentBlock("Tell me a Joke?")), Role.USER); - AnthropicApi api = new AnthropicApi("FAKE_KEY_FOR_ERROR_RESPONSE"); + AnthropicApi api = AnthropicApi.builder().baseUrl("FAKE_KEY_FOR_ERROR_RESPONSE").build(); Flux response = api.chatCompletionStream(new ChatCompletionRequest( AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(), List.of(chatCompletionMessage), null, 100, 0.8, true)); diff --git a/models/spring-ai-bedrock-converse/src/test/java/org/springframework/ai/bedrock/converse/BedrockConverseChatClientIT.java b/models/spring-ai-bedrock-converse/src/test/java/org/springframework/ai/bedrock/converse/BedrockConverseChatClientIT.java index d735dc40a..2c90d25be 100644 --- a/models/spring-ai-bedrock-converse/src/test/java/org/springframework/ai/bedrock/converse/BedrockConverseChatClientIT.java +++ b/models/spring-ai-bedrock-converse/src/test/java/org/springframework/ai/bedrock/converse/BedrockConverseChatClientIT.java @@ -380,7 +380,6 @@ class BedrockConverseChatClientIT { @ParameterizedTest(name = "{0} : {displayName} ") @ValueSource(strings = { "anthropic.claude-3-5-sonnet-20240620-v1:0" }) - @Deprecated void multiModalityImageUrl2(String modelName) throws IOException { // TODO: add url method that wrapps the checked exception. diff --git a/models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/api/OllamaApi.java b/models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/api/OllamaApi.java index a6db84c38..23b5f615b 100644 --- a/models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/api/OllamaApi.java +++ b/models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/api/OllamaApi.java @@ -16,7 +16,6 @@ package org.springframework.ai.ollama.api; -import java.io.IOException; import java.time.Duration; import java.time.Instant; import java.util.List; @@ -30,12 +29,12 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonProperty; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.ai.ollama.api.common.OllamaApiConstants; -import org.springframework.ai.retry.RetryUtils; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import org.springframework.ai.model.ModelOptionsUtils; +import org.springframework.ai.ollama.api.common.OllamaApiConstants; +import org.springframework.ai.retry.RetryUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; @@ -66,35 +65,6 @@ public class OllamaApi { private final WebClient webClient; - /** - * Default constructor that uses the default localhost url. - */ - @Deprecated(since = "1.0.0.M8") - public OllamaApi() { - this(OllamaApiConstants.DEFAULT_BASE_URL); - } - - /** - * Crate a new OllamaApi instance with the given base url. - * @param baseUrl The base url of the Ollama server. - */ - @Deprecated(since = "1.0.0.M8") - public OllamaApi(String baseUrl) { - this(baseUrl, RestClient.builder(), WebClient.builder(), RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER); - } - - /** - * Crate a new OllamaApi instance with the given base url and - * {@link RestClient.Builder}. - * @param baseUrl The base url of the Ollama server. - * @param restClientBuilder The {@link RestClient.Builder} to use. - * @param webClientBuilder The {@link WebClient.Builder} to use. - */ - @Deprecated(since = "1.0.0.M8") - public OllamaApi(String baseUrl, RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder) { - this(baseUrl, restClientBuilder, webClientBuilder, RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER); - } - /** * Create a new OllamaApi instance * @param baseUrl The base url of the Ollama server. diff --git a/vector-stores/spring-ai-opensearch-store/src/test/java/org/springframework/ai/vectorstore/opensearch/OpenSearchVectorStoreWithOllamaIT.java b/vector-stores/spring-ai-opensearch-store/src/test/java/org/springframework/ai/vectorstore/opensearch/OpenSearchVectorStoreWithOllamaIT.java index 9a9b4860c..d53969bac 100644 --- a/vector-stores/spring-ai-opensearch-store/src/test/java/org/springframework/ai/vectorstore/opensearch/OpenSearchVectorStoreWithOllamaIT.java +++ b/vector-stores/spring-ai-opensearch-store/src/test/java/org/springframework/ai/vectorstore/opensearch/OpenSearchVectorStoreWithOllamaIT.java @@ -89,7 +89,7 @@ class OpenSearchVectorStoreWithOllamaIT { } private static void ensureModelIsPresent(final String model) { - final OllamaApi api = new OllamaApi(OLLAMA_LOCAL_URL); + final OllamaApi api = OllamaApi.builder().baseUrl(OLLAMA_LOCAL_URL).build(); final var modelManagementOptions = ModelManagementOptions.builder() .maxRetries(DEFAULT_MAX_RETRIES) .timeout(DEFAULT_TIMEOUT) @@ -200,7 +200,7 @@ class OpenSearchVectorStoreWithOllamaIT { @Bean public EmbeddingModel embeddingModel() { return OllamaEmbeddingModel.builder() - .ollamaApi(new OllamaApi()) + .ollamaApi(OllamaApi.builder().build()) .defaultOptions(OllamaOptions.builder() .model(OllamaModel.MXBAI_EMBED_LARGE) .mainGPU(11)