Skip to content

Commit fc1f598

Browse files
apply builder pattern to AnthropicApi
Signed-off-by: jonghoonpark <[email protected]>
1 parent c0bc623 commit fc1f598

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.M7")
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.M7")
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.M7")
114122
public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVersion,
115123
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
116124
ResponseErrorHandler responseErrorHandler) {
@@ -1450,4 +1458,70 @@ public record PingEvent(
14501458
}
14511459
// @formatter:on
14521460

1461+
public static class Builder {
1462+
1463+
private String baseUrl = DEFAULT_BASE_URL;
1464+
1465+
private String apiKey;
1466+
1467+
private String anthropicVersion = DEFAULT_ANTHROPIC_VERSION;
1468+
1469+
private RestClient.Builder restClientBuilder = RestClient.builder();
1470+
1471+
private WebClient.Builder webClientBuilder = WebClient.builder();
1472+
1473+
private ResponseErrorHandler responseErrorHandler = RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER;
1474+
1475+
private String anthropicBetaFeatures = DEFAULT_ANTHROPIC_BETA_VERSION;
1476+
1477+
public Builder baseUrl(String baseUrl) {
1478+
Assert.hasText(baseUrl, "baseUrl cannot be null or empty");
1479+
this.baseUrl = baseUrl;
1480+
return this;
1481+
}
1482+
1483+
public Builder apiKey(String apiKey) {
1484+
Assert.notNull(apiKey, "apiKey cannot be null");
1485+
this.apiKey = apiKey;
1486+
return this;
1487+
}
1488+
1489+
public Builder anthropicVersion(String anthropicVersion) {
1490+
Assert.notNull(anthropicVersion, "anthropicVersion cannot be null");
1491+
this.anthropicVersion = anthropicVersion;
1492+
return this;
1493+
}
1494+
1495+
public Builder restClientBuilder(RestClient.Builder restClientBuilder) {
1496+
Assert.notNull(restClientBuilder, "restClientBuilder cannot be null");
1497+
this.restClientBuilder = restClientBuilder;
1498+
return this;
1499+
}
1500+
1501+
public Builder webClientBuilder(WebClient.Builder webClientBuilder) {
1502+
Assert.notNull(webClientBuilder, "webClientBuilder cannot be null");
1503+
this.webClientBuilder = webClientBuilder;
1504+
return this;
1505+
}
1506+
1507+
public Builder responseErrorHandler(ResponseErrorHandler responseErrorHandler) {
1508+
Assert.notNull(responseErrorHandler, "responseErrorHandler cannot be null");
1509+
this.responseErrorHandler = responseErrorHandler;
1510+
return this;
1511+
}
1512+
1513+
public Builder anthropicBetaFeatures(String anthropicBetaFeatures) {
1514+
Assert.notNull(anthropicBetaFeatures, "anthropicBetaFeatures cannot be null");
1515+
this.anthropicBetaFeatures = anthropicBetaFeatures;
1516+
return this;
1517+
}
1518+
1519+
public AnthropicApi build() {
1520+
Assert.notNull(this.apiKey, "apiKey must be set");
1521+
return new AnthropicApi(this.baseUrl, this.apiKey, this.anthropicVersion, this.restClientBuilder,
1522+
this.webClientBuilder, this.responseErrorHandler, this.anthropicBetaFeatures);
1523+
}
1524+
1525+
}
1526+
14531527
}

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)