mirror of
https://github.com/mudler/LocalAI.git
synced 2025-05-11 04:52:52 +00:00
feat: refactor the dynamic json configs for api_keys and external_backends (#2055)
* feat: refactor the dynamic json configs for api_keys and external_backends Signed-off-by: Chris Jowett <421501+cryptk@users.noreply.github.com> * fix: remove commented code Signed-off-by: Chris Jowett <421501+cryptk@users.noreply.github.com> --------- Signed-off-by: Chris Jowett <421501+cryptk@users.noreply.github.com> Signed-off-by: Ettore Di Giacinto <mudler@users.noreply.github.com> Co-authored-by: Ettore Di Giacinto <mudler@users.noreply.github.com>
This commit is contained in:
parent
e9f090257c
commit
502c1eedaa
@ -2,7 +2,6 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -65,6 +64,7 @@ func (r *RunCMD) Run(ctx *Context) error {
|
|||||||
config.WithAudioDir(r.AudioPath),
|
config.WithAudioDir(r.AudioPath),
|
||||||
config.WithUploadDir(r.UploadPath),
|
config.WithUploadDir(r.UploadPath),
|
||||||
config.WithConfigsDir(r.ConfigPath),
|
config.WithConfigsDir(r.ConfigPath),
|
||||||
|
config.WithDynamicConfigDir(r.LocalaiConfigDir),
|
||||||
config.WithF16(r.F16),
|
config.WithF16(r.F16),
|
||||||
config.WithStringGalleries(r.Galleries),
|
config.WithStringGalleries(r.Galleries),
|
||||||
config.WithModelLibraryURL(r.RemoteLibrary),
|
config.WithModelLibraryURL(r.RemoteLibrary),
|
||||||
@ -134,17 +134,6 @@ func (r *RunCMD) Run(ctx *Context) error {
|
|||||||
return fmt.Errorf("failed basic startup tasks with error %s", err.Error())
|
return fmt.Errorf("failed basic startup tasks with error %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Watch the configuration directory
|
|
||||||
// If the directory does not exist, we don't watch it
|
|
||||||
if _, err := os.Stat(r.LocalaiConfigDir); err == nil {
|
|
||||||
closeConfigWatcherFn, err := startup.WatchConfigDirectory(r.LocalaiConfigDir, options)
|
|
||||||
defer closeConfigWatcherFn()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed while watching configuration directory %s", r.LocalaiConfigDir)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
appHTTP, err := http.App(cl, ml, options)
|
appHTTP, err := http.App(cl, ml, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("error during HTTP App construction")
|
log.Error().Err(err).Msg("error during HTTP App construction")
|
||||||
|
@ -22,6 +22,7 @@ type ApplicationConfig struct {
|
|||||||
AudioDir string
|
AudioDir string
|
||||||
UploadDir string
|
UploadDir string
|
||||||
ConfigsDir string
|
ConfigsDir string
|
||||||
|
DynamicConfigsDir string
|
||||||
CORS bool
|
CORS bool
|
||||||
PreloadJSONModels string
|
PreloadJSONModels string
|
||||||
PreloadModelsFromPath string
|
PreloadModelsFromPath string
|
||||||
@ -264,6 +265,12 @@ func WithConfigsDir(configsDir string) AppOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithDynamicConfigDir(dynamicConfigsDir string) AppOption {
|
||||||
|
return func(o *ApplicationConfig) {
|
||||||
|
o.DynamicConfigsDir = dynamicConfigsDir
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func WithApiKeys(apiKeys []string) AppOption {
|
func WithApiKeys(apiKeys []string) AppOption {
|
||||||
return func(o *ApplicationConfig) {
|
return func(o *ApplicationConfig) {
|
||||||
o.ApiKeys = apiKeys
|
o.ApiKeys = apiKeys
|
||||||
|
@ -12,89 +12,143 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WatchConfigDirectoryCloser func() error
|
type fileHandler func(fileContent []byte, appConfig *config.ApplicationConfig) error
|
||||||
|
|
||||||
func ReadApiKeysJson(configDir string, appConfig *config.ApplicationConfig) error {
|
type configFileHandler struct {
|
||||||
fileContent, err := os.ReadFile(path.Join(configDir, "api_keys.json"))
|
handlers map[string]fileHandler
|
||||||
if err == nil {
|
|
||||||
// Parse JSON content from the file
|
watcher *fsnotify.Watcher
|
||||||
var fileKeys []string
|
|
||||||
err := json.Unmarshal(fileContent, &fileKeys)
|
configDir string
|
||||||
if err == nil {
|
appConfig *config.ApplicationConfig
|
||||||
appConfig.ApiKeys = append(appConfig.ApiKeys, fileKeys...)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadExternalBackendsJson(configDir string, appConfig *config.ApplicationConfig) error {
|
// TODO: This should be a singleton eventually so other parts of the code can register config file handlers,
|
||||||
fileContent, err := os.ReadFile(path.Join(configDir, "external_backends.json"))
|
// then we can export it to other packages
|
||||||
if err != nil {
|
func newConfigFileHandler(appConfig *config.ApplicationConfig) configFileHandler {
|
||||||
return err
|
c := configFileHandler{
|
||||||
|
handlers: make(map[string]fileHandler),
|
||||||
|
configDir: appConfig.DynamicConfigsDir,
|
||||||
|
appConfig: appConfig,
|
||||||
}
|
}
|
||||||
// Parse JSON content from the file
|
c.Register("api_keys.json", readApiKeysJson(*appConfig), true)
|
||||||
var fileBackends map[string]string
|
c.Register("external_backends.json", readExternalBackendsJson(*appConfig), true)
|
||||||
err = json.Unmarshal(fileContent, &fileBackends)
|
return c
|
||||||
if err != nil {
|
}
|
||||||
return err
|
|
||||||
|
func (c *configFileHandler) Register(filename string, handler fileHandler, runNow bool) error {
|
||||||
|
_, ok := c.handlers[filename]
|
||||||
|
if ok {
|
||||||
|
return fmt.Errorf("handler already registered for file %s", filename)
|
||||||
}
|
}
|
||||||
err = mergo.Merge(&appConfig.ExternalGRPCBackends, fileBackends)
|
c.handlers[filename] = handler
|
||||||
if err != nil {
|
if runNow {
|
||||||
return err
|
c.callHandler(path.Join(c.appConfig.DynamicConfigsDir, filename), handler)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var CONFIG_FILE_UPDATES = map[string]func(configDir string, appConfig *config.ApplicationConfig) error{
|
func (c *configFileHandler) callHandler(filename string, handler fileHandler) {
|
||||||
"api_keys.json": ReadApiKeysJson,
|
fileContent, err := os.ReadFile(filename)
|
||||||
"external_backends.json": ReadExternalBackendsJson,
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
log.Error().Err(err).Str("filename", filename).Msg("could not read file")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = handler(fileContent, c.appConfig); err != nil {
|
||||||
|
log.Error().Err(err).Msg("WatchConfigDirectory goroutine failed to update options")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WatchConfigDirectory(configDir string, appConfig *config.ApplicationConfig) (WatchConfigDirectoryCloser, error) {
|
func (c *configFileHandler) Watch() error {
|
||||||
if len(configDir) == 0 {
|
|
||||||
return nil, fmt.Errorf("configDir blank")
|
|
||||||
}
|
|
||||||
configWatcher, err := fsnotify.NewWatcher()
|
configWatcher, err := fsnotify.NewWatcher()
|
||||||
|
c.watcher = configWatcher
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Msgf("Unable to create a watcher for the LocalAI Configuration Directory: %+v", err)
|
log.Fatal().Err(err).Str("configdir", c.configDir).Msg("wnable to create a watcher for configuration directory")
|
||||||
}
|
|
||||||
ret := func() error {
|
|
||||||
configWatcher.Close()
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start listening for events.
|
// Start listening for events.
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event, ok := <-configWatcher.Events:
|
case event, ok := <-c.watcher.Events:
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if event.Has(fsnotify.Write) {
|
if event.Has(fsnotify.Write | fsnotify.Create | fsnotify.Remove) {
|
||||||
for targetName, watchFn := range CONFIG_FILE_UPDATES {
|
handler, ok := c.handlers[path.Base(event.Name)]
|
||||||
if event.Name == targetName {
|
if !ok {
|
||||||
err := watchFn(configDir, appConfig)
|
continue
|
||||||
log.Warn().Msgf("WatchConfigDirectory goroutine for %s: failed to update options: %+v", targetName, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.callHandler(event.Name, handler)
|
||||||
}
|
}
|
||||||
case _, ok := <-configWatcher.Errors:
|
case err, ok := <-c.watcher.Errors:
|
||||||
|
log.Error().Err(err).Msg("config watcher error received")
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Error().Err(err).Msg("error encountered while watching config directory")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Add a path.
|
// Add a path.
|
||||||
err = configWatcher.Add(configDir)
|
err = c.watcher.Add(c.appConfig.DynamicConfigsDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ret, fmt.Errorf("unable to establish watch on the LocalAI Configuration Directory: %+v", err)
|
return fmt.Errorf("unable to establish watch on the LocalAI Configuration Directory: %+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret, nil
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: When we institute graceful shutdown, this should be called
|
||||||
|
func (c *configFileHandler) Stop() {
|
||||||
|
c.watcher.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func readApiKeysJson(startupAppConfig config.ApplicationConfig) fileHandler {
|
||||||
|
handler := func(fileContent []byte, appConfig *config.ApplicationConfig) error {
|
||||||
|
log.Debug().Msg("processing api_keys.json")
|
||||||
|
|
||||||
|
if len(fileContent) > 0 {
|
||||||
|
// Parse JSON content from the file
|
||||||
|
var fileKeys []string
|
||||||
|
err := json.Unmarshal(fileContent, &fileKeys)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
appConfig.ApiKeys = append(startupAppConfig.ApiKeys, fileKeys...)
|
||||||
|
} else {
|
||||||
|
appConfig.ApiKeys = startupAppConfig.ApiKeys
|
||||||
|
}
|
||||||
|
log.Debug().Msg("api keys loaded from api_keys.json")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func readExternalBackendsJson(startupAppConfig config.ApplicationConfig) fileHandler {
|
||||||
|
handler := func(fileContent []byte, appConfig *config.ApplicationConfig) error {
|
||||||
|
log.Debug().Msg("processing external_backends.json")
|
||||||
|
|
||||||
|
if len(fileContent) > 0 {
|
||||||
|
// Parse JSON content from the file
|
||||||
|
var fileBackends map[string]string
|
||||||
|
err := json.Unmarshal(fileContent, &fileBackends)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
appConfig.ExternalGRPCBackends = startupAppConfig.ExternalGRPCBackends
|
||||||
|
err = mergo.Merge(&appConfig.ExternalGRPCBackends, &fileBackends)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
appConfig.ExternalGRPCBackends = startupAppConfig.ExternalGRPCBackends
|
||||||
|
}
|
||||||
|
log.Debug().Msg("external backends loaded from external_backends.json")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return handler
|
||||||
}
|
}
|
||||||
|
@ -125,6 +125,11 @@ func Startup(opts ...config.AppOption) (*config.BackendConfigLoader, *model.Mode
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Watch the configuration directory
|
||||||
|
// If the directory does not exist, we don't watch it
|
||||||
|
configHandler := newConfigFileHandler(options)
|
||||||
|
configHandler.Watch()
|
||||||
|
|
||||||
log.Info().Msg("core/startup process completed!")
|
log.Info().Msg("core/startup process completed!")
|
||||||
return cl, ml, options, nil
|
return cl, ml, options, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user