Install Cybertron
mainTo use Cybertron as a library in your Go project, ensure you have Go 1.21 installed and run the following command:
go get -u github.com/nlpodyssey/cybertronrepository·main·Indexed 43 days ago
https://github.com/nlpodyssey/cybertronA pure Go package built on spaGO that provides an interface for running inference with pre-trained Transformer models from HuggingFace. It can be used as a library or as a standalone server supporting tasks such as text-generation, zero-shot-classification, question-answering, text-classification, token-classification, text-encoding, and language-modeling.
To use Cybertron as a library in your Go project, ensure you have Go 1.21 installed and run the following command:
go get -u github.com/nlpodyssey/cybertronCybertron can be run as a standalone server for inference. It automatically loads configuration from a .env file or accepts configuration via CLI flags.
Create a .env file in your current directory to define the model, directory, and task. For example, to set up a Machine Translation server:
echo "CYBERTRON_MODEL=Helsinki-NLP/opus-mt-en-it" > .env
echo "CYBERTRON_MODELS_DIR=models" >> .env
echo "CYBERTRON_MODEL_TASK=text-generation" >> .envThen start the server:
GOARCH=amd64 go run ./cmd/server -address 0.0.0.0:8080You can test the /v1/generate endpoint using curl:
curl -X 'POST' \
'0.0.0.0:8080/v1/generate' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{ "input": "You must be the change you wish to see in the world.", "parameters": {} }'echo "CYBERTRON_MODEL=Helsinki-NLP/opus-mt-en-it" > .env
echo "CYBERTRON_MODELS_DIR=models" >> .env
echo "CYBERTRON_MODEL_TASK=text-generation" >> .env
GOARCH=amd64 go run ./cmd/server -address 0.0.0.0:8080To extend or modify the Cybertron gRPC and HTTP APIs, you must use buf and the following Go tools to generate the necessary code:
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpcgo generate ./...go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc
go generate ./...To run a Cybertron server, you need a Config and a RequestHandler. The RequestHandler is a task-specific service (like text generation or classification) that implements the required registration interfaces. Use New() to create the server and Start(ctx) to begin serving gRPC and HTTP/2 requests. The server supports both TLS and insecure (h2c) modes.
// 1. Define configuration
conf := &server.Config{
Network: "tcp4",
Address: ":8080",
AllowedOrigins: []string{"*"},
TLSEnabled: false,
}
// 2. Resolve a handler for your model
// Assuming 'model' is an instance of a task interface (e.g., textgeneration.Interface)
handler, err := server.ResolveRequestHandler(model)
if err != nil {
log.Fatal().Err(err).Msg("Failed to resolve handler")
}
// 3. Create and start the server
s := server.New(conf, handler)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := s.Start(ctx); err != nil {
log.Fatal().Err(err).Msg("Server failed")
}The Cybertron server can be started as a standalone executable. It initializes a server that hosts various NLP tasks (like Text Generation, Question Answering, etc.) by loading a specific model based on the task type.
Configuration follows this priority order:
.env file if present)When running the server, it will automatically log system metrics (CPU and RAM usage) to stderr using zerolog.
The Cybertron server can be configured using environment variables prefixed with CYBERTRON_. These settings control logging, model loading, and server networking.
CYBERTRON_MODELS_DIR: Base directory for models.CYBERTRON_MODEL: Model name (and sub-path of MODELS_DIR).CYBERTRON_HUB_ACCESS_TOKEN: Access token for private Hugging Face Hub models.CYBERTRON_MODEL_DOWNLOAD: Policy for downloading models (always|missing|never).CYBERTRON_MODEL_CONVERSION: Policy for model conversion (always|missing|never).CYBERTRON_MODEL_CONVERSION_PRECISION: Floating-point precision for converted models (32|64).CYBERTRON_MODEL_TASK: The specific task type the model performs (see Supported Task Types).CYBERTRON_NETWORK: Network type for the server.CYBERTRON_ADDRESS: Server listening address.CYBERTRON_ALLOWED_ORIGINS: Comma-separated list of allowed origins.CYBERTRON_TLS_ENABLED: Enable TLS (true|false).CYBERTRON_TLS_CERT: TLS certificate filename.CYBERTRON_TLS_KEY: TLS key filename.CYBERTRON_LOGLEVEL: Global zerolog level..env file if it exists in the working directory. Environment variables are applied after defaults but before command-line flags, allowing flags to override them.You can run pre-configured examples to explore Cybertron's capabilities in library mode.
GOARCH=amd64 go run ./examples/textgenerationPass the target labels as a comma-separated list after the command.
Note: If the model specified in your .env file is incompatible with zero-shot classification, an error will occur. In that case, remove the model from .env to use the default.
GOARCH=amd64 go run ./examples/zeroshotclassification politics,business,science,technology,health,culture,sports# Machine Translation
GOARCH=amd64 go run ./examples/textgeneration
# Zero-Shot Classification
GOARCH=amd64 go run ./examples/zeroshotclassification politics,business,science,technology,health,culture,sportsThe Config struct defines the network and security settings for the server.
| Field | Type | Description |
|---|---|---|
Network | string | The network type (e.g., tcp4). Defaults to tcp4 if empty. |
Address | string | The listening address (e.g., :8080). Defaults to :8080 if empty. |
AllowedOrigins | []string | List of origins allowed for CORS. |
TLSEnabled | bool | Whether to enable TLS encryption. |
TLSCert | string | Path to the TLS certificate file. |
TLSKey | string | Path to the TLS private key file. |
When running the Cybertron server, you can use the following flags to override or specify configurations:
| Flag | Description |
|---|---|
-address | server listening address |
-allowed-origins | allowed origins (comma separated) |
-loglevel | zerolog global level |
-model | model name (and sub-path of models-dir) |
-model-conversion | model conversion policy ("always"|"missing"|"never") |
-model-conversion-precision | floating-point bits of precision to use if the model is converted ("32"|"64") |
-model-download | model downloading policy ("always"|"missing"|"never") |
-models-dir | models's base directory |
-network | network type for server listening |
-task | type of inference/computation ("textgeneration"|"zero-shot-classification"|"question-answering"|"text-classification"|"token-classification"|"text-encoding") |
-tls | whether to enable TLS ("true"|"false") |
-tls-cert | TLS cert filename |
-tls-key | TLS key filename |
Usage of server:
-address value
server listening address
-allowed-origins value
allowed origins (comma separated)
-loglevel value
zerolog global level
-model value
model name (and sub-path of models-dir)
-model-conversion value
model conversion policy ("always"|"missing"|"never")
-model-conversion-precision value
floating-point bits of precision to use if the model is converted ("32"|"64")
-model-download value
model downloading policy ("always"|"missing"|"never")
-models-dir value
models's base directory
-network value
network type for server listening
-task value
type of inference/computation that the model can fulfill ("textgeneration"|"zero-shot-classification"|"question-answering"|"text-classification"|"token-classification"|"text-encoding")
-tls value
whether to enable TLS ("true"|"false")
-tls-cert value
TLS cert filename
-tls-key value
TLS key filenameThe ReadyForConnections(dur time.Duration) bool method allows you to poll the server to see if it is successfully listening on its configured address. It returns true if a connection can be established within the specified duration, and false otherwise.
// Wait up to 5 seconds for the server to be ready
if s.ReadyForConnections(5 * time.Second) {
fmt.Println("Server is up!")
}The ResolveRequestHandler(model any) (RequestHandler, error) function is a factory that automatically selects the correct task-specific server implementation based on the type of the provided model.
Supported model interfaces include:
textgeneration.Interfacezeroshotclassifier.Interfacequestionanswering.Interfacetextclassification.Interfacetextencoding.Interfacetokenclassification.Interfacelanguagemodeling.Interfacehandler, err := server.ResolveRequestHandler(myModel)