deepgram

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 15 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package deepgram adapts Deepgram's streaming recogniser and its speech endpoint to the stable OpenRealtime component API.

Deepgram is here rather than behind the generic transcription adapter for one reason: it actually streams. A batch endpoint can only be asked what a recording said, so a partial hypothesis costs a full re-transcription; Deepgram emits interim results as the audio arrives, which is what the stable-partial observation policy was written for.

Index

Constants

View Source
const (
	DefaultFluxURL        = "wss://api.deepgram.com/v2/listen"
	DefaultFluxModel      = "flux-general-en"
	FluxMultilingualModel = "flux-general-multi"
)

Deepgram Flux is conversational recognition on /v2/listen. It is not Nova-3 with another model name: the protocol reports turns rather than segments - StartOfTurn, Update, EagerEndOfTurn, TurnResumed, EndOfTurn - and replaces Nova-3's interim results, endpointing and Finalize with a turn state machine and ForceEndTurn. So it is its own listener behind the same provider contract, selected by a flux- model.

View Source
const (
	// DefaultListenURL is Deepgram's streaming recognition endpoint.
	DefaultListenURL = "wss://api.deepgram.com/v1/listen"
	// DefaultListenModel is the general-purpose streaming model. Deepgram
	// also ships a turn-taking model for voice agents; this adapter does its
	// own turn-taking, so the general model is the right default here.
	DefaultListenModel = "nova-3"
)

Variables

This section is empty.

Functions

func IsFluxModel added in v0.2.0

func IsFluxModel(model string) bool

IsFluxModel reports whether a Deepgram model is served by Flux.

Types

type FluxConfig added in v0.2.0

type FluxConfig struct {
	URL    string
	Model  string
	APIKey string
	// EOTThreshold is the confidence at which Flux reports EndOfTurn, 0.5 to
	// 1.0. 1.0 suppresses the model's own turn ends.
	EOTThreshold float64
	// EagerEOTThreshold enables EagerEndOfTurn and TurnResumed, 0.3 to 0.9,
	// and never above the EndOfTurn threshold in force.
	EagerEOTThreshold float64
	// EOTTimeout ends a turn after this much silence whatever the confidence,
	// 500 ms to 60 s in whole milliseconds.
	EOTTimeout time.Duration
	Keyterms   []string
	// LanguageHints bias flux-general-multi towards these language codes.
	// The English model takes none.
	LanguageHints []string
	Header        http.Header
	DialTimeout   time.Duration
	DrainTimeout  time.Duration
	WriteTimeout  time.Duration
	HTTPClient    *http.Client
}

FluxConfig configures one Flux stream. Zero thresholds and timeout leave the service defaults: EndOfTurn at 0.7 confidence, no eager events, and a 5 s silence backstop.

type FluxListener added in v0.2.0

type FluxListener struct {
	// contains filtered or unexported fields
}

FluxListener is one session's Flux stream, reused across utterances.

An utterance here is what the engine's acoustic gate bounds, and a Flux turn is what the model decides; they are not the same unit. A person who pauses long enough for Flux but not for the gate produces two turns in one utterance, so ended turns are settled into the utterance's text and the turn in progress is its unstable tail.

func NewFluxListener added in v0.2.0

func NewFluxListener(config FluxConfig) (*FluxListener, error)

NewFluxListener validates the configuration without opening a socket; the stream is dialled by the first frame.

func (*FluxListener) Close added in v0.2.0

func (listener *FluxListener) Close() error

Close releases the stream for a session that ends.

func (*FluxListener) Confidence added in v0.2.0

func (listener *FluxListener) Confidence() float64

Confidence is the mean word confidence of the latest transcript.

func (*FluxListener) Descriptor added in v0.2.0

func (listener *FluxListener) Descriptor() v1.Descriptor

func (*FluxListener) EagerEndOfTurn added in v0.2.0

func (listener *FluxListener) EagerEndOfTurn() bool

EagerEndOfTurn reports that Flux is moderately confident the turn is over: an EagerEndOfTurn has arrived and neither TurnResumed nor EndOfTurn has followed. Unless the turn resumes, the EndOfTurn transcript is exactly the transcript at this moment. It is only ever true with an eager threshold set.

func (*FluxListener) EndUtterance added in v0.2.0

func (listener *FluxListener) EndUtterance() error

EndUtterance implements api/v1.UtteranceReusable: an utterance that will not be finalized is ended at the service and dropped, so its words cannot open the next one.

func (*FluxListener) Finalize added in v0.2.0

