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-05-21 12:38:25 +00:00
|
|
|
|
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-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-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-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")
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|