Audio Multimodality: Expanding AI Interaction with Spring AI and OpenAI
This weblog submit is co-authored by our nice contributor Thomas Vitale.
OpenAI supplies specialised fashions for speech-to-text and text-to-speech conversion, acknowledged for his or her efficiency and cost-efficiency. Spring AI integrates these capabilities through Voice-to-Text and Text-to-Speech (TTS).
The new Audio Generation function (gpt-4o-audio-preview) goes additional, enabling blended enter and output modalities. Audio inputs can comprise richer knowledge than textual content alone. Audio can convey nuanced data like tone and inflection, and along with the audio outputs it permits asynchronous speech-to-speech interactions.
Additionally, this new multimodality opens up prospects for modern purposes, corresponding to structured knowledge extraction. Developers can extract structured data not simply from easy textual content, but additionally from photographs and audio, constructing advanced, structured objects seamlessly.
Spring AI Audio Integrations
The Spring AI Multimodality Message API simplifies the combination of multimodal capabilities with numerous AI fashions.

Now it absolutely helps OpenAI’s Audio Input and Audio Output modalities, thanks largely to neighborhood member Thomas Vitale, who contributed to this function’s improvement.
Setup
Follow the Spring AI-OpenAI integration documentation to arrange your setting.
Audio Input
OpenAI’s User Message API accepts base64-encoded audio recordsdata inside messages utilizing the Media kind. Supported codecs embrace audio/mp3 and audio/wav.
Example: Adding audio to an enter immediate:
// Prepare the audio useful resource
var audioResource = new ClassPathResource("speech1.mp3");
// Create a person message with audio and ship it to the chat mannequin
String response = chatClient.immediate()
.person(u -> u.textual content("What is that this recording about?")
.media(MimeTypeUtils.parseMimeType("audio/mp3"), audioResource))
.choices(OpenAiChatChoices.builder()
.withModel(OpenAiApi.ChatMannequin.GPT_4_O_AUDIO_PREVIEW).construct())
.name()
.content material();
Audio Output Generation
The OpenAI Assistant Message API can return base64-encoded audio recordsdata utilizing the Media kind.
Example: Generating audio output:
// Generate an audio response
ChatResponse response = chatClient
.immediate("Tell me a joke concerning the Spring Framework")
.choices(OpenAiChatChoices.builder()
.withModel(OpenAiApi.ChatMannequin.GPT_4_O_AUDIO_PREVIEW)
.withOutputModalities(List.of("textual content", "audio"))
.withOutputAudio(new AudioParameters(Voice.ALLOY, AudioResponseFormat.WAV))
.construct())
.name()
.chatResponse();
// Access the audio transcript
String audioTranscript = response.getResult().getOutput().getContent();
// Retrieve the generated audio
byte[] generatedAudio = response.getResult().getOutput().getMedia().get(0).getDataAsByteArray();
To generate audio output, specify the audio modality in OpenAiChatChoices. Use the AudioParameters class to customise the voice and the audio format.
Voice ChatBot Demo
This instance demonstrates constructing an interactive chatbot utilizing Spring AI that helps enter and output audio. It reveals how AI can improve person interplay with natural-sounding audio responses.
Setup
Add the Spring AI OpenAI starter:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
Configure the API key, mannequin title, and output audio modality in utility.properties:
spring.most important.web-application-type=none
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.choices.mannequin=gpt-4o-audio-preview
spring.ai.openai.chat.choices.output-modalities=textual content,audio
spring.ai.openai.chat.choices.output-audio.voice=ALLOY
spring.ai.openai.chat.choices.output-audio.format=WAV
Implementation
The Java implementation of the voice chatbot, detailed beneath, creates a conversational AI assistant utilizing audio enter and output. It leverages Spring AI’s integration with OpenAI fashions to allow seamless interactions with customers.
VoiceAssistantSoftware
-
VoiceAssistantSoftwareserves as the principle utility. -
The
CommandLineRunnerbean initializes the chatbot:- The
ChatConsumeris configured utilizing thesystemPromptfor contextual understanding and an in-memory chat reminiscence for dialog historical past. - The
Audioutility is used to document voice enter from the person and play again audio responses generated by the AI.
- The
-
Chat Loop: Inside the loop:
- Voice Recording: The
audio.beginRecording()andaudio.ceaseRecording()strategies deal with the recording course of, pausing for person enter. - Processing AI Response: The person message is shipped to the AI mannequin through
chatClient.immediate(). The audio knowledge is encapsulated within theMediaobject. - Response Handling: The AI-generated response is retrieved as textual content and performed again as audio utilizing the
Audio.play()methodology.
- Voice Recording: The
Refer to the next code snippet for the implementation:
@Bean
public CommandLineRunner chatBot(ChatConsumer.Builder chatClientBuilder,
@Value("${chatbot.immediate:classpath:/marvin.paranoid.android.txt}") Resource systemPrompt) {
return args -> {
var chatClient = chatClientBuilder.defaultSystem(systemPrompt)
.defaultAdvisors(new MessageChatMemoryAdvisor(new InMemoryChatReminiscence()))
.construct();
strive (Scanner scanner = new Scanner(System.in)) {
Audio audio = new Audio();
whereas (true) {
audio.beginRecording();
System.out.print("Recording your query ... press <Enter> to cease! ");
scanner.nextLine();
audio.ceaseRecording();
System.out.print("PROCESSING ... ");
AssistantMessage response = chatClient.immediate()
.messages(new UserMessage("Please reply the questions within the audio enter",
new Media(MediaKind.parseMediaKind("audio/wav"),
new ByteArrayResource(audio.getLastRecording()))))
.name()
.chatResponse()
.getResult()
.getOutput();
System.out.println("ASSISTANT: " + response.getContent());
Audio.play(response.getMedia().get(0).getDataAsByteArray());
}
}
};
}
The Audio utility, for capturing and taking part in again audio, is a single class leveraging the plain Java Sound API.
▗▄▄▖▗▄▄▖ ▗▄▄▖ ▗▄▄▄▖▗▖ ▗▖ ▗▄▄▖ ▗▄▖ ▗▄▄▄▖
▐▌ ▐▌ ▐▌▐▌ ▐▌ █ ▐▛▚▖▐▌▐▌ ▐▌ ▐▌ █
▝▀▚▖▐▛▀▘ ▐▛▀▚▖ █ ▐▌ ▝▜▌▐▌▝▜▌ ▐▛▀▜▌ █
▗▄▄▞▘▐▌ ▐▌ ▐▌▗▄█▄▖▐▌ ▐▌▝▚▄▞▘ ▐▌ ▐▌▗▄█▄▖
▗▄▄▖ ▗▄▖ ▗▄▄▖ ▗▄▖ ▗▖ ▗▖ ▗▄▖ ▗▄▄▄▖▗▄▄▄ ▗▄▖ ▗▖ ▗▖▗▄▄▄ ▗▄▄▖ ▗▄▖ ▗▄▄▄▖▗▄▄▄
▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▛▚▖▐▌▐▌ ▐▌ █ ▐▌ █ ▐▌ ▐▌▐▛▚▖▐▌▐▌ █▐▌ ▐▌▐▌ ▐▌ █ ▐▌ █
▐▛▀▘ ▐▛▀▜▌▐▛▀▚▖▐▛▀▜▌▐▌ ▝▜▌▐▌ ▐▌ █ ▐▌ █ ▐▛▀▜▌▐▌ ▝▜▌▐▌ █▐▛▀▚▖▐▌ ▐▌ █ ▐▌ █
▐▌ ▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▝▚▄▞▘▗▄█▄▖▐▙▄▄▀ ▐▌ ▐▌▐▌ ▐▌▐▙▄▄▀▐▌ ▐▌▝▚▄▞▘▗▄█▄▖▐▙▄▄▀
2024-12-01T11:00:11.274+01:00 INFO 31297 --- [voice-assistant-chatbot] [ main] s.a.d.a.m.VoiceAssistantSoftware : Started VoiceAssistantSoftware in 0.827 seconds (course of working for 1.054)
Recording your query ... press <Enter> to cease!
The full demo is offered on GitHub: voice-assistant-chatbot
Important Considerations
- One hour of audio enter is roughly equal to 128k tokens.
- The mannequin at present helps
modalities = ["text", "audio"]. - Future updates could supply extra versatile modality controls.
Conclusion
The gpt-4o-audio-preview mannequin unlocks new prospects for dynamic audio interactions, enabling builders to construct wealthy, AI-powered audio purposes.
Disclaimer: API capabilities and options could evolve. Refer to the newest OpenAI and Spring AI documentation for updates.
HI-FI News
through Spring https://spring.io
December 5, 2024 at 06:30PM
-
Product on sale
Audiophile Vinyl Records Cleaning BundleOriginal price was: €69.90.€59.90Current price is: €59.90. incl. VAT -
Product on sale
Easy Start Vinyl Records Cleaning KitOriginal price was: €69.90.€59.90Current price is: €59.90. incl. VAT -
Vinyl Records Cleaner Easy Groove Concentrate€34.95 incl. VAT -
Easy Groove Super Set€199.00 incl. VAT -
Easy Groove Enzycaster – vinyl records prewash cleaner€34.95 incl. VAT -
Easy Groove Extreme MK. II- vinyl records prewash cleaner€45.00 incl. VAT -
Vinyl Records Cleaner – Easy Groove Virgin Concentrate€35.00 incl. VAT -
Easy Groove Spray&Wipe vinyl records cleaner€34.95 incl. VAT








