2024-03-01 15:19:53 +00:00
|
|
|
package config
|
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-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/pkg/xsysinfo"
|
2023-07-02 09:15:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-05-21 12:38:25 +00:00
|
|
|
)
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
type ApplicationConfig struct {
|
2023-07-14 23:19:43 +00:00
|
|
|
Context context.Context
|
|
|
|
ConfigFile string
|
2024-03-01 15:19:53 +00:00
|
|
|
ModelPath string
|
2024-06-16 11:58:28 +00:00
|
|
|
LibPath string
|
2023-07-14 23:19:43 +00:00
|
|
|
UploadLimitMB, Threads, ContextSize int
|
2024-05-02 19:14:10 +00:00
|
|
|
DisableWebUI bool
|
2023-07-14 23:19:43 +00:00
|
|
|
F16 bool
|
2024-04-20 08:43:37 +00:00
|
|
|
Debug bool
|
2023-07-14 23:19:43 +00:00
|
|
|
ImageDir string
|
|
|
|
AudioDir string
|
2024-02-18 10:12:02 +00:00
|
|
|
UploadDir string
|
2024-03-26 17:54:35 +00:00
|
|
|
ConfigsDir string
|
2024-04-18 03:21:55 +00:00
|
|
|
DynamicConfigsDir string
|
2024-04-20 00:31:15 +00:00
|
|
|
DynamicConfigsDirPollInterval time.Duration
|
2023-07-14 23:19:43 +00:00
|
|
|
CORS bool
|
2024-06-04 19:43:46 +00:00
|
|
|
CSRF bool
|
2023-07-14 23:19:43 +00:00
|
|
|
PreloadJSONModels string
|
|
|
|
PreloadModelsFromPath string
|
|
|
|
CORSAllowOrigins string
|
2023-08-09 22:06:21 +00:00
|
|
|
ApiKeys []string
|
2024-07-10 11:18:32 +00:00
|
|
|
EnforcePredownloadScans bool
|
2024-06-05 06:45:24 +00:00
|
|
|
OpaqueErrors bool
|
2024-07-08 20:04:06 +00:00
|
|
|
P2PToken string
|
2024-08-07 21:35:44 +00:00
|
|
|
P2PNetworkID string
|
2023-06-01 21:38:52 +00:00
|
|
|
|
2024-01-27 23:14:16 +00:00
|
|
|
ModelLibraryURL string
|
|
|
|
|
2024-06-24 15:32:12 +00:00
|
|
|
Galleries []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-03-01 15:19:53 +00:00
|
|
|
type AppOption func(*ApplicationConfig)
|
2023-05-21 12:38:25 +00:00
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
func NewApplicationConfig(o ...AppOption) *ApplicationConfig {
|
|
|
|
opt := &ApplicationConfig{
|
2024-04-20 08:43:37 +00:00
|
|
|
Context: context.Background(),
|
|
|
|
UploadLimitMB: 15,
|
|
|
|
ContextSize: 512,
|
|
|
|
Debug: 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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
2024-01-01 09:31:03 +00:00
|
|
|
o.ModelsURL = urls
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
func WithModelPath(path string) AppOption {
|
|
|
|
return func(o *ApplicationConfig) {
|
|
|
|
o.ModelPath = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 12:38:25 +00:00
|
|
|
func WithCors(b bool) AppOption {
|
2024-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.CORS = b
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-07 21:35:44 +00:00
|
|
|
func WithP2PNetworkID(s string) AppOption {
|
|
|
|
return func(o *ApplicationConfig) {
|
|
|
|
o.P2PNetworkID = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 19:43:46 +00:00
|
|
|
func WithCsrf(b bool) AppOption {
|
|
|
|
return func(o *ApplicationConfig) {
|
|
|
|
o.CSRF = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-08 20:04:06 +00:00
|
|
|
func WithP2PToken(s string) AppOption {
|
|
|
|
return func(o *ApplicationConfig) {
|
|
|
|
o.P2PToken = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-27 23:14:16 +00:00
|
|
|
func WithModelLibraryURL(url string) AppOption {
|
2024-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
2024-01-27 23:14:16 +00:00
|
|
|
o.ModelLibraryURL = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 11:58:28 +00:00
|
|
|
func WithLibPath(path string) AppOption {
|
|
|
|
return func(o *ApplicationConfig) {
|
|
|
|
o.LibPath = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
var EnableWatchDog = func(o *ApplicationConfig) {
|
2023-11-26 17:36:23 +00:00
|
|
|
o.WatchDog = true
|
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
var EnableWatchDogIdleCheck = func(o *ApplicationConfig) {
|
2023-11-26 17:36:23 +00:00
|
|
|
o.WatchDog = true
|
|
|
|
o.WatchDogIdle = true
|
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
var EnableWatchDogBusyCheck = func(o *ApplicationConfig) {
|
2023-11-26 17:36:23 +00:00
|
|
|
o.WatchDog = true
|
|
|
|
o.WatchDogBusy = true
|
|
|
|
}
|
|
|
|
|
2024-05-02 19:14:10 +00:00
|
|
|
var DisableWebUI = func(o *ApplicationConfig) {
|
|
|
|
o.DisableWebUI = true
|
2024-03-27 20:10:58 +00:00
|
|
|
}
|
|
|
|
|
2023-11-26 17:36:23 +00:00
|
|
|
func SetWatchDogBusyTimeout(t time.Duration) AppOption {
|
2024-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
2023-11-26 17:36:23 +00:00
|
|
|
o.WatchDogBusyTimeout = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetWatchDogIdleTimeout(t time.Duration) AppOption {
|
2024-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
2023-11-26 17:36:23 +00:00
|
|
|
o.WatchDogIdleTimeout = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
var EnableSingleBackend = func(o *ApplicationConfig) {
|
2023-08-18 23:49:33 +00:00
|
|
|
o.SingleBackend = true
|
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
var EnableParallelBackendRequests = func(o *ApplicationConfig) {
|
2023-11-16 07:20:05 +00:00
|
|
|
o.ParallelBackendRequests = true
|
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
var EnableGalleriesAutoload = func(o *ApplicationConfig) {
|
2023-07-20 20:10:12 +00:00
|
|
|
o.AutoloadGalleries = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithExternalBackend(name string, uri string) AppOption {
|
2024-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
2023-07-02 09:15:05 +00:00
|
|
|
if galls == "" {
|
2024-06-24 15:32:12 +00:00
|
|
|
o.Galleries = []Gallery{}
|
2023-07-02 09:15:05 +00:00
|
|
|
return
|
|
|
|
}
|
2024-06-24 15:32:12 +00:00
|
|
|
var galleries []Gallery
|
2023-07-02 09:15:05 +00:00
|
|
|
if err := json.Unmarshal([]byte(galls), &galleries); err != nil {
|
2024-04-04 07:24:22 +00:00
|
|
|
log.Error().Err(err).Msg("failed loading galleries")
|
2023-07-02 09:15:05 +00:00
|
|
|
}
|
2023-07-14 23:19:43 +00:00
|
|
|
o.Galleries = append(o.Galleries, galleries...)
|
2023-07-02 09:15:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 15:32:12 +00:00
|
|
|
func WithGalleries(galleries []Gallery) AppOption {
|
2024-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.ConfigFile = configFile
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithUploadLimitMB(limit int) AppOption {
|
2024-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
2024-06-04 13:23:29 +00:00
|
|
|
if threads == 0 { // 0 is not allowed
|
|
|
|
threads = xsysinfo.CPUPhysicalCores()
|
|
|
|
}
|
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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o.Debug = debug
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 15:53:10 +00:00
|
|
|
func WithAudioDir(audioDir string) AppOption {
|
2024-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
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-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
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
|
|
|
|
2024-02-18 10:12:02 +00:00
|
|
|
func WithUploadDir(uploadDir string) AppOption {
|
2024-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
2024-02-18 10:12:02 +00:00
|
|
|
o.UploadDir = uploadDir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 17:54:35 +00:00
|
|
|
func WithConfigsDir(configsDir string) AppOption {
|
|
|
|
return func(o *ApplicationConfig) {
|
|
|
|
o.ConfigsDir = configsDir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-18 03:21:55 +00:00
|
|
|
func WithDynamicConfigDir(dynamicConfigsDir string) AppOption {
|
|
|
|
return func(o *ApplicationConfig) {
|
|
|
|
o.DynamicConfigsDir = dynamicConfigsDir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-20 00:31:15 +00:00
|
|
|
func WithDynamicConfigDirPollInterval(interval time.Duration) AppOption {
|
|
|
|
return func(o *ApplicationConfig) {
|
|
|
|
o.DynamicConfigsDirPollInterval = interval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-09 22:06:21 +00:00
|
|
|
func WithApiKeys(apiKeys []string) AppOption {
|
2024-03-01 15:19:53 +00:00
|
|
|
return func(o *ApplicationConfig) {
|
2023-08-09 22:06:21 +00:00
|
|
|
o.ApiKeys = apiKeys
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 16:22:53 +00:00
|
|
|
|
2024-07-10 11:18:32 +00:00
|
|
|
func WithEnforcedPredownloadScans(enforced bool) AppOption {
|
|
|
|
return func(o *ApplicationConfig) {
|
|
|
|
o.EnforcePredownloadScans = enforced
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 06:45:24 +00:00
|
|
|
func WithOpaqueErrors(opaque bool) AppOption {
|
|
|
|
return func(o *ApplicationConfig) {
|
|
|
|
o.OpaqueErrors = opaque
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 19:55:11 +00:00
|
|
|
// ToConfigLoaderOptions returns a slice of ConfigLoader Option.
|
|
|
|
// Some options defined at the application level are going to be passed as defaults for
|
|
|
|
// all the configuration for the models.
|
|
|
|
// This includes for instance the context size or the number of threads.
|
|
|
|
// If a model doesn't set configs directly to the config model file
|
|
|
|
// it will use the defaults defined here.
|
|
|
|
func (o *ApplicationConfig) ToConfigLoaderOptions() []ConfigLoaderOption {
|
|
|
|
return []ConfigLoaderOption{
|
|
|
|
LoadOptionContextSize(o.ContextSize),
|
|
|
|
LoadOptionDebug(o.Debug),
|
|
|
|
LoadOptionF16(o.F16),
|
|
|
|
LoadOptionThreads(o.Threads),
|
2024-06-08 20:13:02 +00:00
|
|
|
ModelPath(o.ModelPath),
|
2024-03-22 19:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
// func WithMetrics(meter *metrics.Metrics) AppOption {
|
|
|
|
// return func(o *StartupOptions) {
|
|
|
|
// o.Metrics = meter
|
|
|
|
// }
|
|
|
|
// }
|