Azure OpenAI 转录

本站(springdoc.cn)中的内容来源于 spring.io ,原始版权归属于 spring.io。由 springdoc.cn 进行翻译,整理。可供个人学习、研究,未经许可,不得进行任何转载、商用或与之相关的行为。 商标声明:Spring 是 Pivotal Software, Inc. 在美国以及其他国家的商标。

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 自动配置、启动模块的工件名称发生了重大变化。更多信息请参阅 升级说明。.

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 (Removed and no longer valid)

启用 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

采样温度(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

运行时选项

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);

手动配置

在项目的 Maven pom.xml 文件中添加 spring-ai-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

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);