Lzh on GitHub

Azure OpenAI Transcriptions

Spring AI 支持 Azure Whisper 模型。

Spring AI 支持 Azure Whisper 模型

先决条件

Azure 门户 的 Azure OpenAI 服务部分获取你的 Azure OpenAI 终端节点和 API Key。Spring AI 定义了一个配置属性 spring.ai.azure.openai.api-key,你应将其设置为从 Azure 获取的 API Key 的值。同时,还有一个配置属性 spring.ai.azure.openai.endpoint,应设置为在 Azure 中配置模型时获得的终端节点 URL。

通过导出环境变量是一种设置这些配置属性的方法:

自动配置

Spring AI 自动配置及启动模块的构件名称发生了重大变化,请参考 升级说明 以获取更多信息。

Spring AI 为 Azure OpenAI 转录生成客户端提供了 Spring Boot 自动配置。要启用它,请将以下依赖添加到项目的 Maven pom.xml 文件中:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-azure-openai</artifactId>
</dependency>

或者添加到 Gradle build.gradle 文件中:

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-azure-openai'
}
请参考 依赖管理 章节,将 Spring AI BOM 添加到你的构建文件中。

转录属性

音频转录自动配置的启用与禁用现在通过顶层属性进行管理,前缀为 spring.ai.model.audio.transcription
  • 启用:spring.ai.model.audio.transcription=azure-openai(默认已启用)
  • 禁用:spring.ai.model.audio.transcription=none(或任何不匹配 azure-openai 的值)
此更改允许同时配置多个模型。

前缀 spring.ai.azure.openai.audio.transcription 用于配置 Azure OpenAI 转录模型的相关属性,例如重试机制。

属性描述默认值
spring.ai.azure.openai.audio.transcription.enabled (已移除,不再有效)启用 Azure OpenAI 转录模型true
spring.ai.model.audio.transcription启用 Azure OpenAI 转录模型azure-openai
spring.ai.azure.openai.audio.transcription.options.model使用的模型 ID,目前仅支持 whisperwhisper
spring.ai.azure.openai.audio.transcription.options.deployment-name模型部署的名称-
spring.ai.azure.openai.audio.transcription.options.response-format转录输出格式,可选值:json、text、srt、verbose_json 或 vttjson
spring.ai.azure.openai.audio.transcription.options.prompt可选文本,用于引导模型风格或延续之前的音频段落,需与音频语言匹配-
spring.ai.azure.openai.audio.transcription.options.language输入音频的语言,使用 ISO-639-1 格式可提高准确性和响应速度-
spring.ai.azure.openai.audio.transcription.options.temperature采样温度,0 到 1 之间。较高值(如 0.8)输出更随机,较低值(如 0.2)输出更集中、确定。若设为 0,模型会使用对数概率自动调节温度0
spring.ai.azure.openai.audio.transcription.options.timestamp-granularities转录中时间戳的粒度。response_format 必须为 verbose_json 才能使用时间戳粒度。支持 wordsegment 或两者。注意:生成 segment 时间戳不增加延迟,但生成 word 时间戳会增加延迟segment

运行时选项

AzureOpenAiAudioTranscriptionOptions 类提供了进行音频转录时可用的配置选项。在应用启动时,会使用 spring.ai.azure.openai.audio.transcription 中指定的选项,但你也可以在运行时覆盖这些选项。

例如:

// 设置转录输出格式为 VTT
AzureOpenAiAudioTranscriptionOptions.TranscriptResponseFormat responseFormat = 
    AzureOpenAiAudioTranscriptionOptions.TranscriptResponseFormat.VTT;

// 构建转录选项
AzureOpenAiAudioTranscriptionOptions transcriptionOptions = AzureOpenAiAudioTranscriptionOptions.builder()
    .language("en")                          // 设置音频语言为英文
    .prompt("Ask not this, but ask that")    // 可选提示文本
    .temperature(0f)                          // 设置采样温度
    .responseFormat(this.responseFormat)      // 设置输出格式
    .build();

// 创建转录请求
AudioTranscriptionPrompt transcriptionRequest = new AudioTranscriptionPrompt(audioFile, this.transcriptionOptions);

// 调用 Azure OpenAI 转录模型执行转录
AudioTranscriptionResponse response = azureOpenAiTranscriptionModel.call(this.transcriptionRequest);

该示例展示了如何在运行时针对单个请求自定义语言、提示文本、温度和输出格式,从而覆盖默认配置。

手动配置

在项目的 Maven pom.xml 文件中添加 spring-ai-azure-openai 依赖:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-azure-openai</artifactId>
</dependency>

或者在 Gradle build.gradle 文件中添加:

dependencies {
    implementation 'org.springframework.ai:spring-ai-azure-openai'
}
参考 依赖管理 部分,将 Spring AI BOM 添加到你的构建文件中。

接下来,创建 AzureOpenAiAudioTranscriptionModel 实例:

// 创建 Azure OpenAI 客户端
var openAIClient = new OpenAIClientBuilder()
    .credential(new AzureKeyCredential(System.getenv("AZURE_OPENAI_API_KEY")))
    .endpoint(System.getenv("AZURE_OPENAI_ENDPOINT"))
    .buildClient();

// 创建 AzureOpenAiAudioTranscriptionModel 实例
var azureOpenAiAudioTranscriptionModel = new AzureOpenAiAudioTranscriptionModel(this.openAIClient, null);

// 配置转录选项
var transcriptionOptions = AzureOpenAiAudioTranscriptionOptions.builder()
    .responseFormat(TranscriptResponseFormat.TEXT)
    .temperature(0f)
    .build();

// 指定音频文件资源
var audioFile = new FileSystemResource("/path/to/your/resource/speech/jfk.flac");

// 构建转录请求
AudioTranscriptionPrompt transcriptionRequest = new AudioTranscriptionPrompt(this.audioFile, this.transcriptionOptions);

// 调用 Azure OpenAI 转录模型进行转录
AudioTranscriptionResponse response = this.azureOpenAiAudioTranscriptionModel.call(this.transcriptionRequest);

该示例展示了如何在非 Spring Boot 环境下手动配置 Azure OpenAI 转录模型,并执行音频转录操作。