Azure OpenAI 转录
Spring AI 支持 Azure Whisper 模型。
前提条件
从 Azure Portal 的 Azure OpenAI 服务部分获取你的 Azure OpenAI endpoint 和 api-key。Spring AI 定义了一个名为 spring.ai.azure.openai.api-key 的配置属性,你应该将其设置为从 Azure 获取的 API Key 的值。还有一个名为 spring.ai.azure.openai.endpoint 的配置属性,你应该将其设置为在 Azure 中预配模型时获得的端点 URL。导出环境变量是设置该配置属性的一种方式:
自动配置
Spring AI 自动配置和 starter 模块的 artifact 名称已发生重大变化。更多信息请参阅升级说明。
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.openai.audio.transcription 用作属性前缀,允许您配置 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 指定的选项,但你可以在运行时覆盖这些设置。
例如:
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);
AudioTranscriptionResponse response = azureOpenAiTranscriptionModel.call(this.transcriptionRequest);
手动配置
将 spring-ai-openai 依赖添加到您项目的 Maven pom.xml 文件中:
<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
var openAIClient = new OpenAIClientBuilder()
.credential(new AzureKeyCredential(System.getenv("AZURE_OPENAI_API_KEY")))
.endpoint(System.getenv("AZURE_OPENAI_ENDPOINT"))
.buildClient();
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);
AudioTranscriptionResponse response = this.azureOpenAiAudioTranscriptionModel.call(this.transcriptionRequest);