AgentSea

Guide

Voice

Speech-to-text and text-to-speech providers, and a VoiceAgent that places an agent between them.

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. Takes whisperPath and modelPath. whisperPath defaults to whisper.
TS
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 an OpenAITTSConfig object.
  • LemonFoxTTSProvider: LemonFox speech, using the OpenAI-compatible API.
  • ElevenLabsTTSProvider: ElevenLabs. Takes an apiKey. getVoices() lists the voices.
  • PiperTTSProvider: runs a piper binary with a local .onnx voice model. Takes piperPath, modelPath and configPath.

03Synthesize speech

synthesize(text, config?) returns a result that holds the audio. Write the audio to a file or send it to a client.

TS
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

Audio comes back as 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.

TS
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.

TS
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 when autoSpeak is on.
  • speak(text, context): runs the agent on text and returns the spoken reply.
  • transcribe(audio): returns text without running the agent.
  • synthesize(text) and synthesizeStream(text): speech from text.
  • setTTSConfig() and setSTTConfig() change settings after construction. setAutoSpeak() changes the auto-speak setting.
  • getHistory(), clearHistory() and exportConversation(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.

BASH
ffmpeg -i input.m4a -ar 16000 output.wav