Skip to content
Open
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
@@ -0,0 +1,113 @@
/*
* Copyright 2023-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.model.anthropic.autoconfigure;

import java.util.List;

import com.anthropic.client.AnthropicClient;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.observation.ObservationRegistry;

import org.springframework.ai.anthropic.AnthropicBatchModel;
import org.springframework.ai.anthropic.AnthropicBatchObservationConvention;
import org.springframework.ai.anthropic.AnthropicChatOptions;
import org.springframework.ai.anthropic.DefaultAnthropicBatchModel;
import org.springframework.ai.anthropic.http.okhttp.AnthropicHttpClientBuilderCustomizer;
import org.springframework.ai.model.tool.ToolCallingManager;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
* {@link AutoConfiguration Auto-configuration} for the Anthropic Message Batches model.
*
* <p>
* Opt-in: the bean is only created when {@code spring.ai.anthropic.batch.enabled=true},
* so applications that do not submit batches do not pay for a second HTTP client.
* Connection settings ({@code spring.ai.anthropic.*}) and model defaults
* ({@code spring.ai.anthropic.chat.*}) are shared with
* {@link AnthropicChatAutoConfiguration}, so a batch reuses the same credentials, base
* URL, timeout, retries, proxy, custom headers and HTTP client customizers as realtime
* calls.
*
* @author Ricken Bazolo
* @since 2.0.0
*/
@AutoConfiguration(after = AnthropicChatAutoConfiguration.class)
@EnableConfigurationProperties({ AnthropicConnectionProperties.class, AnthropicChatProperties.class,
AnthropicBatchProperties.class })
@ConditionalOnClass(AnthropicClient.class)
@ConditionalOnProperty(prefix = AnthropicBatchProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true")
public class AnthropicBatchAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public AnthropicBatchModel anthropicBatchModel(AnthropicConnectionProperties connectionProperties,
AnthropicChatProperties chatProperties, AnthropicBatchProperties batchProperties,
ToolCallingManager toolCallingManager, ObjectProvider<ObservationRegistry> observationRegistry,
ObjectProvider<MeterRegistry> meterRegistry,
ObjectProvider<AnthropicBatchObservationConvention> observationConvention,
ObjectProvider<AnthropicHttpClientBuilderCustomizer> httpClientBuilderCustomizers) {

AnthropicChatOptions.Builder builder = chatProperties.toOptions().mutate();
if (connectionProperties.getApiKey() != null) {
builder.apiKey(connectionProperties.getApiKey());
}
if (connectionProperties.getBaseUrl() != null) {
builder.baseUrl(connectionProperties.getBaseUrl());
}
if (connectionProperties.getTimeout() != null) {
builder.timeout(connectionProperties.getTimeout());
}
if (connectionProperties.getMaxRetries() != null) {
builder.maxRetries(connectionProperties.getMaxRetries());
}
if (connectionProperties.getProxy() != null) {
builder.proxy(connectionProperties.getProxy());
}
if (!connectionProperties.getCustomHeaders().isEmpty()) {
builder.customHeaders(connectionProperties.getCustomHeaders());
}
// Batch-specific overrides of the shared chat defaults.
if (batchProperties.getModel() != null) {
builder.model(batchProperties.getModel());
}
if (batchProperties.getMaxTokens() != null) {
builder.maxTokens(batchProperties.getMaxTokens());
}
AnthropicChatOptions options = builder.build();

List<AnthropicHttpClientBuilderCustomizer> customizers = httpClientBuilderCustomizers.orderedStream().toList();

DefaultAnthropicBatchModel batchModel = DefaultAnthropicBatchModel.builder()
.options(options)
.toolCallingManager(toolCallingManager)
.observationRegistry(observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP))
.meterRegistry(chatProperties.isConnectionPoolMetricsEnabled() ? meterRegistry.getIfAvailable() : null)
.httpClientBuilderCustomizers(customizers)
.build();

observationConvention.ifAvailable(batchModel::setObservationConvention);

return batchModel;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2023-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.model.anthropic.autoconfigure;

import org.jspecify.annotations.Nullable;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Anthropic Message Batches autoconfiguration properties.
*
* <p>
* The batch model is opt-in: set {@code spring.ai.anthropic.batch.enabled=true} to get an
* {@link org.springframework.ai.anthropic.AnthropicBatchModel} bean. Connection settings
* ({@code spring.ai.anthropic.*}) and the model defaults
* ({@code spring.ai.anthropic.chat.*}) are shared with the chat model; the two properties
* here only override the model and output-token ceiling for batch entries when batches
* need to differ from realtime calls.
*
* @author Ricken Bazolo
* @since 2.0.0
*/
@ConfigurationProperties(AnthropicBatchProperties.CONFIG_PREFIX)
public class AnthropicBatchProperties {

public static final String CONFIG_PREFIX = "spring.ai.anthropic.batch";

/**
* Whether to expose an Anthropic batch model bean. Disabled by default so that
* applications that do not use batches do not pay for a second HTTP client.
*/
private boolean enabled = false;

/**
* Model to use for batch entries. Falls back to
* {@code spring.ai.anthropic.chat.model}.
*/
private @Nullable String model;

/**
* Maximum number of tokens to generate per batch entry. Falls back to
* {@code spring.ai.anthropic.chat.max-tokens}.
*/
private @Nullable Integer maxTokens;

public boolean isEnabled() {
return this.enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public @Nullable String getModel() {
return this.model;
}

public void setModel(@Nullable String model) {
this.model = model;
}

public @Nullable Integer getMaxTokens() {
return this.maxTokens;
}

public void setMaxTokens(@Nullable Integer maxTokens) {
this.maxTokens = maxTokens;
}

}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.springframework.ai.model.anthropic.autoconfigure.AnthropicChatAutoConfiguration
org.springframework.ai.model.anthropic.autoconfigure.AnthropicBatchAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright 2023-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.model.anthropic.autoconfigure;

import java.time.Duration;
import java.util.List;

import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;

import org.springframework.ai.anthropic.AnthropicBatch;
import org.springframework.ai.anthropic.AnthropicBatchModel;
import org.springframework.ai.anthropic.AnthropicBatchRequest;
import org.springframework.ai.anthropic.AnthropicBatchResult;
import org.springframework.ai.anthropic.DefaultAnthropicBatchModel;
import org.springframework.ai.model.tool.autoconfigure.ToolCallingAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link AnthropicBatchAutoConfiguration}: the batch model must be opt-in, must
* back off when the application defines its own bean, and must inherit the shared
* Anthropic connection and chat defaults.
*
* @author Ricken Bazolo
*/
class AnthropicBatchAutoConfigurationTests {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ToolCallingAutoConfiguration.class,
AnthropicChatAutoConfiguration.class, AnthropicBatchAutoConfiguration.class))
.withPropertyValues("spring.ai.anthropic.api-key=test-key");

