Class OpenAiAudioApi
java.lang.Object
org.springframework.ai.openai.api.OpenAiAudioApi
Turn audio into text or text into audio. Based on
OpenAI Audio
- Since:
- 0.8.1
- Author:
- Christian Tzolov
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final record
Request to generates audio from the input text.static final record
The Transcription Object represents a verbose json transcription response returned by model, based on the provided input.static final record
Request to transcribe an audio file to text.static enum
The format of the transcript and translation outputs, in one of these options: json, text, srt, verbose_json, or vtt.static final record
Request to translate an audio file to English.static enum
TTS is an AI model that converts text to natural sounding spoken text.static enum
Whisper is a general-purpose speech recognition model. -
Constructor Summary
ConstructorDescriptionOpenAiAudioApi
(String openAiToken) Create a new audio api.OpenAiAudioApi
(String baseUrl, String apiKey, org.springframework.util.MultiValueMap<String, String> headers, org.springframework.web.client.RestClient.Builder restClientBuilder, org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder, org.springframework.web.client.ResponseErrorHandler responseErrorHandler) Create a new audio api.OpenAiAudioApi
(String baseUrl, String openAiToken, org.springframework.web.client.RestClient.Builder restClientBuilder, org.springframework.web.client.ResponseErrorHandler responseErrorHandler) Create a new audio api.OpenAiAudioApi
(String baseUrl, String apiKey, org.springframework.web.client.RestClient.Builder restClientBuilder, org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder, org.springframework.web.client.ResponseErrorHandler responseErrorHandler) Create a new audio api. -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ResponseEntity<byte[]>
createSpeech
(OpenAiAudioApi.SpeechRequest requestBody) Request to generates audio from the input text.org.springframework.http.ResponseEntity<?>
createTranscription
(OpenAiAudioApi.TranscriptionRequest requestBody) Transcribes audio into the input language.<T> org.springframework.http.ResponseEntity<T>
createTranscription
(OpenAiAudioApi.TranscriptionRequest requestBody, Class<T> responseType) Transcribes audio into the input language.org.springframework.http.ResponseEntity<?>
createTranslation
(OpenAiAudioApi.TranslationRequest requestBody) Translates audio into English.<T> org.springframework.http.ResponseEntity<T>
createTranslation
(OpenAiAudioApi.TranslationRequest requestBody, Class<T> responseType) Translates audio into English.reactor.core.publisher.Flux<org.springframework.http.ResponseEntity<byte[]>>
stream
(OpenAiAudioApi.SpeechRequest requestBody) Streams audio generated from the input text.
-
Constructor Details
-
OpenAiAudioApi
Create a new audio api.- Parameters:
openAiToken
- OpenAI apiKey.
-
OpenAiAudioApi
public OpenAiAudioApi(String baseUrl, String openAiToken, org.springframework.web.client.RestClient.Builder restClientBuilder, org.springframework.web.client.ResponseErrorHandler responseErrorHandler) Create a new audio api.- Parameters:
baseUrl
- api base URL.openAiToken
- OpenAI apiKey.restClientBuilder
- RestClient builder.responseErrorHandler
- Response error handler.
-
OpenAiAudioApi
public OpenAiAudioApi(String baseUrl, String apiKey, org.springframework.web.client.RestClient.Builder restClientBuilder, org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder, org.springframework.web.client.ResponseErrorHandler responseErrorHandler) Create a new audio api.- Parameters:
baseUrl
- api base URL.apiKey
- OpenAI apiKey.restClientBuilder
- RestClient builder.webClientBuilder
- WebClient builder.responseErrorHandler
- Response error handler.
-
OpenAiAudioApi
public OpenAiAudioApi(String baseUrl, String apiKey, org.springframework.util.MultiValueMap<String, String> headers, org.springframework.web.client.RestClient.Builder restClientBuilder, org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder, org.springframework.web.client.ResponseErrorHandler responseErrorHandler) Create a new audio api.- Parameters:
baseUrl
- api base URL.apiKey
- OpenAI apiKey.headers
- the http headers to use.restClientBuilder
- RestClient builder.webClientBuilder
- WebClient builder.responseErrorHandler
- Response error handler.
-
-
Method Details
-
createSpeech
public org.springframework.http.ResponseEntity<byte[]> createSpeech(OpenAiAudioApi.SpeechRequest requestBody) Request to generates audio from the input text.- Parameters:
requestBody
- The request body.- Returns:
- Response entity containing the audio binary.
-
stream
public reactor.core.publisher.Flux<org.springframework.http.ResponseEntity<byte[]>> stream(OpenAiAudioApi.SpeechRequest requestBody) Streams audio generated from the input text. This method sends a POST request to the OpenAI API to generate audio from the provided text. The audio is streamed back as a Flux of ResponseEntity objects, each containing a byte array of the audio data.- Parameters:
requestBody
- The request body containing the details for the audio generation, such as the input text, model, voice, and response format.- Returns:
- A Flux of ResponseEntity objects, each containing a byte array of the audio data.
-
createTranscription
public org.springframework.http.ResponseEntity<?> createTranscription(OpenAiAudioApi.TranscriptionRequest requestBody) Transcribes audio into the input language.- Parameters:
requestBody
- The request body.- Returns:
- Response entity containing the transcribed text in either json or text format.
-
createTranscription
public <T> org.springframework.http.ResponseEntity<T> createTranscription(OpenAiAudioApi.TranscriptionRequest requestBody, Class<T> responseType) Transcribes audio into the input language. The response type is specified by the responseType parameter.- Type Parameters:
T
- The response type.- Parameters:
requestBody
- The request body.responseType
- The response type class.- Returns:
- Response entity containing the transcribed text in the responseType format.
-
createTranslation
public org.springframework.http.ResponseEntity<?> createTranslation(OpenAiAudioApi.TranslationRequest requestBody) Translates audio into English.- Parameters:
requestBody
- The request body.- Returns:
- Response entity containing the transcribed text in either json or text format.
-
createTranslation
public <T> org.springframework.http.ResponseEntity<T> createTranslation(OpenAiAudioApi.TranslationRequest requestBody, Class<T> responseType) Translates audio into English. The response type is specified by the responseType parameter.- Type Parameters:
T
- The response type.- Parameters:
requestBody
- The request body.responseType
- The response type class.- Returns:
- Response entity containing the transcribed text in the responseType format.
-