Files
LocalAI/pkg/model/loader_options.go
T
9bd7d17ff6 [model-config] feat: add environment variables support for backends (#10721)
* feat: add environment variables support for backends in model configurations

- Add field to model configuration to pass environment variables to backend processes
- Update backend options and model configuration handling
- Add documentation for environment variables configuration with examples including CUDA_VISIBLE_DEVICES

Assisted-by: qwen-agentworld-35b-a3b
Signed-off-by: nold <nold42@pm.me>

* fix(test):  Test environment variables configuration parsing from YAML

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: nold <Nold360@users.noreply.github.com>

---------

Signed-off-by: nold <nold42@pm.me>
Signed-off-by: nold <Nold360@users.noreply.github.com>
Co-authored-by: nold <nold42@pm.me>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: localai-org-maint-bot <306269227+localai-org-maint-bot@users.noreply.github.com>
2026-09-11 18:02:08 +02:00

136 lines
2.8 KiB
Go

package model
import (
"context"
pb "github.com/mudler/LocalAI/pkg/grpc/proto"
)
type Options struct {
backendString string
model string
modelFile string
modelID string
configRevision string
context context.Context
gRPCOptions *pb.ModelOptions
externalBackends map[string]string
grpcAttempts int
grpcAttemptsDelay int
parallelRequests bool
// modelSizeBytes is the estimated total weight size in bytes, pre-computed
// by the caller using the vram estimation scaffolding. When non-zero it is
// registered with the watchdog so size-aware eviction can rank models.
modelSizeBytes int64
// envVars contains model-specific environment variables to pass to the backend process
envVars map[string]string
}
// WithConfigRevision binds a load to the semantic revision of the resolved
// model configuration. The value is copied into Options and therefore remains
// stable even if the configuration loader refreshes while a load is running.
func WithConfigRevision(revision string) Option {
return func(o *Options) { o.configRevision = revision }
}
type Option func(*Options)
var EnableParallelRequests = func(o *Options) {
o.parallelRequests = true
}
func WithExternalBackend(name string, uri string) Option {
return func(o *Options) {
if o.externalBackends == nil {
o.externalBackends = make(map[string]string)
}
o.externalBackends[name] = uri
}
}
func WithGRPCAttempts(attempts int) Option {
return func(o *Options) {
o.grpcAttempts = attempts
}
}
func WithGRPCAttemptsDelay(delay int) Option {
return func(o *Options) {
o.grpcAttemptsDelay = delay
}
}
func WithBackendString(backend string) Option {
return func(o *Options) {
o.backendString = backend
}
}
func WithDefaultBackendString(backend string) Option {
return func(o *Options) {
if o.backendString == "" {
o.backendString = backend
}
}
}
func WithModel(modelFile string) Option {
return func(o *Options) {
o.model = modelFile
}
}
func WithModelFile(modelFile string) Option {
return func(o *Options) {
o.modelFile = modelFile
}
}
func WithLoadGRPCLoadModelOpts(opts *pb.ModelOptions) Option {
return func(o *Options) {
o.gRPCOptions = opts
}
}
func WithContext(ctx context.Context) Option {
return func(o *Options) {
o.context = ctx
}
}
func WithModelID(id string) Option {
return func(o *Options) {
o.modelID = id
}
}
func WithModelSizeBytes(bytes int64) Option {
return func(o *Options) {
o.modelSizeBytes = bytes
}
}
func WithEnvVars(envVars map[string]string) Option {
return func(o *Options) {
o.envVars = envVars
}
}
func NewOptions(opts ...Option) *Options {
o := &Options{
gRPCOptions: &pb.ModelOptions{},
context: context.Background(),
grpcAttempts: 20,
grpcAttemptsDelay: 2,
}
for _, opt := range opts {
opt(o)
}
return o
}