Documentation
¶
Overview ¶
Package classifier answers typed questions about some state.
A classifier is a plain object. Whoever needs answers creates one, keeps it, and calls it. It does not sit in a pipeline and no frames flow into it. A question is a YesNoQuestion, a ChoiceQuestion or a ScoreQuestion. Questions are asked by name, several about one state at once: Ask takes any mix of kinds, and YesNo, Choice and Score take questions of one kind and return typed results. Questions can come with inputs alongside the state, of the kinds the classifier takes: images (Image), clips of audio (Audio) and files such as PDFs or videos (File).
The state a question is about is plain text, or structured data such as a transcript with speaker labels or a trimmed screen snapshot: a string, or a value that encodes to a JSON object or array. The instructions, the meanings and the options of a question take the same.
The implementations live in the subpackages: classifier/llm answers through any LLM service that runs a one-shot inference, and classifier/jev through Jev, TypeSafe's hosted classification model.
Index ¶
- Constants
- Variables
- type Asker
- type Audio
- type Base
- func (b *Base) Ask(ctx context.Context, state any, questions []Named[Question], inputs ...Input) (map[string]Result, error)
- func (b *Base) AudioSampleRate() int
- func (b *Base) Choice(ctx context.Context, state any, questions []Named[ChoiceQuestion], ...) (map[string]ChoiceResult, error)
- func (b *Base) Cleanup(ctx context.Context) error
- func (b *Base) Events() *events.Registry
- func (b *Base) FileTypes() []string
- func (b *Base) MaxAudio() time.Duration
- func (b *Base) MinAudio() time.Duration
- func (b *Base) Name() string
- func (b *Base) PreferredAudio() time.Duration
- func (b *Base) Score(ctx context.Context, state any, questions []Named[ScoreQuestion], ...) (map[string]ScoreResult, error)
- func (b *Base) Setup(context.Context) error
- func (b *Base) String() string
- func (b *Base) SupportsAudio() bool
- func (b *Base) SupportsImages() bool
- func (b *Base) YesNo(ctx context.Context, state any, questions []Named[YesNoQuestion], ...) (map[string]YesNoResult, error)
- type Capabilities
- type ChoiceQuestion
- type ChoiceResult
- type Classifier
- type File
- type Image
- type Input
- type Named
- type Option
- type Question
- type Result
- type ScoreLevel
- type ScoreQuestion
- type ScoreResult
- type YesNoQuestion
- type YesNoResult
Constants ¶
const ( ImagePNG = "image/png" ImageJPEG = "image/jpeg" ImageWebP = "image/webp" )
The image types every classifier that sees images accepts.
const EventMetrics = "on_metrics"
EventMetrics is raised after every call with its metrics, a []frames.MetricsData: the time the call took and, when the classifier knows it, the tokens it used. A classifier cannot push frames, so the owner is the one to put them in a MetricsFrame.
Variables ¶
var ErrClassifier = errors.New("classifier")
ErrClassifier is wrapped by every error a classifier returns because it could not answer a question. Test for it with errors.Is.
var ErrInvalidInput = errors.New("classifier: invalid input")
ErrInvalidInput reports an input that cannot be built as given: an image that is not one, a file with both data and a URL or neither.
var ErrInvalidQuestion = errors.New("classifier: invalid question")
ErrInvalidQuestion reports a question that cannot be asked as it stands.
var ErrNoSuchLevel = errors.New("classifier: not a level of this scale")
ErrNoSuchLevel reports a level a scale does not have.
Functions ¶
This section is empty.
Types ¶
type Asker ¶
type Asker interface {
// AskQuestions answers the questions, one result per name, each of the type
// its question calls for. usage is nil when the call's tokens are unknown.
// It answers or fails within a bound of its own, so a caller waiting on it
// is never left hanging, and its errors wrap ErrClassifier.
//
// inputs holds only the kinds the classifier takes, in the order they were
// given, each clip of an accepted length and at its AudioSampleRate. It is
// empty, never nil, when the questions came with none.
AskQuestions(
ctx context.Context, state any, questions []Named[Question], inputs []Input,
) (results map[string]Result, usage *frames.LLMTokenUsage, err error)
// Model is the model that answers, named in the metrics, or "".
Model() string
}
Asker is what a classifier implements: answering the questions about a state in one call, and saying what tokens the call used when that is known.
type Audio ¶ added in v0.5.0
type Audio struct {
// Audio is the clip, as 16-bit mono PCM.
Audio []byte
// SampleRate is the clip's sample rate in Hz.
SampleRate int
}
Audio is a clip of audio the questions can hear, alongside the state.
type Base ¶
type Base struct {
// contains filtered or unexported fields
}
Base holds what every classifier shares: its name, its events, and the typed ways of asking, built on the Asker it is given. A classifier embeds it.
An owner calls Setup before the first question and Cleanup when it is done. A classifier raises EventMetrics after every call; its owner puts the data in a MetricsFrame:
events.On(c.Events(), classifier.EventMetrics, func(ctx context.Context, data []frames.MetricsData) {
_ = p.PushFrame(ctx, frames.NewMetricsFrame(data...), processor.Downstream)
})
func NewBase ¶
NewBase builds the base of a classifier of the named type, answering through asker. name names this instance; empty uses "<typeName>#<n>".
func (*Base) Ask ¶
func (b *Base) Ask( ctx context.Context, state any, questions []Named[Question], inputs ...Input, ) (map[string]Result, error)
Ask answers several questions about one state, one result per name, each of the type its question calls for. inputs are the images, clips of audio and files that come with the state, of the kinds the classifier takes.
It fails with an error wrapping ErrClassifier when the answers could not be produced, or not in time: every classifier answers or fails within a bound of its own, so a caller waiting on it is never left hanging. Also when an input is of a kind the classifier does not take, or a clip of a length it does not accept.
func (*Base) AudioSampleRate ¶ added in v0.5.0
AudioSampleRate is the sample rate clips are resampled to, or zero to keep their own, the default.
func (*Base) Choice ¶
func (b *Base) Choice( ctx context.Context, state any, questions []Named[ChoiceQuestion], inputs ...Input, ) (map[string]ChoiceResult, error)
Choice asks which option fits the state, for each question, and returns the option that fits and how likely each one is, by the same names.
func (*Base) FileTypes ¶ added in v0.5.0
FileTypes is the MIME types of the files questions can come with: none by default.
func (*Base) MaxAudio ¶ added in v0.5.0
MaxAudio is the longest clip the classifier accepts, or zero for no limit, the default.
func (*Base) MinAudio ¶ added in v0.5.0
MinAudio is the shortest clip the classifier accepts: zero by default.
func (*Base) PreferredAudio ¶ added in v0.5.0
PreferredAudio is how long a clip the classifier answers best: its shortest by default.
func (*Base) Score ¶
func (b *Base) Score( ctx context.Context, state any, questions []Named[ScoreQuestion], inputs ...Input, ) (map[string]ScoreResult, error)
Score asks where the state falls on each scale, and returns the position on each scale and how likely each level is, by the same names.
func (*Base) Setup ¶
Setup prepares the classifier ahead of the first question. The base has nothing to prepare.
func (*Base) SupportsAudio ¶ added in v0.5.0
SupportsAudio reports whether questions can come with a clip of audio: not by default.
func (*Base) SupportsImages ¶ added in v0.5.0
SupportsImages reports whether questions can come with images: not by default.
type Capabilities ¶ added in v0.5.0
type Capabilities interface {
// SupportsImages reports whether questions can come with images. False by
// default.
SupportsImages() bool
// SupportsAudio reports whether questions can come with a clip of audio.
// False by default.
SupportsAudio() bool
// MinAudio is the shortest clip the classifier accepts. Zero by default.
MinAudio() time.Duration
// MaxAudio is the longest clip the classifier accepts, or zero for no
// limit, the default.
MaxAudio() time.Duration
// PreferredAudio is how long a clip the classifier answers best: its
// MinAudio by default.
PreferredAudio() time.Duration
// AudioSampleRate is the sample rate clips are resampled to, or zero to keep
// their own, the default.
AudioSampleRate() int
// FileTypes is the MIME types of the files questions can come with; none by
// default.
FileTypes() []string
}
Capabilities is what a classifier says about the inputs it takes. Base implements each with its default; a classifier that takes more overrides the ones that differ, and Base reads them through the classifier it was given.
type ChoiceQuestion ¶
type ChoiceQuestion struct {
// Instructions is what is being decided.
Instructions any
// Options are the options to choose from, in order.
Options []Option
}
ChoiceQuestion asks which of several options fits the state.
func (ChoiceQuestion) Has ¶
func (q ChoiceQuestion) Has(name string) bool
Has reports whether the question offers an option named name.
func (ChoiceQuestion) OptionNames ¶
func (q ChoiceQuestion) OptionNames() []string
OptionNames is the names of the options, in order.
type ChoiceResult ¶
type ChoiceResult struct {
// Choice is the option that fits best.
Choice string
// Probabilities is how likely each option is, keyed by option.
Probabilities map[string]float64
// Confidence is how sure the classifier is of Choice, from 0 to 1.
Confidence float64
}
ChoiceResult is the answer to a ChoiceQuestion.
type Classifier ¶
type Classifier interface {
Capabilities
// Name is the classifier's name, which its metrics are reported under.
Name() string
// Model is the model that answers, named in the metrics, or "".
Model() string
// Events is the classifier's event registry, raising EventMetrics.
Events() *events.Registry
// Setup prepares the classifier ahead of the first question.
Setup(ctx context.Context) error
// Cleanup releases what the classifier holds.
Cleanup(ctx context.Context) error
// Ask answers several questions of any kinds about one state, with the
// inputs that come with it.
Ask(ctx context.Context, state any, questions []Named[Question], inputs ...Input) (map[string]Result, error)
// YesNo asks whether the state meets each condition.
YesNo(
ctx context.Context, state any, questions []Named[YesNoQuestion], inputs ...Input,
) (map[string]YesNoResult, error)
// Choice asks which option fits the state, for each question.
Choice(
ctx context.Context, state any, questions []Named[ChoiceQuestion], inputs ...Input,
) (map[string]ChoiceResult, error)
// Score asks where the state falls on each scale.
Score(
ctx context.Context, state any, questions []Named[ScoreQuestion], inputs ...Input,
) (map[string]ScoreResult, error)
}
Classifier answers typed questions about a state. Base implements everything but Model, which the classifier embedding it provides.
Questions can come with inputs: images, clips of audio and files. A classifier says which it takes through its Capabilities and refuses the rest. One that hears declares the clips it accepts: from MinAudio to MaxAudio long, resampled to AudioSampleRate before AskQuestions gets them. A clip of another length is refused. It also says how long a clip it answers best, in PreferredAudio.
type File ¶ added in v0.5.0
type File struct {
// ContentType is the file's MIME type, such as "application/pdf" or
// "video/mp4".
ContentType string
// Data is the file's bytes, or nil for a file given by URL.
Data []byte
// URL is where the file can be fetched from, for a classifier whose provider
// fetches files itself, or empty for a file given by its bytes.
URL string
}
File is an encoded file the questions can look at, alongside the state, such as a PDF or a video. It holds its bytes or the URL it can be fetched from, never both. Build one with NewFile or FileFromFrame.
func FileFromFrame ¶ added in v0.5.0
func FileFromFrame(frame frames.FileRawData) (File, error)
FileFromFrame builds a file from a file frame's payload, such as a file the user sent: a base64 data URL is decoded, and a URL is kept.
type Image ¶ added in v0.5.0
type Image struct {
// Data is the encoded image: PNG, JPEG or WebP.
Data []byte
// ContentType is the image's MIME type: ImagePNG, ImageJPEG or ImageWebP.
ContentType string
}
Image is an encoded image the questions can look at, alongside the state. Build one with NewImage or ImageFromFrame.
func ImageFromFrame ¶ added in v0.5.0
func ImageFromFrame(frame frames.ImageRawData) (Image, error)
ImageFromFrame builds an image from an image frame's payload, such as a frame from the user's camera. Raw pixels are encoded as a JPEG. A payload whose format is already a MIME type keeps its bytes.
type Input ¶ added in v0.5.0
type Input interface {
// contains filtered or unexported methods
}
Input is what can come with the state: an Image, a clip of Audio or a File.
type Option ¶
type Option struct {
// Name is the option, as the answer names it.
Name string
// Description is when the option applies, or nil when its name says enough.
Description any
}
Option is one option of a ChoiceQuestion.
type Question ¶
type Question interface {
// contains filtered or unexported methods
}
Question is one of YesNoQuestion, ChoiceQuestion or ScoreQuestion.
type Result ¶
type Result interface {
// contains filtered or unexported methods
}
Result is one of YesNoResult, ChoiceResult or ScoreResult.
type ScoreLevel ¶
type ScoreLevel struct {
// Level is the level as the question gave it.
Level any
// Probability is how likely the state is at this level, from 0 to 1.
Probability float64
}
ScoreLevel is one level of a ScoreQuestion's scale and how likely it is.
type ScoreQuestion ¶
type ScoreQuestion struct {
// Instructions is what is being rated.
Instructions any
// Levels are the levels of the scale in order, lowest first, each described
// in a few words or as structured data. At least two.
Levels []any
}
ScoreQuestion asks where the state falls on an ordered scale.
type ScoreResult ¶
type ScoreResult struct {
// Score is where the state falls on the scale, as a position from 0 (the
// first level) to one less than the number of levels. It is the
// probability-weighted position, so it may fall between two levels.
Score float64
// Levels is how likely each level is, in the question's order.
Levels []ScoreLevel
// Confidence is how sure the classifier is of Score, from 0 to 1.
Confidence float64
}
ScoreResult is the answer to a ScoreQuestion.
func (ScoreResult) Probability ¶
func (r ScoreResult) Probability(level any) (float64, error)
Probability is how likely one level is, given as the question gave it. It reports ErrNoSuchLevel for a level the scale does not have.
type YesNoQuestion ¶
type YesNoQuestion struct {
// Instructions is what is being checked for, as a yes or no question. Text,
// or structured data holding the question in one field and what it refers
// to in others.
Instructions any
// Yes is what counts as a yes, when the question alone leaves it open; nil
// when it does not.
Yes any
// No is what counts as a no; nil when the question alone says.
No any
}
YesNoQuestion asks whether the state meets a condition.
type YesNoResult ¶
type YesNoResult struct {
// Probability is how likely the answer is yes, from 0 to 1.
Probability float64
}
YesNoResult is the answer to a YesNoQuestion.
func (YesNoResult) IsYes ¶
func (r YesNoResult) IsYes() bool
IsYes reports whether yes is the likelier answer. A caller that needs more certainty than that compares Probability with a threshold of its own.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package jev is a classifier backed by Jev, TypeSafe's hosted classification model.
|
Package jev is a classifier backed by Jev, TypeSafe's hosted classification model. |
|
Package llm is a classifier backed by any LLM service that runs a one-shot inference.
|
Package llm is a classifier backed by any LLM service that runs a one-shot inference. |
|
Package tone is a classifier backed by the tone reader model, a local model of how one turn of text was meant (alpha).
|
Package tone is a classifier backed by the tone reader model, a local model of how one turn of text was meant (alpha). |