Documentation
¶
Overview ¶
Package classmesh provides the core classification vocabulary and cascade.
Example ¶
package main
import (
"context"
"fmt"
"github.com/ClassMesh/classmesh"
"github.com/ClassMesh/classmesh/rules"
)
func main() {
ruleStage, err := rules.New([]rules.Rule{{ID: "question-terminal", Category: "question", Regex: []string{`[??]['")\]}»’”›]*$`}}})
if err != nil {
panic(err)
}
mesh, err := classmesh.New(ruleStage)
if err != nil {
panic(err)
}
result, err := mesh.Classify(context.Background(), classmesh.Record{ID: "sentence-1", Kind: classmesh.KindText, Data: []byte("Ready?")})
if err != nil {
panic(err)
}
fmt.Printf("category=%s confidence=%.0f stage=%s\n", result.Category, result.Confidence, result.Stage)
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnclassified = errors.New("stage: unclassified")
ErrUnclassified is returned by Classify when the stage cannot decide on a category for the record. The cascade then hands the record to the next stage; a record unclassified by every stage is routed for review.
Functions ¶
This section is empty.
Types ¶
type Cascade ¶
type Cascade struct {
// contains filtered or unexported fields
}
Cascade classifies records through an ordered set of stages.
func NewWithOptions ¶
NewWithOptions validates options and constructs a Cascade.
func (*Cascade) ClassifyBatch ¶
ClassifyBatch classifies records sequentially in input order.
type Classification ¶
type Classification struct {
// Category is the assigned label.
Category string
// Confidence is in [0, 1]. Deterministic stages emit 1.
Confidence float64
// Stage names the stage that produced this classification.
Stage string
// Reasons is optional evidence for the classification. Treat it as
// read-only: a stage may hand back a shared slice (the rules stage does,
// to stay allocation-free), so callers must not mutate it in place.
Reasons []Reason
}
Classification is the outcome of running a Record through a stage.
func (Classification) IsValid ¶
func (c Classification) IsValid() bool
IsValid reports whether the classification is well-formed: a non-empty category and a confidence within [0, 1].
type Gate ¶
type Gate struct {
// contains filtered or unexported fields
}
Gate is a minimum confidence. A decision scoring below it is treated as undecided, so the cascade escalates to the next stage. The zero Gate admits everything, which turns gating off; deterministic stages emit 1, so they always pass.
type Kind ¶
type Kind string
Kind identifies the shape of a Record's payload. The zero value is KindUnknown.
type Reason ¶
type Reason struct {
// Code is a short tag for the evidence, like a rule ID.
Code string `json:"code"`
// Detail is a readable description.
Detail string `json:"detail,omitempty"`
}
Reason explains why a stage picked a category. A stage may attach zero or more.
type Record ¶
type Record struct {
// ID uniquely identifies the record within a run. Sources assign it.
ID string
// Kind identifies the payload shape. Zero value is KindUnknown.
Kind Kind
// Data is the raw payload (a log line, a JSON document, ...).
Data []byte
// Fields holds structured attributes decoded from the payload, when a
// source has them. Nil for unstructured payloads.
Fields map[string]any
// Meta carries source-specific context (file name, line number, ...).
Meta map[string]string
}
Record is a single unit of data moving through the pipeline. Sources produce Records, stages classify them, sinks consume them.
type Result ¶
type Result struct {
Classification Classification
Err error
}
Result pairs a Classification with its classification error.
type Stage ¶
type Stage interface {
// Name identifies the stage in classifications, logs, and metrics.
Name() string
// Classify assigns a category to the record, or returns
// ErrUnclassified when this stage cannot decide. It must not mutate
// the record, which a concurrent engine shares with later consumers.
Classify(ctx context.Context, r Record) (Classification, error)
}
Stage classifies records. Implementations range from deterministic rule matching to in-process model inference to remote API calls.
type StageError ¶
type StageError struct {
// Stage is the stage that failed.
Stage string
// Err is the underlying cause.
Err error
}
StageError reports that a stage itself broke, as opposed to ErrUnclassified, which just asks the cascade to try the next stage. Stage names the stage that failed and Unwrap gives the cause, so callers can tell which stage failed and why with errors.As and errors.Is.
func (*StageError) Unwrap ¶
func (e *StageError) Unwrap() error
Unwrap returns the cause so errors.Is and errors.As reach through.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
classmesh
command
Command classmesh is the ClassMesh CLI: a classification cascade pipeline in one binary.
|
Command classmesh is the ClassMesh CLI: a classification cascade pipeline in one binary. |
|
examples
|
|
|
library
command
|
|
|
internal
|
|
|
cli
Package cli wires sources, stages, and sinks into the classmesh CLI.
|
Package cli wires sources, stages, and sinks into the classmesh CLI. |
|
config
Package config parses and validates a declarative cascade configuration: a versioned YAML document that declares the input, the ordered stages and their gates, and where classified records are routed.
|
Package config parses and validates a declarative cascade configuration: a versioned YAML document that declares the input, the ordered stages and their gates, and where classified records are routed. |
|
fieldpath
Package fieldpath reads nested values out of a decoded JSON object (map[string]any) by path.
|
Package fieldpath reads nested values out of a decoded JSON object (map[string]any) by path. |
|
logfields
Package logfields extracts common fields from a text log line, best-effort: a leading timestamp, a level token, and logfmt key=value pairs.
|
Package logfields extracts common fields from a text log line, best-effort: a leading timestamp, a level token, and logfmt key=value pairs. |
|
mockstage
Package mockstage implements the internal deterministic mock stage.
|
Package mockstage implements the internal deterministic mock stage. |
|
tokenizer/wordpiece
Package wordpiece implements BERT-style WordPiece tokenization in pure Go, with no cgo and no external tokenizer runtime.
|
Package wordpiece implements BERT-style WordPiece tokenization in pure Go, with no cgo and no external tokenizer runtime. |
|
version
Package version exposes build metadata injected at link time via -ldflags.
|
Package version exposes build metadata injected at link time via -ldflags. |
|
Package rules implements deterministic matching as a classmesh.Stage.
|
Package rules implements deterministic matching as a classmesh.Stage. |
|
Package schema implements a classmesh.Stage that validates a structured record's decoded Fields against a declared shape.
|
Package schema implements a classmesh.Stage that validates a structured record's decoded Fields against a declared shape. |
|
services
|
|
|
cli
module
|
|
|
shared
module
|
|
|
Package stream moves records from a Source through a Cascade into a Sink.
|
Package stream moves records from a Source through a Cascade into a Sink. |
|
sink
Package sink defines where classified records go: stdout, files, review queues, downstream pipelines.
|
Package sink defines where classified records go: stdout, files, review queues, downstream pipelines. |
|
sink/jsonl
Package jsonl implements sink.Sink as JSON Lines: one JSON object per classified record, the pipe-friendly lingua franca of log tooling.
|
Package jsonl implements sink.Sink as JSON Lines: one JSON object per classified record, the pipe-friendly lingua franca of log tooling. |
|
source
Package source defines where Records come from.
|
Package source defines where Records come from. |
|
source/jsonl
Package jsonl implements source.Source over JSON Lines: one JSON object per line.
|
Package jsonl implements source.Source over JSON Lines: one JSON object per line. |
|
source/text
Package text implements source.Source over line-oriented text: files and stdin.
|
Package text implements source.Source over line-oriented text: files and stdin. |