Vercel AI SDK

View as Markdown

Use Smallest AI as a speech and transcription provider in the Vercel AI SDK. Generate speech and transcribe audio with a few lines of code.

Installation

$npm install smallestai-vercel-provider ai

Setup

Get your API key from waves.smallest.ai and set it as an environment variable:

$export SMALLEST_API_KEY="your_key_here"

Text-to-Speech

Supported models: lightning-v3.1 (recommended — 44.1 kHz, natural expressive speech) and lightning-v2 (16 languages, voice cloning).

1import { experimental_generateSpeech as generateSpeech } from 'ai';
2import { smallestai } from 'smallestai-vercel-provider';
3
4const { audio } = await generateSpeech({
5 model: smallestai.speech('lightning-v3.1'),
6 text: 'Hello from Smallest AI!',
7 voice: 'sophia',
8 speed: 1.0,
9});
10
11// audio.uint8Array — raw WAV bytes
12// audio.base64 — base64 encoded audio

Speech-to-Text

1import { experimental_transcribe as transcribe } from 'ai';
2import { smallestai } from 'smallestai-vercel-provider';
3import { readFileSync } from 'fs';
4
5const { text, segments } = await transcribe({
6 model: smallestai.transcription('pulse'),
7 audio: readFileSync('recording.wav'),
8 mediaType: 'audio/wav',
9});
10
11console.log(text); // "Hello from Smallest AI!"
12console.log(segments); // [{ text: "Hello", startSecond: 0, endSecond: 0.5 }, ...]

Next.js API Route Example

Create a TTS endpoint in your Next.js app:

1// app/api/speak/route.ts
2import { experimental_generateSpeech as generateSpeech } from 'ai';
3import { smallestai } from 'smallestai-vercel-provider';
4
5export async function POST(req: Request) {
6 const { text, voice } = await req.json();
7
8 const { audio } = await generateSpeech({
9 model: smallestai.speech('lightning-v3.1'),
10 text,
11 voice: voice || 'sophia',
12 });
13
14 return new Response(Buffer.from(audio.uint8Array), {
15 headers: { 'Content-Type': 'audio/wav' },
16 });
17}

Play it in the browser:

1const res = await fetch('/api/speak', {
2 method: 'POST',
3 body: JSON.stringify({ text: 'Hello!', voice: 'sophia' }),
4});
5const blob = await res.blob();
6new Audio(URL.createObjectURL(blob)).play();

Provider Options

TTS Options

1const { audio } = await generateSpeech({
2 model: smallestai.speech('lightning-v3.1'),
3 text: 'Hello!',
4 voice: 'robert',
5 providerOptions: {
6 smallestai: {
7 sampleRate: 48000, // 8000 | 16000 | 24000 | 44100 | 48000
8 outputFormat: 'mp3', // pcm | mp3 | wav | mulaw
9 language: 'en', // ISO 639-1 code
10 consistency: 0.5, // voice consistency (0–1)
11 similarity: 0.5, // voice similarity (0–1)
12 enhancement: 1, // audio enhancement level (0–2)
13 },
14 },
15});

STT Options

1const result = await transcribe({
2 model: smallestai.transcription('pulse'),
3 audio: audioBuffer,
4 mediaType: 'audio/wav',
5 providerOptions: {
6 smallestai: {
7 language: 'hi', // ISO 639-1 code
8 diarize: true, // speaker identification
9 emotionDetection: true, // detect emotions
10 ageDetection: true, // detect speaker age
11 genderDetection: true, // detect speaker gender
12 },
13 },
14});

Available Voices

80+ voices across multiple languages. Popular voices:

VoiceGenderAccentBest For
sophiaFemaleAmericanGeneral use (default)
robertMaleAmericanProfessional
advikaFemaleIndianHindi, code-switching
vivaanMaleIndianBilingual English/Hindi
camillaFemaleMexican/LatinSpanish

Fetch the full voice list programmatically:

$curl -s "https://api.smallest.ai/waves/v1/lightning-v3.1/get_voices" \
> -H "Authorization: Bearer $SMALLEST_API_KEY"