01Speech-to-text providers
Each provider has a transcribe method. The OpenAI classes accept an API key as a string, or a config object with apiKey and baseURL.
OpenAIWhisperProvider: the OpenAI Whisper API.LemonFoxSTTProvider: LemonFox transcription, which uses the OpenAI-compatible API.LocalWhisperProvider: runs a whisper binary on your machine. TakeswhisperPathandmodelPath.whisperPathdefaults towhisper.
import { OpenAIWhisperProvider } from '@lov3kaizen/agentsea-core';
const stt = new OpenAIWhisperProvider(process.env.OPENAI_API_KEY);
const result = await stt.transcribe('./audio.mp3', {
model: 'whisper-1',
language: 'en',
});
console.log(result.text);Note
transcribe() takes a file path and options such as model and language.02Text-to-speech providers
OpenAITTSProvider: OpenAI speech. Accepts an API key string or anOpenAITTSConfigobject.LemonFoxTTSProvider: LemonFox speech, using the OpenAI-compatible API.ElevenLabsTTSProvider: ElevenLabs. Takes anapiKey.getVoices()lists the voices.PiperTTSProvider: runs a piper binary with a local.onnxvoice model. TakespiperPath,modelPathandconfigPath.
03Synthesize speech
synthesize(text, config?) returns a result that holds the audio. Write the audio to a file or send it to a client.
import { writeFileSync } from 'node:fs';
import { OpenAITTSProvider } from '@lov3kaizen/agentsea-core';
const tts = new OpenAITTSProvider(process.env.OPENAI_API_KEY);
const result = await tts.synthesize('Hello, world!', { voice: 'nova', format: 'mp3' });
writeFileSync('./output.mp3', result.audio);Note
result.audio. Use format to choose the file type.04Streaming speech
OpenAITTSProvider.synthesizeStream(text, config?) is an async generator that yields audio chunks. Send each chunk as it arrives.
for await (const chunk of tts.synthesizeStream('A longer passage to read aloud.')) {
// Each chunk is a Buffer of audio.
console.log('chunk size:', chunk.length);
}05VoiceAgent
VoiceAgent wraps an Agent with an STT provider and a TTS provider. The constructor takes the agent and a config with sttProvider, ttsProvider, ttsConfig, sttConfig and autoSpeak. The agent and the context are built as in the other guides, and audioBuffer is a Buffer of recorded audio.
import { OpenAITTSProvider, OpenAIWhisperProvider, VoiceAgent } from '@lov3kaizen/agentsea-core';
const voiceAgent = new VoiceAgent(agent, {
sttProvider: new OpenAIWhisperProvider(process.env.OPENAI_API_KEY),
ttsProvider: new OpenAITTSProvider(process.env.OPENAI_API_KEY),
ttsConfig: { voice: 'nova' },
autoSpeak: true,
});
const result = await voiceAgent.processVoice(audioBuffer, context);
console.log(result.text, result.response.content);Note
processVoice returns text, response and audio.06VoiceAgent methods
processVoice(audio, context): transcribes the audio, runs the agent, and speaks the reply whenautoSpeakis on.speak(text, context): runs the agent on text and returns the spoken reply.transcribe(audio): returns text without running the agent.synthesize(text)andsynthesizeStream(text): speech from text.setTTSConfig()andsetSTTConfig()change settings after construction.setAutoSpeak()changes the auto-speak setting.getHistory(),clearHistory()andexportConversation(outputDir): read, clear or export the conversation.supportsStreaming(): reports which parts can stream.
07Audio input
Unsupported audio formats fail. Convert the file first, for example with ffmpeg.
ffmpeg -i input.m4a -ar 16000 output.wav