2023-07-14 23:19:43 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2023-07-14 23:19:43 +00:00
|
|
|
"context"
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
pb "github.com/go-skynet/LocalAI/pkg/grpc/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
backendString string
|
2023-08-07 20:39:10 +00:00
|
|
|
model string
|
2023-07-14 23:19:43 +00:00
|
|
|
threads uint32
|
|
|
|
assetDir string
|
2023-07-14 23:19:43 +00:00
|
|
|
context context.Context
|
2023-07-14 23:19:43 +00:00
|
|
|
|
|
|
|
gRPCOptions *pb.ModelOptions
|
2023-07-20 20:10:12 +00:00
|
|
|
|
|
|
|
externalBackends map[string]string
|
2023-08-15 23:11:32 +00:00
|
|
|
|
2023-08-18 23:49:33 +00:00
|
|
|
grpcAttempts int
|
|
|
|
grpcAttemptsDelay int
|
|
|
|
singleActiveBackend bool
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*Options)
|
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 23:11:32 +00:00
|
|
|
func WithGRPCAttempts(attempts int) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.grpcAttempts = attempts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithGRPCAttemptsDelay(delay int) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.grpcAttemptsDelay = delay
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
func WithBackendString(backend string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.backendString = backend
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-07 20:39:10 +00:00
|
|
|
func WithModel(modelFile string) Option {
|
2023-07-14 23:19:43 +00:00
|
|
|
return func(o *Options) {
|
2023-08-07 20:39:10 +00:00
|
|
|
o.model = modelFile
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-09 06:38:51 +00:00
|
|
|
func WithLoadGRPCLoadModelOpts(opts *pb.ModelOptions) Option {
|
2023-07-14 23:19:43 +00:00
|
|
|
return func(o *Options) {
|
|
|
|
o.gRPCOptions = opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithThreads(threads uint32) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.threads = threads
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithAssetDir(assetDir string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.assetDir = assetDir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
func WithContext(ctx context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 23:49:33 +00:00
|
|
|
func WithSingleActiveBackend() Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.singleActiveBackend = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
func NewOptions(opts ...Option) *Options {
|
2023-07-14 23:19:43 +00:00
|
|
|
o := &Options{
|
2023-08-15 23:11:32 +00:00
|
|
|
gRPCOptions: &pb.ModelOptions{},
|
|
|
|
context: context.Background(),
|
|
|
|
grpcAttempts: 20,
|
|
|
|
grpcAttemptsDelay: 2,
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
2023-07-14 23:19:43 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(o)
|
|
|
|
}
|
|
|
|
return o
|
|
|
|
}
|