Azure OpenAI 转录

Spring AI 支持 Azure Whisper 模型

先决条件

Azure 门户 的 Azure OpenAI 服务部分获取您的 Azure OpenAI endpointapi-key。 Spring AI 定义了一个名为 spring.ai.azure.openai.api-key 的配置属性,您应该将其设置为从 Azure 获取的 API 密钥 的值。 还有一个名为 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.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

转录输出的格式,可选值:jsontextsrtverbose_jsonvtt

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 才能使用时间戳粒度。支持以下任一或全部选项:wordsegment。注意: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);