From b8be96cf79900efa71e16e84606450a8952e05f8 Mon Sep 17 00:00:00 2001 From: jonghoonpark Date: Mon, 21 Apr 2025 10:51:13 +0900 Subject: [PATCH] apply builder pattern to AnthropicApi Signed-off-by: jonghoonpark --- .../ai/anthropic/api/AnthropicApi.java | 76 ++++++++++++++++++- ...ropicChatModelAdditionalHttpHeadersIT.java | 2 +- .../ai/anthropic/AnthropicChatModelIT.java | 4 +- .../AnthropicChatModelObservationIT.java | 4 +- .../anthropic/AnthropicTestConfiguration.java | 4 +- .../anthropic/ChatCompletionRequestTests.java | 2 +- .../ai/anthropic/api/AnthropicApiIT.java | 4 +- .../api/tool/AnthropicApiLegacyToolIT.java | 4 +- .../api/tool/AnthropicApiToolIT.java | 4 +- 9 files changed, 89 insertions(+), 15 deletions(-) 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 4dbff7952..7f4e5706e 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 @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,10 +57,15 @@ import org.springframework.web.reactive.function.client.WebClient; * @author Thomas Vitale * @author Jihoon Kim * @author Alexandros Pappas + * @author Jonghoon Park * @since 1.0.0 */ public class AnthropicApi { + public static Builder builder() { + return new Builder(); + } + public static final String PROVIDER_NAME = AiProvider.ANTHROPIC.value(); public static final String DEFAULT_BASE_URL = "https://api.anthropic.com"; @@ -89,6 +94,7 @@ public class AnthropicApi { * 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); } @@ -98,6 +104,7 @@ public class AnthropicApi { * @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); @@ -111,6 +118,7 @@ public class AnthropicApi { * @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) { @@ -1323,4 +1331,70 @@ public class AnthropicApi { } // @formatter:on + public static class Builder { + + private String baseUrl = DEFAULT_BASE_URL; + + private String apiKey; + + private String anthropicVersion = DEFAULT_ANTHROPIC_VERSION; + + private RestClient.Builder restClientBuilder = RestClient.builder(); + + private WebClient.Builder webClientBuilder = WebClient.builder(); + + private ResponseErrorHandler responseErrorHandler = RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER; + + private String anthropicBetaFeatures = DEFAULT_ANTHROPIC_BETA_VERSION; + + public Builder baseUrl(String baseUrl) { + Assert.hasText(baseUrl, "baseUrl cannot be null or empty"); + this.baseUrl = baseUrl; + return this; + } + + public Builder apiKey(String apiKey) { + Assert.notNull(apiKey, "apiKey cannot be null"); + this.apiKey = apiKey; + return this; + } + + public Builder anthropicVersion(String anthropicVersion) { + Assert.notNull(anthropicVersion, "anthropicVersion cannot be null"); + this.anthropicVersion = anthropicVersion; + return this; + } + + public Builder restClientBuilder(RestClient.Builder restClientBuilder) { + Assert.notNull(restClientBuilder, "restClientBuilder cannot be null"); + this.restClientBuilder = restClientBuilder; + return this; + } + + public Builder webClientBuilder(WebClient.Builder webClientBuilder) { + Assert.notNull(webClientBuilder, "webClientBuilder cannot be null"); + this.webClientBuilder = webClientBuilder; + return this; + } + + public Builder responseErrorHandler(ResponseErrorHandler responseErrorHandler) { + Assert.notNull(responseErrorHandler, "responseErrorHandler cannot be null"); + this.responseErrorHandler = responseErrorHandler; + return this; + } + + public Builder anthropicBetaFeatures(String anthropicBetaFeatures) { + Assert.notNull(anthropicBetaFeatures, "anthropicBetaFeatures cannot be null"); + this.anthropicBetaFeatures = anthropicBetaFeatures; + return this; + } + + public AnthropicApi build() { + Assert.notNull(this.apiKey, "apiKey must be set"); + return new AnthropicApi(this.baseUrl, this.apiKey, this.anthropicVersion, this.restClientBuilder, + this.webClientBuilder, this.responseErrorHandler, this.anthropicBetaFeatures); + } + + } + } diff --git a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicChatModelAdditionalHttpHeadersIT.java b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicChatModelAdditionalHttpHeadersIT.java index 79742e733..dc76d7646 100644 --- a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicChatModelAdditionalHttpHeadersIT.java +++ b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicChatModelAdditionalHttpHeadersIT.java @@ -64,7 +64,7 @@ public class AnthropicChatModelAdditionalHttpHeadersIT { @Bean public AnthropicApi anthropicApi() { - return new AnthropicApi("Invalid API Key"); + return AnthropicApi.builder().apiKey("Invalid API Key").build(); } @Bean diff --git a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicChatModelIT.java b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicChatModelIT.java index e629c3411..e404187f6 100644 --- a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicChatModelIT.java +++ b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicChatModelIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -455,7 +455,7 @@ class AnthropicChatModelIT { @Bean public AnthropicApi anthropicApi() { - return new AnthropicApi(getApiKey()); + return AnthropicApi.builder().apiKey(getApiKey()).build(); } private String getApiKey() { diff --git a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicChatModelObservationIT.java b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicChatModelObservationIT.java index 3db3961be..910f572d2 100644 --- a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicChatModelObservationIT.java +++ b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicChatModelObservationIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -164,7 +164,7 @@ public class AnthropicChatModelObservationIT { @Bean public AnthropicApi anthropicApi() { - return new AnthropicApi(System.getenv("ANTHROPIC_API_KEY")); + return AnthropicApi.builder().apiKey(System.getenv("ANTHROPIC_API_KEY")).build(); } @Bean diff --git a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicTestConfiguration.java b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicTestConfiguration.java index 62e5663cf..221e33fea 100644 --- a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicTestConfiguration.java +++ b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/AnthropicTestConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,7 @@ public class AnthropicTestConfiguration { @Bean public AnthropicApi anthropicApi() { - return new AnthropicApi(getApiKey()); + return AnthropicApi.builder().apiKey(getApiKey()).build(); } private String getApiKey() { diff --git a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/ChatCompletionRequestTests.java b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/ChatCompletionRequestTests.java index 5c723fdff..53bb77131 100644 --- a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/ChatCompletionRequestTests.java +++ b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/ChatCompletionRequestTests.java @@ -34,7 +34,7 @@ public class ChatCompletionRequestTests { public void createRequestWithChatOptions() { var client = AnthropicChatModel.builder() - .anthropicApi(new AnthropicApi("TEST")) + .anthropicApi(AnthropicApi.builder().apiKey("TEST").build()) .defaultOptions(AnthropicChatOptions.builder().model("DEFAULT_MODEL").temperature(66.6).build()) .build(); 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 7eb86034c..efa5eda2b 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 @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; @EnabledIfEnvironmentVariable(named = "ANTHROPIC_API_KEY", matches = ".+") public class AnthropicApiIT { - AnthropicApi anthropicApi = new AnthropicApi(System.getenv("ANTHROPIC_API_KEY")); + AnthropicApi anthropicApi = AnthropicApi.builder().apiKey(System.getenv("ANTHROPIC_API_KEY")).build(); @Test void chatCompletionEntity() { diff --git a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/tool/AnthropicApiLegacyToolIT.java b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/tool/AnthropicApiLegacyToolIT.java index 0be31a138..4b32b9e8e 100644 --- a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/tool/AnthropicApiLegacyToolIT.java +++ b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/tool/AnthropicApiLegacyToolIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -83,7 +83,7 @@ public class AnthropicApiLegacyToolIT { private static final Logger logger = LoggerFactory.getLogger(AnthropicApiLegacyToolIT.class); - AnthropicApi anthropicApi = new AnthropicApi(System.getenv("ANTHROPIC_API_KEY")); + AnthropicApi anthropicApi = AnthropicApi.builder().apiKey(System.getenv("ANTHROPIC_API_KEY")).build(); @Test void toolCalls() { diff --git a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/tool/AnthropicApiToolIT.java b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/tool/AnthropicApiToolIT.java index 6ac322220..d2f6d30f2 100644 --- a/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/tool/AnthropicApiToolIT.java +++ b/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/tool/AnthropicApiToolIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,7 +58,7 @@ public class AnthropicApiToolIT { private static final Logger logger = LoggerFactory.getLogger(AnthropicApiToolIT.class); - AnthropicApi anthropicApi = new AnthropicApi(System.getenv("ANTHROPIC_API_KEY")); + AnthropicApi anthropicApi = AnthropicApi.builder().apiKey(System.getenv("ANTHROPIC_API_KEY")).build(); List tools = List.of(new Tool("getCurrentWeather", "Get the weather in location. Return temperature in 30°F or 30°C format.", ModelOptionsUtils.jsonToMap("""