2024-01-05 17:04:46 +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
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
"github.com/go-skynet/LocalAI/metrics"
|
2023-06-24 06:18:17 +00:00
|
|
|
"github.com/go-skynet/LocalAI/pkg/gallery"
|
2024-01-05 17:04:46 +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
|
|
|
)
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
type Option struct {
|
2023-07-14 23:19:43 +00:00
|
|
|
Context context.Context
|
|
|
|
ConfigFile string
|
2024-01-05 17:04:46 +00:00
|
|
|
Loader *model.ModelLoader
|
2023-07-14 23:19:43 +00:00
|
|
|
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
|
2024-01-05 17:04:46 +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
|
|
|
|
2024-01-01 09:31:03 +00:00
|
|
|
WatchDogIdle bool
|
|
|
|
WatchDogBusy bool
|
|
|
|
WatchDog bool
|
|
|
|
|
|
|
|
ModelsURL []string
|
|
|
|
|
2023-11-26 17:36:23 +00:00
|
|
|
WatchDogBusyTimeout, WatchDogIdleTimeout time.Duration
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
type AppOption func(*Option)
|
2023-05-21 12:38:25 +00:00
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
func NewOptions(o ...AppOption) *Option {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-01 09:31:03 +00:00
|
|
|
func WithModelsURL(urls ...string) AppOption {
|
2024-01-05 17:04:46 +00:00
|
|
|
return func(o *Option) {
|
2024-01-01 09:31:03 +00:00
|
|
|
o.ModelsURL = urls
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 12:38:25 +00:00
|
|
|
func WithCors(b bool) AppOption {
|
2024-01-05 17:04:46 +00:00
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.CORS = b
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
var EnableWatchDog = func(o *Option) {
|
2023-11-26 17:36:23 +00:00
|
|
|
o.WatchDog = true
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
var EnableWatchDogIdleCheck = func(o *Option) {
|
2023-11-26 17:36:23 +00:00
|
|
|
o.WatchDog = true
|
|
|
|
o.WatchDogIdle = true
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
var EnableWatchDogBusyCheck = func(o *Option) {
|
2023-11-26 17:36:23 +00:00
|
|
|
o.WatchDog = true
|
|
|
|
o.WatchDogBusy = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetWatchDogBusyTimeout(t time.Duration) AppOption {
|
2024-01-05 17:04:46 +00:00
|
|
|
return func(o *Option) {
|
2023-11-26 17:36:23 +00:00
|
|
|
o.WatchDogBusyTimeout = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetWatchDogIdleTimeout(t time.Duration) AppOption {
|
2024-01-05 17:04:46 +00:00
|
|
|
return func(o *Option) {
|
2023-11-26 17:36:23 +00:00
|
|
|
o.WatchDogIdleTimeout = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
var EnableSingleBackend = func(o *Option) {
|
2023-08-18 23:49:33 +00:00
|
|
|
o.SingleBackend = true
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
var EnableParallelBackendRequests = func(o *Option) {
|
2023-11-16 07:20:05 +00:00
|
|
|
o.ParallelBackendRequests = true
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
var EnableGalleriesAutoload = func(o *Option) {
|
2023-07-20 20:10:12 +00:00
|
|
|
o.AutoloadGalleries = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithExternalBackend(name string, uri string) AppOption {
|
2024-01-05 17:04:46 +00:00
|
|
|
return func(o *Option) {
|
2023-07-20 20:10:12 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
return func(o *Option) {
|
2023-07-02 09:15:05 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
return func(o *Option) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.ConfigFile = configFile
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
func WithModelLoader(loader *model.ModelLoader) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.Loader = loader
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithUploadLimitMB(limit int) AppOption {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
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 {
|
2024-01-05 17:04:46 +00:00
|
|
|
return func(o *Option) {
|
2023-08-09 22:06:21 +00:00
|
|
|
o.ApiKeys = apiKeys
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 16:22:53 +00:00
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
func WithMetrics(meter *metrics.Metrics) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.Metrics = meter
|
2023-10-17 16:22:53 +00:00
|
|
|
}
|
|
|
|
}
|