func (listener *FluxListener) Finalize(
	ctx context.Context, sourceSample uint64,
) (v1.PerceptionRevision, error)

Finalize ends the utterance. A turn Flux has already ended is final as it stands; a turn still open is ended with ForceEndTurn, whose EndOfTurn carries the transcript decoded so far without another decode pass.

func (*FluxListener) PushFrame added in v0.2.0

func (listener *FluxListener) PushFrame(
	ctx context.Context, frame v1.AudioFrame,
) ([]v1.PerceptionRevision, error)

PushFrame sends one PCM16LE frame and returns what Flux has reported since the previous call.

func (*FluxListener) SpeechEndpointed added in v0.2.0

func (listener *FluxListener) SpeechEndpointed() bool

SpeechEndpointed reports that Flux ended the turn in progress: EndOfTurn has arrived and no new turn has started since. The cascade reads it the way it reads Nova-3's speech_final.

type LanguageMux

type LanguageMux struct {
	// contains filtered or unexported fields
}

LanguageMux keeps two explicitly selected Deepgram language streams over the same utterance. Deepgram's streaming language detection is unavailable, and Nova-3's native multi mode does not include Mandarin. A deployment that knows it needs English and Mandarin therefore has to preserve both readings until the script itself distinguishes them.

The first language is the ordinary/default lane. The second must be a Chinese locale. A lane wins only after Deepgram gives its current reading a high confidence; this keeps both an English phonetic rendering of Mandarin and a Chinese model's occasional Han-like rendering of English away from the transcript-event policy. Once selected, the lane remains fixed for the utterance.

func NewLanguageMux

func NewLanguageMux(config ListenConfig, languages []string) (*LanguageMux, error)

NewLanguageMux creates an explicit English/Chinese streaming recogniser. Both lanes use the same Deepgram model and connection settings; only the service language restriction differs.

func (*LanguageMux) Close

func (mux *LanguageMux) Close() error

func (*LanguageMux) Descriptor

func (mux *LanguageMux) Descriptor() v1.Descriptor

func (*LanguageMux) EndUtterance added in v0.2.0

func (mux *LanguageMux) EndUtterance() error

EndUtterance implements api/v1.UtteranceReusable for both lanes. The lane selection is per utterance - the next sentence may be in the other language - so it is forgotten with everything else.

func (*LanguageMux) Finalize

func (mux *LanguageMux) Finalize(
	ctx context.Context, sourceSample uint64,
) (v1.PerceptionRevision, error)

func (*LanguageMux) PushFrame

func (mux *LanguageMux) PushFrame(
	ctx context.Context, frame v1.AudioFrame,
) ([]v1.PerceptionRevision, error)

func (*LanguageMux) SpeechEndpointed

func (mux *LanguageMux) SpeechEndpointed() bool

type ListenConfig

