SpeechResponse packages synthesized audio and response information when SpeechModel processes a SpeechPrompt. Extract the generated bytes through response.getResult().getOutput(). section 8.1.2
Use SpeechResponse when the speech request needs configurable generation options:
- Create a SpeechPrompt with an OpenAiAudioSpeechOptions object. section 8.1.2
- Select the voice, audio model, and response format through the options builder. section 8.1.2
- Extract the audio bytes and wrap them in a ByteArrayResource for an HTTP response. section 8.1.2
Returning generated speech
A board-game voice endpoint can transcribe an uploaded question, generate an answer, synthesize that answer, and return audio/mpeg bytes. section 8.1.2
public Resource textToSpeech(String text) {
SpeechPrompt speechPrompt = new SpeechPrompt(text);
SpeechResponse response = speechModel.call(speechPrompt);
byte[] speechBytes = response.getResult().getOutput();
return new ByteArrayResource(speechBytes);
}
Inspect the concrete extraction path from SpeechResponse to ByteArrayResource. section 8.1.2
