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
Expand Up @@ -70,8 +70,9 @@ public AnthropicApi anthropicApi(AnthropicConnectionProperties connectionPropert
ObjectProvider<RestClient.Builder> restClientBuilderProvider,
ObjectProvider<WebClient.Builder> webClientBuilderProvider, ResponseErrorHandler responseErrorHandler) {

return new AnthropicApi(connectionProperties.getBaseUrl(), connectionProperties.getApiKey(),
connectionProperties.getVersion(), restClientBuilderProvider.getIfAvailable(RestClient::builder),
return new AnthropicApi(connectionProperties.getBaseUrl(), connectionProperties.getCompletionsPath(),
connectionProperties.getApiKey(), connectionProperties.getVersion(),
restClientBuilderProvider.getIfAvailable(RestClient::builder),
webClientBuilderProvider.getIfAvailable(WebClient::builder), responseErrorHandler,
connectionProperties.getBetaVersion());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class AnthropicConnectionProperties {
*/
private String baseUrl = AnthropicApi.DEFAULT_BASE_URL;

/**
* Path to append to the base URL
*/
private String completionsPath = AnthropicApi.DEFAULT_MESSAGE_COMPLETIONS_PATH;

/**
* Anthropic API version.
*/
Expand Down Expand Up @@ -67,6 +72,14 @@ public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}

public String getCompletionsPath() {
return this.completionsPath;
}

public void setCompletionsPath(String completionsPath) {
this.completionsPath = completionsPath;
}

public String getVersion() {
return this.version;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void connectionProperties() {
new ApplicationContextRunner().withPropertyValues(
// @formatter:off
"spring.ai.anthropic.base-url=TEST_BASE_URL",
"spring.ai.anthropic.completions-path=message-path",
"spring.ai.anthropic.api-key=abc123",
"spring.ai.anthropic.version=6666",
"spring.ai.anthropic.beta-version=7777",
Expand All @@ -53,6 +54,7 @@ public void connectionProperties() {
assertThat(connectionProperties.getBaseUrl()).isEqualTo("TEST_BASE_URL");
assertThat(connectionProperties.getVersion()).isEqualTo("6666");
assertThat(connectionProperties.getBetaVersion()).isEqualTo("7777");
assertThat(connectionProperties.getCompletionsPath()).isEqualTo("message-path");

assertThat(chatProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ");
assertThat(chatProperties.getOptions().getTemperature()).isEqualTo(0.55);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public class AnthropicApi {

public static final String DEFAULT_BASE_URL = "https://api.anthropic.com";

public static final String DEFAULT_MESSAGE_COMPLETIONS_PATH = "/v1/messages";

public static final String DEFAULT_ANTHROPIC_VERSION = "2023-06-01";

public static final String DEFAULT_ANTHROPIC_BETA_VERSION = "tools-2024-04-04,pdfs-2024-09-25";
Expand All @@ -79,6 +81,8 @@ public class AnthropicApi {

private static final Predicate<String> SSE_DONE_PREDICATE = "[DONE]"::equals;

private final String completionsPath;

private final RestClient restClient;

private final StreamHelper streamHelper = new StreamHelper();
Expand All @@ -90,45 +94,48 @@ public class AnthropicApi {
* @param anthropicApiKey Anthropic api Key.
*/
public AnthropicApi(String anthropicApiKey) {
this(DEFAULT_BASE_URL, anthropicApiKey);
this(DEFAULT_BASE_URL, DEFAULT_MESSAGE_COMPLETIONS_PATH, anthropicApiKey);
}

/**
* Create a new client api.
* @param baseUrl api base URL.
* @param completionsPath path to append to the base URL.
* @param anthropicApiKey Anthropic api Key.
*/
public AnthropicApi(String baseUrl, String anthropicApiKey) {
this(baseUrl, anthropicApiKey, DEFAULT_ANTHROPIC_VERSION, RestClient.builder(), WebClient.builder(),
RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
public AnthropicApi(String baseUrl, String completionsPath, String anthropicApiKey) {
this(baseUrl, completionsPath, anthropicApiKey, DEFAULT_ANTHROPIC_VERSION, RestClient.builder(),
WebClient.builder(), RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
}

/**
* Create a new client api.
* @param baseUrl api base URL.
* @param completionsPath path to append to the base URL.
* @param anthropicApiKey Anthropic api Key.
* @param restClientBuilder RestClient builder.
* @param webClientBuilder WebClient builder.
* @param responseErrorHandler Response error handler.
*/
public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVersion,
public AnthropicApi(String baseUrl, String completionsPath, String anthropicApiKey, String anthropicVersion,
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
ResponseErrorHandler responseErrorHandler) {
this(baseUrl, anthropicApiKey, anthropicVersion, restClientBuilder, webClientBuilder, responseErrorHandler,
DEFAULT_ANTHROPIC_BETA_VERSION);
this(baseUrl, completionsPath, anthropicApiKey, anthropicVersion, restClientBuilder, webClientBuilder,
responseErrorHandler, DEFAULT_ANTHROPIC_BETA_VERSION);
}

/**
* Create a new client api.
* @param baseUrl api base URL.
* @param completionsPath path to append to the base URL.
* @param anthropicApiKey Anthropic api Key.
* @param anthropicVersion Anthropic version.
* @param restClientBuilder RestClient builder.
* @param webClientBuilder WebClient builder.
* @param responseErrorHandler Response error handler.
* @param anthropicBetaFeatures Anthropic beta features.
*/
public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVersion,
public AnthropicApi(String baseUrl, String completionsPath, String anthropicApiKey, String anthropicVersion,
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
ResponseErrorHandler responseErrorHandler, String anthropicBetaFeatures) {

Expand All @@ -139,6 +146,8 @@ public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVers
headers.setContentType(MediaType.APPLICATION_JSON);
};

this.completionsPath = completionsPath;

this.restClient = restClientBuilder.baseUrl(baseUrl)
.defaultHeaders(jsonContentHeaders)
.defaultStatusHandler(responseErrorHandler)
Expand Down Expand Up @@ -178,7 +187,7 @@ public ResponseEntity<ChatCompletionResponse> chatCompletionEntity(ChatCompletio
Assert.notNull(additionalHttpHeader, "The additional HTTP headers can not be null.");

return this.restClient.post()
.uri("/v1/messages")
.uri(this.completionsPath)
.headers(headers -> headers.addAll(additionalHttpHeader))
.body(chatRequest)
.retrieve()
Expand Down Expand Up @@ -214,7 +223,7 @@ public Flux<ChatCompletionResponse> chatCompletionStream(ChatCompletionRequest c
AtomicReference<ChatCompletionResponseBuilder> chatCompletionReference = new AtomicReference<>();

return this.webClient.post()
.uri("/v1/messages")
.uri(this.completionsPath)
.headers(headers -> headers.addAll(additionalHttpHeader))
.body(Mono.just(chatRequest), ChatCompletionRequest.class)
.retrieve()
Expand Down