Azure OpenAI 听录
Spring AI 支持 Azure Whisper 模型。
先决条件
获取 Azure OpenAIendpoint
和api-key
从 Azure 门户上的 Azure OpenAI 服务部分。
Spring AI 定义了一个名为spring.ai.azure.openai.api-key
,您应该设置为API Key
从 Azure 获得。
还有一个名为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>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-azure-openai-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
转录属性
前缀spring.ai.openai.audio.transcription
用作属性前缀,允许您为 OpenAI 图像模型配置重试机制。
财产 | 描述 | 违约 |
---|---|---|
spring.ai.azure.openai.audio.transcription.enabled |
启用 Azure OpenAI 听录模型。 |
真 |
spring.ai.azure.openai.audio.transcription.options.model |
要使用的模型的 ID。目前只有 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 |
要为此转录填充的时间戳粒度。必须verbose_json response_format 设置为使用时间戳粒度。支持以下选项中的一个或两个:word 或 segment。注意:段时间戳没有额外的延迟,但生成单词时间戳会产生额外的延迟。 |
段 |
运行时选项
这AzureOpenAiAudioTranscriptionOptions
class 提供进行转录时要使用的选项。
启动时,由spring.ai.azure.openai.audio.transcription
,但您可以在运行时覆盖这些 ID。
例如:
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
依赖项添加到项目的 Mavenpom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-azure-openai</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-azure-openai'
}
请参阅 Dependency Management 部分,将 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);