|
| 1 | +/* |
| 2 | + * Copyright 2023-2023 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 | +package org.springframework.ai.openai; |
| 17 | + |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | +import org.springframework.ai.chat.metadata.RateLimit; |
| 21 | +import org.springframework.ai.model.ModelOptionsUtils; |
| 22 | +import org.springframework.ai.openai.api.OpenAiApi; |
| 23 | +import org.springframework.ai.openai.api.OpenAiApi.OpenAiApiException; |
| 24 | +import org.springframework.ai.openai.metadata.OpenAiSpeechResponseMetadata; |
| 25 | +import org.springframework.ai.openai.metadata.support.OpenAiResponseHeaderExtractor; |
| 26 | +import org.springframework.ai.speech.*; |
| 27 | +import org.springframework.http.ResponseEntity; |
| 28 | +import org.springframework.retry.support.RetryTemplate; |
| 29 | +import org.springframework.util.Assert; |
| 30 | +import reactor.core.publisher.Flux; |
| 31 | + |
| 32 | +import java.time.Duration; |
| 33 | + |
| 34 | +/** |
| 35 | + * {@link SpeechClient} implementation for {@literal OpenAI} backed by {@link OpenAiApi}. |
| 36 | + * |
| 37 | + * @author Ahmed Yousri |
| 38 | + * @see SpeechClient |
| 39 | + * @see OpenAiApi |
| 40 | + */ |
| 41 | +public class OpenAiSpeechClient implements SpeechClient, StreamingSpeechClient { |
| 42 | + |
| 43 | + private final Logger logger = LoggerFactory.getLogger(getClass()); |
| 44 | + |
| 45 | + private OpenAiSpeechOptions defaultOptions = OpenAiSpeechOptions.builder() |
| 46 | + .withModel("tts-1") |
| 47 | + .withResponseFormat("mp3") |
| 48 | + .withSpeed(1.0f) |
| 49 | + .withVoice("alloy") |
| 50 | + .build(); |
| 51 | + |
| 52 | + public final RetryTemplate retryTemplate = RetryTemplate.builder() |
| 53 | + .maxAttempts(10) |
| 54 | + .retryOn(OpenAiApiException.class) |
| 55 | + .exponentialBackoff(Duration.ofMillis(2000), 5, Duration.ofMillis(3 * 60000)) |
| 56 | + .build(); |
| 57 | + |
| 58 | + private final OpenAiApi openAiApi; |
| 59 | + |
| 60 | + public OpenAiSpeechClient(OpenAiApi openAiApi) { |
| 61 | + Assert.notNull(openAiApi, "OpenAiApi must not be null"); |
| 62 | + this.openAiApi = openAiApi; |
| 63 | + } |
| 64 | + |
| 65 | + public OpenAiSpeechClient withDefaultOptions(OpenAiSpeechOptions options) { |
| 66 | + this.defaultOptions = options; |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public SpeechResponse call(SpeechPrompt speechPrompt) { |
| 72 | + |
| 73 | + return this.retryTemplate.execute(ctx -> { |
| 74 | + |
| 75 | + OpenAiApi.SpeechRequest speechRequest = createRequest(speechPrompt); |
| 76 | + |
| 77 | + ResponseEntity<OpenAiApi.SpeechResponse> SpeechEntity = this.openAiApi |
| 78 | + .textToSpeechEntityJson(speechRequest); |
| 79 | + var speech = SpeechEntity.getBody(); |
| 80 | + |
| 81 | + if (speech == null) { |
| 82 | + logger.warn("No speech response returned for speechRequest: {}", speechRequest); |
| 83 | + return new SpeechResponse(convertResponse(OpenAiApi.SpeechResponse.NULL)); |
| 84 | + } |
| 85 | + |
| 86 | + RateLimit rateLimits = OpenAiResponseHeaderExtractor.extractAiResponseHeaders(SpeechEntity); |
| 87 | + |
| 88 | + return new SpeechResponse(convertResponse(speech), new OpenAiSpeechResponseMetadata(rateLimits)); |
| 89 | + |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + private OpenAiApi.SpeechRequest createRequest(SpeechPrompt speechPrompt) { |
| 94 | + String instructions = speechPrompt.getInstructions().get(0).getText(); |
| 95 | + |
| 96 | + OpenAiApi.SpeechRequest speechRequest = new OpenAiApi.SpeechRequest(instructions); |
| 97 | + |
| 98 | + if (this.defaultOptions != null) { |
| 99 | + speechRequest = ModelOptionsUtils.merge(this.defaultOptions, speechRequest, OpenAiApi.SpeechRequest.class); |
| 100 | + } |
| 101 | + |
| 102 | + if (speechPrompt.getOptions() != null) { |
| 103 | + speechRequest = ModelOptionsUtils.merge(toOpenAiSpeechOptions(speechPrompt.getOptions()), speechRequest, |
| 104 | + OpenAiApi.SpeechRequest.class); |
| 105 | + } |
| 106 | + return speechRequest; |
| 107 | + } |
| 108 | + |
| 109 | + private Speech convertResponse(OpenAiApi.SpeechResponse speechResponse) { |
| 110 | + return new Speech(speechResponse.audio()); |
| 111 | + } |
| 112 | + |
| 113 | + private OpenAiSpeechOptions toOpenAiSpeechOptions(SpeechOptions runtimeSpeechOptions) { |
| 114 | + OpenAiSpeechOptions.Builder openAiSpeechOptionBuilder = OpenAiSpeechOptions.builder(); |
| 115 | + if (runtimeSpeechOptions != null) { |
| 116 | + // Handle portable speech options |
| 117 | + if (runtimeSpeechOptions.getModel() != null) { |
| 118 | + openAiSpeechOptionBuilder.withModel(runtimeSpeechOptions.getModel()); |
| 119 | + } |
| 120 | + if (runtimeSpeechOptions.getResponseFormat() != null) { |
| 121 | + openAiSpeechOptionBuilder.withResponseFormat(runtimeSpeechOptions.getResponseFormat()); |
| 122 | + } |
| 123 | + if (runtimeSpeechOptions.getSpeed() != null) { |
| 124 | + openAiSpeechOptionBuilder.withSpeed(runtimeSpeechOptions.getSpeed()); |
| 125 | + } |
| 126 | + if (runtimeSpeechOptions.getVoice() != null) { |
| 127 | + openAiSpeechOptionBuilder.withVoice(runtimeSpeechOptions.getVoice()); |
| 128 | + } |
| 129 | + // Handle OpenAI specific speech options |
| 130 | + if (runtimeSpeechOptions instanceof OpenAiSpeechOptions) { |
| 131 | + OpenAiSpeechOptions runtimeOpenAiSpeechOptions = (OpenAiSpeechOptions) runtimeSpeechOptions; |
| 132 | + if (runtimeOpenAiSpeechOptions.getModel() != null) { |
| 133 | + openAiSpeechOptionBuilder.withModel(runtimeOpenAiSpeechOptions.getModel()); |
| 134 | + } |
| 135 | + if (runtimeOpenAiSpeechOptions.getSpeed() != null) { |
| 136 | + openAiSpeechOptionBuilder.withSpeed(runtimeOpenAiSpeechOptions.getSpeed()); |
| 137 | + } |
| 138 | + if (runtimeOpenAiSpeechOptions.getVoice() != null) { |
| 139 | + openAiSpeechOptionBuilder.withVoice(runtimeOpenAiSpeechOptions.getVoice()); |
| 140 | + } |
| 141 | + if (runtimeOpenAiSpeechOptions.getResponseFormat() != null) { |
| 142 | + openAiSpeechOptionBuilder.withResponseFormat(runtimeOpenAiSpeechOptions.getResponseFormat()); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + return openAiSpeechOptionBuilder.build(); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public Flux<SpeechResponse> stream(SpeechPrompt prompt) { |
| 151 | + return this.openAiApi.textToSpeechStreaming(this.createRequest(prompt)) |
| 152 | + .map(entity -> new SpeechResponse(new Speech(entity.getBody()), |
| 153 | + new OpenAiSpeechResponseMetadata(OpenAiResponseHeaderExtractor.extractAiResponseHeaders(entity)))); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments