跳到主要内容

Azure OpenAI 转录

DeepSeek V3 中英对照 Azure OpenAI Azure OpenAI Transcriptions

Spring AI 支持 Azure Whisper 模型

先决条件

Azure 门户 的 Azure OpenAI 服务部分获取你的 Azure OpenAI endpointapi-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-azure-openai-spring-boot-starter</artifactId>
</dependency>
xml

或者到你的 Gradle build.gradle 构建文件中。

dependencies {
implementation 'org.springframework.ai:spring-ai-azure-openai-spring-boot-starter'
}
groovy

:::提示
请参考依赖管理部分,将 Spring AI BOM 添加到您的构建文件中。
:::

转录属性

前缀 spring.ai.openai.audio.transcription 用作属性前缀,允许你为 OpenAI 音频转录模型配置重试机制。

属性描述默认值
spring.ai.azure.openai.audio.transcription.enabled启用 Azure OpenAI 转录模型。true
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);
java

手动配置

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

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

或者到你的 Gradle build.gradle 构建文件中。

dependencies {
implementation 'org.springframework.ai:spring-ai-azure-openai'
}
groovy
提示

请参考 依赖管理 部分,将 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);
java