@Test
void batchModelIsNotCreatedByDefault() {
this.contextRunner.run(context -> assertThat(context).doesNotHaveBean(AnthropicBatchModel.class));
}

@Test
void batchModelIsNotCreatedWhenExplicitlyDisabled() {
this.contextRunner.withPropertyValues("spring.ai.anthropic.batch.enabled=false")
.run(context -> assertThat(context).doesNotHaveBean(AnthropicBatchModel.class));
}

@Test
void batchModelIsCreatedWhenEnabled() {
this.contextRunner.withPropertyValues("spring.ai.anthropic.batch.enabled=true").run(context -> {
assertThat(context).hasSingleBean(AnthropicBatchModel.class);
assertThat(context.getBean(AnthropicBatchModel.class)).isInstanceOf(DefaultAnthropicBatchModel.class);
});
}

@Test
void batchModelInheritsConnectionAndChatDefaults() {
this.contextRunner
.withPropertyValues("spring.ai.anthropic.batch.enabled=true", "spring.ai.anthropic.base-url=https://proxy",
"spring.ai.anthropic.timeout=45s", "spring.ai.anthropic.max-retries=5",
"spring.ai.anthropic.chat.model=claude-sonnet-4-5", "spring.ai.anthropic.chat.max-tokens=1234")
.run(context -> {
var options = context.getBean(DefaultAnthropicBatchModel.class).getOptions();
assertThat(options.getApiKey()).isEqualTo("test-key");
assertThat(options.getBaseUrl()).isEqualTo("https://proxy");
assertThat(options.getTimeout()).isEqualTo(Duration.ofSeconds(45));
assertThat(options.getMaxRetries()).isEqualTo(5);
assertThat(options.getModel()).isEqualTo("claude-sonnet-4-5");
assertThat(options.getMaxTokens()).isEqualTo(1234);
});
}

@Test
void batchPropertiesOverrideChatDefaults() {
this.contextRunner
.withPropertyValues("spring.ai.anthropic.batch.enabled=true",
"spring.ai.anthropic.chat.model=claude-sonnet-4-5", "spring.ai.anthropic.chat.max-tokens=1234",
"spring.ai.anthropic.batch.model=claude-haiku-4-5", "spring.ai.anthropic.batch.max-tokens=64")
.run(context -> {
var options = context.getBean(DefaultAnthropicBatchModel.class).getOptions();
assertThat(options.getModel()).isEqualTo("claude-haiku-4-5");
assertThat(options.getMaxTokens()).isEqualTo(64);
});
}

@Test
void applicationDefinedBatchModelWins() {
this.contextRunner.withPropertyValues("spring.ai.anthropic.batch.enabled=true")
.withUserConfiguration(CustomBatchModelConfiguration.class)
.run(context -> {
assertThat(context).hasSingleBean(AnthropicBatchModel.class);
assertThat(context.getBean(AnthropicBatchModel.class))
.isNotInstanceOf(DefaultAnthropicBatchModel.class);
});
}

@Configuration(proxyBeanMethods = false)
static class CustomBatchModelConfiguration {

@Bean
AnthropicBatchModel customBatchModel() {
return new AnthropicBatchModel() {

@Override
public AnthropicBatch submit(List<AnthropicBatchRequest> requests) {
throw new UnsupportedOperationException();
}

@Override
public AnthropicBatch retrieve(String batchId) {
throw new UnsupportedOperationException();
}

@Override
public Flux<AnthropicBatchResult> results(String batchId) {
return Flux.empty();
}

@Override
public AnthropicBatch cancel(String batchId) {
throw new UnsupportedOperationException();
}

@Override
public void delete(String batchId) {
}

};
}

}

}
Loading
Loading