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.
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 Base
- func (b *Base) Ask(ctx context.Context, state any, questions []Named[Question]) (map[string]Result, error)
- 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) Name() string
- 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) YesNo(ctx context.Context, state any, questions []Named[YesNoQuestion]) (map[string]YesNoResult, error)
- type ChoiceQuestion
- type ChoiceResult
- type Classifier
- type Named
- type Option
- type Question
- type Result
- type ScoreLevel
- type ScoreQuestion
- type ScoreResult
- type YesNoQuestion
- type YesNoResult
Constants ¶
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 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.
AskQuestions(
ctx context.Context, state any, questions []Named[Question],
) (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 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]) (map[string]Result, error)
Ask answers several questions about one state, one result per name, each of the type its question calls for. It fails with an error wrapping ErrClassifier when the answers could not be produced, or not in time.
func (*Base) Choice ¶
func (b *Base) Choice( ctx context.Context, state any, questions []Named[ChoiceQuestion], ) (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) Score ¶
func (b *Base) Score( ctx context.Context, state any, questions []Named[ScoreQuestion], ) (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.
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 {
// 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.
Ask(ctx context.Context, state any, questions []Named[Question]) (map[string]Result, error)
// YesNo asks whether the state meets each condition.
YesNo(ctx context.Context, state any, questions []Named[YesNoQuestion]) (map[string]YesNoResult, error)
// Choice asks which option fits the state, for each question.
Choice(ctx context.Context, state any, questions []Named[ChoiceQuestion]) (map[string]ChoiceResult, error)
// Score asks where the state falls on each scale.
Score(ctx context.Context, state any, questions []Named[ScoreQuestion]) (map[string]ScoreResult, error)
}
Classifier answers typed questions about a state. Base implements everything but Model, which the classifier embedding it provides.
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. |