Class OpenAiAudioApi

java.lang.Object
org.springframework.ai.openai.api.OpenAiAudioApi

public class OpenAiAudioApi extends Object
Turn audio into text or text into audio. Based on OpenAI Audio
Since:
0.8.1
Author:
Christian Tzolov
  • Constructor Details

    • OpenAiAudioApi

      public OpenAiAudioApi(String openAiToken)
      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.