Transcription API

Spring AI は、TranscriptionModel インターフェースを通じて音声テキスト変換用の統合 API を提供します。これにより、異なる音声テキスト変換プロバイダー間で動作する移植性の高いコードを作成できます。

サポートされているプロバイダー

共通インターフェース

すべてのトランスクリプションプロバイダーは、次の共有インターフェースを実装します。

TranscriptionModel

TranscriptionModel インターフェースは、オーディオをテキストに変換するためのメソッドを提供します。

public interface TranscriptionModel extends Model<AudioTranscriptionPrompt, AudioTranscriptionResponse> {

    /**
     * Transcribes the audio from the given prompt.
     */
    AudioTranscriptionResponse call(AudioTranscriptionPrompt transcriptionPrompt);

    /**
     * A convenience method for transcribing an audio resource.
     */
    default String transcribe(Resource resource) {
        AudioTranscriptionPrompt prompt = new AudioTranscriptionPrompt(resource);
        return this.call(prompt).getResult().getOutput();
    }

    /**
     * A convenience method for transcribing an audio resource with options.
     */
    default String transcribe(Resource resource, AudioTranscriptionOptions options) {
        AudioTranscriptionPrompt prompt = new AudioTranscriptionPrompt(resource, options);
        return this.call(prompt).getResult().getOutput();
    }
}

AudioTranscriptionPrompt

AudioTranscriptionPrompt クラスは入力オーディオとオプションをカプセル化します。

Resource audioFile = new FileSystemResource("/path/to/audio.mp3");
AudioTranscriptionPrompt prompt = new AudioTranscriptionPrompt(
    audioFile,
    options
);

AudioTranscriptionResponse

AudioTranscriptionResponse クラスには、転記されたテキストとメタデータが含まれています。

AudioTranscriptionResponse response = model.call(prompt);
String transcribedText = response.getResult().getOutput();
AudioTranscriptionResponseMetadata metadata = response.getMetadata();

プロバイダーに依存しないコードを書く

共有トランスクリプションインターフェースの主な利点の一つは、変更を加えることなく、あらゆるトランスクリプションプロバイダーで動作するコードを記述できることです。実際のプロバイダー(OpenAI、Azure、OpenAI など)は Spring Boot の設定によって決定されるため、アプリケーションコードを変更することなくプロバイダーを切り替えることができます。

基本的なサービス例

共有インターフェースを使用すると、任意のトランスクリプションプロバイダーで動作するコードを記述できます。

@Service
public class TranscriptionService {

    private final TranscriptionModel transcriptionModel;

    public TranscriptionService(TranscriptionModel transcriptionModel) {
        this.transcriptionModel = transcriptionModel;
    }

    public String transcribeAudio(Resource audioFile) {
        return transcriptionModel.transcribe(audioFile);
    }

    public String transcribeWithOptions(Resource audioFile, AudioTranscriptionOptions options) {
        AudioTranscriptionPrompt prompt = new AudioTranscriptionPrompt(audioFile, options);
        AudioTranscriptionResponse response = transcriptionModel.call(prompt);
        return response.getResult().getOutput();
    }
}

このサービスは、OpenAI、Azure、OpenAI、その他のトランスクリプションプロバイダーとシームレスに連携し、実際の実装は Spring Boot の構成によって決まります。

プロバイダー固有の機能

共通インターフェースは移植性を提供しますが、各プロバイダはプロバイダ固有のオプションクラス(例: OpenAiAudioTranscriptionOptionsAzureOpenAiAudioTranscriptionOptions)を通じて独自の機能も提供します。これらのクラスは、プロバイダ固有の機能を追加しながら、AudioTranscriptionOptions インターフェースを実装します。

プロバイダー固有の機能の詳細については、個々のプロバイダーのドキュメントページを参照してください。