语音转录 API
Spring AI 通过 TranscriptionModel 接口为语音转文本转录提供了一个统一的API。这使您能够编写可跨不同转录提供商工作的可移植代码。
公共接口
所有转录服务提供商都实现了以下共享接口:
转录模型
TranscriptionModel 接口提供了将音频转换为文本的方法:
public interface TranscriptionModel extends Model<AudioTranscriptionPrompt, AudioTranscriptionResponse> {
/**
* Transcribes the audio from the given prompt.
*/
AudioTranscriptionResponse call(AudioTranscriptionPrompt transcriptionPrompt);
/**
* A convenience method for transcribing an audio resource.
*/
default String transcribe(Resource resource) {
AudioTranscriptionPrompt prompt = new AudioTranscriptionPrompt(resource);
return this.call(prompt).getResult().getOutput();
}
/**
* A convenience method for transcribing an audio resource with options.
*/
default String transcribe(Resource resource, AudioTranscriptionOptions options) {
AudioTranscriptionPrompt prompt = new AudioTranscriptionPrompt(resource, options);
return this.call(prompt).getResult().getOutput();
}
}
编写与提供者无关的代码
共享转录接口的主要优势之一是能够编写代码,使其无需修改即可与任何转录服务提供商协同工作。实际使用的提供商(如OpenAI、Azure OpenAI等)由您的Spring Boot配置决定,这意味着您可以更换提供商而无需更改应用程序代码。
基础服务示例
共享接口使您能够编写可与任何转录服务提供商配合使用的代码。
@Service
public class TranscriptionService {
private final TranscriptionModel transcriptionModel;
public TranscriptionService(TranscriptionModel transcriptionModel) {
this.transcriptionModel = transcriptionModel;
}
public String transcribeAudio(Resource audioFile) {
return transcriptionModel.transcribe(audioFile);
}
public String transcribeWithOptions(Resource audioFile, AudioTranscriptionOptions options) {
AudioTranscriptionPrompt prompt = new AudioTranscriptionPrompt(audioFile, options);
AudioTranscriptionResponse response = transcriptionModel.call(prompt);
return response.getResult().getOutput();
}
}
此服务无缝集成了OpenAI、Azure OpenAI或任何其他转录服务提供商,具体实现由您的Spring Boot配置决定。
提供商特定功能
共享接口提供了可移植性,但每个提供商也通过特定的选项类(例如,OpenAiAudioTranscriptionOptions、AzureOpenAiAudioTranscriptionOptions)提供特殊功能。这些类在实现AudioTranscriptionOptions接口的同时添加了提供商特有的功能。
关于特定提供商的详细功能信息,请参阅各个提供商的文档页面。