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

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

相关推荐
7177776 小时前
国内 AI 研发效能工具怎么选:2026 年基于公开信息的选型框架与 Gitee 实践
人工智能·gitee
Csvn7 小时前
换供应商要改一遍代码?把模型接入收进一层网关(C04)
人工智能·aigc·agent
孟健7 小时前
开源AutoClip切直播:1小时48分出19段,整月API只花0.08元
人工智能·开源·aigc
翰墨之道7 小时前
AI日报:豆包工作ARR超20亿,GPT-6.2跑分与实测割裂
人工智能·gpt·数据挖掘
Maynor9968 小时前
Trymodel 使用介绍:一个平台同时用 Claude、GPT、Gemini 和 Grok
人工智能·gpt·ai·程序员·教程·claude·语音
字节跳动视频云技术团队9 小时前
接大模型别再自己配 Key、搭代理了,火山引擎 IGA Pages 一步搞定
人工智能
阿里云大数据AI技术9 小时前
EMR Serverless StarRocks 产品介绍:从统一 OLAP 走向多模态分析与检索
人工智能
Forget the Dream10 小时前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai
郝学胜-神的一滴10 小时前
Effective Python 条款35:用类封装替代生成器异常注入
开发语言·人工智能·python·金融·pandas·scikit-learn·matplotlib
火山引擎开发者社区11 小时前
豆包工作,3 个月,800 万,20 亿
人工智能