Grok Speech to Text API in Node.js
GrokSTT transcribes the user with the Grok streaming speech to text by xAI, for @micdrop/server.
With GrokSTT as the only component, the server transcribes the call without answering.
Installation
npm install @micdrop/grok @micdrop/openaiThe same package also provides Grok Voice, a model that hears and speaks, and a text to speech.
Usage with MicdropServer
import { GrokSTT, GrokTTS } from '@micdrop/grok'import { OpenaiAgent } from '@micdrop/openai'import { MicdropServer } from '@micdrop/server'import { WebSocketServer } from 'ws'
const wss = new WebSocketServer({ port: 8081 })
wss.on('connection', (socket) => { new MicdropServer(socket, { stt: new GrokSTT({ apiKey: process.env.XAI_API_KEY || '', language: 'en', keyterms: ['Micdrop'], }), agent: new OpenaiAgent({ apiKey: process.env.OPENAI_API_KEY || '', systemPrompt: 'You are a helpful assistant', }), tts: new GrokTTS({ apiKey: process.env.XAI_API_KEY || '', language: 'en', }), })})Usage without MicdropServer
import { GrokSTT } from '@micdrop/grok'import { createReadStream } from 'fs'
const stt = new GrokSTT({ apiKey: process.env.XAI_API_KEY || '' })
stt.on('Transcript', (text) => console.log('Transcript:', text))stt.on('Failed', (chunks) => console.error('Failed:', chunks.length, 'chunks'))
// Audio is raw PCM, 16 bits, 16 kHz, monostt.transcribe(createReadStream('speech.pcm'))Events
| Event | Payload | Description |
|---|---|---|
Transcript | string | Transcription of one utterance. The text is empty when nothing was recognized. |
Failed | Buffer[] | Transcription gave up after its retries, with the audio chunks left pending. |
See the STT interface for the full contract.
Options
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | Required | Your xAI API key |
model | string | 'grok-voice-transcribe-2.0' | Model transcribing the speech |
language | string | undefined | Language of the speech, such as en or fr, detected when unset |
keyterms | string[] | undefined | Names and terms to expect, 100 at most |
format | boolean | false | Writes numbers, amounts and units in their written form, needs language |
endpointing | number | 5000 | Silence after which the API ends an utterance on its own, in milliseconds |
connectionTimeout | number | 5000 | Time to open the connection, in milliseconds |
transcriptionTimeout | number | 4000 | Time an utterance waits for its transcript once it ended, in milliseconds |
retryDelay | number | 1000 | Delay between two connection attempts, in milliseconds |
maxRetry | number | 3 | Failed attempts in a row before Failed |
How an Utterance Ends
Each stream given to transcribe() is one utterance and gets one Transcript event. When the stream ends, GrokSTT sends finalize, and the API finalizes every word heard so far at once. The transcript joins the final words of the utterance, in order.
The Micdrop turns decide when the user is done, so the silence after which the API ends an utterance on its own is set to its longest, five seconds. A shorter pause inside the turn keeps the utterance whole. An utterance the API never ends is emitted after transcriptionTimeout with the words it finalized.
Connection and Retries
GrokSTT opens one connection per call and keeps it between the turns. A connection that drops is opened again, and the audio of every utterance still waiting for its transcript goes out again. After maxRetry failed attempts in a row, Failed hands that audio back, for a FallbackSTT to transcribe it, and the next utterance tries to connect again.