type ListenConfig struct {
	// URL is the WebSocket endpoint. Empty selects DefaultListenURL.
	URL string
	// Model is the recognition model.
	Model string
	// APIKey is the Deepgram key. It is sent as an Authorization: Token
	// header rather than in the URL, so it does not reach proxy logs.
	APIKey string
	// Language selects recognition language. Empty leaves Deepgram's service
	// default in force. The model-specific "multi" value is not automatic
	// detection for every language; for example, Nova-3 multi does not include
	// Mandarin, which requires a Chinese locale such as zh-CN.
	Language string
	// InterimResults asks for hypotheses before a segment is finished. It
	// defaults on, because the reason to choose a streaming recogniser is to
	// get text before the speaker stops.
	InterimResults *bool
	// SmartFormat applies Deepgram's punctuation and entity formatting.
	SmartFormat *bool
	// Endpointing is the silence Deepgram's own VAD requires before marking a
	// Results event speech_final. Zero leaves the service default in force.
	Endpointing time.Duration
	// VADEvents asks Deepgram to expose its speech detector. It defaults on;
	// the adapter currently consumes speech_final and retains the explicit
	// switch so deployments can make the wire behavior reproducible.
	VADEvents *bool
	// Keywords biases recognition toward expected vocabulary.
	Keywords []string
	// Keyterms biases Nova-3 toward exact words or phrases. Deepgram accepts
	// this option as a repeated singular keyterm query parameter; keeping it
	// distinct from the legacy weighted keywords option prevents a frozen
	// deployment from silently changing wire semantics.
	Keyterms []string
	// Extra adds query parameters this adapter does not model.
	Extra map[string]string
	// Header adds request headers.
	Header http.Header
	// DialTimeout bounds connection setup.
	DialTimeout time.Duration
	// DrainTimeout bounds how long Finalize waits for Deepgram to flush the
	// results it still owes after the utterance is closed.
	DrainTimeout time.Duration
	// WriteTimeout bounds one send on an established stream. Zero selects the
	// shipped default; a negative value removes the bound.
	//
	// The dial and the drain were bounded and the sends between them were not,
	// which leaves the one call on the hot path unbounded: PushFrame writes a
	// frame every cadence, holding this listener's lock, on a context that
	// comes from the session and therefore has no deadline. A stalled socket
	// there does not slow recognition down, it stops it - and the block
	// propagates back through the observer to the binding and then to the
	// session's own event loop.
	//
	// The bound is this provider's cadence rather than a shared default: a
	// frame of audio is a few kilobytes, and a streaming recogniser that has
	// not accepted one in five seconds is not keeping up with a conversation
	// whatever it does next.
	WriteTimeout time.Duration
	// Persistent keeps one WebSocket open across utterances. Finalize then
	// asks Deepgram to flush the utterance rather than close the stream, the
	// next utterance goes down the same socket, and a KeepAlive is sent while
	// the person pauses. Off, every utterance dials its own stream and
	// Finalize closes it, which is the one-utterance contract this adapter
	// started with.
	//
	// The difference is at the start of every utterance: a dial, a TLS
	// handshake and Deepgram's own warm-up sit between the first frame and
	// the first hypothesis, and on a persistent stream they have already
	// happened.
	Persistent bool
	// KeepAliveInterval is how often a silent persistent stream is kept
	// alive. Zero selects the shipped default.
	KeepAliveInterval time.Duration
	// HTTPClient dials the WebSocket. Empty uses the default client, which is
	// what a test server needs overridden.
	HTTPClient *http.Client
}

ListenConfig configures recognition.

The audio sample rate is deliberately absent: it is taken from the first frame and declared to Deepgram, so nothing is resampled on the way in. A recogniser that resamples before recognising has thrown away information no later stage can recover.

type Listener

type Listener struct {
	// contains filtered or unexported fields
}

Listener recognises speech over one Deepgram stream. It is safe for concurrent use. Not persistent, it represents exactly one utterance, like every perception provider here; persistent, it represents one session's stream and each utterance in turn.

func NewListener

func NewListener(config ListenConfig) (*Listener, error)

NewListener validates configuration and returns a listener with no stream yet; the first frame dials.

func (*Listener) Close

func (listener *Listener) Close() error

Close releases the stream for a session that ends.

It exists because a session can end without an endpoint - the caller hangs up, the process stops - and a streaming recogniser holds a socket and a goroutine that nothing else will reclaim. The audio observer closes the provider it drops for exactly this reason.

func (*Listener) Confidence

func (listener *Listener) Confidence() float64

Confidence is Deepgram's confidence in the transcript currently exposed by this stream. It is retained across an empty endpoint marker because that marker closes the preceding text; it does not replace it with a hypothesis that the speaker said nothing.

func (*Listener) Descriptor

func (listener *Listener) Descriptor() v1.Descriptor

Descriptor implements api/v1.PerceptionProvider.

func (*Listener) EndUtterance added in v0.2.0

func (listener *Listener) EndUtterance() error

EndUtterance implements api/v1.UtteranceReusable. It retires an utterance that will not be finalized - the speaker was cut off, the turn was discarded - without giving up the stream. Whatever Deepgram still holds for it is flushed and dropped, so it cannot surface as the opening words of the next utterance.

func (*Listener) Finalize

func (listener *Listener) Finalize(
	ctx context.Context, sourceSample uint64,
) (v1.PerceptionRevision, error)

Finalize ends the utterance, waits for the results Deepgram still owes, and emits one final revision.

On a one-utterance stream that means closing it. On a persistent stream it means asking Deepgram to flush what it has heard and reading until the flushed results arrive; the socket stays open and the next utterance uses it.

func (*Listener) PushFrame

func (listener *Listener) PushFrame(
	ctx context.Context, frame v1.AudioFrame,
) ([]v1.PerceptionRevision, error)

PushFrame sends one contiguous PCM16LE frame and returns whatever Deepgram has said since the previous call.

func (*Listener) SpeechEndpointed

func (listener *Listener) SpeechEndpointed() bool

SpeechEndpointed reports that Deepgram's own VAD marked the current utterance complete. It is an optional capability consumed by the event-aware cascade path; the existing acoustic endpoint path never asks.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL