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 87098c6d938..c01bd702525 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 @@ * @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.M7") public AnthropicApi(String anthropicApiKey) { this(DEFAULT_BASE_URL, anthropicApiKey); } @@ -98,6 +104,7 @@ public AnthropicApi(String anthropicApiKey) { * @param baseUrl api base URL. * @param anthropicApiKey Anthropic api Key. */ + @Deprecated(since = "1.0.0.M7") 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 AnthropicApi(String baseUrl, String anthropicApiKey) { * @param webClientBuilder WebClient builder. * @param responseErrorHandler Response error handler. */ + @Deprecated(since = "1.0.0.M7") public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVersion, RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder, ResponseErrorHandler responseErrorHandler) { @@ -1450,4 +1458,70 @@ public record PingEvent( } // @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 79742e733ab..dc76d7646cc 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 @@ static class Config { @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 e629c3411ad..e404187f636 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 @@ public static class Config { @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 3db3961be86..910f572d208 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 TestObservationRegistry observationRegistry() { @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 62e5663cf53..221e33fea7a 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 5c723fdff9f..53bb771319a 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 7eb86034c75..efa5eda2b80 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 @@ @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 0be31a1386d..4b32b9e8ee4 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 6ac322220fd..d2f6d30f2ec 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("""