Documentation
¶
Overview ¶
Package cli provides the command-line interface framework for Zerfoo. (Stability: stable)
Architecture ¶
The package is built around three abstractions:
- Command — the interface every CLI command implements (Name, Description, Run, Usage, Examples).
- CommandRegistry — a name-keyed store of Command values. Use NewCommandRegistry to create one, then call Register and Get.
- CLI — the top-level dispatcher. It owns a CommandRegistry, matches the first positional argument to a registered command, and delegates execution.
Built-in commands ¶
The following commands ship with Zerfoo:
- run — interactive prompt-response generation (RunCommand)
- serve — start an OpenAI-compatible HTTP server (ServeCommand)
- pull — download and cache a model from a registry (PullCommand)
- list — list locally cached models (ListCommand)
- rm — remove a cached model (RmCommand)
- worker — start a distributed training worker (WorkerCommand)
- predict — batch model inference on CSV/JSON data (PredictCommand)
- tokenize — tokenize text with the Zerfoo tokenizer (TokenizeCommand)
Adding a new command ¶
Implement the Command interface and register it with the CLI:
type MyCommand struct{}
func (c *MyCommand) Name() string { return "mycommand" }
func (c *MyCommand) Description() string { return "Do something useful" }
func (c *MyCommand) Usage() string { return "mycommand [OPTIONS]" }
func (c *MyCommand) Examples() []string { return nil }
func (c *MyCommand) Run(ctx context.Context, args []string) error {
// Command logic here.
return nil
}
Then register it during CLI setup:
app := cli.NewCLI()
app.RegisterCommand(&MyCommand{})
Configuration ¶
BaseConfig provides common options (verbose, output path, format, config file) that command-specific configs can embed. See PredictCommandConfig for an example of extending BaseConfig with domain-specific fields.
Signal handling ¶
SignalContext creates a context that cancels on SIGINT/SIGTERM and optionally triggers a shutdown.Coordinator for graceful shutdown. Long-running commands (serve, worker) use this to clean up on exit. Stability: stable
Index ¶
- func SignalContext(parent context.Context, coord *shutdown.Coordinator) (context.Context, context.CancelFunc)
- type AutoMLCommand
- type BaseConfig
- type CLI
- type Command
- type CommandRegistry
- type EagleTrainCommand
- type FineTuneSentimentCommand
- type ForecastCommand
- type GuardCommand
- type ListCommand
- type ModelLoaderFunc
- type PredictCommand
- type PredictCommandConfig
- type PredictionResult
- type PullCommand
- type RmCommand
- type RunCommand
- type SentimentCommand
- type ServeCommand
- type TokenizeCommand
- func (c *TokenizeCommand) Description() string
- func (c *TokenizeCommand) Examples() []string
- func (c *TokenizeCommand) Name() string
- func (c *TokenizeCommand) Run(_ context.Context, args []string) error
- func (c *TokenizeCommand) Tok() *tokenizer.WhitespaceTokenizer
- func (c *TokenizeCommand) Usage() string
- type TrainCommand
- type TransMLACommand
- type TransMLAValidateCommand
- type TranscribeCommand
- type VersionCommand
- type WorkerCommand
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SignalContext ¶
func SignalContext(parent context.Context, coord *shutdown.Coordinator) (context.Context, context.CancelFunc)
SignalContext returns a context that is canceled when SIGINT or SIGTERM is received. If a non-nil shutdown.Coordinator is provided, its Shutdown method is called before the context is canceled. The returned cancel function should be deferred by the caller to release signal resources.
Types ¶
type AutoMLCommand ¶ added in v1.5.0
type AutoMLCommand struct {
// contains filtered or unexported fields
}
AutoMLCommand implements the "automl" CLI command for automated hyperparameter optimization.
func NewAutoMLCommand ¶ added in v1.5.0
func NewAutoMLCommand(out io.Writer) *AutoMLCommand
NewAutoMLCommand creates a new automl command.
func (*AutoMLCommand) Description ¶ added in v1.5.0
func (c *AutoMLCommand) Description() string
Description implements Command.Description.
func (*AutoMLCommand) Examples ¶ added in v1.5.0
func (c *AutoMLCommand) Examples() []string
Examples implements Command.Examples.
func (*AutoMLCommand) Name ¶ added in v1.5.0
func (c *AutoMLCommand) Name() string
Name implements Command.Name.
func (*AutoMLCommand) Run ¶ added in v1.5.0
func (c *AutoMLCommand) Run(ctx context.Context, args []string) error
Run implements Command.Run.
func (*AutoMLCommand) Usage ¶ added in v1.5.0
func (c *AutoMLCommand) Usage() string
Usage implements Command.Usage.
type BaseConfig ¶
type BaseConfig struct {
// Common options
Verbose bool `json:"verbose"`
Output string `json:"output"`
Format string `json:"format"` // "json", "yaml", "csv", "text"
ConfigFile string `json:"configFile"`
// Plugin configuration
Plugins map[string]interface{} `json:"plugins"`
// Extension point for command-specific configuration
Extensions map[string]interface{} `json:"extensions"`
}
BaseConfig provides common configuration options for CLI commands.
type CLI ¶
type CLI struct {
// contains filtered or unexported fields
}
CLI provides the main command-line interface.
func (*CLI) RegisterCommand ¶
RegisterCommand adds a command to the CLI.
type Command ¶
type Command interface {
// Name returns the command name
Name() string
// Description returns the command description
Description() string
// Run executes the command with the given arguments
Run(ctx context.Context, args []string) error
// Usage returns usage information
Usage() string
// Examples returns usage examples
Examples() []string
}
Command represents a generic CLI command with pluggable functionality.
type CommandRegistry ¶
type CommandRegistry struct {
// contains filtered or unexported fields
}
CommandRegistry manages available CLI commands.
func NewCommandRegistry ¶
func NewCommandRegistry() *CommandRegistry
NewCommandRegistry creates a new command registry.
func (*CommandRegistry) Get ¶
func (r *CommandRegistry) Get(name string) (Command, bool)
Get retrieves a command by name.
func (*CommandRegistry) List ¶
func (r *CommandRegistry) List() []string
List returns all registered command names in sorted order.
func (*CommandRegistry) Register ¶
func (r *CommandRegistry) Register(cmd Command)
Register adds a command to the registry.
type EagleTrainCommand ¶ added in v1.31.0
type EagleTrainCommand struct {
// contains filtered or unexported fields
}
EagleTrainCommand implements the "eagle-train" CLI command for training an EAGLE speculative decoding head.
func NewEagleTrainCommand ¶ added in v1.31.0
func NewEagleTrainCommand(out io.Writer) *EagleTrainCommand
NewEagleTrainCommand creates a new EagleTrainCommand.
func (*EagleTrainCommand) Description ¶ added in v1.31.0
func (c *EagleTrainCommand) Description() string
Description implements Command.Description.
func (*EagleTrainCommand) Examples ¶ added in v1.31.0
func (c *EagleTrainCommand) Examples() []string
Examples implements Command.Examples.
func (*EagleTrainCommand) Name ¶ added in v1.31.0
func (c *EagleTrainCommand) Name() string
Name implements Command.Name.
func (*EagleTrainCommand) Run ¶ added in v1.31.0
func (c *EagleTrainCommand) Run(ctx context.Context, args []string) error
Run implements Command.Run.
func (*EagleTrainCommand) Usage ¶ added in v1.31.0
func (c *EagleTrainCommand) Usage() string
Usage implements Command.Usage.
type FineTuneSentimentCommand ¶ added in v1.9.0
type FineTuneSentimentCommand struct {
// contains filtered or unexported fields
}
FineTuneSentimentCommand implements the "finetune-sentiment" CLI command for fine-tuning sentiment classification models.
func NewFineTuneSentimentCommand ¶ added in v1.9.0
func NewFineTuneSentimentCommand(out io.Writer) *FineTuneSentimentCommand
NewFineTuneSentimentCommand creates a new FineTuneSentimentCommand.
func (*FineTuneSentimentCommand) Description ¶ added in v1.9.0
func (c *FineTuneSentimentCommand) Description() string
Description implements Command.Description.
func (*FineTuneSentimentCommand) Examples ¶ added in v1.9.0
func (c *FineTuneSentimentCommand) Examples() []string
Examples implements Command.Examples.
func (*FineTuneSentimentCommand) Name ¶ added in v1.9.0
func (c *FineTuneSentimentCommand) Name() string
Name implements Command.Name.
func (*FineTuneSentimentCommand) Run ¶ added in v1.9.0
func (c *FineTuneSentimentCommand) Run(_ context.Context, args []string) error
Run implements Command.Run.
func (*FineTuneSentimentCommand) Usage ¶ added in v1.9.0
func (c *FineTuneSentimentCommand) Usage() string
Usage implements Command.Usage.
type ForecastCommand ¶ added in v1.37.0
type ForecastCommand struct {
// contains filtered or unexported fields
}
ForecastCommand implements the "forecast" CLI command for time-series forecasting using a foundation model.
func NewForecastCommand ¶ added in v1.37.0
func NewForecastCommand(out io.Writer) *ForecastCommand
NewForecastCommand creates a new ForecastCommand using the given output writer.
func (*ForecastCommand) Description ¶ added in v1.37.0
func (c *ForecastCommand) Description() string
Description implements Command.Description.
func (*ForecastCommand) Examples ¶ added in v1.37.0
func (c *ForecastCommand) Examples() []string
Examples implements Command.Examples.
func (*ForecastCommand) Name ¶ added in v1.37.0
func (c *ForecastCommand) Name() string
Name implements Command.Name.
func (*ForecastCommand) Run ¶ added in v1.37.0
func (c *ForecastCommand) Run(ctx context.Context, args []string) error
Run implements Command.Run.
func (*ForecastCommand) Usage ¶ added in v1.37.0
func (c *ForecastCommand) Usage() string
Usage implements Command.Usage.
type GuardCommand ¶ added in v1.22.0
type GuardCommand struct {
// contains filtered or unexported fields
}
GuardCommand implements the "guard" CLI command for content moderation.
func NewGuardCommand ¶ added in v1.22.0
func NewGuardCommand(out io.Writer) *GuardCommand
NewGuardCommand creates a new GuardCommand that writes output to out.
func NewGuardCommandWithLoader ¶ added in v1.22.0
func NewGuardCommandWithLoader(out io.Writer, loader ModelLoaderFunc) *GuardCommand
NewGuardCommandWithLoader creates a GuardCommand with a custom model loader for testing.
func (*GuardCommand) Description ¶ added in v1.22.0
func (c *GuardCommand) Description() string
Description implements Command.Description.
func (*GuardCommand) Examples ¶ added in v1.22.0
func (c *GuardCommand) Examples() []string
Examples implements Command.Examples.
func (*GuardCommand) Name ¶ added in v1.22.0
func (c *GuardCommand) Name() string
Name implements Command.Name.
func (*GuardCommand) Run ¶ added in v1.22.0
func (c *GuardCommand) Run(ctx context.Context, args []string) error
Run implements Command.Run.
func (*GuardCommand) Usage ¶ added in v1.22.0
func (c *GuardCommand) Usage() string
Usage implements Command.Usage.
type ListCommand ¶
type ListCommand struct {
// contains filtered or unexported fields
}
ListCommand implements the "list" CLI command for showing cached models.
func NewListCommand ¶
func NewListCommand(reg registry.ModelRegistry, out io.Writer) *ListCommand
NewListCommand creates a new ListCommand. If reg is nil, a default LocalRegistry will be created when the command runs.
func (*ListCommand) Description ¶
func (c *ListCommand) Description() string
Description implements Command.Description.
func (*ListCommand) Examples ¶
func (c *ListCommand) Examples() []string
Examples implements Command.Examples.
type ModelLoaderFunc ¶ added in v1.22.0
type ModelLoaderFunc func(modelPath string, opts ...guardian.EvaluatorOption) (*guardian.Evaluator, error)
ModelLoaderFunc loads a Guardian model from a path and returns an Evaluator. This abstraction allows testing without loading a real model.
type PredictCommand ¶
PredictCommand implements model prediction using the plugin system.
func NewPredictCommand ¶
func NewPredictCommand[T tensor.Numeric](registry *model.ModelRegistry[T], fromFloat64 func(float64) T, toFloat64 func(T) float64) *PredictCommand[T]
NewPredictCommand creates a new predict command. fromFloat64 converts a float64 CSV value to type T. toFloat64 converts a prediction value of type T back to float64 for output.
func (*PredictCommand[T]) Description ¶
func (c *PredictCommand[T]) Description() string
Description implements Command.Description
func (*PredictCommand[T]) Examples ¶
func (c *PredictCommand[T]) Examples() []string
Examples implements Command.Examples
func (*PredictCommand[T]) Name ¶
func (c *PredictCommand[T]) Name() string
Name implements Command.Name
func (*PredictCommand[T]) Run ¶
func (c *PredictCommand[T]) Run(ctx context.Context, args []string) error
Run implements Command.Run
func (*PredictCommand[T]) Usage ¶
func (c *PredictCommand[T]) Usage() string
Usage implements Command.Usage
type PredictCommandConfig ¶
type PredictCommandConfig struct {
BaseConfig
// Model configuration
ModelPath string `json:"modelPath"`
ModelProvider string `json:"modelProvider"` // Registry key for model provider
ModelConfig map[string]interface{} `json:"modelConfig"`
// Data configuration
DataPath string `json:"dataPath"`
DataProvider string `json:"dataProvider"` // Registry key for data provider
FeatureColumns []string `json:"featureColumns"`
IDColumn string `json:"idColumn"`
GroupColumn string `json:"groupColumn"`
// Prediction configuration
BatchSize int `json:"batchSize"`
IncludeProbs bool `json:"includeProbs"`
Overwrite bool `json:"overwrite"`
}
PredictCommandConfig configures model prediction.
type PredictionResult ¶
type PredictionResult struct {
ModelPath string `json:"modelPath"`
DataPath string `json:"dataPath"`
OutputPath string `json:"outputPath"`
Timestamp time.Time `json:"timestamp"`
Config *PredictCommandConfig `json:"config"`
NumSamples int `json:"numSamples"`
NumFeatures int `json:"numFeatures"`
Predictions []float64 `json:"predictions,omitempty"`
IDs []string `json:"ids,omitempty"`
Duration time.Duration `json:"duration"`
Success bool `json:"success"`
}
PredictionResult contains prediction results and metadata.
type PullCommand ¶
type PullCommand struct {
// contains filtered or unexported fields
}
PullCommand implements the "pull" CLI command for downloading and caching a model from a remote registry.
func NewPullCommand ¶
func NewPullCommand(reg registry.ModelRegistry, out io.Writer) *PullCommand
NewPullCommand creates a new PullCommand. If reg is nil, a default LocalRegistry will be created when the command runs.
func (*PullCommand) Description ¶
func (c *PullCommand) Description() string
Description implements Command.Description.
func (*PullCommand) Examples ¶
func (c *PullCommand) Examples() []string
Examples implements Command.Examples.
type RmCommand ¶
type RmCommand struct {
// contains filtered or unexported fields
}
RmCommand implements the "rm" CLI command for removing cached models.
func NewRmCommand ¶
func NewRmCommand(reg registry.ModelRegistry, out io.Writer) *RmCommand
NewRmCommand creates a new RmCommand. If reg is nil, a default LocalRegistry will be created when the command runs.
func (*RmCommand) Description ¶
Description implements Command.Description.
type RunCommand ¶
type RunCommand struct {
// contains filtered or unexported fields
}
RunCommand implements the "run" CLI command for interactive prompt-response generation.
func NewRunCommand ¶
func NewRunCommand(in io.Reader, out io.Writer) *RunCommand
NewRunCommand creates a new RunCommand using the given I/O streams.
func (*RunCommand) Description ¶
func (c *RunCommand) Description() string
Description implements Command.Description.
func (*RunCommand) Examples ¶
func (c *RunCommand) Examples() []string
Examples implements Command.Examples.
type SentimentCommand ¶ added in v1.9.0
type SentimentCommand struct {
// contains filtered or unexported fields
}
SentimentCommand implements the "sentiment" CLI command for running sentiment classification from the command line.
func NewSentimentCommand ¶ added in v1.9.0
func NewSentimentCommand(out io.Writer) *SentimentCommand
NewSentimentCommand creates a new SentimentCommand.
func (*SentimentCommand) Description ¶ added in v1.9.0
func (c *SentimentCommand) Description() string
Description implements Command.Description.
func (*SentimentCommand) Examples ¶ added in v1.9.0
func (c *SentimentCommand) Examples() []string
Examples implements Command.Examples.
func (*SentimentCommand) Name ¶ added in v1.9.0
func (c *SentimentCommand) Name() string
Name implements Command.Name.
func (*SentimentCommand) Run ¶ added in v1.9.0
func (c *SentimentCommand) Run(ctx context.Context, args []string) error
Run implements Command.Run.
func (*SentimentCommand) Usage ¶ added in v1.9.0
func (c *SentimentCommand) Usage() string
Usage implements Command.Usage.
type ServeCommand ¶
type ServeCommand struct {
// contains filtered or unexported fields
}
ServeCommand implements the "serve" CLI command for starting an OpenAI-compatible HTTP inference server.
func NewServeCommand ¶
func NewServeCommand(coord *shutdown.Coordinator, out io.Writer) *ServeCommand
NewServeCommand creates a new ServeCommand.
func (*ServeCommand) Description ¶
func (c *ServeCommand) Description() string
Description implements Command.Description.
func (*ServeCommand) Examples ¶
func (c *ServeCommand) Examples() []string
Examples implements Command.Examples.
type TokenizeCommand ¶
type TokenizeCommand struct {
// contains filtered or unexported fields
}
TokenizeCommand implements text tokenization.
func NewTokenizeCommand ¶
func NewTokenizeCommand() *TokenizeCommand
NewTokenizeCommand creates a new tokenize command.
func (*TokenizeCommand) Description ¶
func (c *TokenizeCommand) Description() string
Description implements Command.Description
func (*TokenizeCommand) Examples ¶
func (c *TokenizeCommand) Examples() []string
Examples implements Command.Examples
func (*TokenizeCommand) Run ¶
func (c *TokenizeCommand) Run(_ context.Context, args []string) error
Run implements Command.Run
func (*TokenizeCommand) Tok ¶
func (c *TokenizeCommand) Tok() *tokenizer.WhitespaceTokenizer
Tok returns the underlying tokenizer for testing.
func (*TokenizeCommand) Usage ¶
func (c *TokenizeCommand) Usage() string
Usage implements Command.Usage
type TrainCommand ¶ added in v1.8.0
type TrainCommand struct {
// contains filtered or unexported fields
}
TrainCommand implements the "train" CLI command for local and distributed training. It reuses the distributed/ and training/ packages and supports single-GPU (--world-size 1) and multi-GPU modes.
func NewTrainCommand ¶ added in v1.8.0
func NewTrainCommand(out io.Writer) *TrainCommand
NewTrainCommand creates a new TrainCommand.
func (*TrainCommand) Description ¶ added in v1.8.0
func (c *TrainCommand) Description() string
Description implements Command.Description.
func (*TrainCommand) Examples ¶ added in v1.8.0
func (c *TrainCommand) Examples() []string
Examples implements Command.Examples.
func (*TrainCommand) Name ¶ added in v1.8.0
func (c *TrainCommand) Name() string
Name implements Command.Name.
func (*TrainCommand) Run ¶ added in v1.8.0
func (c *TrainCommand) Run(ctx context.Context, args []string) error
Run implements Command.Run.
func (*TrainCommand) Usage ¶ added in v1.8.0
func (c *TrainCommand) Usage() string
Usage implements Command.Usage.
type TransMLACommand ¶ added in v1.29.0
type TransMLACommand struct {
// contains filtered or unexported fields
}
TransMLACommand implements the "transmla" CLI command for converting standard MHA weights to multi-head latent attention (MLA) form.
func NewTransMLACommand ¶ added in v1.29.0
func NewTransMLACommand(out io.Writer) *TransMLACommand
NewTransMLACommand creates a new TransMLACommand that writes status output to out.
func (*TransMLACommand) Description ¶ added in v1.29.0
func (c *TransMLACommand) Description() string
Description implements Command.Description.
func (*TransMLACommand) Examples ¶ added in v1.29.0
func (c *TransMLACommand) Examples() []string
Examples implements Command.Examples.
func (*TransMLACommand) Name ¶ added in v1.29.0
func (c *TransMLACommand) Name() string
Name implements Command.Name.
func (*TransMLACommand) Run ¶ added in v1.29.0
func (c *TransMLACommand) Run(_ context.Context, args []string) error
Run implements Command.Run.
func (*TransMLACommand) Usage ¶ added in v1.29.0
func (c *TransMLACommand) Usage() string
Usage implements Command.Usage.
type TransMLAValidateCommand ¶ added in v1.30.0
type TransMLAValidateCommand struct {
// contains filtered or unexported fields
}
TransMLAValidateCommand implements "transmla validate" for comparing perplexity between an original and TransMLA-converted model.
func NewTransMLAValidateCommand ¶ added in v1.30.0
func NewTransMLAValidateCommand(out io.Writer) *TransMLAValidateCommand
NewTransMLAValidateCommand creates a new TransMLAValidateCommand.
func (*TransMLAValidateCommand) Description ¶ added in v1.30.0
func (c *TransMLAValidateCommand) Description() string
Description implements Command.Description.
func (*TransMLAValidateCommand) Name ¶ added in v1.30.0
func (c *TransMLAValidateCommand) Name() string
Name implements Command.Name.
type TranscribeCommand ¶ added in v1.36.0
type TranscribeCommand struct {
// contains filtered or unexported fields
}
TranscribeCommand implements the "transcribe" CLI command for speech-to-text transcription using Voxtral or Whisper models.
func NewTranscribeCommand ¶ added in v1.36.0
func NewTranscribeCommand(out io.Writer) *TranscribeCommand
NewTranscribeCommand creates a new TranscribeCommand.
func (*TranscribeCommand) Description ¶ added in v1.36.0
func (c *TranscribeCommand) Description() string
Description implements Command.Description.
func (*TranscribeCommand) Examples ¶ added in v1.36.0
func (c *TranscribeCommand) Examples() []string
Examples implements Command.Examples.
func (*TranscribeCommand) Name ¶ added in v1.36.0
func (c *TranscribeCommand) Name() string
Name implements Command.Name.
func (*TranscribeCommand) Run ¶ added in v1.36.0
func (c *TranscribeCommand) Run(ctx context.Context, args []string) error
Run implements Command.Run.
func (*TranscribeCommand) Usage ¶ added in v1.36.0
func (c *TranscribeCommand) Usage() string
Usage implements Command.Usage.
type VersionCommand ¶ added in v1.4.0
type VersionCommand struct {
// contains filtered or unexported fields
}
VersionCommand implements the "version" CLI command.
func NewVersionCommand ¶ added in v1.4.0
func NewVersionCommand(version string, out io.Writer) *VersionCommand
NewVersionCommand creates a new VersionCommand. The version string is typically set at build time via -ldflags "-X main.version=v1.2.3". If version is empty, "(devel)" is displayed.
func (*VersionCommand) Description ¶ added in v1.4.0
func (c *VersionCommand) Description() string
Description implements Command.Description.
func (*VersionCommand) Examples ¶ added in v1.4.0
func (c *VersionCommand) Examples() []string
Examples implements Command.Examples.
func (*VersionCommand) Name ¶ added in v1.4.0
func (c *VersionCommand) Name() string
Name implements Command.Name.
func (*VersionCommand) Run ¶ added in v1.4.0
func (c *VersionCommand) Run(_ context.Context, _ []string) error
Run implements Command.Run.
func (*VersionCommand) Usage ¶ added in v1.4.0
func (c *VersionCommand) Usage() string
Usage implements Command.Usage.
type WorkerCommand ¶
type WorkerCommand struct {
// contains filtered or unexported fields
}
WorkerCommand implements the "worker" CLI command for starting a distributed training worker.
func NewWorkerCommand ¶
func NewWorkerCommand(coord *shutdown.Coordinator) *WorkerCommand
NewWorkerCommand creates a new WorkerCommand. The shutdown coordinator is used to register the worker node for orderly shutdown on signal.
func (*WorkerCommand) Description ¶
func (c *WorkerCommand) Description() string
Description implements Command.Description.
func (*WorkerCommand) Examples ¶
func (c *WorkerCommand) Examples() []string
Examples implements Command.Examples.
func (*WorkerCommand) Run ¶
func (c *WorkerCommand) Run(ctx context.Context, args []string) error
Run implements Command.Run. It parses flags, creates a WorkerNode, starts it, and blocks until the context is canceled (e.g. by SIGTERM).
func (*WorkerCommand) Usage ¶
func (c *WorkerCommand) Usage() string
Usage implements Command.Usage.