Class VoiceEngine
RagEngine and MilvusEngine.
What this class actually does
It is the single place that knows about AudioTranscriptionModel and
TextToSpeechModel. Everything above it (EasyAI.listen(),
EasyAI.speak()) deals only in byte[], String and Speech — the
shapes a Jakarta EE web tier already has in hand. Four small jobs live here:
- Build the model from a
VoiceConfig(provider, key, base URL, model name). - Wrap the bytes as a LangChain4J
Audio— including the detail that an upload arrives as abyte[]while a model may want Base64. - Unwrap the answer — a transcript as
String, synthesized audio asSpeechwith its MIME type intact. - Fail with a usable message when a key is missing, instead of a provider-level NPE.
Providers
Only "openai" is built in, which covers any OpenAI-compatible endpoint via
VoiceConfig.baseUrl() — Groq's Whisper being the obvious example, and the reason voice
typically needs no new provider and no new key. Any other provider id fails fast with a message
naming what is supported, rather than silently doing something else. New providers plug in here
and nowhere else.
Analogy: the adapter plug for a foreign appliance. The appliance (your web endpoint) keeps its own plain plug; the adapter knows the local socket's shape. Change country and you change the adapter, not the appliance.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic dev.langchain4j.model.audio.TextToSpeechModelspeechModel(VoiceConfig config) Build the LangChain4J speech-synthesis model described byconfig.static Speechsynthesize(String text, VoiceConfig config) Synthesize speech for one piece of text.static Stringtranscribe(byte[] audio, String mimeType, VoiceConfig config) Transcribe one spoken utterance.static dev.langchain4j.model.audio.AudioTranscriptionModeltranscriptionModel(VoiceConfig config) Build the LangChain4J transcription model described byconfig.
-
Method Details
-
transcribe
Transcribe one spoken utterance.- Parameters:
audio- the recorded bytes (e.g. a WebM/Opus blob from a browser's MediaRecorder)mimeType- the audio MIME type, e.g."audio/webm";nullis allowed but providers generally transcribe more reliably when toldconfig- which model to call and how to reach it- Returns:
- the recognised text (possibly empty if nothing intelligible was heard)
- Throws:
IllegalArgumentException- ifaudiois null or emptyIllegalStateException- if no API key is configured
-
synthesize
Synthesize speech for one piece of text.- Parameters:
text- the words to speak (required, non-blank)config- which model and voice to use, and how to reach it- Returns:
- the audio plus its MIME type
- Throws:
IllegalArgumentException- iftextis null or blankIllegalStateException- if no API key is configured, or the provider returned no audio
-
transcriptionModel
public static dev.langchain4j.model.audio.AudioTranscriptionModel transcriptionModel(VoiceConfig config) Build the LangChain4J transcription model described byconfig.Exposed for callers who want the raw LangChain4J model — the escape hatch, so choosing EasyAI never means losing access to the layer underneath.
- Parameters:
config- the voice settings- Returns:
- a ready
AudioTranscriptionModel
-
speechModel
Build the LangChain4J speech-synthesis model described byconfig.Exposed as the same escape hatch as
transcriptionModel(VoiceConfig).- Parameters:
config- the voice settings- Returns:
- a ready
TextToSpeechModel
-