Skip to content

Commit 7121cf0

Browse files
committed
feat(zhipuai): Add Builder pattern for ZhiPuAiApi
- Added builder and mutate methods to construct ZhiPuAiApi instances - Use more fine-grained code formatter control - Allowed custom completionsPath and embeddingsPath - Existing constructor marked as Deprecated - Added WebClient.Builder parameter - Updated integration tests, using Builder pattern to construct ZhiPuAiApi - Added Builder unit test Signed-off-by: YunKui Lu <[email protected]>
1 parent f3a132e commit 7121cf0

File tree

10 files changed

+880
-289
lines changed

10 files changed

+880
-289
lines changed

auto-configurations/models/spring-ai-autoconfigure-model-zhipuai/src/main/java/org/springframework/ai/model/zhipuai/autoconfigure/ZhiPuAiChatAutoConfiguration.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.micrometer.observation.ObservationRegistry;
2020

2121
import org.springframework.ai.chat.observation.ChatModelObservationConvention;
22+
import org.springframework.ai.model.SimpleApiKey;
2223
import org.springframework.ai.model.SpringAIModelProperties;
2324
import org.springframework.ai.model.SpringAIModels;
2425
import org.springframework.ai.model.tool.DefaultToolExecutionEligibilityPredicate;
@@ -42,6 +43,7 @@
4243
import org.springframework.util.StringUtils;
4344
import org.springframework.web.client.ResponseErrorHandler;
4445
import org.springframework.web.client.RestClient;
46+
import org.springframework.web.reactive.function.client.WebClient;
4547

