Documentation
¶
Overview ¶
Package tone is a classifier backed by the tone reader model, a local model of how one turn of text was meant (alpha).
The model (https://huggingface.co/pipecat-ai/tone-models) reads one turn of text, with the turn before it, and returns the speaker's emotion, whether the turn is sarcastic, the move it makes and whether it is only a listening sound, each with a calibrated confidence. It runs on the CPU in a few milliseconds, through the ONNX runtime.
These types are likely to change between releases.
Index ¶
- Constants
- Variables
- type BackchannelTag
- type Classifier
- func (c *Classifier) AskQuestions(_ context.Context, state any, ...) (map[string]classifier.Result, *frames.LLMTokenUsage, error)
- func (c *Classifier) Model() string
- func (c *Classifier) Questions() ([]classifier.Named[classifier.Question], error)
- func (c *Classifier) Setup(ctx context.Context) error
- type Config
- type EmotionTag
- type Model
- type MoveTag
- type Precision
- type Probability
- type SarcasmTag
- type Tags
Constants ¶
const ( Emotion = "emotion" Move = "move" Sarcasm = "sarcasm" Backchannel = "backchannel" )
The names of the questions the model answers.
const ( // Repo is the Hugging Face repository the tone models are released in. Repo = "pipecat-ai/tone-models" // Revision is the reader model's release downloaded by default. Revision = "reader-rc2" )
Variables ¶
var ErrPrecision = errors.New("tone: unknown precision")
ErrPrecision is returned for a precision the model is not released in.
Functions ¶
This section is empty.
Types ¶
type BackchannelTag ¶
BackchannelTag is whether the turn is only a listening sound.
type Classifier ¶
type Classifier struct {
*classifier.Base
// contains filtered or unexported fields
}
Classifier answers the tone reader model's own questions about one turn of text.
It answers only the questions built into the model, the ones Questions returns: the turn's "emotion" and "move", whether it is sarcastic ("sarcasm"), and whether it is only a listening sound such as "mhm" ("backchannel"). Any other question fails with an error wrapping classifier.ErrClassifier. The questions take their options from the model, which Setup loads.
The state is the turn's text, or a map with "user" and optionally "assistant" to read it with the bot's words before it.
The model loads in Setup, once a process, and every classifier shares it. The first load downloads it from Hugging Face.
c, err := tone.New(tone.Config{})
if err := c.Setup(ctx); err != nil { ... }
questions, _ := c.Questions()
results, err := c.Ask(ctx, map[string]any{
"assistant": "Your table is ready in forty minutes.", "user": "Oh great.",
}, questions)
sarcastic := results[tone.Sarcasm].(classifier.YesNoResult).Probability
func New ¶
func New(cfg Config) (*Classifier, error)
New builds a tone classifier. It fails on a configuration it cannot use.
func (*Classifier) AskQuestions ¶
func (c *Classifier) AskQuestions( _ context.Context, state any, questions []classifier.Named[classifier.Question], _ []classifier.Input, ) (map[string]classifier.Result, *frames.LLMTokenUsage, error)
AskQuestions implements classifier.Asker, answering the questions with one read of the turn. The call reports no token usage.
func (*Classifier) Model ¶
func (c *Classifier) Model() string
Model is the model's folder, or its release on Hugging Face.
func (*Classifier) Questions ¶
func (c *Classifier) Questions() ([]classifier.Named[classifier.Question], error)
Questions is the questions the model answers, by name. It fails, with an error wrapping classifier.ErrClassifier, before the model is loaded.
type Config ¶
type Config struct {
// Name names the classifier in its metrics; empty uses
// "ToneClassifier#<n>".
Name string
// Revision is the model's release on Hugging Face, such as "reader-rc2".
// Empty is Revision.
Revision string
// ModelDir is a folder to load the model from instead of downloading one,
// laid out as a release's "reader" folder. Revision is then unused.
ModelDir string
// Precision is the weights to load. Empty is FP16.
Precision Precision `validate:"omitempty,oneof=fp16 fp32"`
// Threads is the CPU threads for one read. Zero is 4.
Threads int `validate:"min=0"`
// CacheDir is where a downloaded model is kept. Empty is Hugging Face's own
// cache, ~/.cache/huggingface/hub.
CacheDir string
}
Config configures a Classifier. The zero value downloads the default release at FP16 and reads with four threads.
type EmotionTag ¶
type EmotionTag struct {
Label string
Intensity string
// Confidence is the likeliest label's probability.
Confidence float64
// Probabilities is every emotion's probability, in the model's order.
Probabilities []Probability
}
EmotionTag is the feeling the turn's words show.
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is the tone reader model, on the CPU.
func DownloadModel ¶
func DownloadModel( ctx context.Context, revision string, precision Precision, threads int, cacheDir string, ) (*Model, error)
DownloadModel loads the model's release from Hugging Face, downloading its files into cacheDir the first time. An empty cacheDir is Hugging Face's own cache.
func LoadModel ¶
LoadModel loads the model from a folder holding its config.json, tokenizer.json and ONNX file, using threads CPU threads for one read.
type MoveTag ¶
type MoveTag struct {
Label string
Confidence float64
Probabilities []Probability
}
MoveTag is what the turn does, such as asking or answering.
type Probability ¶
Probability is one label's probability.
type SarcasmTag ¶
type SarcasmTag struct {
Sarcastic bool
// Probability is the calibrated chance it is sarcastic.
Probability float64
}
SarcasmTag is whether the turn is sarcastic.
type Tags ¶
type Tags struct {
Emotion EmotionTag
Move MoveTag
Sarcasm SarcasmTag
Backchannel BackchannelTag
}
Tags is what the model reads of one turn.