Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -26,7 +26,7 @@ public class AnthropicTestConfiguration {

@Bean
public AnthropicApi anthropicApi() {
return new AnthropicApi(getApiKey());
return AnthropicApi.builder().apiKey(getApiKey()).build();
}

private String getApiKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<Tool> tools = List.of(new Tool("getCurrentWeather",
"Get the weather in location. Return temperature in 30°F or 30°C format.", ModelOptionsUtils.jsonToMap("""
Expand Down