Azure OpenAI 轉錄

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 為 Azure OpenAI 轉錄生成用戶端提供 Spring Boot 自動組態。若要啟用它,請將以下相依性新增至您專案的 Maven pom.xml 檔案

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
</dependency>

或新增至您的 Gradle build.gradle 組建檔案。

dependencies {
    implementation 'org.springframework.ai:spring-ai-azure-openai-spring-boot-starter'
}
請參閱相依性管理章節,將 Spring AI BOM 新增至您的組建檔案。

轉錄屬性

前綴 spring.ai.openai.audio.transcription 用作屬性前綴,讓您可以設定 OpenAI 影像模型的重試機制。

屬性 描述 預設值

spring.ai.azure.openai.audio.transcription.enabled

啟用 Azure OpenAI 轉錄模型。

true

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

取樣溫度,介於 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()
    .withLanguage("en")
    .withPrompt("Ask not this, but ask that")
    .withTemperature(0f)
    .withResponseFormat(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()
    .withResponseFormat(TranscriptResponseFormat.TEXT)
    .withTemperature(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);