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 <ilayaperumal.gopinathan@broadcom.com>
This commit is contained in:
Ilayaperumal Gopinathan
2025-05-05 00:00:06 +01:00
parent 76bee8ceb2
commit 46be8987d6
9 changed files with 25 additions and 84 deletions

View File

@@ -67,11 +67,16 @@ public class AnthropicChatAutoConfiguration {
ObjectProvider<RestClient.Builder> restClientBuilderProvider,
ObjectProvider<WebClient.Builder> 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

View File

@@ -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 -> {

View File

@@ -51,9 +51,11 @@ public class OllamaApiAutoConfiguration {
public OllamaApi ollamaApi(OllamaConnectionDetails connectionDetails,
ObjectProvider<RestClient.Builder> restClientBuilderProvider,
ObjectProvider<WebClient.Builder> 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 {

View File

@@ -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;
}

View File

@@ -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) {

View File

@@ -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<ChatCompletionResponse> response = api.chatCompletionStream(new ChatCompletionRequest(
AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(), List.of(chatCompletionMessage), null, 100, 0.8, true));

View File

@@ -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.

View File

@@ -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.

View File

@@ -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)