Skip to content

Commit b8be96c

Browse files
dev-jonghoonparkilayaperumalg
authored andcommitted
apply builder pattern to AnthropicApi
Signed-off-by: jonghoonpark <[email protected]>
1 parent b20e802 commit b8be96c

File tree

9 files changed

+89
-15
lines changed

9 files changed

+89
-15
lines changed

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

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,10 +57,15 @@
5757
* @author Thomas Vitale
5858
* @author Jihoon Kim
5959
* @author Alexandros Pappas
60+
* @author Jonghoon Park
6061
* @since 1.0.0
6162
*/
6263
public class AnthropicApi {
6364

65+
public static Builder builder() {
66+
return new Builder();
67+
}
68+
6469
public static final String PROVIDER_NAME = AiProvider.ANTHROPIC.value();
6570

6671
public static final String DEFAULT_BASE_URL = "https://api.anthropic.com";
@@ -89,6 +94,7 @@ public class AnthropicApi {
8994
* Create a new client api with DEFAULT_BASE_URL
9095
* @param anthropicApiKey Anthropic api Key.
9196
*/
97+
@Deprecated(since = "1.0.0.M8")
9298
public AnthropicApi(String anthropicApiKey) {
9399
this(DEFAULT_BASE_URL, anthropicApiKey);
94100
}
@@ -98,6 +104,7 @@ public AnthropicApi(String anthropicApiKey) {
98104
* @param baseUrl api base URL.
99105
* @param anthropicApiKey Anthropic api Key.
100106
*/
107+
@Deprecated(since = "1.0.0.M8")
101108
public AnthropicApi(String baseUrl, String anthropicApiKey) {
102109
this(baseUrl, anthropicApiKey, DEFAULT_ANTHROPIC_VERSION, RestClient.builder(), WebClient.builder(),
103110
RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
@@ -111,6 +118,7 @@ public AnthropicApi(String baseUrl, String anthropicApiKey) {
111118
* @param webClientBuilder WebClient builder.
112119
* @param responseErrorHandler Response error handler.
113120
*/
121+
@Deprecated(since = "1.0.0.M8")
114122
public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVersion,
115123
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
116124
ResponseErrorHandler responseErrorHandler) {
@@ -1323,4 +1331,70 @@ public record PingEvent(
13231331
}
13241332
// @formatter:on
13251333

1334+
public static class Builder {
1335+
1336+
private String baseUrl = DEFAULT_BASE_URL;
1337+
1338+
private String apiKey;
1339+
1340+
private String anthropicVersion = DEFAULT_ANTHROPIC_VERSION;
1341+
1342+
private RestClient.Builder restClientBuilder = RestClient.builder();
1343+
1344+
private WebClient.Builder webClientBuilder = WebClient.builder();
1345+
1346+
private ResponseErrorHandler responseErrorHandler = RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER;
1347+
1348+
private String anthropicBetaFeatures = DEFAULT_ANTHROPIC_BETA_VERSION;
1349+
1350+
public Builder baseUrl(String baseUrl) {
1351+
Assert.hasText(baseUrl, "baseUrl cannot be null or empty");
1352+
this.baseUrl = baseUrl;
1353+
return this;
1354+
}
1355+
1356+
public Builder apiKey(String apiKey) {
1357+
Assert.notNull(apiKey, "apiKey cannot be null");
1358+
this.apiKey = apiKey;
1359+
return this;
1360+
}
1361+
1362+
public Builder anthropicVersion(String anthropicVersion) {
1363+
Assert.notNull(anthropicVersion, "anthropicVersion cannot be null");
1364+
this.anthropicVersion = anthropicVersion;
1365+
return this;
1366+
}
1367+
1368+
public Builder restClientBuilder(RestClient.Builder restClientBuilder) {
1369+
Assert.notNull(restClientBuilder, "restClientBuilder cannot be null");
1370+
this.restClientBuilder = restClientBuilder;
1371+
return this;
1372+
}
1373+
1374+
public Builder webClientBuilder(WebClient.Builder webClientBuilder) {
1375+
Assert.notNull(webClientBuilder, "webClientBuilder cannot be null");
1376+
this.webClientBuilder = webClientBuilder;
1377+
return this;
1378+
}
1379+
1380+
public Builder responseErrorHandler(ResponseErrorHandler responseErrorHandler) {
1381+
Assert.notNull(responseErrorHandler, "responseErrorHandler cannot be null");
1382+
this.responseErrorHandler = responseErrorHandler;
1383+
return this;
1384+
}
1385+
1386+
public Builder anthropicBetaFeatures(String anthropicBetaFeatures) {
1387+
Assert.notNull(anthropicBetaFeatures, "anthropicBetaFeatures cannot be null");
1388+
this.anthropicBetaFeatures = anthropicBetaFeatures;
1389+
return this;
1390+
}
1391+
1392+
public AnthropicApi build() {
1393+
Assert.notNull(this.apiKey, "apiKey must be set");
1394+
return new AnthropicApi(this.baseUrl, this.apiKey, this.anthropicVersion, this.restClientBuilder,
1395+
this.webClientBuilder, this.responseErrorHandler, this.anthropicBetaFeatures);
1396+
}
1397+
1398+
}
1399+
13261400
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static class Config {
6464

6565
@Bean
6666
public AnthropicApi anthropicApi() {
67-
return new AnthropicApi("Invalid API Key");
67+
return AnthropicApi.builder().apiKey("Invalid API Key").build();
6868
}
6969

7070
@Bean

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -455,7 +455,7 @@ public static class Config {
455455

456456
@Bean
457457
public AnthropicApi anthropicApi() {
458-
return new AnthropicApi(getApiKey());
458+
return AnthropicApi.builder().apiKey(getApiKey()).build();
459459
}
460460

461461
private String getApiKey() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -164,7 +164,7 @@ public TestObservationRegistry observationRegistry() {
164164

165165
@Bean
166166
public AnthropicApi anthropicApi() {
167-
return new AnthropicApi(System.getenv("ANTHROPIC_API_KEY"));
167+
return AnthropicApi.builder().apiKey(System.getenv("ANTHROPIC_API_KEY")).build();
168168
}
169169

170170
@Bean

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,7 @@ public class AnthropicTestConfiguration {
2626

2727
@Bean
2828
public AnthropicApi anthropicApi() {
29-
return new AnthropicApi(getApiKey());
29+
return AnthropicApi.builder().apiKey(getApiKey()).build();
3030
}
3131

3232
private String getApiKey() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class ChatCompletionRequestTests {
3434
public void createRequestWithChatOptions() {
3535

3636
var client = AnthropicChatModel.builder()
37-
.anthropicApi(new AnthropicApi("TEST"))
37+
.anthropicApi(AnthropicApi.builder().apiKey("TEST").build())
3838
.defaultOptions(AnthropicChatOptions.builder().model("DEFAULT_MODEL").temperature(66.6).build())
3939
.build();
4040

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@
4040
@EnabledIfEnvironmentVariable(named = "ANTHROPIC_API_KEY", matches = ".+")
4141
public class AnthropicApiIT {
4242

43-
AnthropicApi anthropicApi = new AnthropicApi(System.getenv("ANTHROPIC_API_KEY"));
43+
AnthropicApi anthropicApi = AnthropicApi.builder().apiKey(System.getenv("ANTHROPIC_API_KEY")).build();
4444

4545
@Test
4646
void chatCompletionEntity() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,7 +83,7 @@ public class AnthropicApiLegacyToolIT {
8383

8484
private static final Logger logger = LoggerFactory.getLogger(AnthropicApiLegacyToolIT.class);
8585

86-
AnthropicApi anthropicApi = new AnthropicApi(System.getenv("ANTHROPIC_API_KEY"));
86+
AnthropicApi anthropicApi = AnthropicApi.builder().apiKey(System.getenv("ANTHROPIC_API_KEY")).build();
8787

8888
@Test
8989
void toolCalls() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,7 +58,7 @@ public class AnthropicApiToolIT {
5858

5959
private static final Logger logger = LoggerFactory.getLogger(AnthropicApiToolIT.class);
6060

61-
AnthropicApi anthropicApi = new AnthropicApi(System.getenv("ANTHROPIC_API_KEY"));
61+
AnthropicApi anthropicApi = AnthropicApi.builder().apiKey(System.getenv("ANTHROPIC_API_KEY")).build();
6262

6363
List<Tool> tools = List.of(new Tool("getCurrentWeather",
6464
"Get the weather in location. Return temperature in 30°F or 30°C format.", ModelOptionsUtils.jsonToMap("""

0 commit comments

Comments
 (0)