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.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
beginner - Run:
npm run example -- typescript src/examples/typescript/audio/speech-audio.ts - Source: src/examples/typescript/audio/speech-audio.ts
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.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
intermediate - Run:
npm run example -- typescript src/examples/typescript/audio/transcribe-audio.ts - Source: src/examples/typescript/audio/transcribe-audio.ts
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.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
advanced - Run:
npm run example -- typescript src/examples/typescript/audio/pipeline-audio.ts - Source: src/examples/typescript/audio/pipeline-audio.ts
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));