Class WhisperClient

java.lang.Object
com.tnsai.llm.audio.WhisperClient

public class WhisperClient extends Object
OpenAI Whisper client for audio transcription and translation.

Whisper is a general-purpose speech recognition model that can:

  • Transcribe audio in multiple languages
  • Translate non-English audio to English
  • Detect spoken language
  • Provide word-level timestamps

Supported audio formats:

  • mp3, mp4, mpeg, mpga, m4a, wav, webm

Usage examples:


 WhisperClient whisper = new WhisperClient();

 // Simple transcription
 String text = whisper.transcribe(new File("speech.mp3"));

 // Transcription with options
 TranscriptionResult result = whisper.transcribe(
     AudioPart.fromFile(new File("meeting.wav")),
     TranscriptionOptions.builder()
         .language("en")
         .responseFormat(ResponseFormat.VERBOSE_JSON)
         .timestampGranularities(List.of("word", "segment"))
         .build()
 );

 // Translation to English
 String english = whisper.translate(new File("french_speech.mp3"));
 

Configuration via environment variables:

  • OPENAI_API_KEY - Required API key
  • OPENAI_BASE_URL - Optional, defaults to https://api.openai.com/v1
See Also:
  • Constructor Details

    • WhisperClient

      public WhisperClient()
      Creates a WhisperClient with default settings. API key is read from OPENAI_API_KEY environment variable.
    • WhisperClient

      public WhisperClient(String model)
      Creates a WhisperClient with the specified model.
      Parameters:
      model - The model to use (e.g., "whisper-1")
    • WhisperClient

      public WhisperClient(String baseUrl, String model, String apiKey)
      Creates a WhisperClient with full configuration.
      Parameters:
      baseUrl - The base URL (null for default)
      model - The model to use
      apiKey - The API key (null to read from environment)
  • Method Details

    • transcribe

      public String transcribe(File file)
      Transcribes audio from a file.
      Parameters:
      file - The audio file to transcribe
      Returns:
      The transcribed text
    • transcribe

      public WhisperClient.TranscriptionResult transcribe(com.tnsai.models.chat.AudioPart audio)
      Transcribes audio with default options.
      Parameters:
      audio - The audio to transcribe
      Returns:
      The transcription result
    • transcribe

      public WhisperClient.TranscriptionResult transcribe(com.tnsai.models.chat.AudioPart audio, WhisperClient.TranscriptionOptions options)
      Transcribes audio with custom options.
      Parameters:
      audio - The audio to transcribe
      options - Transcription options
      Returns:
      The transcription result
    • translate

      public String translate(File file)
      Translates audio to English. Input can be in any supported language.
      Parameters:
      file - The audio file to translate
      Returns:
      The translated English text
    • translate

      public String translate(com.tnsai.models.chat.AudioPart audio, WhisperClient.TranslationOptions options)
      Translates audio to English with custom options.
      Parameters:
      audio - The audio to translate
      options - Translation options
      Returns:
      The translated English text
    • getModel

      public String getModel()
      Returns the model being used.
    • builder

      public static WhisperClient.Builder builder()
      Builder for WhisperClient.