Skip to content

Commit 2449d1c

Browse files
author
Manuel Andreo Garcia
committed
allow multiple ordered customizers (like the RestClientCustomizer)
1 parent 8fb9d33 commit 2449d1c

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/azure/openai/AzureOpenAiAutoConfiguration.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public OpenAIClientBuilderCustomizer openAIClientBuilderCustomizer() {
7474
@Bean
7575
@ConditionalOnMissingBean // ({ OpenAIClient.class, TokenCredential.class })
7676
public OpenAIClientBuilder openAIClientBuilder(AzureOpenAiConnectionProperties connectionProperties,
77-
OpenAIClientBuilderCustomizer customizer) {
77+
ObjectProvider<OpenAIClientBuilderCustomizer> customizers) {
7878

7979
if (StringUtils.hasText(connectionProperties.getApiKey())) {
8080

@@ -89,7 +89,7 @@ public OpenAIClientBuilder openAIClientBuilder(AzureOpenAiConnectionProperties c
8989
OpenAIClientBuilder clientBuilder = new OpenAIClientBuilder().endpoint(connectionProperties.getEndpoint())
9090
.credential(new AzureKeyCredential(connectionProperties.getApiKey()))
9191
.clientOptions(clientOptions);
92-
customizer.customize(clientBuilder);
92+
applyOpenAIClientBuilderCustomizers(clientBuilder, customizers);
9393
return clientBuilder;
9494
}
9595

@@ -99,7 +99,7 @@ public OpenAIClientBuilder openAIClientBuilder(AzureOpenAiConnectionProperties c
9999
OpenAIClientBuilder clientBuilder = new OpenAIClientBuilder().endpoint("https://api.openai.com/v1")
100100
.credential(new KeyCredential(connectionProperties.getOpenAiApiKey()))
101101
.clientOptions(new ClientOptions().setApplicationId(APPLICATION_ID));
102-
customizer.customize(clientBuilder);
102+
applyOpenAIClientBuilderCustomizers(clientBuilder, customizers);
103103
return clientBuilder;
104104
}
105105

@@ -110,15 +110,15 @@ public OpenAIClientBuilder openAIClientBuilder(AzureOpenAiConnectionProperties c
110110
@ConditionalOnMissingBean
111111
@ConditionalOnBean(TokenCredential.class)
112112
public OpenAIClientBuilder openAIClientWithTokenCredential(AzureOpenAiConnectionProperties connectionProperties,
113-
TokenCredential tokenCredential, OpenAIClientBuilderCustomizer customizer) {
113+
TokenCredential tokenCredential, ObjectProvider<OpenAIClientBuilderCustomizer> customizers) {
114114

115115
Assert.notNull(tokenCredential, "TokenCredential must not be null");
116116
Assert.hasText(connectionProperties.getEndpoint(), "Endpoint must not be empty");
117117

118118
OpenAIClientBuilder clientBuilder = new OpenAIClientBuilder().endpoint(connectionProperties.getEndpoint())
119119
.credential(tokenCredential)
120120
.clientOptions(new ClientOptions().setApplicationId(APPLICATION_ID));
121-
customizer.customize(clientBuilder);
121+
applyOpenAIClientBuilderCustomizers(clientBuilder, customizers);
122122
return clientBuilder;
123123
}
124124

@@ -184,4 +184,9 @@ public AzureOpenAiAudioTranscriptionModel azureOpenAiAudioTranscriptionModel(Ope
184184
return new AzureOpenAiAudioTranscriptionModel(openAIClient.buildClient(), audioProperties.getOptions());
185185
}
186186

187+
private void applyOpenAIClientBuilderCustomizers(OpenAIClientBuilder clientBuilder,
188+
ObjectProvider<OpenAIClientBuilderCustomizer> customizers) {
189+
customizers.orderedStream().forEach(customizer -> customizer.customize(clientBuilder));
190+
}
191+
187192
}

spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/azure/AzureOpenAiAutoConfigurationIT.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,17 @@ void audioTranscriptionActivation() {
232232

233233
@Test
234234
void openAIClientBuilderCustomizer() {
235-
AtomicBoolean customized = new AtomicBoolean(false);
235+
AtomicBoolean firstCustomizationApplied = new AtomicBoolean(false);
236+
AtomicBoolean secondCustomizationApplied = new AtomicBoolean(false);
236237
this.contextRunner
237-
.withBean(OpenAIClientBuilderCustomizer.class,
238-
(OpenAIClientBuilderCustomizer) clientBuilder -> customized.set(true))
238+
.withBean("first", OpenAIClientBuilderCustomizer.class,
239+
() -> clientBuilder -> firstCustomizationApplied.set(true))
240+
.withBean("second", OpenAIClientBuilderCustomizer.class,
241+
() -> clientBuilder -> secondCustomizationApplied.set(true))
239242
.run(context -> {
240243
context.getBean(OpenAIClientBuilder.class);
241-
assertThat(customized.get()).isEqualTo(true);
244+
assertThat(firstCustomizationApplied.get()).isTrue();
245+
assertThat(secondCustomizationApplied.get()).isTrue();
242246
});
243247
}
244248

0 commit comments

Comments
 (0)