Audio Audio — TypeScript examples backed by real provider calls. typescript examples examples/audio src/examples/typescript/audio example Audio

These TypeScript examples are real runnable files. Edit the source file first; this page is rebuilt from the checked-in example and its metadata header.

TypeScript Text To Speech

Generates speech audio through OpenAI.

TypeScript
import { writeFileSync } from 'node:fs';
import { AxAIOpenAIModel, ai } from '@ax-llm/ax';

const apiKey = process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY;
if (!apiKey) {
  throw new Error('Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.');
}

const llm = ai({
  name: 'openai',
  apiKey,
  config: {
    model: AxAIOpenAIModel.GPT54Mini,
    temperature: 0,
  },
});

const speech = await llm.speak({
  text: 'Ax turns LLM prompts into typed programs.',
  voice: 'alloy',
  format: 'mp3',
});

writeFileSync(
  './src/examples/output/public-openai-speech.mp3',
  Buffer.from(speech.data, 'base64')
);
console.log(
  JSON.stringify(
    { format: speech.format, transcript: speech.transcript },
    null,
    2
  )
);

TypeScript Speech To Text

Transcribes a checked-in WAV file through OpenAI.

TypeScript
import { readFileSync } from 'node:fs';
import { AxAIOpenAIModel, ai } from '@ax-llm/ax';

const apiKey = process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY;
if (!apiKey) {
  throw new Error('Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.');
}

const llm = ai({
  name: 'openai',
  apiKey,
  config: {
    model: AxAIOpenAIModel.GPT54Mini,
    temperature: 0,
  },
});

const audio = readFileSync('./src/examples/assets/presentation.wav').toString(
  'base64'
);
const transcript = await llm.transcribe({
  audio: { data: audio, format: 'wav', filename: 'presentation.wav' },
  language: 'en',
});

console.log(JSON.stringify(transcript, null, 2));

TypeScript Audio Summary Pipeline

Transcribes audio and summarizes the transcript with an OpenAI-backed generator.

TypeScript
import { readFileSync } from 'node:fs';
import { AxAIOpenAIModel, ai, ax } from '@ax-llm/ax';

const apiKey = process.env.OPENAI_API_KEY ?? process.env.OPENAI_APIKEY;
if (!apiKey) {
  throw new Error('Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.');
}

const llm = ai({
  name: 'openai',
  apiKey,
  config: {
    model: AxAIOpenAIModel.GPT54Mini,
    temperature: 0,
  },
});

const audio = readFileSync('./src/examples/assets/presentation.wav').toString(
  'base64'
);
const transcript = await llm.transcribe({
  audio: { data: audio, format: 'wav', filename: 'presentation.wav' },
  language: 'en',
});
const summarize = ax('transcript:string -> summary:string, followUps:string[]');
const result = await summarize.forward(llm, { transcript: transcript.text });

console.log(JSON.stringify({ transcript: transcript.text, result }, null, 2));
Docs