tone

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: 17 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 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

View Source
const (
	Emotion     = "emotion"
	Move        = "move"
	Sarcasm     = "sarcasm"
	Backchannel = "backchannel"
)

The names of the questions the model answers.

View Source
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

View Source
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

type BackchannelTag struct {
	IsBackchannel bool
	Probability   float64
}

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.

func (*Classifier) Setup

func (c *Classifier) Setup(ctx context.Context) error

Setup loads the model, or shares the one already loaded. It fails, with an error wrapping classifier.ErrClassifier, when the model cannot be 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

func LoadModel(dir string, precision Precision, threads int) (*Model, error)

LoadModel loads the model from a folder holding its config.json, tokenizer.json and ONNX file, using threads CPU threads for one read.

func (*Model) Emotions

func (m *Model) Emotions() []string

Emotions is the emotions the model reads.

func (*Model) Moves

func (m *Model) Moves() []string

Moves is the moves the model reads.

func (*Model) Tag

func (m *Model) Tag(text, previous string) (Tags, error)

Tag reads one turn, with the turn before it when there is one.

type MoveTag

type MoveTag struct {
	Label         string
	Confidence    float64
	Probabilities []Probability
}

MoveTag is what the turn does, such as asking or answering.

type Precision

type Precision string

Precision is the weights a model is loaded with.

const (
	// FP16 is the weights in 16 bits, which give the same tags at half the size.
	FP16 Precision = "fp16"
	// FP32 is the weights in 32 bits.
	FP32 Precision = "fp32"
)

type Probability

type Probability struct {
	Label       string
	Probability float64
}

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.

Jump to

Keyboard shortcuts

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