Octoroom.Agent.STT behaviour (octoroom_agents v0.1.0)

Copy Markdown View Source

Streaming speech-to-text providers.

A provider is a process, usually a WebSocket client, that one owner feeds with audio and that reports back with messages:

{:ok, stt} = Octoroom.Agent.STT.start_link("soniox:stt-rt-v6", sample_rate: 16_000)
Octoroom.Agent.STT.send_audio(stt, pcm)
# receive {:stt, ^stt, {:final, "hello"}}

Options

start_link/2 takes a provider and options. The provider is a "provider:model" string, a registered name, or a module; options can also name it with :provider and :model. Every provider takes:

  • :owner - the process that gets the events (default: the caller)
  • :api_key - defaults to the provider's environment variable
  • :model - needed: no provider picks one for you
  • :sample_rate - of the audio sent: 16-bit little-endian mono PCM
  • :language_hints

Anything else is passed to the provider, which ignores what it doesn't use.

Events

The owner gets {:stt, pid, event}, where event is:

  • :connected - audio sent from now on is heard
  • {:partial, text} - an interim transcript of the current utterance
  • {:final, text} - the final transcript of an utterance, after the provider's endpointing or a finalize/1. After finalize/1, it comes even if text is ""
  • :finished - the provider is done, after close/1
  • {:warning, reason} - something went wrong but the session goes on
  • {:error, reason} - the session failed; reason is a string or a map with :message when the provider gave one
  • {:disconnected, reason, %{intentional?: boolean}} - the connection closed; intentional after close/1

Adding a provider

Implement this behaviour, sending events with notify/2, and register it:

config :octoroom_agents, stt_providers: [acme: MyApp.AcmeSTT]

A module that implements the behaviour also works as :provider without being registered.

Summary

Callbacks

Flushes the audio sent, then ends the session.

Options used when the caller doesn't give them, such as :sample_rate.

Finalizes what's been heard so far, without waiting for the provider's endpointing. Answered with a {:final, text}. Providers without it just wait for their endpointing.

Starts a session that sends events to opts[:owner].

Functions

A message for a user to read about an {:error, reason} event.

Asks module's session to finalize, if the provider can.

Sends event from the calling provider process to owner.

The registered providers, by name.

Finds the provider for opts. Returns the module and its options, with the provider's defaults filled in.

Starts a session. See the module docs for provider and opts.

Types

event()

@type event() ::
  :connected
  | {:partial, String.t()}
  | {:final, String.t()}
  | :finished
  | {:warning, term()}
  | {:error, term()}
  | {:disconnected, term(), %{intentional?: boolean()}}

Callbacks

close(pid)

@callback close(pid()) :: :ok

Flushes the audio sent, then ends the session.

default_options()

(optional)
@callback default_options() :: keyword()

Options used when the caller doesn't give them, such as :sample_rate.

finalize(pid)

(optional)
@callback finalize(pid()) :: :ok

Finalizes what's been heard so far, without waiting for the provider's endpointing. Answered with a {:final, text}. Providers without it just wait for their endpointing.

send_audio(pid, audio)

@callback send_audio(pid(), audio :: binary()) :: :ok

start_link(opts)

@callback start_link(opts :: keyword()) :: {:ok, pid()} | {:error, term()}

Starts a session that sends events to opts[:owner].

Functions

error_message(message)

A message for a user to read about an {:error, reason} event.

finalize(module, pid)

Asks module's session to finalize, if the provider can.

notify(owner, event)

Sends event from the calling provider process to owner.

providers()

The registered providers, by name.

resolve(opts)

Finds the provider for opts. Returns the module and its options, with the provider's defaults filled in.

start_link(provider, opts \\ [])

Starts a session. See the module docs for provider and opts.