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=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 |
一个可选的文本,用于引导模型的风格或继续之前的音频片段。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);