Skip to content

Commit d7eb9bb

Browse files
Claudio-codeilayaperumalg
authored andcommitted
Added the possibility to configure the path together with the base-url
Signed-off-by: “claudio-code” <[email protected]>
1 parent a1db00a commit d7eb9bb

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ public AnthropicApi anthropicApi(AnthropicConnectionProperties connectionPropert
6767
ObjectProvider<RestClient.Builder> restClientBuilderProvider,
6868
ObjectProvider<WebClient.Builder> webClientBuilderProvider, ResponseErrorHandler responseErrorHandler) {
6969

70-
return new AnthropicApi(connectionProperties.getBaseUrl(), connectionProperties.getApiKey(),
71-
connectionProperties.getVersion(), restClientBuilderProvider.getIfAvailable(RestClient::builder),
70+
return new AnthropicApi(connectionProperties.getBaseUrl(), connectionProperties.getCompletionsPath(),
71+
connectionProperties.getApiKey(), connectionProperties.getVersion(),
72+
restClientBuilderProvider.getIfAvailable(RestClient::builder),
7273
webClientBuilderProvider.getIfAvailable(WebClient::builder), responseErrorHandler,
7374
connectionProperties.getBetaVersion());
7475
}

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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
* @author Jihoon Kim
5959
* @author Alexandros Pappas
6060
* @author Jonghoon Park
61+
* @author Claudio Silva Junior
6162
* @since 1.0.0
6263
*/
6364
public class AnthropicApi {
@@ -70,6 +71,8 @@ public static Builder builder() {
7071

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

74+
public static final String DEFAULT_MESSAGE_COMPLETIONS_PATH = "/v1/messages";
75+
7376
public static final String DEFAULT_ANTHROPIC_VERSION = "2023-06-01";
7477

7578
public static final String DEFAULT_ANTHROPIC_BETA_VERSION = "tools-2024-04-04,pdfs-2024-09-25";
@@ -84,6 +87,8 @@ public static Builder builder() {
8487

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

90+
private final String completionsPath;
91+
8792
private final RestClient restClient;
8893

8994
private final StreamHelper streamHelper = new StreamHelper();
@@ -122,21 +127,22 @@ public AnthropicApi(String baseUrl, String anthropicApiKey) {
122127
public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVersion,
123128
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
124129
ResponseErrorHandler responseErrorHandler) {
125-
this(baseUrl, anthropicApiKey, anthropicVersion, restClientBuilder, webClientBuilder, responseErrorHandler,
126-
DEFAULT_ANTHROPIC_BETA_VERSION);
130+
this(baseUrl, DEFAULT_MESSAGE_COMPLETIONS_PATH, anthropicApiKey, anthropicVersion, restClientBuilder,
131+
webClientBuilder, responseErrorHandler, DEFAULT_ANTHROPIC_BETA_VERSION);
127132
}
128133

129134
/**
130135
* Create a new client api.
131136
* @param baseUrl api base URL.
137+
* @param completionsPath path to append to the base URL.
132138
* @param anthropicApiKey Anthropic api Key.
133139
* @param anthropicVersion Anthropic version.
134140
* @param restClientBuilder RestClient builder.
135141
* @param webClientBuilder WebClient builder.
136142
* @param responseErrorHandler Response error handler.
137143
* @param anthropicBetaFeatures Anthropic beta features.
138144
*/
139-
public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVersion,
145+
public AnthropicApi(String baseUrl, String completionsPath, String anthropicApiKey, String anthropicVersion,
140146
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
141147
ResponseErrorHandler responseErrorHandler, String anthropicBetaFeatures) {
142148

@@ -147,6 +153,8 @@ public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVers
147153
headers.setContentType(MediaType.APPLICATION_JSON);
148154
};
149155

156+
this.completionsPath = completionsPath;
157+
150158
this.restClient = restClientBuilder.baseUrl(baseUrl)
151159
.defaultHeaders(jsonContentHeaders)
152160
.defaultStatusHandler(responseErrorHandler)
@@ -186,7 +194,7 @@ public ResponseEntity<ChatCompletionResponse> chatCompletionEntity(ChatCompletio
186194
Assert.notNull(additionalHttpHeader, "The additional HTTP headers can not be null.");
187195

188196
return this.restClient.post()
189-
.uri("/v1/messages")
197+
.uri(this.completionsPath)
190198
.headers(headers -> headers.addAll(additionalHttpHeader))
191199
.body(chatRequest)
192200
.retrieve()
@@ -222,7 +230,7 @@ public Flux<ChatCompletionResponse> chatCompletionStream(ChatCompletionRequest c
222230
AtomicReference<ChatCompletionResponseBuilder> chatCompletionReference = new AtomicReference<>();
223231

224232
return this.webClient.post()
225-
.uri("/v1/messages")
233+
.uri(this.completionsPath)
226234
.headers(headers -> headers.addAll(additionalHttpHeader))
227235
.body(Mono.just(chatRequest), ChatCompletionRequest.class)
228236
.retrieve()
@@ -1335,6 +1343,8 @@ public static class Builder {
13351343

13361344
private String baseUrl = DEFAULT_BASE_URL;
13371345

1346+
private String completionsPath = DEFAULT_MESSAGE_COMPLETIONS_PATH;
1347+
13381348
private String apiKey;
13391349

13401350
private String anthropicVersion = DEFAULT_ANTHROPIC_VERSION;
@@ -1353,6 +1363,12 @@ public Builder baseUrl(String baseUrl) {
13531363
return this;
13541364
}
13551365

1366+
public Builder completionsPath(String completionsPath) {
1367+
Assert.hasText(completionsPath, "completionsPath cannot be null or empty");
1368+
this.completionsPath = completionsPath;
1369+
return this;
1370+
}
1371+
13561372
public Builder apiKey(String apiKey) {
13571373
Assert.notNull(apiKey, "apiKey cannot be null");
13581374
this.apiKey = apiKey;
@@ -1391,8 +1407,9 @@ public Builder anthropicBetaFeatures(String anthropicBetaFeatures) {
13911407

13921408
public AnthropicApi build() {
13931409
Assert.notNull(this.apiKey, "apiKey must be set");
1394-
return new AnthropicApi(this.baseUrl, this.apiKey, this.anthropicVersion, this.restClientBuilder,
1395-
this.webClientBuilder, this.responseErrorHandler, this.anthropicBetaFeatures);
1410+
return new AnthropicApi(this.baseUrl, this.completionsPath, this.apiKey, this.anthropicVersion,
1411+
this.restClientBuilder, this.webClientBuilder, this.responseErrorHandler,
1412+
this.anthropicBetaFeatures);
13961413
}
13971414

13981415
}

0 commit comments

Comments
 (0)