LocalAI/api/options/options.go

170 lines
3.4 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/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
Galleries []gallery.Gallery
BackendAssets embed.FS
AssetsDestination string
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
}
}
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")
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
}
}