Azure OpenAI 轉錄

Spring AI 支援 Azure Whisper 模型

先決條件

Azure Portal 上的 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 自動配置、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 的頂級屬性進行配置。

要啟用,設定 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);