|
| 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.elevenlabs; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | +import reactor.core.publisher.Flux; |
| 24 | + |
| 25 | +import org.springframework.ai.elevenlabs.api.ElevenLabsApi; |
| 26 | +import org.springframework.ai.elevenlabs.tts.Speech; |
| 27 | +import org.springframework.ai.elevenlabs.tts.StreamingTextToSpeechModel; |
| 28 | +import org.springframework.ai.elevenlabs.tts.TextToSpeechModel; |
| 29 | +import org.springframework.ai.elevenlabs.tts.TextToSpeechPrompt; |
| 30 | +import org.springframework.ai.elevenlabs.tts.TextToSpeechResponse; |
| 31 | +import org.springframework.ai.retry.RetryUtils; |
| 32 | +import org.springframework.retry.support.RetryTemplate; |
| 33 | +import org.springframework.util.Assert; |
| 34 | +import org.springframework.util.LinkedMultiValueMap; |
| 35 | +import org.springframework.util.MultiValueMap; |
| 36 | + |
| 37 | +/** |
| 38 | + * Implementation of the {@link TextToSpeechModel} and {@link StreamingTextToSpeechModel} |
| 39 | + * interfaces |
| 40 | + * |
| 41 | + * @author Alexandros Pappas |
| 42 | + */ |
| 43 | +public class ElevenLabsTextToSpeechModel implements TextToSpeechModel, StreamingTextToSpeechModel { |
| 44 | + |
| 45 | + private final Logger logger = LoggerFactory.getLogger(getClass()); |
| 46 | + |
| 47 | + private final ElevenLabsApi elevenLabsApi; |
| 48 | + |
| 49 | + private final RetryTemplate retryTemplate; |
| 50 | + |
| 51 | + private final ElevenLabsTextToSpeechOptions defaultOptions; |
| 52 | + |
| 53 | + public ElevenLabsTextToSpeechModel(ElevenLabsApi elevenLabsApi, ElevenLabsTextToSpeechOptions defaultOptions) { |
| 54 | + this(elevenLabsApi, defaultOptions, RetryUtils.DEFAULT_RETRY_TEMPLATE); |
| 55 | + } |
| 56 | + |
| 57 | + public ElevenLabsTextToSpeechModel(ElevenLabsApi elevenLabsApi, ElevenLabsTextToSpeechOptions defaultOptions, |
| 58 | + RetryTemplate retryTemplate) { |
| 59 | + Assert.notNull(elevenLabsApi, "ElevenLabsApi must not be null"); |
| 60 | + Assert.notNull(defaultOptions, "ElevenLabsSpeechOptions must not be null"); |
| 61 | + Assert.notNull(retryTemplate, "RetryTemplate must not be null"); |
| 62 | + |
| 63 | + this.elevenLabsApi = elevenLabsApi; |
| 64 | + this.defaultOptions = defaultOptions; |
| 65 | + this.retryTemplate = retryTemplate; |
| 66 | + } |
| 67 | + |
| 68 | + public static Builder builder() { |
| 69 | + return new Builder(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public TextToSpeechResponse call(TextToSpeechPrompt prompt) { |
| 74 | + ElevenLabsApi.SpeechRequest request = createRequest(prompt); |
| 75 | + String voiceId = getOptions(prompt).getVoice(); |
| 76 | + |
| 77 | + MultiValueMap<String, String> queryParameters = new LinkedMultiValueMap<>(); |
| 78 | + if (getOptions(prompt).getEnableLogging() != null) { |
| 79 | + queryParameters.add("enable_logging", getOptions(prompt).getEnableLogging().toString()); |
| 80 | + } |
| 81 | + if (getOptions(prompt).getFormat() != null) { |
| 82 | + queryParameters.add("output_format", getOptions(prompt).getFormat()); |
| 83 | + } |
| 84 | + |
| 85 | + byte[] audioData = retryTemplate.execute(context -> { |
| 86 | + var response = elevenLabsApi.textToSpeech(request, voiceId, queryParameters); |
| 87 | + if (response.getBody() == null) { |
| 88 | + logger.warn("No speech response returned for request: {}", request); |
| 89 | + return new byte[0]; |
| 90 | + } |
| 91 | + return response.getBody(); |
| 92 | + }); |
| 93 | + |
| 94 | + return new TextToSpeechResponse(List.of(new Speech(audioData))); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Flux<TextToSpeechResponse> stream(TextToSpeechPrompt prompt) { |
| 99 | + ElevenLabsApi.SpeechRequest request = createRequest(prompt); |
| 100 | + String voiceId = getOptions(prompt).getVoice(); |
| 101 | + |
| 102 | + MultiValueMap<String, String> queryParameters = new LinkedMultiValueMap<>(); |
| 103 | + if (getOptions(prompt).getEnableLogging() != null) { |
| 104 | + queryParameters.add("enable_logging", getOptions(prompt).getEnableLogging().toString()); |
| 105 | + } |
| 106 | + if (getOptions(prompt).getFormat() != null) { |
| 107 | + queryParameters.add("output_format", getOptions(prompt).getFormat()); |
| 108 | + } |
| 109 | + |
| 110 | + return retryTemplate.execute(context -> elevenLabsApi.textToSpeechStream(request, voiceId, queryParameters) |
| 111 | + .map(entity -> new TextToSpeechResponse(List.of(new Speech(entity.getBody()))))); |
| 112 | + } |
| 113 | + |
| 114 | + private ElevenLabsApi.SpeechRequest createRequest(TextToSpeechPrompt prompt) { |
| 115 | + ElevenLabsTextToSpeechOptions options = getOptions(prompt); |
| 116 | + |
| 117 | + String voiceId = options.getVoice(); |
| 118 | + Assert.notNull(voiceId, "A voiceId must be specified in the ElevenLabsSpeechOptions."); |
| 119 | + |
| 120 | + String text = prompt.getInstructions().getText(); |
| 121 | + Assert.hasText(text, "Prompt must contain text to convert to speech."); |
| 122 | + |
| 123 | + return ElevenLabsApi.SpeechRequest.builder() |
| 124 | + .text(text) |
| 125 | + .modelId(options.getModelId()) |
| 126 | + .voiceSettings(options.getVoiceSettings()) |
| 127 | + .languageCode(options.getLanguageCode()) |
| 128 | + .pronunciationDictionaryLocators(options.getPronunciationDictionaryLocators()) |
| 129 | + .seed(options.getSeed()) |
| 130 | + .previousText(options.getPreviousText()) |
| 131 | + .nextText(options.getNextText()) |
| 132 | + .previousRequestIds(options.getPreviousRequestIds()) |
| 133 | + .nextRequestIds(options.getNextRequestIds()) |
| 134 | + .usePvcAsIvc(options.getUsePvcAsIvc()) |
| 135 | + .applyTextNormalization(options.getApplyTextNormalization()) |
| 136 | + .build(); |
| 137 | + } |
| 138 | + |
| 139 | + private ElevenLabsTextToSpeechOptions getOptions(TextToSpeechPrompt prompt) { |
| 140 | + ElevenLabsTextToSpeechOptions runtimeOptions = (prompt |
| 141 | + .getOptions() instanceof ElevenLabsTextToSpeechOptions elevenLabsSpeechOptions) ? elevenLabsSpeechOptions |
| 142 | + : null; |
| 143 | + return (runtimeOptions != null) ? merge(runtimeOptions, this.defaultOptions) : this.defaultOptions; |
| 144 | + } |
| 145 | + |
| 146 | + private ElevenLabsTextToSpeechOptions merge(ElevenLabsTextToSpeechOptions runtimeOptions, |
| 147 | + ElevenLabsTextToSpeechOptions defaultOptions) { |
| 148 | + return ElevenLabsTextToSpeechOptions.builder() |
| 149 | + .modelId(getOrDefault(runtimeOptions.getModelId(), defaultOptions.getModelId())) |
| 150 | + .voice(getOrDefault(runtimeOptions.getVoice(), defaultOptions.getVoice())) |
| 151 | + .voiceId(getOrDefault(runtimeOptions.getVoiceId(), defaultOptions.getVoiceId())) |
| 152 | + .format(getOrDefault(runtimeOptions.getFormat(), defaultOptions.getFormat())) |
| 153 | + .outputFormat(getOrDefault(runtimeOptions.getOutputFormat(), defaultOptions.getOutputFormat())) |
| 154 | + .voiceSettings(getOrDefault(runtimeOptions.getVoiceSettings(), defaultOptions.getVoiceSettings())) |
| 155 | + .languageCode(getOrDefault(runtimeOptions.getLanguageCode(), defaultOptions.getLanguageCode())) |
| 156 | + .pronunciationDictionaryLocators(getOrDefault(runtimeOptions.getPronunciationDictionaryLocators(), |
| 157 | + defaultOptions.getPronunciationDictionaryLocators())) |
| 158 | + .seed(getOrDefault(runtimeOptions.getSeed(), defaultOptions.getSeed())) |
| 159 | + .previousText(getOrDefault(runtimeOptions.getPreviousText(), defaultOptions.getPreviousText())) |
| 160 | + .nextText(getOrDefault(runtimeOptions.getNextText(), defaultOptions.getNextText())) |
| 161 | + .previousRequestIds( |
| 162 | + getOrDefault(runtimeOptions.getPreviousRequestIds(), defaultOptions.getPreviousRequestIds())) |
| 163 | + .nextRequestIds(getOrDefault(runtimeOptions.getNextRequestIds(), defaultOptions.getNextRequestIds())) |
| 164 | + .usePvcAsIvc(getOrDefault(runtimeOptions.getUsePvcAsIvc(), defaultOptions.getUsePvcAsIvc())) |
| 165 | + .applyTextNormalization(getOrDefault(runtimeOptions.getApplyTextNormalization(), |
| 166 | + defaultOptions.getApplyTextNormalization())) |
| 167 | + .build(); |
| 168 | + } |
| 169 | + |
| 170 | + private <T> T getOrDefault(T runtimeValue, T defaultValue) { |
| 171 | + return runtimeValue != null ? runtimeValue : defaultValue; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public ElevenLabsTextToSpeechOptions getDefaultOptions() { |
| 176 | + return this.defaultOptions; |
| 177 | + } |
| 178 | + |
| 179 | + public static class Builder { |
| 180 | + |
| 181 | + private ElevenLabsApi elevenLabsApi; |
| 182 | + |
| 183 | + private RetryTemplate retryTemplate = RetryUtils.DEFAULT_RETRY_TEMPLATE; |
| 184 | + |
| 185 | + private ElevenLabsTextToSpeechOptions defaultOptions = ElevenLabsTextToSpeechOptions.builder().build(); |
| 186 | + |
| 187 | + public Builder elevenLabsApi(ElevenLabsApi elevenLabsApi) { |
| 188 | + this.elevenLabsApi = elevenLabsApi; |
| 189 | + return this; |
| 190 | + } |
| 191 | + |
| 192 | + public Builder retryTemplate(RetryTemplate retryTemplate) { |
| 193 | + this.retryTemplate = retryTemplate; |
| 194 | + return this; |
| 195 | + } |
| 196 | + |
| 197 | + public Builder defaultOptions(ElevenLabsTextToSpeechOptions defaultOptions) { |
| 198 | + this.defaultOptions = defaultOptions; |
| 199 | + return this; |
| 200 | + } |
| 201 | + |
| 202 | + public ElevenLabsTextToSpeechModel build() { |
| 203 | + Assert.notNull(elevenLabsApi, "ElevenLabsApi must not be null"); |
| 204 | + Assert.notNull(defaultOptions, "ElevenLabsSpeechOptions must not be null"); |
| 205 | + return new ElevenLabsTextToSpeechModel(elevenLabsApi, defaultOptions, retryTemplate); |
| 206 | + } |
| 207 | + |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments