Mastra における Voice
Mastra の Voice システムは、音声インタラクション向けの統一されたインターフェースを提供し、アプリケーションでの text-to-speech (TTS)、speech-to-text (STT)、およびリアルタイムの speech-to-speech (STS) を実現します。
エージェントに音声を追加する
エージェントに音声機能を組み込む方法は、Adding Voice to Agents のドキュメントをご参照ください。このセクションでは、単一・複数の音声プロバイダの使い分けやリアルタイム対話の実装方法を解説します。
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
import { OpenAIVoice } from "@mastra/voice-openai";
// TTS 用に OpenAI の音声を初期化
const voiceAgent = new Agent({
name: "Voice Agent",
instructions:
"You are a voice assistant that can help users with their tasks.",
model: openai("gpt-4o"),
voice: new OpenAIVoice(),
});
続いて、以下の音声機能が利用できます。
Text to Speech (TTS)
Mastra の TTS 機能を使って、エージェントの応答を自然な音声に変換します。 OpenAI、ElevenLabs など、複数のプロバイダから選べます。
詳細な設定や高度な機能については、Text-to-Speech ガイド をご覧ください。
OpenAI
import { Agent } from '@mastra/core/agent';
import { openai } from '@ai-sdk/openai';
import { OpenAIVoice } from "@mastra/voice-openai";
import { playAudio } from "@mastra/node-audio";
const voiceAgent = new Agent({
name: "Voice Agent",
instructions: "You are a voice assistant that can help users with their tasks.",
model: openai("gpt-4o"),
voice: new OpenAIVoice(),
});
const { text } = await voiceAgent.generate('What color is the sky?');
// Convert text to speech to an Audio Stream
const audioStream = await voiceAgent.voice.speak(text, {
speaker: "default", // Optional: specify a speaker
responseFormat: "wav", // Optional: specify a response format
});
playAudio(audioStream);
OpenAI の音声プロバイダーについては、OpenAI Voice Reference を参照してください。
音声認識(STT)
OpenAI、ElevenLabs などの各種プロバイダーを利用して、音声コンテンツをテキストに変換します。詳細な設定オプションについては、Speech to Text をご覧ください。
サンプル音声ファイルはこちら からダウンロードできます。
OpenAI
import { Agent } from '@mastra/core/agent';
import { openai } from '@ai-sdk/openai';
import { OpenAIVoice } from "@mastra/voice-openai";
import { createReadStream } from 'fs';
const voiceAgent = new Agent({
name: "Voice Agent",
instructions: "You are a voice assistant that can help users with their tasks.",
model: openai("gpt-4o"),
voice: new OpenAIVoice(),
});
// URL からの音声ファイルを使用
const audioStream = await createReadStream("./how_can_i_help_you.mp3");
// 音声をテキストに変換
const transcript = await voiceAgent.voice.listen(audioStream);
console.log(`User said: ${transcript}`);
// 文字起こしに基づいて応答を生成
const { text } = await voiceAgent.generate(transcript);
OpenAI の音声プロバイダーの詳細は、OpenAI Voice Reference を参照してください。
音声対話(STS)
音声同士のやり取りで自然な会話体験を実現します。統合APIにより、ユーザーとAIエージェントの間でリアルタイムの音声インタラクションが可能です。 詳細な設定オプションや高度な機能については、Speech to Speechをご覧ください。
OpenAI
import { Agent } from '@mastra/core/agent';
import { openai } from '@ai-sdk/openai';
import { playAudio, getMicrophoneStream } from '@mastra/node-audio';
import { OpenAIRealtimeVoice } from "@mastra/voice-openai-realtime";
const voiceAgent = new Agent({
name: "Voice Agent",
instructions: "You are a voice assistant that can help users with their tasks.",
model: openai("gpt-4o"),
voice: new OpenAIRealtimeVoice(),
});
// Listen for agent audio responses
voiceAgent.voice.on('speaker', ({ audio }) => {
playAudio(audio);
});
// Initiate the conversation
await voiceAgent.voice.speak('How can I help you today?');
// Send continuous audio from the microphone
const micStream = getMicrophoneStream();
await voiceAgent.voice.send(micStream);
OpenAI の音声プロバイダの詳細は、OpenAI Voice Referenceをご覧ください。
音声設定
各音声プロバイダーは、異なるモデルやオプションで構成できます。以下に、サポートされているすべてのプロバイダーの詳細な設定オプションを示します。
OpenAI
// OpenAI Voice の設定
const voice = new OpenAIVoice({
speechModel: {
name: "gpt-3.5-turbo", // 例: モデル名
apiKey: process.env.OPENAI_API_KEY,
language: "en-US", // 言語コード
voiceType: "neural", // 音声モデルのタイプ
},
listeningModel: {
name: "whisper-1", // 例: モデル名
apiKey: process.env.OPENAI_API_KEY,
language: "en-US", // 言語コード
format: "wav", // 音声フォーマット
},
speaker: "alloy", // 例: 話者名
});
OpenAI の音声プロバイダーの詳細は、OpenAI Voice リファレンスをご覧ください。
複数の音声プロバイダの利用
この例では、Mastra で OpenAI を音声認識(STT)に、PlayAI を音声合成(TTS)に用い、2 つの異なる音声プロバイダを作成して使う方法を示します。
まず、必要な設定を指定して音声プロバイダのインスタンスを作成します。
import { OpenAIVoice } from "@mastra/voice-openai";
import { PlayAIVoice } from "@mastra/voice-playai";
import { CompositeVoice } from "@mastra/core/voice";
import { playAudio, getMicrophoneStream } from "@mastra/node-audio";
// STT 用に OpenAI の音声を初期化
const input = new OpenAIVoice({
listeningModel: {
name: "whisper-1",
apiKey: process.env.OPENAI_API_KEY,
},
});
// TTS 用に PlayAI の音声を初期化
const output = new PlayAIVoice({
speechModel: {
name: "playai-voice",
apiKey: process.env.PLAYAI_API_KEY,
},
});
// CompositeVoice でプロバイダを統合
const voice = new CompositeVoice({
input,
output,
});
// 統合した音声プロバイダで対話を実装
const audioStream = getMicrophoneStream(); // この関数で音声入力を取得すると仮定
const transcript = await voice.listen(audioStream);
// 文字起こし結果をログ出力
console.log("Transcribed text:", transcript);
// テキストを音声に変換
const responseAudio = await voice.speak(`You said: ${transcript}`, {
speaker: "default", // 任意: 話者を指定
responseFormat: "wav", // 任意: 返却フォーマットを指定
});
// 音声応答を再生
playAudio(responseAudio);
CompositeVoice の詳細は、CompositeVoice Referenceを参照してください。