🎤Micdrop

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

Terminal window
npm install @micdrop/grok @micdrop/openai

The 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, mono
stt.transcribe(createReadStream('speech.pcm'))

Events

EventPayloadDescription
TranscriptstringTranscription of one utterance. The text is empty when nothing was recognized.
FailedBuffer[]Transcription gave up after its retries, with the audio chunks left pending.

See the STT interface for the full contract.

Options

OptionTypeDefaultDescription
apiKeystringRequiredYour xAI API key
modelstring'grok-voice-transcribe-2.0'Model transcribing the speech
languagestringundefinedLanguage of the speech, such as en or fr, detected when unset
keytermsstring[]undefinedNames and terms to expect, 100 at most
formatbooleanfalseWrites numbers, amounts and units in their written form, needs language
endpointingnumber5000Silence after which the API ends an utterance on its own, in milliseconds
connectionTimeoutnumber5000Time to open the connection, in milliseconds
transcriptionTimeoutnumber4000Time an utterance waits for its transcript once it ended, in milliseconds
retryDelaynumber1000Delay between two connection attempts, in milliseconds
maxRetrynumber3Failed 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.