Skip to main content

ElevenLabs

Setup

  1. You can get an API key from ElevenLabs.
  2. The API key can be configured as an environment variable (ELEVENLABS_API_KEY) or passed in as an option into the model constructor.

Model Functions

Examples | ElevenLabsSpeechModel API

Generate Speech

import { elevenlabs, generateSpeech } from "modelfusion";

const speech = await generateSpeech({
model: elevenlabs.SpeechGenerator({
voice: "pNInz6obpgDQGcFmaJgB", // Adam
}),
text:
"Good evening, ladies and gentlemen! Exciting news on the airwaves tonight " +
"as The Rolling Stones unveil 'Hackney Diamonds,' their first collection of " +
"fresh tunes in nearly twenty years, featuring the illustrious Lady Gaga, the " +
"magical Stevie Wonder, and the final beats from the late Charlie Watts.",
});

const path = `./elevenlabs-speech-example.mp3`;
fs.writeFileSync(path, speech);

Stream Speech

const textStream = await streamText(/* ... */);

const speechStream = await streamSpeech({
model: elevenlabs.SpeechGenerator({
model: "eleven_turbo_v2",
voice: "pNInz6obpgDQGcFmaJgB", // Adam
optimizeStreamingLatency: 1,
voiceSettings: { stability: 1, similarityBoost: 0.35 },
generationConfig: {
chunkLengthSchedule: [50, 90, 120, 150, 200],
},
}),
text: textStream,
});

for await (const part of speechStream) {
// each part is a Uint8Array with MP3 audio data
}

Configuration

API Configuration

ElevenLabs API Configuration

const api = elevenlabs.Api({
apiKey: "my-api-key",
// ...
});

const model = elevenlabs.SpeechGenerator({
api,
// ...
});