llmclassifier

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: BSD-2-Clause Imports: 14 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 llmclassifier is a classifier backed by any LLM service that runs a one-shot inference.

Every question about a state goes to the LLM in one out-of-pipeline call, through the service's RunInference. The LLM is asked for one JSON object with an answer per question, and the object is parsed into results.

Index

Constants

View Source
const DefaultInstructions = "You are a classifier. Each message describes some input and asks one or " +
	"more questions about it. Every question has a name and says the shape its " +
	"answer must have. Reply with a single JSON object, and nothing else: one " +
	"key per question name, each holding that question's answer in the shape " +
	"asked for. Probabilities and confidences are numbers from 0 to 1. For " +
	`example, a yes or no question named "voicemail" is answered ` +
	`{"voicemail": {"probability": 0.97}}. A choice question named "department" ` +
	"with the options billing, support and sales is answered " +
	`{"department": {"choice": "billing", "probabilities": {"billing": 0.92, ` +
	`"support": 0.06, "sales": 0.02}}}. A score question named "mood" on a ` +
	"scale of three levels is answered with a probability per level, by position, " +
	`{"mood": {"probabilities": {"0": 0.1, "1": 0.7, "2": 0.2}}}. ` +
	"Several questions about one input get one object with an answer for each " +
	"of them."

DefaultInstructions is the system instruction the LLM answers under unless the classifier is given its own.

Variables

This section is empty.

Functions

This section is empty.

Types

type Classifier

type Classifier struct {
	*classifier.Base
	// contains filtered or unexported fields
}

Classifier answers questions by asking an LLM for a JSON object.

The probabilities are whatever the LLM wrote, so they are not calibrated. Any service that runs a one-shot inference can back a classifier. The reply's shape is enforced by the provider where the service supports a reply schema, and otherwise asked for in the prompt and parsed from the reply.

c, err := llm.New(llm.Config{LLM: service})
results, err := c.YesNo(ctx, "Hi, you've reached Dana. Leave a message.",
	[]classifier.Named[classifier.YesNoQuestion]{
		{Name: "voicemail", Question: classifier.YesNoQuestion{Instructions: "is this a voicemail greeting?"}},
	})
results["voicemail"].Probability

func New

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

New builds an LLM classifier.

func (*Classifier) AskQuestions

func (c *Classifier) AskQuestions(
	ctx 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 in one LLM call. The call reports no token usage.

func (*Classifier) LLM

func (c *Classifier) LLM() llmservice.Inferencer

LLM is the service that answers the questions.

func (*Classifier) Model

func (c *Classifier) Model() string

Model is the LLM service's model, or "" when it does not say.

type Config

type Config struct {
	// LLM is the service that answers the questions. Required.
	LLM llmservice.Inferencer `validate:"required"`
	// Name names the classifier in its metrics; empty uses "LLMClassifier#<n>".
	Name string
	// Instructions is the system instruction for the LLM. Empty uses
	// DefaultInstructions, which asks for one JSON object with an answer per
	// question.
	Instructions string
	// MaxTokens caps the reply's length, for services that take one. Zero
	// leaves the service's own bound in place.
	MaxTokens int `validate:"min=0"`
	// Timeout is how long to wait for the LLM's reply before giving up; zero
	// uses 10s.
	Timeout time.Duration `validate:"min=0"`
}

Config configures an LLM classifier.

Jump to

Keyboard shortcuts

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