4648
/**
4749
* Chat {@link AutoConfiguration Auto-configuration} for ZhiPuAI.
@@ -63,14 +65,15 @@ public class ZhiPuAiChatAutoConfiguration {
6365
@ConditionalOnMissingBean
6466
public ZhiPuAiChatModel zhiPuAiChatModel(ZhiPuAiConnectionProperties commonProperties,
6567
ZhiPuAiChatProperties chatProperties, ObjectProvider<RestClient.Builder> restClientBuilderProvider,
66-
RetryTemplate retryTemplate, ResponseErrorHandler responseErrorHandler,
67-
ObjectProvider<ObservationRegistry> observationRegistry,
68+
ObjectProvider<WebClient.Builder> webClientBuilderProvider, RetryTemplate retryTemplate,
69+
ResponseErrorHandler responseErrorHandler, ObjectProvider<ObservationRegistry> observationRegistry,
6870
ObjectProvider<ChatModelObservationConvention> observationConvention, ToolCallingManager toolCallingManager,
6971
ObjectProvider<ToolExecutionEligibilityPredicate> toolExecutionEligibilityPredicate) {
7072

7173
var zhiPuAiApi = zhiPuAiApi(chatProperties.getBaseUrl(), commonProperties.getBaseUrl(),
7274
chatProperties.getApiKey(), commonProperties.getApiKey(),
73-
restClientBuilderProvider.getIfAvailable(RestClient::builder), responseErrorHandler);
75+
restClientBuilderProvider.getIfAvailable(RestClient::builder),
76+
webClientBuilderProvider.getIfAvailable(WebClient::builder), responseErrorHandler);
7477

7578
var chatModel = new ZhiPuAiChatModel(zhiPuAiApi, chatProperties.getOptions(), toolCallingManager, retryTemplate,
7679
observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP),
@@ -82,15 +85,23 @@ public ZhiPuAiChatModel zhiPuAiChatModel(ZhiPuAiConnectionProperties commonPrope
8285
}
8386

8487
private ZhiPuAiApi zhiPuAiApi(String baseUrl, String commonBaseUrl, String apiKey, String commonApiKey,
85-
RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler) {
88+
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
89+
ResponseErrorHandler responseErrorHandler) {
8690

8791
String resolvedBaseUrl = StringUtils.hasText(baseUrl) ? baseUrl : commonBaseUrl;
8892
Assert.hasText(resolvedBaseUrl, "ZhiPuAI base URL must be set");
8993

9094
String resolvedApiKey = StringUtils.hasText(apiKey) ? apiKey : commonApiKey;
9195
Assert.hasText(resolvedApiKey, "ZhiPuAI API key must be set");
9296

93-
return new ZhiPuAiApi(resolvedBaseUrl, resolvedApiKey, restClientBuilder, responseErrorHandler);
97+
return ZhiPuAiApi.builder()
98+
.baseUrl(resolvedBaseUrl)
99+
.apiKey(new SimpleApiKey(resolvedApiKey))
100+
.restClientBuilder(restClientBuilder)
101+
.webClientBuilder(webClientBuilder)
102+
.responseErrorHandler(responseErrorHandler)
103+
.build();
104+
94105
}
95106

96107
}

auto-configurations/models/spring-ai-autoconfigure-model-zhipuai/src/main/java/org/springframework/ai/model/zhipuai/autoconfigure/ZhiPuAiEmbeddingAutoConfiguration.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.micrometer.observation.ObservationRegistry;
2020

2121
import org.springframework.ai.embedding.observation.EmbeddingModelObservationConvention;
22+
import org.springframework.ai.model.SimpleApiKey;
2223
import org.springframework.ai.model.SpringAIModelProperties;
2324
import org.springframework.ai.model.SpringAIModels;
2425
import org.springframework.ai.retry.autoconfigure.SpringAiRetryAutoConfiguration;
@@ -37,6 +38,7 @@
3738
import org.springframework.util.StringUtils;
3839
import org.springframework.web.client.ResponseErrorHandler;
3940
import org.springframework.web.client.RestClient;
41+
import org.springframework.web.reactive.function.client.WebClient;
4042

4143
/**
4244
* Embedding {@link AutoConfiguration Auto-configuration} for ZhiPuAI.
@@ -54,13 +56,16 @@ public class ZhiPuAiEmbeddingAutoConfiguration {
5456
@Bean
5557
@ConditionalOnMissingBean
5658
public ZhiPuAiEmbeddingModel zhiPuAiEmbeddingModel(ZhiPuAiConnectionProperties commonProperties,
57-
ZhiPuAiEmbeddingProperties embeddingProperties, RestClient.Builder restClientBuilder,
58-
RetryTemplate retryTemplate, ResponseErrorHandler responseErrorHandler,
59-
ObjectProvider<ObservationRegistry> observationRegistry,
59+
ZhiPuAiEmbeddingProperties embeddingProperties,
60+
ObjectProvider<RestClient.Builder> restClientBuilderProvider,
61+
ObjectProvider<WebClient.Builder> webClientBuilderProvider, RetryTemplate retryTemplate,
62+
ResponseErrorHandler responseErrorHandler, ObjectProvider<ObservationRegistry> observationRegistry,
6063
ObjectProvider<EmbeddingModelObservationConvention> observationConvention) {
6164

6265
var zhiPuAiApi = zhiPuAiApi(embeddingProperties.getBaseUrl(), commonProperties.getBaseUrl(),
63-
embeddingProperties.getApiKey(), commonProperties.getApiKey(), restClientBuilder, responseErrorHandler);
66+
embeddingProperties.getApiKey(), commonProperties.getApiKey(),
67+
restClientBuilderProvider.getIfAvailable(RestClient::builder),
68+
webClientBuilderProvider.getIfAvailable(WebClient::builder), responseErrorHandler);
6469

6570
var embeddingModel = new ZhiPuAiEmbeddingModel(zhiPuAiApi, embeddingProperties.getMetadataMode(),
6671
embeddingProperties.getOptions(), retryTemplate,
@@ -72,15 +77,22 @@ public ZhiPuAiEmbeddingModel zhiPuAiEmbeddingModel(ZhiPuAiConnectionProperties c
7277
}
7378

7479
private ZhiPuAiApi zhiPuAiApi(String baseUrl, String commonBaseUrl, String apiKey, String commonApiKey,
75-
RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler) {
80+
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
81+
ResponseErrorHandler responseErrorHandler) {
7682

7783
String resolvedBaseUrl = StringUtils.hasText(baseUrl) ? baseUrl : commonBaseUrl;
7884
Assert.hasText(resolvedBaseUrl, "ZhiPuAI base URL must be set");
7985

8086
String resolvedApiKey = StringUtils.hasText(apiKey) ? apiKey : commonApiKey;
8187
Assert.hasText(resolvedApiKey, "ZhiPuAI API key must be set");
8288

83-
return new ZhiPuAiApi(resolvedBaseUrl, resolvedApiKey, restClientBuilder, responseErrorHandler);
89+
return ZhiPuAiApi.builder()
90+
.baseUrl(resolvedBaseUrl)
91+
.apiKey(new SimpleApiKey(resolvedApiKey))
92+
.restClientBuilder(restClientBuilder)
93+
.webClientBuilder(webClientBuilder)
94+
.responseErrorHandler(responseErrorHandler)
95+
.build();
8496
}
8597

8698
}

0 commit comments

Comments
 (0)