perception

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: BSD-2-Clause Imports: 18 Imported by: ? modules, 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 perception reads how each user turn was said by asking classifiers (alpha).

A Reader goes between the STT service and the user aggregator, and sets each final transcription's Perception from the answers of its sources: a classifier each, with the questions it is asked. Part of expressive conversation (alpha). These types are likely to change between releases.

Index

Constants

View Source
const (
	// DefaultTextTimeout is how long a transcription waits for a text
	// classifier's answers by default, counted from when it arrives.
	DefaultTextTimeout = 500 * time.Millisecond
	// DefaultAudioTimeout is how long a transcription waits for the answers of
	// a classifier that hears by default, counted from when the user stops
	// speaking.
	DefaultAudioTimeout = 2 * time.Second
)

Variables

View Source
var ErrInvalidConfig = errors.New("perception: invalid configuration")

ErrInvalidConfig reports a reader or a source that cannot be built as given.

Functions

This section is empty.

Types

type BaseSource

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

BaseSource is a source whose questions are known when it is built.

hume, err := perception.NewSource(perception.SourceConfig{
	Classifier: audioClassifier,
	Questions: []perception.NamedQuestion{{
		Name: "hesitation",
		Question: perception.Question{
			Question:    classifier.YesNoQuestion{Instructions: "Does the speaker sound unsure?"},
			Description: "The chance the user sounded unsure, from 0 to 1.",
		},
	}},
})

func NewSource

func NewSource(cfg SourceConfig) (*BaseSource, error)

NewSource builds a source. It fails with ErrInvalidConfig when a name cannot be written in a reading.

func (*BaseSource) Classifier

func (s *BaseSource) Classifier() classifier.Classifier

Classifier is the classifier that answers the questions.

func (*BaseSource) Names

func (s *BaseSource) Names() []string

Names is the names of the signals the source reports.

func (*BaseSource) Questions

func (s *BaseSource) Questions() ([]NamedQuestion, error)

Questions is the questions asked about each turn, by the name of the signal each reports.

func (*BaseSource) Timeout

func (s *BaseSource) Timeout() time.Duration

Timeout is how long a transcription waits for the classifier's answers.

type Config

type Config struct {
	// Name is the processor's name. Empty is "PerceptionReader".
	Name string
	// Sources is the classifiers to ask, each with its questions and its
	// timeout. At least one; a classifier goes in one source, and a signal's
	// name in one.
	Sources []Source
	// ReplacePerception makes the reader's reading replace the one a
	// transcription carries, such as the STT service's, instead of being added
	// to it.
	ReplacePerception bool
}

Config configures a Reader.

type NamedQuestion

type NamedQuestion struct {
	// Name is the signal's name, as written in a reading: no whitespace, "=",
	// ",", "[", "]", "(" or ")".
	Name     string
	Question Question
}

NamedQuestion is a question with the name of the signal it reports.

type Question

type Question struct {
	// Question is what the classifier is asked. A choice is reported as its
	// likeliest options, a yes/no as its probability and a score as its
	// position on the scale.
	Question classifier.Question
	// Description is a short line telling the LLM how to read the signal, such
	// as its range, or empty.
	Description string
	// MaxLabels is the most options a choice reports, likeliest first. Zero is
	// 2; it is at least 1.
	MaxLabels int
}

Question is one signal a Reader reports, and the question it asks for it.

type Reader

type Reader struct {
	*processor.Base
	// contains filtered or unexported fields
}

Reader reads how each user turn was said by asking classifiers.

It goes between the STT service and the user aggregator. Each final transcription gets the classifiers' answers as its Perception, one signal per question, named after it. Every classifier's reading is also broadcast in a frames.PerceptionFrame as soon as it is made, and the signals are declared at start in a frames.PerceptionMetadataFrame.

A text classifier reads each final transcription, with the bot's reply before it. A classifier that can hear, as its SupportsAudio says, hears the user's speech each time they stop: the latest, topped up with the end of what they said before it since the bot last spoke when that is shorter than the classifier answers best. It hears one clip at a time, and of the clips that come while it is busy, only the newest. The transcriptions of that speech wait for its answers, up to its source's timeout after the user stopped.

A reader asks each of its sources about the same turn, all at once, and their signals go in the turn's one reading, in the sources' order. A transcription goes on once every source has read it or run out of time. A source that is late or whose classifier fails leaves its signals out, and a transcription none of them read passes unread. A classifier that cannot be set up leaves the reader unusable.

A transcription that already carries a reading, such as the STT service's, gets the reader's added to it, and the reader declares the signals declared before it together with its own. With Config.ReplacePerception, the reader's reading replaces the one the transcription carries instead.

Turn expressive conversation on with pipeline.Params.EnableExpressiveConversation for the LLM to read the turns:

reader, err := perception.New(perception.Config{Sources: []perception.Source{frustration}})
p := pipeline.New(transport.Input(), stt, reader, userAggregator, llm, ...)

func New

func New(cfg Config) (*Reader, error)

New builds a Reader. It fails with ErrInvalidConfig when there are no sources, or when two report the same signal or ask the same classifier.

func (*Reader) Cleanup

func (r *Reader) Cleanup(ctx context.Context) error

Cleanup stops the reads and the release of the transcriptions held, and cleans the classifiers up.

func (*Reader) ProcessFrame

func (r *Reader) ProcessFrame(ctx context.Context, f frames.Frame, dir processor.Direction) error

ProcessFrame reads the user's turns, and keeps the bot's replies to read them against.

func (*Reader) Setup

func (r *Reader) Setup(ctx context.Context, s processor.Setup) error

Setup sets the processor and its classifiers up. A classifier that cannot be set up is reported as an error that leaves the reader unusable.

func (*Reader) Sources

func (r *Reader) Sources() []Source

Sources is the classifiers asked about each turn, each with its questions.

type Source

type Source interface {
	// Classifier is what answers the questions about each turn.
	Classifier() classifier.Classifier
	// Questions is the questions asked about each turn, in the order their
	// signals are reported. It fails, with an error wrapping
	// classifier.ErrClassifier, while they are not known yet.
	Questions() ([]NamedQuestion, error)
	// Names is the names of the signals the source reports, known before it is
	// set up.
	Names() []string
	// Timeout is how long a transcription waits for the classifier's answers.
	Timeout() time.Duration
}

Source is one classifier a Reader asks, and the questions it is asked.

A reader with several sources asks each one about the same turn and writes their answers in one reading, so a signal's name belongs to one source. The classifier says how its source reads: a text classifier reads each final transcription, and one that can hear, as its SupportsAudio says, hears the user's audio.

NewSource builds a source whose questions are known up front. A classifier whose questions are not known until it is set up gets a source of its own that embeds a BaseSource and overrides Questions, and Names to name its signals before then.

type SourceConfig

type SourceConfig struct {
	// Classifier is what answers the questions about each turn. Required.
	Classifier classifier.Classifier `validate:"required"`
	// Questions is the questions, each with the name of the signal it reports.
	Questions []NamedQuestion
	// Timeout is how long a transcription waits for the classifier's answers.
	// Nil is DefaultTextTimeout for a text classifier, counted from when the
	// transcription arrives, and DefaultAudioTimeout for one that hears,
	// counted from when the user stops speaking.
	Timeout *time.Duration
}

SourceConfig configures a BaseSource.

Jump to

Keyboard shortcuts

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