53_Spring AI 干货笔记之转录API

一、转录 API

Spring AI 通过 TranscriptionModel 接口为语音转文字转录提供了统一的 API。这使您能够编写可在不同转录提供商之间移植的代码。

二、支持的提供商

  • OpenAI 的 Whisper API

  • Azure OpenAI Whisper API

三、通用接口

所有转录提供商都实现了以下共享接口:

3.1 TranscriptionModel

TranscriptionModel 接口提供了将音频转换为文本的方法:

java 复制代码
public interface TranscriptionModel extends Model<AudioTranscriptionPrompt, AudioTranscriptionResponse> {

    /**
     * 转录给定提示中的音频。
     */
    AudioTranscriptionResponse call(AudioTranscriptionPrompt transcriptionPrompt);

    /**
     * 转录音频资源的便捷方法。
     */
    default String transcribe(Resource resource) {
        AudioTranscriptionPrompt prompt = new AudioTranscriptionPrompt(resource);
        return this.call(prompt).getResult().getOutput();
    }

    /**
     * 使用选项转录音频资源的便捷方法。
     */
    default String transcribe(Resource resource, AudioTranscriptionOptions options) {
        AudioTranscriptionPrompt prompt = new AudioTranscriptionPrompt(resource, options);
        return this.call(prompt).getResult().getOutput();
    }
}

3.2 AudioTranscriptionPrompt

AudioTranscriptionPrompt 类封装了输入音频和选项:

java 复制代码
Resource audioFile = new FileSystemResource("/path/to/audio.mp3");
AudioTranscriptionPrompt prompt = new AudioTranscriptionPrompt(
    audioFile,
    options
);

3.3 AudioTranscriptionResponse

AudioTranscriptionResponse 类包含转录文本和元数据:

java 复制代码
AudioTranscriptionResponse response = model.call(prompt);
String transcribedText = response.getResult().getOutput();
AudioTranscriptionResponseMetadata metadata = response.getMetadata();

四、编写与提供商无关的代码

共享转录接口的主要好处之一是能够编写无需修改即可与任何转录提供商一起工作的代码。实际的提供商(OpenAI、Azure OpenAI 等)由您的 Spring Boot 配置决定,允许您在不更改应用程序代码的情况下切换提供商。

4.1 基础服务示例

共享接口允许您编写与任何转录提供商一起工作的代码:

java 复制代码
@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 接口的同时,添加了提供商特定的能力。

有关提供商特定功能的详细信息,请参阅各个提供商的文档页面。

相关推荐
Sam09274 分钟前
【AI 算法精讲 14】TF-IDF:词频与逆文档频率
人工智能·python·算法·ai
m0_626535208 分钟前
MRR(Mean Reciprocal Rank)和 NDCG(Normalized Discounted Cumulative Gain)
人工智能·机器学习
长和信泰光伏储能9 分钟前
探索未来能源:光伏储能技术解析
大数据·人工智能·能源
寻道码路11 分钟前
LangChain4j Java AI 应用开发实战(二十六):多模型集成策略 —— OpenAI、DeepSeek、阿里百炼混合使用
java·开发语言·人工智能·ai
直接冲冲冲16 分钟前
65-批量归一化
人工智能·深度学习·计算机视觉
树獭非懒16 分钟前
六、Plan-and-Solve智能体:学会三思而后行
人工智能·llm·agent
武子康18 分钟前
调查研究-214 OpenAI:Agent 不是更聪明的聊天框,而是新的工作组织方式
人工智能·openai·agent
火山引擎开发者社区19 分钟前
告别手动翻资料:用 Agent Plan 搞定销售档案与问答
人工智能
鹰影4720 分钟前
一款AI笔记助手和远程同步的markdown笔记idea-note
人工智能·笔记·rust·typescript·react
城事漫游Molly24 分钟前
如何写出有说服力的研究论文Introduction——论证框架切入法
人工智能·论文写作·ai for science·博士生必读