Package dyntabs.ai

Class ListenBuilder

java.lang.Object
dyntabs.ai.ListenBuilder

public class ListenBuilder extends Object
Turns recorded speech into text — the inbound half of the voice channel.

The shortest useful form


 String text = EasyAI.listen().from(audioBytes, "audio/webm");
 

That is the whole thing. No model builder, no request object, no Base64, no provider name: the model, key and base URL come from easyai.properties, and — the point worth knowing — they default to your chat settings. An app that already talks to an OpenAI-compatible provider for chat gains speech input with no new configuration at all (see VoiceConfig).

Why this exists at all

Voice should be a new channel into the assistant you already have, not a second brain. The transcript this returns is ordinary text, so you hand it to the same assistant — and spoken turns inherit your tools, memory and context for free. That is also why this class returns a String rather than an answer: transcribing and deciding are deliberately separate jobs.

Overriding per call


 String text = EasyAI.listen()
                     .withModel("whisper-large-v3")      // e.g. when the base URL points at Groq
                     .withLanguage("sr")                 // skip language detection
                     .withEventListener(e -> log.info("{}", e))
                     .from(audioBytes, "audio/webm");
 

Analogy: a dictation service. You slide in the recording and get back a typed page. It does not answer the letter — that is the next desk along.

See Also:
  • Method Details

    • withConfig

      public ListenBuilder withConfig(VoiceConfig config)
      Replace the whole configuration instead of overriding single values.
      Parameters:
      config - fully built voice settings; null restores properties-based defaults
      Returns:
      this builder
    • withModel

      public ListenBuilder withModel(String modelName)
      Name the transcription model for this call.

      Needed when the base URL points somewhere other than OpenAI — Groq's Whisper is whisper-large-v3, OpenAI's default is VoiceConfig.DEFAULT_TRANSCRIPTION_MODEL. Nothing is guessed from the URL.

      Parameters:
      modelName - the provider's model id
      Returns:
      this builder
    • withLanguage

      public ListenBuilder withLanguage(String language)
      Tell the model which language to expect, as an ISO-639-1 code (e.g. "sr", "en").

      Leave it unset to let the model detect the language — which is what you want for a bilingual audience, and a real advantage over a browser recognizer pinned to one locale. Set it when you know the language and want the accuracy.

      Parameters:
      language - ISO-639-1 language code
      Returns:
      this builder
    • withApiKey

      public ListenBuilder withApiKey(String apiKey)
      Override the API key for this call (otherwise easyai.stt.api-key, then easyai.api-key).
      Parameters:
      apiKey - the provider API key
      Returns:
      this builder
    • withBaseUrl

      public ListenBuilder withBaseUrl(String baseUrl)
      Override the base URL for this call (otherwise easyai.stt.base-url, then easyai.base-url) — this is how you point transcription at any OpenAI-compatible service.
      Parameters:
      baseUrl - e.g. https://api.groq.com/openai/v1/
      Returns:
      this builder
    • withEventListener

      public ListenBuilder withEventListener(EasyAIListener eventListener)
      Register a listener for the live EasyAIEvent stream, so a transcription shows up in your activity timeline like every other EasyAI operation.

      Emits EasyAIEvent.Phase.STARTED when the call begins and EasyAIEvent.Phase.RESULT carrying the transcript (or EasyAIEvent.Phase.ERROR), under EasyAIEvent.Source.VOICE. A UI can therefore show what it heard — which is the one thing users most want to verify about a voice interface.

      Parameters:
      eventListener - the listener (may be null to disable events)
      Returns:
      this builder
    • from

      public String from(byte[] audio, String mimeType)
      Transcribe recorded audio.
      Parameters:
      audio - the audio bytes
      mimeType - the audio MIME type, e.g. "audio/webm", "audio/mpeg"
      Returns:
      the recognised text, trimmed (empty if nothing intelligible was heard)
    • from

      public String from(byte[] audio)
      Transcribe recorded audio whose MIME type you do not have at hand.
      Parameters:
      audio - the audio bytes
      Returns:
      the recognised text
    • from

      public String from(InputStream audio, String mimeType)
      Transcribe audio read from a stream — the shape a servlet or JAX-RS multipart part gives you.
      Parameters:
      audio - the stream to read fully (not closed by this method)
      mimeType - the audio MIME type, e.g. the part's declared type
      Returns:
      the recognised text
      Throws:
      IllegalStateException - if the stream cannot be read
    • from

      public String from(Path audioFile)
      Transcribe an audio file, detecting its MIME type from the file itself.
      Parameters:
      audioFile - path to the recording
      Returns:
      the recognised text
      Throws:
      IllegalStateException - if the file cannot be read