Documentation
¶
Overview ¶
Package stt provides speech-to-text services for converting audio to text.
The package defines a Service interface that extends base.STTProvider, enabling voice AI applications to transcribe speech from users.
Architecture ¶
The package provides:
- Service interface (extends base.STTProvider) for STT providers
- TranscriptionConfig for audio format configuration
- Multiple provider implementations (OpenAI Whisper, etc.)
Usage ¶
Basic usage with OpenAI Whisper via base.STTProvider:
service := stt.NewOpenAI(os.Getenv("OPENAI_API_KEY"))
resp, err := service.Transcribe(ctx, base.STTRequest{
Audio: audioData,
MIMEType: "audio/pcm",
Hints: map[string]string{"sample_rate": "16000", "language": "en"},
})
if err != nil {
log.Fatal(err)
}
fmt.Println("User said:", resp.Text)
Available Providers ¶
The package includes implementations for:
- OpenAI Whisper (whisper-1 model)
- More providers can be added following the Service interface
Index ¶
- Constants
- Variables
- func APIKeyFromCredential(c credentials.Credential) string
- func PricingFromSpec(spec ProviderSpec) *base.PricingDescriptor
- func RegisterFactory(providerType string, factory Factory)
- func ResolveCredential(ctx context.Context, providerType string, cfgDir string, ...) (credentials.Credential, error)
- func TranscribeWithRetry(ctx context.Context, svc base.STTProvider, req base.STTRequest, ...) (base.STTResponse, error)
- func WrapPCMAsWAV(pcmData []byte, sampleRate, channels, bitsPerSample int) []byte
- type Factory
- type OpenAIOption
- type OpenAIService
- type ProviderSpec
- type RetryConfig
- type Service
- type TranscriptionConfig
- type TranscriptionError
Constants ¶
const ( // Default audio settings. DefaultSampleRate = 16000 DefaultChannels = 1 DefaultBitDepth = 16 // Common audio formats. FormatPCM = "pcm" FormatWAV = "wav" FormatMP3 = "mp3" )
const (
// ModelWhisper1 is the OpenAI Whisper model for transcription.
ModelWhisper1 = "whisper-1"
)
Variables ¶
var ( // ErrEmptyAudio is returned when audio data is empty. ErrEmptyAudio = errors.New("audio data is empty") // ErrRateLimited is returned when the provider rate limits requests. ErrRateLimited = errors.New("rate limited by provider") // ErrInvalidFormat is returned when the audio format is not supported. ErrInvalidFormat = errors.New("unsupported audio format") // ErrAudioTooShort is returned when audio is too short to transcribe. ErrAudioTooShort = errors.New("audio too short to transcribe") )
Common errors for STT services.
Functions ¶
func APIKeyFromCredential ¶ added in v1.4.5
func APIKeyFromCredential(c credentials.Credential) string
APIKeyFromCredential returns the raw API key from an APIKey credential.
func PricingFromSpec ¶ added in v1.4.7
func PricingFromSpec(spec ProviderSpec) *base.PricingDescriptor
PricingFromSpec extracts an optional pricing override from spec.AdditionalConfig.
func RegisterFactory ¶ added in v1.4.5
RegisterFactory registers a factory for the given provider type. Typically called from per-provider package init().
func ResolveCredential ¶ added in v1.4.5
func ResolveCredential( ctx context.Context, providerType string, cfgDir string, cred *credentials.CredentialConfig, ) (credentials.Credential, error)
ResolveCredential is a thin wrapper around base.ResolveCredential, kept here for back-compat with callers that resolve STT-specific credential configs.
func TranscribeWithRetry ¶ added in v1.4.2
func TranscribeWithRetry( ctx context.Context, svc base.STTProvider, req base.STTRequest, retry RetryConfig, ) (base.STTResponse, error)
TranscribeWithRetry calls svc.Transcribe with bounded retry on transient errors. Only errors where TranscriptionError.Retryable is true are retried; all others are returned immediately. Uses full jitter backoff to avoid synchronized retries across concurrent callers.
func WrapPCMAsWAV ¶
WrapPCMAsWAV wraps raw PCM audio data in a WAV header. This is necessary for APIs like OpenAI Whisper that expect file uploads.
Parameters:
- pcmData: Raw PCM audio bytes (little-endian, signed)
- sampleRate: Sample rate in Hz (e.g., 16000)
- channels: Number of channels (1=mono, 2=stereo)
- bitsPerSample: Bits per sample (typically 16)
Returns a byte slice containing WAV-formatted audio.
Types ¶
type OpenAIOption ¶
type OpenAIOption = base.HTTPServiceOption
OpenAIOption configures the OpenAI STT service. It is a type alias for base.HTTPServiceOption so callers can pass base.WithBaseURL, base.WithClient, base.WithModel, etc. directly.
type OpenAIService ¶
type OpenAIService struct {
*base.Implementation // provides Name, Type, Pricing, Validate, Init, HealthCheck, Close
*base.HTTPServiceFields // APIKey, BaseURL, Model, Client
}
OpenAIService implements STT using OpenAI's Whisper API.
func NewOpenAI ¶
func NewOpenAI(apiKey string, opts ...OpenAIOption) *OpenAIService
NewOpenAI creates an OpenAI STT service using Whisper.
func (*OpenAIService) SupportedFormats ¶
func (s *OpenAIService) SupportedFormats() []string
SupportedFormats returns audio formats supported by OpenAI Whisper.
func (*OpenAIService) Transcribe ¶
func (s *OpenAIService) Transcribe(ctx context.Context, req base.STTRequest) (base.STTResponse, error)
Transcribe implements base.STTProvider. It converts audio to text, computes per-second cost from the pricing descriptor, and returns both the transcript and cost metadata. Duration is estimated from the audio length for PCM/WAV input, or from the API's verbose_json response for other formats.
func (*OpenAIService) TranscribeBytes ¶ added in v1.4.7
func (s *OpenAIService) TranscribeBytes( ctx context.Context, audio []byte, config TranscriptionConfig, ) (string, error)
TranscribeBytes converts raw audio bytes to text using the legacy config shape. It delegates to the Whisper API using the simple (non-verbose) response format.
type ProviderSpec ¶ added in v1.4.5
type ProviderSpec = base.CapabilitySpec
ProviderSpec is the runtime form of an STT-provider declaration. It is a type alias for base.CapabilitySpec so the field shape is shared with TTS, embedding, and image factories without code duplication.
type RetryConfig ¶ added in v1.4.2
type RetryConfig struct {
// MaxAttempts is the total number of attempts including the initial
// call. 3 means "initial + up to 2 retries". Values < 1 are
// treated as 1 (no retry).
MaxAttempts int
// InitialDelay is the base backoff before the first retry.
InitialDelay time.Duration
// MaxDelay caps the per-attempt backoff.
MaxDelay time.Duration
}
RetryConfig configures bounded retry for STT transcription calls. Defaults are on (unlike streaming retry) because STT calls are one-shot and idempotent — retry has no content-duplication risk, and the alternative is silently dropped speech.
func DefaultRetryConfig ¶ added in v1.4.2
func DefaultRetryConfig() RetryConfig
DefaultRetryConfig returns sensible defaults for STT retry.
type Service ¶
type Service interface {
base.STTProvider
// TranscribeBytes converts raw audio bytes to text using the given config.
// The returned string is the transcribed text, or an error if transcription fails.
TranscribeBytes(ctx context.Context, audio []byte, config TranscriptionConfig) (string, error)
// SupportedFormats returns supported audio input formats.
// Common values: "pcm", "wav", "mp3", "m4a", "webm"
SupportedFormats() []string
}
Service transcribes audio to text. This interface abstracts different STT providers (OpenAI Whisper, Google, etc.) enabling voice AI applications to use any provider interchangeably.
Service extends base.STTProvider so the STT stage and retry layer work with any implementation without requiring a double type-assertion.
func CreateFromSpec ¶ added in v1.4.5
func CreateFromSpec(spec ProviderSpec) (Service, error)
CreateFromSpec returns a Service implementation for the given spec.
type TranscriptionConfig ¶
type TranscriptionConfig struct {
// Format is the audio format ("pcm", "wav", "mp3").
// Default: "pcm"
Format string
// SampleRate is the audio sample rate in Hz.
// Default: 16000
SampleRate int
// Channels is the number of audio channels (1=mono, 2=stereo).
// Default: 1
Channels int
// BitDepth is the bits per sample for PCM audio.
// Default: 16
BitDepth int
// Language is a hint for the transcription language (e.g., "en", "es").
// Optional - improves accuracy if provided.
Language string
// Model is the STT model to use (provider-specific).
// For OpenAI: "whisper-1"
Model string
// Prompt is a text prompt to guide transcription (provider-specific).
// Can improve accuracy for domain-specific vocabulary.
Prompt string
}
TranscriptionConfig configures speech-to-text transcription.
func DefaultTranscriptionConfig ¶
func DefaultTranscriptionConfig() TranscriptionConfig
DefaultTranscriptionConfig returns sensible defaults for transcription.
type TranscriptionError ¶
type TranscriptionError struct {
// Provider is the STT provider name.
Provider string
// Code is the provider-specific error code.
Code string
// Message is a human-readable error message.
Message string
// Cause is the underlying error, if any.
Cause error
// Retryable indicates whether the request can be retried.
Retryable bool
}
TranscriptionError represents an error during transcription.
func NewTranscriptionError ¶
func NewTranscriptionError(provider, code, message string, cause error, retryable bool) *TranscriptionError
NewTranscriptionError creates a new TranscriptionError.
func (*TranscriptionError) Error ¶
func (e *TranscriptionError) Error() string
Error implements the error interface.
func (*TranscriptionError) Is ¶
func (e *TranscriptionError) Is(target error) bool
Is implements error matching for errors.Is.
func (*TranscriptionError) Unwrap ¶
func (e *TranscriptionError) Unwrap() error
Unwrap returns the underlying error.