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"
|
|
|
|
)
|
|
|
|
|
2024-01-05 14:34:56 +00:00
|
|
|
type ModelOptions struct {
|
2023-07-14 23:19:43 +00:00
|
|
|
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-11-16 07:20:05 +00:00
|
|
|
parallelRequests bool
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 14:34:56 +00:00
|
|
|
type Option func(*ModelOptions)
|
2023-07-14 23:19:43 +00:00
|
|
|
|
2024-01-05 14:34:56 +00:00
|
|
|
var EnableParallelRequests = func(o *ModelOptions) {
|
2023-11-16 07:20:05 +00:00
|
|
|
o.parallelRequests = true
|
|
|
|
}
|
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
func WithExternalBackend(name string, uri string) Option {
|
2024-01-05 14:34:56 +00:00
|
|
|
return func(o *ModelOptions) {
|
2023-07-20 20:10:12 +00:00
|
|
|
if o.externalBackends == nil {
|
|
|
|
o.externalBackends = make(map[string]string)
|
|
|
|
}
|
|
|
|
o.externalBackends[name] = uri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 14:34:56 +00:00
|
|
|
// Currently, LocalAI isn't ready for backends to be yanked out from under it - so this is a little overcomplicated to allow non-overwriting updates
|
|
|
|
func WithExternalBackends(backends map[string]string, overwrite bool) Option {
|
|
|
|
return func(o *ModelOptions) {
|
|
|
|
if backends == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if o.externalBackends == nil {
|
|
|
|
o.externalBackends = backends
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for name, url := range backends {
|
|
|
|
_, exists := o.externalBackends[name]
|
|
|
|
if !exists || overwrite {
|
|
|
|
o.externalBackends[name] = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 23:11:32 +00:00
|
|
|
func WithGRPCAttempts(attempts int) Option {
|
2024-01-05 14:34:56 +00:00
|
|
|
return func(o *ModelOptions) {
|
2023-08-15 23:11:32 +00:00
|
|
|
o.grpcAttempts = attempts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithGRPCAttemptsDelay(delay int) Option {
|
2024-01-05 14:34:56 +00:00
|
|
|
return func(o *ModelOptions) {
|
2023-08-15 23:11:32 +00:00
|
|
|
o.grpcAttemptsDelay = delay
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
func WithBackendString(backend string) Option {
|
2024-01-05 14:34:56 +00:00
|
|
|
return func(o *ModelOptions) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.backendString = backend
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-07 20:39:10 +00:00
|
|
|
func WithModel(modelFile string) Option {
|
2024-01-05 14:34:56 +00:00
|
|
|
return func(o *ModelOptions) {
|
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 {
|
2024-01-05 14:34:56 +00:00
|
|
|
return func(o *ModelOptions) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.gRPCOptions = opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithThreads(threads uint32) Option {
|
2024-01-05 14:34:56 +00:00
|
|
|
return func(o *ModelOptions) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.threads = threads
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithAssetDir(assetDir string) Option {
|
2024-01-05 14:34:56 +00:00
|
|
|
return func(o *ModelOptions) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.assetDir = assetDir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
func WithContext(ctx context.Context) Option {
|
2024-01-05 14:34:56 +00:00
|
|
|
return func(o *ModelOptions) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 23:49:33 +00:00
|
|
|
func WithSingleActiveBackend() Option {
|
2024-01-05 14:34:56 +00:00
|
|
|
return func(o *ModelOptions) {
|
2023-08-18 23:49:33 +00:00
|
|
|
o.singleActiveBackend = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 14:34:56 +00:00
|
|
|
func NewOptions(opts ...Option) *ModelOptions {
|
|
|
|
o := &ModelOptions{
|
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
|
|
|
|
}
|