1. 概述
企業經常需要從各種音頻內容中提取有意義的數據,例如對客户支持通話進行情感分析、為視頻生成字幕或從會議中生成會議記錄。然而,手動轉錄音頻文件既耗時又昂貴。
為了自動化這個過程,OpenAI 提供強大的 語音轉文本模型,這些模型能夠準確地轉錄多種語言的音頻文件。
在本教程中,我們將探討如何使用 Spring AI 與 OpenAI 的語音轉文本模型轉錄音頻文件。
為了跟上本教程,我們需要一個 OpenAI API 密鑰。
2. 項目設置
在開始實施我們的音頻轉錄器之前,我們需要包含必要的依賴項並正確配置我們的應用程序。
2.1. 依賴項
讓我們首先將 Spring AI 的 OpenAI 啓動器依賴項 添加到我們項目的 pom.xml 文件中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
<version>1.0.0-M7</version>
</dependency>由於當前版本 1.0.0-M7 是里程碑發佈,我們需要在 pom.xml 中添加 Spring Milestones 倉庫:
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>此倉庫用於發佈里程碑版本,與標準的 Maven Central 倉庫不同。
2.2. 配置 OpenAI 屬性
接下來,讓我們在我們的 application.yaml 文件中配置 OpenAI API 密鑰和語音轉文本模型:
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
audio:
transcription:
options:
model: whisper-1
language: en我們使用 ${} 屬性佔位符從環境變量中加載我們的 API 密鑰的值。
這裏,我們通過其模型 ID whisper-1 指定 OpenAI 的 Whisper 模型。 重要的是要注意,OpenAI 提供更高級、更高質量的語音轉文本模型,例如 gpt-4o-transcribe 和 gpt-4o-mini-transcribe。 但是,這些模型不受當前 Spring AI 版本支持。
此外,我們指定音頻文件的語言為 en。 替代地,我們可以根據要求,在 ISO-639-1 格式中指定不同的輸入語言。
如果未指定,則指定模型將嘗試自動檢測音頻中使用的語言。
配置上述屬性後,Spring AI 自動創建一個類型為 OpenAiAudioTranscriptionModel 的 Bean,從而允許我們與指定模型進行交互。
3. 構建我們的音頻轉錄服務
有了我們的配置就緒,讓我們創建一個 AudioTranscriber 服務類。我們將注入 Spring AI 自動為我們創建的 OpenAiAudioTranscriptionModel Bean。
不過,首先讓我們定義兩個簡單的記錄來表示請求和響應的負載:
record TranscriptionRequest(MultipartFile audioFile, @Nullable String context) {}
record TranscriptionResponse(String transcription) {}TranscriptionRequest 包含需要轉錄的 音頻文件 以及可選的 上下文,以幫助模型進行轉錄過程。請注意,OpenAI 目前支持 mp3、mp4、mpeg、mpga、m4a、wav 和 webm 格式的音頻文件。
同樣,TranscriptionResponse 僅存儲生成的 轉錄文本。
現在,讓我們來實現所需的功能:
TranscriptionResponse transcribe(TranscriptionRequest transcriptionRequest) {
AudioTranscriptionPrompt prompt = new AudioTranscriptionPrompt(
transcriptionRequest.audioFile().getResource(),
OpenAiAudioTranscriptionOptions
.builder()
.prompt(transcriptionRequest.context())
.build()
);
AudioTranscriptionResponse response = openAiAudioTranscriptionModel.call(prompt);
return new TranscriptionResponse(response.getResult().getOutput());
}在這裏,我們向我們的 AudioTranscriber 類添加了一個新的 transcribe() 方法。
我們使用 audioFile 資源和可選的 context 提示創建了一個 AudioTranscriptionPrompt 對象。然後,我們使用它來調用自動注入的 OpenAiAudioTranscriptionModel Bean 的 call() 方法。
最後,我們從 response 中提取轉錄文本,並將其包裝在我們的 TranscriptionResponse 記錄中返回。
目前,對於語音到文本模型,音頻文件大小限制為 25 MB。但是,默認情況下,Spring Boot 將上傳文件的最大大小限制為 1 MB。 讓我們在我們的 application.yaml 文件中增加此限制:
spring:
servlet:
multipart:
max-file-size: 25MB
max-request-size: 25MB我們設置了最大文件大小和請求大小限制為 25MB,這對於大多數音頻轉錄請求來説應該足夠了。
4. 測試我們的音頻轉錄器
現在我們已經實現了服務層,讓我們在之上暴露一個 REST API:
@PostMapping("/transcribe")
ResponseEntity<TranscriptionResponse> transcribe(
@RequestParam("audioFile") MultipartFile audioFile,
@RequestParam("context") String context
) {
TranscriptionRequest transcriptionRequest = new TranscriptionRequest(audioFile, context);
TranscriptionResponse response = audioTranscriber.transcribe(transcriptionRequest);
return ResponseEntity.ok(response);
}接下來,我們使用 HTTPie CLI 調用上述 API 端點:
http -f POST :8080/transcribe [email protected] context="Short description about Baeldung"在這裏,我們調用我們的 /transcribe API,併發送 audioFile 及其 context。 為了演示,我們準備了一個音頻文件,其中包含對 Baeldung 的簡短描述。該文件位於代碼庫的 src/test/resources/audio 文件夾中。
讓我們看看我們收到的響應結果:
{
"transcription": "Baeldung is a top-notch educational platform that specializes in Java, Spring, and related technologies. It offers a wealth of tutorials, articles, and courses that help developers master programming concepts. Known for its clear examples and practical guides, Baeldung is a go-to resource for developers looking to level up their skills."
}如我們所見,API 返回提供的音頻文件的正確 轉錄。
請注意,提供 上下文 提示有助於模型正確轉錄 貝靈頓 這個名字。如果沒有這個 上下文,Whisper 模型會將這個詞轉錄為 貝靈頓。
5. 結論
在本文中,我們探討了使用 OpenAI 在 Spring AI 中轉錄音頻文件的過程。
我們詳細介紹了所需的配置,並使用 OpenAI 的 Whisper 語音轉文本模型實現了音頻轉錄功能。我們還對應用程序進行了測試,並觀察到提供上下文提示可以提高生成轉錄的準確性,尤其是在處理特定領域的名稱時。