收藏本站,收获最前沿的人工智能与编程资讯!!

Spring AI 使用​ Azure OpenAI 语音转文字

技术文档 10℃ 0

Spring AI 支持 Azure Whisper 模型。

前提条件

从 Azure 门户的 Azure OpenAI 服务部分获取你的 Azure OpenAI 终端节点和 API 密钥。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,目前仅支持 whisperwhisper
spring.ai.azure.openai.audio.transcription.options.deployment-name模型部署的名称
spring.ai.azure.openai.audio.transcription.options.response-format语音转文字输出格式,可选值:json、text、srt、verbose_json、vttjson
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

运行时选项

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

相关推荐