Skip to content

Commit 46be898

Browse files
committed
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 <[email protected]>
1 parent 76bee8c commit 46be898

File tree

9 files changed

+25
-84
lines changed

9 files changed

+25
-84
lines changed

auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/main/java/org/springframework/ai/model/anthropic/autoconfigure/AnthropicChatAutoConfiguration.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,16 @@ public AnthropicApi anthropicApi(AnthropicConnectionProperties connectionPropert
6767
ObjectProvider<RestClient.Builder> restClientBuilderProvider,
6868
ObjectProvider<WebClient.Builder> webClientBuilderProvider, ResponseErrorHandler responseErrorHandler) {
6969

70-
return new AnthropicApi(connectionProperties.getBaseUrl(), connectionProperties.getCompletionsPath(),
71-
connectionProperties.getApiKey(), connectionProperties.getVersion(),
72-
restClientBuilderProvider.getIfAvailable(RestClient::builder),
73-
webClientBuilderProvider.getIfAvailable(WebClient::builder), responseErrorHandler,
74-
connectionProperties.getBetaVersion());
70+
return AnthropicApi.builder()
71+
.baseUrl(connectionProperties.getBaseUrl())
72+
.completionsPath(connectionProperties.getCompletionsPath())
73+
.apiKey(connectionProperties.getApiKey())
74+
.anthropicVersion(connectionProperties.getVersion())
75+
.restClientBuilder(restClientBuilderProvider.getIfAvailable(RestClient::builder))
76+
.webClientBuilder(webClientBuilderProvider.getIfAvailable(WebClient::builder))
77+
.responseErrorHandler(responseErrorHandler)
78+
.anthropicBetaFeatures(connectionProperties.getBetaVersion())
79+
.build();
7580
}
7681

7782
@Bean

auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/test/java/org/springframework/ai/model/anthropic/autoconfigure/AnthropicPropertiesTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void chatOptionsTest() {
102102
public void chatCompletionDisabled() {
103103

104104
// It is enabled by default
105-
new ApplicationContextRunner()
105+
new ApplicationContextRunner().withPropertyValues("spring.ai.anthropic.api-key=API_KEY")
106106
.withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class,
107107
RestClientAutoConfiguration.class, AnthropicChatAutoConfiguration.class))
108108
.run(context -> {
@@ -111,7 +111,8 @@ public void chatCompletionDisabled() {
111111
});
112112

113113
// Explicitly enable the chat auto-configuration.
114-
new ApplicationContextRunner().withPropertyValues("spring.ai.model.chat=anthropic")
114+
new ApplicationContextRunner()
115+
.withPropertyValues("spring.ai.anthropic.api-key=API_KEY", "spring.ai.model.chat=anthropic")
115116
.withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class,
116117
RestClientAutoConfiguration.class, AnthropicChatAutoConfiguration.class))
117118
.run(context -> {

auto-configurations/models/spring-ai-autoconfigure-model-ollama/src/main/java/org/springframework/ai/model/ollama/autoconfigure/OllamaApiAutoConfiguration.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ public PropertiesOllamaConnectionDetails ollamaConnectionDetails(OllamaConnectio
5151
public OllamaApi ollamaApi(OllamaConnectionDetails connectionDetails,
5252
ObjectProvider<RestClient.Builder> restClientBuilderProvider,
5353
ObjectProvider<WebClient.Builder> webClientBuilderProvider) {
54-
return new OllamaApi(connectionDetails.getBaseUrl(),
55-
restClientBuilderProvider.getIfAvailable(RestClient::builder),
56-
webClientBuilderProvider.getIfAvailable(WebClient::builder));
54+
return OllamaApi.builder()
55+
.baseUrl(connectionDetails.getBaseUrl())
56+
.restClientBuilder(restClientBuilderProvider.getIfAvailable(RestClient::builder))
57+
.webClientBuilder(webClientBuilderProvider.getIfAvailable(WebClient::builder))
58+
.build();
5759
}
5860

5961
static class PropertiesOllamaConnectionDetails implements OllamaConnectionDetails {

auto-configurations/models/spring-ai-autoconfigure-model-ollama/src/test/java/org/springframework/ai/model/ollama/autoconfigure/BaseOllamaIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static void tearDown() {
9393

9494
public static OllamaApi buildOllamaApiWithModel(final String model) {
9595
final String baseUrl = SKIP_CONTAINER_CREATION ? OLLAMA_LOCAL_URL : ollamaContainer.getEndpoint();
96-
final OllamaApi api = new OllamaApi(baseUrl);
96+
final OllamaApi api = OllamaApi.builder().baseUrl(baseUrl).build();
9797
ensureModelIsPresent(api, model);
9898
return api;
9999
}

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

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -95,42 +95,6 @@ public static Builder builder() {
9595

9696
private final WebClient webClient;
9797

98-
/**
99-
* Create a new client api with DEFAULT_BASE_URL
100-
* @param anthropicApiKey Anthropic api Key.
101-
*/
102-
@Deprecated(since = "1.0.0.M8")
103-
public AnthropicApi(String anthropicApiKey) {
104-
this(DEFAULT_BASE_URL, anthropicApiKey);
105-
}
106-
107-
/**
108-
* Create a new client api.
109-
* @param baseUrl api base URL.
110-
* @param anthropicApiKey Anthropic api Key.
111-
*/
112-
@Deprecated(since = "1.0.0.M8")
113-
public AnthropicApi(String baseUrl, String anthropicApiKey) {
114-
this(baseUrl, anthropicApiKey, DEFAULT_ANTHROPIC_VERSION, RestClient.builder(), WebClient.builder(),
115-
RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
116-
}
117-
118-
/**
119-
* Create a new client api.
120-
* @param baseUrl api base URL.
121-
* @param anthropicApiKey Anthropic api Key.
122-
* @param restClientBuilder RestClient builder.
123-
* @param webClientBuilder WebClient builder.
124-
* @param responseErrorHandler Response error handler.
125-
*/
126-
@Deprecated(since = "1.0.0.M8")
127-
public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVersion,
128-
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
129-
ResponseErrorHandler responseErrorHandler) {
130-
this(baseUrl, DEFAULT_MESSAGE_COMPLETIONS_PATH, anthropicApiKey, anthropicVersion, restClientBuilder,
131-
webClientBuilder, responseErrorHandler, DEFAULT_ANTHROPIC_BETA_VERSION);
132-
}
133-
13498
/**
13599
* Create a new client api.
136100
* @param baseUrl api base URL.
@@ -142,7 +106,7 @@ public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVers
142106
* @param responseErrorHandler Response error handler.
143107
* @param anthropicBetaFeatures Anthropic beta features.
144108
*/
145-
public AnthropicApi(String baseUrl, String completionsPath, String anthropicApiKey, String anthropicVersion,
109+
private AnthropicApi(String baseUrl, String completionsPath, String anthropicApiKey, String anthropicVersion,
146110
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
147111
ResponseErrorHandler responseErrorHandler, String anthropicBetaFeatures) {
148112

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void chatCompletionStream() {
110110
void chatCompletionStreamError() {
111111
AnthropicMessage chatCompletionMessage = new AnthropicMessage(List.of(new ContentBlock("Tell me a Joke?")),
112112
Role.USER);
113-
AnthropicApi api = new AnthropicApi("FAKE_KEY_FOR_ERROR_RESPONSE");
113+
AnthropicApi api = AnthropicApi.builder().baseUrl("FAKE_KEY_FOR_ERROR_RESPONSE").build();
114114

115115
Flux<ChatCompletionResponse> response = api.chatCompletionStream(new ChatCompletionRequest(
116116
AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(), List.of(chatCompletionMessage), null, 100, 0.8, true));

models/spring-ai-bedrock-converse/src/test/java/org/springframework/ai/bedrock/converse/BedrockConverseChatClientIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@ void multiModalityEmbeddedImage(String modelName) throws IOException {
380380

381381
@ParameterizedTest(name = "{0} : {displayName} ")
382382
@ValueSource(strings = { "anthropic.claude-3-5-sonnet-20240620-v1:0" })
383-
@Deprecated
384383
void multiModalityImageUrl2(String modelName) throws IOException {
385384

386385
// TODO: add url method that wrapps the checked exception.

models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/api/OllamaApi.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.ai.ollama.api;
1818

19-
import java.io.IOException;
2019
import java.time.Duration;
2120
import java.time.Instant;
2221
import java.util.List;
@@ -30,12 +29,12 @@
3029
import com.fasterxml.jackson.annotation.JsonProperty;
3130
import org.apache.commons.logging.Log;
3231
import org.apache.commons.logging.LogFactory;
33-
import org.springframework.ai.ollama.api.common.OllamaApiConstants;
34-
import org.springframework.ai.retry.RetryUtils;
3532
import reactor.core.publisher.Flux;
3633
import reactor.core.publisher.Mono;
3734

3835
import org.springframework.ai.model.ModelOptionsUtils;
36+
import org.springframework.ai.ollama.api.common.OllamaApiConstants;
37+
import org.springframework.ai.retry.RetryUtils;
3938
import org.springframework.http.HttpHeaders;
4039
import org.springframework.http.HttpMethod;
4140
import org.springframework.http.MediaType;
@@ -66,35 +65,6 @@ public class OllamaApi {
6665

6766
private final WebClient webClient;
6867

69-
/**
70-
* Default constructor that uses the default localhost url.
71-
*/
72-
@Deprecated(since = "1.0.0.M8")
73-
public OllamaApi() {
74-
this(OllamaApiConstants.DEFAULT_BASE_URL);
75-
}
76-
77-
/**
78-
* Crate a new OllamaApi instance with the given base url.
79-
* @param baseUrl The base url of the Ollama server.
80-
*/
81-
@Deprecated(since = "1.0.0.M8")
82-
public OllamaApi(String baseUrl) {
83-
this(baseUrl, RestClient.builder(), WebClient.builder(), RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
84-
}
85-
86-
/**
87-
* Crate a new OllamaApi instance with the given base url and
88-
* {@link RestClient.Builder}.
89-
* @param baseUrl The base url of the Ollama server.
90-
* @param restClientBuilder The {@link RestClient.Builder} to use.
91-
* @param webClientBuilder The {@link WebClient.Builder} to use.
92-
*/
93-
@Deprecated(since = "1.0.0.M8")
94-
public OllamaApi(String baseUrl, RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder) {
95-
this(baseUrl, restClientBuilder, webClientBuilder, RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
96-
}
97-
9868
/**
9969
* Create a new OllamaApi instance
10070
* @param baseUrl The base url of the Ollama server.

vector-stores/spring-ai-opensearch-store/src/test/java/org/springframework/ai/vectorstore/opensearch/OpenSearchVectorStoreWithOllamaIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static void beforeAll() {
8989
}
9090

9191
private static void ensureModelIsPresent(final String model) {
92-
final OllamaApi api = new OllamaApi(OLLAMA_LOCAL_URL);
92+
final OllamaApi api = OllamaApi.builder().baseUrl(OLLAMA_LOCAL_URL).build();
9393
final var modelManagementOptions = ModelManagementOptions.builder()
9494
.maxRetries(DEFAULT_MAX_RETRIES)
9595
.timeout(DEFAULT_TIMEOUT)
@@ -200,7 +200,7 @@ public OpenSearchVectorStore anotherVectorStore(EmbeddingModel embeddingModel) {
200200
@Bean
201201
public EmbeddingModel embeddingModel() {
202202
return OllamaEmbeddingModel.builder()
203-
.ollamaApi(new OllamaApi())
203+
.ollamaApi(OllamaApi.builder().build())
204204
.defaultOptions(OllamaOptions.builder()
205205
.model(OllamaModel.MXBAI_EMBED_LARGE)
206206
.mainGPU(11)

0 commit comments

Comments
 (0)