|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.model.elevenlabs.autoconfigure; |
| 18 | + |
| 19 | +import org.springframework.ai.elevenlabs.ElevenLabsTextToSpeechModel; |
| 20 | +import org.springframework.ai.elevenlabs.api.ElevenLabsApi; |
| 21 | +import org.springframework.ai.retry.autoconfigure.SpringAiRetryAutoConfiguration; |
| 22 | +import org.springframework.beans.factory.ObjectProvider; |
| 23 | +import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 24 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 25 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 26 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 27 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 28 | +import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration; |
| 29 | +import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration; |
| 30 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 31 | +import org.springframework.context.annotation.Bean; |
| 32 | +import org.springframework.retry.support.RetryTemplate; |
| 33 | +import org.springframework.web.client.ResponseErrorHandler; |
| 34 | +import org.springframework.web.client.RestClient; |
| 35 | +import org.springframework.web.reactive.function.client.WebClient; |
| 36 | + |
| 37 | +/** |
| 38 | + * {@link AutoConfiguration Auto-configuration} for ElevenLabs. |
| 39 | + * |
| 40 | + * @author Alexandros Pappas |
| 41 | + */ |
| 42 | +@AutoConfiguration(after = { RestClientAutoConfiguration.class, SpringAiRetryAutoConfiguration.class, |
| 43 | + WebClientAutoConfiguration.class }) |
| 44 | +@ConditionalOnClass(ElevenLabsApi.class) |
| 45 | +@EnableConfigurationProperties({ ElevenLabsSpeechProperties.class, ElevenLabsConnectionProperties.class }) |
| 46 | +@ConditionalOnProperty(prefix = ElevenLabsSpeechProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true", |
| 47 | + matchIfMissing = true) |
| 48 | +@ImportAutoConfiguration(classes = { SpringAiRetryAutoConfiguration.class, RestClientAutoConfiguration.class, |
| 49 | + WebClientAutoConfiguration.class }) |
| 50 | +public class ElevenLabsAutoConfiguration { |
| 51 | + |
| 52 | + @Bean |
| 53 | + @ConditionalOnMissingBean |
| 54 | + public ElevenLabsApi elevenLabsApi(ElevenLabsConnectionProperties connectionProperties, |
| 55 | + ObjectProvider<RestClient.Builder> restClientBuilderProvider, |
| 56 | + ObjectProvider<WebClient.Builder> webClientBuilderProvider, ResponseErrorHandler responseErrorHandler) { |
| 57 | + |
| 58 | + return ElevenLabsApi.builder() |
| 59 | + .baseUrl(connectionProperties.getBaseUrl()) |
| 60 | + .apiKey(connectionProperties.getApiKey()) |
| 61 | + .restClientBuilder(restClientBuilderProvider.getIfAvailable(RestClient::builder)) |
| 62 | + .webClientBuilder(webClientBuilderProvider.getIfAvailable(WebClient::builder)) |
| 63 | + .responseErrorHandler(responseErrorHandler) |
| 64 | + .build(); |
| 65 | + } |
| 66 | + |
| 67 | + @Bean |
| 68 | + @ConditionalOnMissingBean |
| 69 | + public ElevenLabsTextToSpeechModel elevenLabsSpeechModel(ElevenLabsApi elevenLabsApi, |
| 70 | + ElevenLabsSpeechProperties speechProperties, RetryTemplate retryTemplate) { |
| 71 | + |
| 72 | + return ElevenLabsTextToSpeechModel.builder() |
| 73 | + .elevenLabsApi(elevenLabsApi) |
| 74 | + .defaultOptions(speechProperties.getOptions()) |
| 75 | + .retryTemplate(retryTemplate) |
| 76 | + .build(); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments