LocalAI/api/options/options.go

209 lines
4.2 KiB
Go
Raw Normal View History

package options
2023-05-21 12:38:25 +00:00
import (
"context"
"embed"
"encoding/json"
2023-05-21 12:38:25 +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"
"github.com/go-skynet/LocalAI/metrics"
"github.com/rs/zerolog/log"
2023-05-21 12:38:25 +00:00
)
type Option struct {
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
ApiKeys []string
Metrics *metrics.Metrics
Galleries []gallery.Gallery
BackendAssets embed.FS
AssetsDestination string
ExternalGRPCBackends map[string]string
AutoloadGalleries bool
SingleBackend bool
2023-05-21 12:38:25 +00:00
}
type AppOption func(*Option)
func NewOptions(o ...AppOption) *Option {
2023-05-21 12:38:25 +00:00
opt := &Option{
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) {
o.CORS = b
2023-05-21 12:38:25 +00:00
}
}
var EnableSingleBackend = func(o *Option) {
o.SingleBackend = true
}
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) {
o.CORSAllowOrigins = b
2023-05-21 12:38:25 +00:00
}
}
func WithBackendAssetsOutput(out string) AppOption {
return func(o *Option) {
o.AssetsDestination = out
}
}
func WithBackendAssets(f embed.FS) AppOption {
return func(o *Option) {
o.BackendAssets = f
}
}
func WithStringGalleries(galls string) AppOption {
return func(o *Option) {
if galls == "" {
log.Debug().Msgf("no galleries to load")
o.Galleries = []gallery.Gallery{}
return
}
var galleries []gallery.Gallery
if err := json.Unmarshal([]byte(galls), &galleries); err != nil {
log.Error().Msgf("failed loading galleries: %s", err.Error())
}
o.Galleries = append(o.Galleries, galleries...)
}
}
func WithGalleries(galleries []gallery.Gallery) AppOption {
return func(o *Option) {
o.Galleries = append(o.Galleries, galleries...)
}
}
2023-05-21 12:38:25 +00:00
func WithContext(ctx context.Context) AppOption {
return func(o *Option) {
o.Context = ctx
2023-05-21 12:38:25 +00:00
}
}
func WithYAMLConfigPreload(configFile string) AppOption {
return func(o *Option) {
o.PreloadModelsFromPath = configFile
}
}
func WithJSONStringPreload(configFile string) AppOption {
return func(o *Option) {
o.PreloadJSONModels = configFile
}
}
2023-05-21 12:38:25 +00:00
func WithConfigFile(configFile string) AppOption {
return func(o *Option) {
o.ConfigFile = configFile
2023-05-21 12:38:25 +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 {
return func(o *Option) {
o.UploadLimitMB = limit
2023-05-21 12:38:25 +00:00
}
}
func WithThreads(threads int) AppOption {
return func(o *Option) {
o.Threads = threads
2023-05-21 12:38:25 +00:00
}
}
func WithContextSize(ctxSize int) AppOption {
return func(o *Option) {
o.ContextSize = ctxSize
2023-05-21 12:38:25 +00:00
}
}
func WithF16(f16 bool) AppOption {
return func(o *Option) {
o.F16 = f16
2023-05-21 12:38:25 +00:00
}
}
func WithDebug(debug bool) AppOption {
return func(o *Option) {
o.Debug = debug
2023-05-21 12:38:25 +00:00
}
}
func WithDisableMessage(disableMessage bool) AppOption {
return func(o *Option) {
o.DisableMessage = disableMessage
2023-05-21 12:38:25 +00:00
}
}
func WithAudioDir(audioDir string) AppOption {
return func(o *Option) {
o.AudioDir = audioDir
}
}
2023-05-21 12:38:25 +00:00
func WithImageDir(imageDir string) AppOption {
return func(o *Option) {
o.ImageDir = imageDir
2023-05-21 12:38:25 +00:00
}
}
func WithApiKeys(apiKeys []string) AppOption {
return func(o *Option) {
o.ApiKeys = apiKeys
}
}
func WithMetrics(meter *metrics.Metrics) AppOption {
return func(o *Option) {
o.Metrics = meter
}
}