Mastraの音声機能
Mastraの音声システムは、音声インタラクションのための統一されたインターフェースを提供し、アプリケーションでテキスト読み上げ(TTS)、音声認識(STT)、リアルタイム音声変換(STS)機能を実現します。
エージェントにボイス機能を追加する
エージェントにボイス機能を統合する方法については、エージェントにボイス機能を追加するのドキュメントをご確認ください。このセクションでは、単一および複数のボイスプロバイダーの使用方法、およびリアルタイムインタラクションについて説明しています。
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
import { OpenAIVoice } from "@mastra/voice-openai";
// Initialize OpenAI voice for TTS
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(),
});
以下のボイス機能を使用できます:
テキスト読み上げ(TTS)
MastraのTTS機能を使用して、エージェントの応答を自然な音声に変換します。 OpenAI、ElevenLabsなど、複数のプロバイダーから選択できます。
詳細な設定オプションと高度な機能については、テキスト読み上げガイドをご確認ください。
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 Voice Reference をご覧ください。
音声認識(STT)
OpenAI や ElevenLabs など、さまざまなプロバイダーを使って話された内容を文字起こしできます。詳細な設定オプションなどについては、音声認識をご覧ください。
サンプル音声ファイルはこちら からダウンロードできます。
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(),
});
// Use an audio file from a URL
const audioStream = await createReadStream("./how_can_i_help_you.mp3");
// Convert audio to text
const transcript = await voiceAgent.voice.listen(audioStream);
console.log(`User said: ${transcript}`);
// Generate a response based on the transcript
const { text } = await voiceAgent.generate(transcript);
詳細については、OpenAI Voice Reference をご覧ください。
Speech to Speech (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をご覧ください。
Voice設定
各音声プロバイダーは、異なるモデルとオプションで設定できます。以下は、サポートされているすべてのプロバイダーの詳細な設定オプションです:
OpenAI
// OpenAI Voice Configuration
const voice = new OpenAIVoice({
speechModel: {
name: "gpt-3.5-turbo", // Example model name
apiKey: process.env.OPENAI_API_KEY,
language: "en-US", // Language code
voiceType: "neural", // Type of voice model
},
listeningModel: {
name: "whisper-1", // Example model name
apiKey: process.env.OPENAI_API_KEY,
language: "en-US", // Language code
format: "wav", // Audio format
},
speaker: "alloy", // Example speaker name
});
OpenAI音声プロバイダーの詳細については、OpenAI Voice Referenceをご覧ください。
複数の音声プロバイダーの使用
この例では、Mastraで2つの異なる音声プロバイダーを作成して使用する方法を示します:音声認識(STT)用のOpenAIと音声合成(TTS)用のPlayAIです。
必要な設定を含む音声プロバイダーのインスタンスを作成することから始めます。
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リファレンスを参照してください。