Streaming

Stream audio while the person speaks, and get the text a moment after they stop. Open a WebSocket to the transcriptions path, with the model in the URL and your key in the Authorization header:

Text
wss://functionvoice.ai/api/v1/audio/transcriptions?model=function/dictate-1.2&language=en

The URL also takes sample_rate and repeated keyterms. A client that can't set headers, as in a browser, sends the key as two subprotocols instead: function-api and bearer. followed by the key. The server answers with function-api. Never put a key in a page you ship to others.

Flow

  1. The server sends session.created.
  2. Optionally, before any audio, send session.update. The server answers session.updated.
  3. Send audio as it is recorded: binary messages of raw 16-bit little-endian mono PCM, 16 kHz unless sample_rate says otherwise. Every 100 to 250 ms is a good pace.
  4. When the speaker stops, send input_audio_buffer.commit. The server sends input_audio_buffer.committed, then transcript.text.done with the text and the usage, and closes the socket.
Messages
→ {"type": "session.update", "session": {"keyterms": ["Priya"], "sample_rate": 16000}}
← {"type": "session.updated", "session": {"id": "gen-…", "model": "function/dictate-1.2", "sample_rate": 16000, …}}
→ (binary PCM) (binary PCM) …
→ {"type": "input_audio_buffer.commit"}
← {"type": "input_audio_buffer.committed", "audio_seconds": 3.82}
← {"type": "transcript.text.done", "text": "Can you send the deck to Priya by Friday?", "usage": {"seconds": 3.82, "cost": 0.0013611111}}

Events

EventFromWhat it is
session.createdServerThe stream is open. session has its id, model, sample rate and language.
session.updateYouOptional, before any audio: session with keyterms, language or sample_rate.
session.updatedServerThe settings now in force.
input_audio_buffer.appendYouAudio as base64 16-bit PCM in audio. Binary messages of raw PCM work too.
input_audio_buffer.commitYouThe speaker stopped. No more audio.
input_audio_buffer.committedServerThe audio was received: audio_seconds.
transcript.text.doneServerThe text and the usage. The socket closes after it.
errorServererror in the same shape as an HTTP error's body. One that ends the stream is followed by a close.

Example

import WebSocket from "ws";

const socket = new WebSocket("wss://functionvoice.ai/api/v1/audio/transcriptions?model=function/dictate-1.2", {
  headers: { Authorization: `Bearer ${process.env.FUNCTION_API_KEY}` },
});

socket.on("open", () => {
  microphone.on("data", (pcm) => socket.send(pcm)); // 16-bit mono, 16 kHz
  microphone.on("end", () => socket.send(JSON.stringify({ type: "input_audio_buffer.commit" })));
});

socket.on("message", (data) => {
  const event = JSON.parse(data.toString());
  if (event.type === "transcript.text.done") console.log(event.text);
  if (event.type === "error") console.error(event.error.message);
});

Refusals and limits

A stream checks the key and its rules, the model and the payment method before it opens. A refusal is an HTTP error, as for a POST. After it opens, a problem is an error event, and one that ends the stream is followed by a close.

  • A stream with no message for 20 seconds before its commit is closed with timeout.
  • One recording a stream, up to 10 minutes and 24 MB.
  • A stream is billed like a file: its audio, at least 10 seconds.

View as Markdown