Skip to content

Commit 88f05d0

Browse files
committed
Added the possibility to configure the path together with the base-url
1 parent 687dea5 commit 88f05d0

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/main/java/org/springframework/ai/model/anthropic/autoconfigure/AnthropicChatAutoConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public AnthropicApi anthropicApi(AnthropicConnectionProperties connectionPropert
7070
ObjectProvider<RestClient.Builder> restClientBuilderProvider,
7171
ObjectProvider<WebClient.Builder> webClientBuilderProvider, ResponseErrorHandler responseErrorHandler) {
7272

73-
return new AnthropicApi(connectionProperties.getBaseUrl(), connectionProperties.getApiKey(),
73+
return new AnthropicApi(connectionProperties.getBaseUrl(), connectionProperties.getCompletionsPath(), connectionProperties.getApiKey(),
7474
connectionProperties.getVersion(), restClientBuilderProvider.getIfAvailable(RestClient::builder),
7575
webClientBuilderProvider.getIfAvailable(WebClient::builder), responseErrorHandler,
7676
connectionProperties.getBetaVersion());

auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/main/java/org/springframework/ai/model/anthropic/autoconfigure/AnthropicConnectionProperties.java

+13
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public class AnthropicConnectionProperties {
4040
*/
4141
private String baseUrl = AnthropicApi.DEFAULT_BASE_URL;
4242

43+
/**
44+
* Path to append to the base URL
45+
*/
46+
private String completionsPath = AnthropicApi.DEFAULT_MESSAGE_COMPLETIONS_PATH;
47+
4348
/**
4449
* Anthropic API version.
4550
*/
@@ -67,6 +72,14 @@ public void setBaseUrl(String baseUrl) {
6772
this.baseUrl = baseUrl;
6873
}
6974

75+
public String getCompletionsPath() {
76+
return this.completionsPath;
77+
}
78+
79+
public void setCompletionsPath(String completionsPath) {
80+
this.completionsPath = completionsPath;
81+
}
82+
7083
public String getVersion() {
7184
return this.version;
7285
}

auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/test/java/org/springframework/ai/model/anthropic/autoconfigure/AnthropicPropertiesTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public void connectionProperties() {
3737
new ApplicationContextRunner().withPropertyValues(
3838
// @formatter:off
3939
"spring.ai.anthropic.base-url=TEST_BASE_URL",
40+
"spring.ai.anthropic.completions-path=message-path",
4041
"spring.ai.anthropic.api-key=abc123",
4142
"spring.ai.anthropic.version=6666",
4243
"spring.ai.anthropic.beta-version=7777",
@@ -53,6 +54,7 @@ public void connectionProperties() {
5354
assertThat(connectionProperties.getBaseUrl()).isEqualTo("TEST_BASE_URL");
5455
assertThat(connectionProperties.getVersion()).isEqualTo("6666");
5556
assertThat(connectionProperties.getBetaVersion()).isEqualTo("7777");
57+
assertThat(connectionProperties.getCompletionsPath()).isEqualTo("message-path");
5658

5759
assertThat(chatProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ");
5860
assertThat(chatProperties.getOptions().getTemperature()).isEqualTo(0.55);

models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/api/AnthropicApi.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public class AnthropicApi {
6565

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

68+
public static final String DEFAULT_MESSAGE_COMPLETIONS_PATH = "/v1/messages";
69+
6870
public static final String DEFAULT_ANTHROPIC_VERSION = "2023-06-01";
6971

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

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

84+
private final String completionsPath;
85+
8286
private final RestClient restClient;
8387

8488
private final StreamHelper streamHelper = new StreamHelper();
@@ -90,45 +94,48 @@ public class AnthropicApi {
9094
* @param anthropicApiKey Anthropic api Key.
9195
*/
9296
public AnthropicApi(String anthropicApiKey) {
93-
this(DEFAULT_BASE_URL, anthropicApiKey);
97+
this(DEFAULT_BASE_URL, DEFAULT_MESSAGE_COMPLETIONS_PATH, anthropicApiKey);
9498
}
9599

96100
/**
97101
* Create a new client api.
98102
* @param baseUrl api base URL.
103+
* @param completionsPath path to append to the base URL.
99104
* @param anthropicApiKey Anthropic api Key.
100105
*/
101-
public AnthropicApi(String baseUrl, String anthropicApiKey) {
102-
this(baseUrl, anthropicApiKey, DEFAULT_ANTHROPIC_VERSION, RestClient.builder(), WebClient.builder(),
106+
public AnthropicApi(String baseUrl, String completionsPath, String anthropicApiKey) {
107+
this(baseUrl, completionsPath, anthropicApiKey, DEFAULT_ANTHROPIC_VERSION, RestClient.builder(), WebClient.builder(),
103108
RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
104109
}
105110

106111
/**
107112
* Create a new client api.
108113
* @param baseUrl api base URL.
114+
* @param completionsPath path to append to the base URL.
109115
* @param anthropicApiKey Anthropic api Key.
110116
* @param restClientBuilder RestClient builder.
111117
* @param webClientBuilder WebClient builder.
112118
* @param responseErrorHandler Response error handler.
113119
*/
114-
public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVersion,
120+
public AnthropicApi(String baseUrl, String completionsPath, String anthropicApiKey, String anthropicVersion,
115121
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
116122
ResponseErrorHandler responseErrorHandler) {
117-
this(baseUrl, anthropicApiKey, anthropicVersion, restClientBuilder, webClientBuilder, responseErrorHandler,
123+
this(baseUrl, completionsPath, anthropicApiKey, anthropicVersion, restClientBuilder, webClientBuilder, responseErrorHandler,
118124
DEFAULT_ANTHROPIC_BETA_VERSION);
119125
}
120126

121127
/**
122128
* Create a new client api.
123129
* @param baseUrl api base URL.
130+
* @param completionsPath path to append to the base URL.
124131
* @param anthropicApiKey Anthropic api Key.
125132
* @param anthropicVersion Anthropic version.
126133
* @param restClientBuilder RestClient builder.
127134
* @param webClientBuilder WebClient builder.
128135
* @param responseErrorHandler Response error handler.
129136
* @param anthropicBetaFeatures Anthropic beta features.
130137
*/
131-
public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVersion,
138+
public AnthropicApi(String baseUrl, String completionsPath, String anthropicApiKey, String anthropicVersion,
132139
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
133140
ResponseErrorHandler responseErrorHandler, String anthropicBetaFeatures) {
134141

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

149+
this.completionsPath = completionsPath;
150+
142151
this.restClient = restClientBuilder.baseUrl(baseUrl)
143152
.defaultHeaders(jsonContentHeaders)
144153
.defaultStatusHandler(responseErrorHandler)
@@ -178,7 +187,7 @@ public ResponseEntity<ChatCompletionResponse> chatCompletionEntity(ChatCompletio
178187
Assert.notNull(additionalHttpHeader, "The additional HTTP headers can not be null.");
179188

180189
return this.restClient.post()
181-
.uri("/v1/messages")
190+
.uri(this.completionsPath)
182191
.headers(headers -> headers.addAll(additionalHttpHeader))
183192
.body(chatRequest)
184193
.retrieve()
@@ -214,7 +223,7 @@ public Flux<ChatCompletionResponse> chatCompletionStream(ChatCompletionRequest c
214223
AtomicReference<ChatCompletionResponseBuilder> chatCompletionReference = new AtomicReference<>();
215224

216225
return this.webClient.post()
217-
.uri("/v1/messages")
226+
.uri(this.completionsPath)
218227
.headers(headers -> headers.addAll(additionalHttpHeader))
219228
.body(Mono.just(chatRequest), ChatCompletionRequest.class)
220229
.retrieve()

0 commit comments

Comments
 (0)