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のテキスト読み上げ機能を使用して、エージェントの応答を自然な音声に変換します。 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の音声プロバイダーについて詳しくは、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音声プロバイダーの詳細については、OpenAI Voice Referenceをご覧ください。
音声から音声へ(STS)
音声から音声への機能で会話体験を作成します。統一されたAPIにより、ユーザーとAIエージェント間のリアルタイムの音声インタラクションが可能になります。 詳細な設定オプションと高度な機能については、音声から音声へをご確認ください。
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音声リファレンスをご覧ください。
ボイス設定
各ボイスプロバイダーは、さまざまなモデルやオプションで設定できます。以下に、すべての対応プロバイダーの詳細な設定オプションを示します。
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 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リファレンスを参照してください。