Package com.tnsai.llm.audio
Class WhisperClient
java.lang.Object
com.tnsai.llm.audio.WhisperClient
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic enumResponse format for transcription/translation.static classTranscription options.static classTranscription result with optional metadata.static classTranslation options. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a WhisperClient with default settings.WhisperClient(String model) Creates a WhisperClient with the specified model.WhisperClient(String baseUrl, String model, String apiKey) Creates a WhisperClient with full configuration. -
Method Summary
Modifier and TypeMethodDescriptionstatic WhisperClient.Builderbuilder()Builder for WhisperClient.getModel()Returns the model being used.transcribe(com.tnsai.models.chat.AudioPart audio) Transcribes audio with default options.transcribe(com.tnsai.models.chat.AudioPart audio, WhisperClient.TranscriptionOptions options) Transcribes audio with custom options.transcribe(File file) Transcribes audio from a file.translate(com.tnsai.models.chat.AudioPart audio, WhisperClient.TranslationOptions options) Translates audio to English with custom options.Translates audio to English.
-
Constructor Details
-
WhisperClient
public WhisperClient()Creates a WhisperClient with default settings. API key is read from OPENAI_API_KEY environment variable. -
WhisperClient
Creates a WhisperClient with the specified model.- Parameters:
model- The model to use (e.g., "whisper-1")
-
WhisperClient
Creates a WhisperClient with full configuration.- Parameters:
baseUrl- The base URL (null for default)model- The model to useapiKey- The API key (null to read from environment)
-
-
Method Details
-
transcribe
Transcribes audio from a file.- Parameters:
file- The audio file to transcribe- Returns:
- The transcribed text
-
transcribe
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 transcribeoptions- Transcription options- Returns:
- The transcription result
-
translate
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 translateoptions- Translation options- Returns:
- The translated English text
-
getModel
Returns the model being used. -
builder
Builder for WhisperClient.
-