2023-07-14 23:19:43 +00:00
|
|
|
package options
|
2023-05-21 12:38:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-01 21:38:52 +00:00
|
|
|
"embed"
|
2023-07-02 09:15:05 +00:00
|
|
|
"encoding/json"
|
2023-11-26 17:36:23 +00:00
|
|
|
"time"
|
2023-05-21 12:38:25 +00:00
|
|
|
|
2023-11-16 07:20:05 +00:00
|
|
|
"github.com/go-skynet/LocalAI/metrics"
|
2023-06-24 06:18:17 +00:00
|
|
|
"github.com/go-skynet/LocalAI/pkg/gallery"
|
2023-05-21 12:38:25 +00:00
|
|
|
model "github.com/go-skynet/LocalAI/pkg/model"
|
2023-07-02 09:15:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-05-21 12:38:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Option struct {
|
2023-07-14 23:19:43 +00:00
|
|
|
Context context.Context
|
|
|
|
ConfigFile string
|
|
|
|
Loader *model.ModelLoader
|
|
|
|
UploadLimitMB, Threads, ContextSize int
|
|
|
|
F16 bool
|
|
|
|
Debug, DisableMessage bool
|
|
|
|
ImageDir string
|
|
|
|
AudioDir string
|
|
|
|
CORS bool
|
|
|
|
PreloadJSONModels string
|
|
|
|
PreloadModelsFromPath string
|
|
|
|
CORSAllowOrigins string
|
2023-08-09 22:06:21 +00:00
|
|
|
ApiKeys []string
|
2023-10-17 16:22:53 +00:00
|
|
|
Metrics *metrics.Metrics
|
2023-06-01 21:38:52 +00:00
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
Galleries []gallery.Gallery
|
2023-06-24 06:18:17 +00:00
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
BackendAssets embed.FS
|
|
|
|
AssetsDestination string
|
2023-07-20 20:10:12 +00:00
|
|
|
|
|
|
|
ExternalGRPCBackends map[string]string
|
|
|
|
|
|
|
|
AutoloadGalleries bool
|
2023-08-18 23:49:33 +00:00
|
|
|
|
2023-11-16 07:20:05 +00:00
|
|
|
SingleBackend bool
|
|
|
|
ParallelBackendRequests bool
|
2023-11-26 17:36:23 +00:00
|
|
|
|
|
|
|
WatchDogIdle bool
|
|
|
|
WatchDogBusy bool
|
|
|
|
WatchDog bool
|
|
|
|
WatchDogBusyTimeout, WatchDogIdleTimeout time.Duration
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AppOption func(*Option)
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
func NewOptions(o ...AppOption) *Option {
|
2023-05-21 12:38:25 +00:00
|
|
|
opt := &Option{
|
2023-07-14 23:19:43 +00:00
|
|
|
Context: context.Background(),
|
|
|
|
UploadLimitMB: 15,
|
|
|
|
Threads: 1,
|
|
|
|
ContextSize: 512,
|
|
|
|
Debug: true,
|
|
|
|
DisableMessage: true,
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
for _, oo := range o {
|
|
|
|
oo(opt)
|
|
|
|
}
|
|
|
|
return opt
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithCors(b bool) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.CORS = b
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-26 17:36:23 +00:00
|
|
|
var EnableWatchDog = func(o *Option) {
|
|
|
|
o.WatchDog = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var EnableWatchDogIdleCheck = func(o *Option) {
|
|
|
|
o.WatchDog = true
|
|
|
|
o.WatchDogIdle = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var EnableWatchDogBusyCheck = func(o *Option) {
|
|
|
|
o.WatchDog = true
|
|
|
|
o.WatchDogBusy = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetWatchDogBusyTimeout(t time.Duration) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.WatchDogBusyTimeout = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetWatchDogIdleTimeout(t time.Duration) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.WatchDogIdleTimeout = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 23:49:33 +00:00
|
|
|
var EnableSingleBackend = func(o *Option) {
|
|
|
|
o.SingleBackend = true
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:20:05 +00:00
|
|
|
var EnableParallelBackendRequests = func(o *Option) {
|
|
|
|
o.ParallelBackendRequests = true
|
|
|
|
}
|
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
var EnableGalleriesAutoload = func(o *Option) {
|
|
|
|
o.AutoloadGalleries = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithExternalBackend(name string, uri string) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
if o.ExternalGRPCBackends == nil {
|
|
|
|
o.ExternalGRPCBackends = make(map[string]string)
|
|
|
|
}
|
|
|
|
o.ExternalGRPCBackends[name] = uri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 12:38:25 +00:00
|
|
|
func WithCorsAllowOrigins(b string) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.CORSAllowOrigins = b
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 21:38:52 +00:00
|
|
|
func WithBackendAssetsOutput(out string) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.AssetsDestination = out
|
2023-06-01 21:38:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithBackendAssets(f embed.FS) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.BackendAssets = f
|
2023-06-01 21:38:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-02 09:15:05 +00:00
|
|
|
func WithStringGalleries(galls string) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
if galls == "" {
|
|
|
|
log.Debug().Msgf("no galleries to load")
|
2023-09-02 07:00:44 +00:00
|
|
|
o.Galleries = []gallery.Gallery{}
|
2023-07-02 09:15:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var galleries []gallery.Gallery
|
|
|
|
if err := json.Unmarshal([]byte(galls), &galleries); err != nil {
|
|
|
|
log.Error().Msgf("failed loading galleries: %s", err.Error())
|
|
|
|
}
|
2023-07-14 23:19:43 +00:00
|
|
|
o.Galleries = append(o.Galleries, galleries...)
|
2023-07-02 09:15:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-24 06:18:17 +00:00
|
|
|
func WithGalleries(galleries []gallery.Gallery) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.Galleries = append(o.Galleries, galleries...)
|
2023-06-24 06:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 12:38:25 +00:00
|
|
|
func WithContext(ctx context.Context) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.Context = ctx
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-27 07:26:33 +00:00
|
|
|
func WithYAMLConfigPreload(configFile string) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.PreloadModelsFromPath = configFile
|
2023-05-27 07:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithJSONStringPreload(configFile string) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.PreloadJSONModels = configFile
|
2023-05-27 07:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-21 12:38:25 +00:00
|
|
|
func WithConfigFile(configFile string) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.ConfigFile = configFile
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithModelLoader(loader *model.ModelLoader) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.Loader = loader
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithUploadLimitMB(limit int) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.UploadLimitMB = limit
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithThreads(threads int) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.Threads = threads
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithContextSize(ctxSize int) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.ContextSize = ctxSize
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithF16(f16 bool) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.F16 = f16
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithDebug(debug bool) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.Debug = debug
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithDisableMessage(disableMessage bool) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.DisableMessage = disableMessage
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 15:53:10 +00:00
|
|
|
func WithAudioDir(audioDir string) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.AudioDir = audioDir
|
2023-06-22 15:53:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 12:38:25 +00:00
|
|
|
func WithImageDir(imageDir string) AppOption {
|
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.ImageDir = imageDir
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-09 22:06:21 +00:00
|
|
|
|
|
|
|
func WithApiKeys(apiKeys []string) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.ApiKeys = apiKeys
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 16:22:53 +00:00
|
|
|
|
|
|
|
func WithMetrics(meter *metrics.Metrics) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.Metrics = meter
|
|
|
|
}
|
|
|
|
}
|