We follow the OpenAI signature where you can send the input text and the voice option as a part of the API request. All the output formats mp3, opus, aac, flac, and pcm are supported. Portkey also supports real time audio streaming for TTS models.
Here’s an example:
Copy
Ask AI
import fs from "fs";import path from "path";import OpenAI from "openai";import { PORTKEY_GATEWAY_URL } from 'portkey-ai'const openai = new OpenAI({ apiKey: "PORTKEY_API_KEY", baseURL: PORTKEY_GATEWAY_URL});const speechFile = path.resolve("./speech.mp3");async function main() { const mp3 = await openai.audio.speech.create({ model: "@openai/tts-1", voice: "alloy", input: "Today is a wonderful day to build something people love!", }); const buffer = Buffer.from(await mp3.arrayBuffer()); await fs.promises.writeFile(speechFile, buffer);}main();
On completion, the request will get logged in the logs UI and show the cost and latency incurred.