TranscriptionModel is Spring AI’s abstraction for converting spoken audio into text. Its current implementations target OpenAI and Azure OpenAI, and its transcription operation accepts a Resource and returns a String. section 8.1.1
Use the interface within a voice service:
- Inject an OpenAiAudioTranscriptionModel into a Spring service. section 8.1.1
- Pass an uploaded audio Resource to call() and return the resulting transcription. section 8.1.1
- Forward the transcription into an application operation such as a question-answering service. section 8.1.1
Transcribing an audio question
An /audioAsk endpoint receives an MP3 upload, calls voiceService.transcribe(audioBlob.getResource()), creates a question from the returned text, and sends it to boardGameService.askQuestion(). section 8.1.1
Listing 8.1 Implementing voice transcription
package com.example.boardgamebuddy;
import org.springframework.ai.openai.OpenAiAudioTranscriptionModel;
import org.springframework.ai.openai.audio.speech.SpeechModel;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
@Service
public class OpenAiVoiceService implements VoiceService {
private final OpenAiAudioTranscriptionModel transcriptionModel;
public OpenAiVoiceService(
OpenAiAudioTranscriptionModel transcriptionModel) {
this.transcriptionModel = transcriptionModel; #1
}
@Override
public String transcribe(Resource audioFileResource) {
return transcriptionModel.call(audioFileResource); #2
}
@Override
public Resource textToSpeech(String text) {
throw new UnsupportedOperationException("Not implemented yet");
}
}
Inspect the concrete OpenAI service implementation, including constructor injection and the transcription call. section 8.1.1
