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

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

相关推荐
美酒没故事°16 小时前
Open WebUI安装指南。搭建自己的自托管 AI 平台
人工智能·windows·ai
云烟成雨TD16 小时前
Spring AI Alibaba 1.x 系列【6】ReactAgent 同步执行 & 流式执行
java·人工智能·spring
AI攻城狮16 小时前
用 Obsidian CLI + LLM 构建本地 RAG:让你的笔记真正「活」起来
人工智能·云原生·aigc
鸿乃江边鸟16 小时前
Nanobot 从onboard启动命令来看个人助理Agent的实现
人工智能·ai
lpfasd12316 小时前
基于Cloudflare生态的应用部署与开发全解
人工智能·agent·cloudflare
俞凡16 小时前
DevOps 2.0:智能体如何接管故障修复和基础设施维护
人工智能
comedate16 小时前
[OpenClaw] GLM 5 关于电影 - 人工智能 - 的思考
人工智能·电影评价
财迅通Ai16 小时前
6000万吨产能承压 卫星化学迎来战略窗口期
大数据·人工智能·物联网·卫星化学
liliangcsdn16 小时前
Agent Memory智能体记忆系统的示例分析
数据库·人工智能·全文检索
GISer_Jing16 小时前
Page-agent MCP结构
前端·人工智能