2023-05-21 12:38:25 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
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 {
|
|
|
|
context context.Context
|
|
|
|
configFile string
|
|
|
|
loader *model.ModelLoader
|
|
|
|
uploadLimitMB, threads, ctxSize int
|
|
|
|
f16 bool
|
|
|
|
debug, disableMessage bool
|
|
|
|
imageDir string
|
2023-06-22 15:53:10 +00:00
|
|
|
audioDir string
|
2023-05-21 12:38:25 +00:00
|
|
|
cors bool
|
2023-05-27 07:26:33 +00:00
|
|
|
preloadJSONModels string
|
|
|
|
preloadModelsFromPath string
|
2023-05-21 12:38:25 +00:00
|
|
|
corsAllowOrigins string
|
2023-06-01 21:38:52 +00:00
|
|
|
|
2023-06-24 06:18:17 +00:00
|
|
|
galleries []gallery.Gallery
|
|
|
|
|
2023-06-01 21:38:52 +00:00
|
|
|
backendAssets embed.FS
|
|
|
|
assetsDestination string
|
2023-05-21 12:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AppOption func(*Option)
|
|
|
|
|
|
|
|
func newOptions(o ...AppOption) *Option {
|
|
|
|
opt := &Option{
|
|
|
|
context: context.Background(),
|
|
|
|
uploadLimitMB: 15,
|
|
|
|
threads: 1,
|
|
|
|
ctxSize: 512,
|
|
|
|
debug: true,
|
|
|
|
disableMessage: true,
|
|
|
|
}
|
|
|
|
for _, oo := range o {
|
|
|
|
oo(opt)
|
|
|
|
}
|
|
|
|
return opt
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithCors(b bool) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.cors = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithCorsAllowOrigins(b string) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.corsAllowOrigins = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 21:38:52 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
o.galleries = append(o.galleries, galleries...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-24 06:18:17 +00:00
|
|
|
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-27 07:26:33 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithModelLoader(loader *model.ModelLoader) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.loader = loader
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithUploadLimitMB(limit int) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.uploadLimitMB = limit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithThreads(threads int) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.threads = threads
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithContextSize(ctxSize int) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.ctxSize = ctxSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithF16(f16 bool) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.f16 = f16
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithDebug(debug bool) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.debug = debug
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithDisableMessage(disableMessage bool) AppOption {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.disableMessage = disableMessage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 15:53:10 +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
|
|
|
|
}
|
|
|
|
}
|