Azure OpenAI Transcriptions
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 为 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.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,目前仅支持 whisper | whisper |
| spring.ai.azure.openai.audio.transcription.options.deployment-name | 模型部署的名称 | - |
| spring.ai.azure.openai.audio.transcription.options.response-format | 转录输出格式,可选值:json、text、srt、verbose_json 或 vtt | json |
| 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 才能使用时间戳粒度。支持 word 或 segment 或两者。注意:生成 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'
}
接下来,创建 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 转录模型,并执行音频转录操作。