2024-03-01 15:19:53 +00:00
|
|
|
package startup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/core"
|
|
|
|
"github.com/mudler/LocalAI/core/config"
|
|
|
|
"github.com/mudler/LocalAI/core/services"
|
|
|
|
"github.com/mudler/LocalAI/internal"
|
|
|
|
"github.com/mudler/LocalAI/pkg/assets"
|
|
|
|
"github.com/mudler/LocalAI/pkg/library"
|
|
|
|
"github.com/mudler/LocalAI/pkg/model"
|
|
|
|
pkgStartup "github.com/mudler/LocalAI/pkg/startup"
|
|
|
|
"github.com/mudler/LocalAI/pkg/xsysinfo"
|
2024-03-01 15:19:53 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
func Startup(opts ...config.AppOption) (*config.BackendConfigLoader, *model.ModelLoader, *config.ApplicationConfig, error) {
|
2024-03-01 15:19:53 +00:00
|
|
|
options := config.NewApplicationConfig(opts...)
|
|
|
|
|
|
|
|
log.Info().Msgf("Starting LocalAI using %d threads, with models path: %s", options.Threads, options.ModelPath)
|
|
|
|
log.Info().Msgf("LocalAI version: %s", internal.PrintableVersion())
|
2024-05-05 07:10:23 +00:00
|
|
|
caps, err := xsysinfo.CPUCapabilities()
|
|
|
|
if err == nil {
|
|
|
|
log.Debug().Msgf("CPU capabilities: %v", caps)
|
|
|
|
}
|
|
|
|
gpus, err := xsysinfo.GPUs()
|
|
|
|
if err == nil {
|
|
|
|
log.Debug().Msgf("GPU count: %d", len(gpus))
|
|
|
|
for _, gpu := range gpus {
|
|
|
|
log.Debug().Msgf("GPU: %s", gpu.String())
|
|
|
|
}
|
|
|
|
}
|
2024-03-01 15:19:53 +00:00
|
|
|
|
|
|
|
// Make sure directories exists
|
|
|
|
if options.ModelPath == "" {
|
2024-04-17 21:33:49 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("options.ModelPath cannot be empty")
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
2024-05-05 07:10:23 +00:00
|
|
|
err = os.MkdirAll(options.ModelPath, 0750)
|
2024-03-01 15:19:53 +00:00
|
|
|
if err != nil {
|
2024-04-17 21:33:49 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("unable to create ModelPath: %q", err)
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
if options.ImageDir != "" {
|
2024-04-25 22:47:06 +00:00
|
|
|
err := os.MkdirAll(options.ImageDir, 0750)
|
2024-03-01 15:19:53 +00:00
|
|
|
if err != nil {
|
2024-04-17 21:33:49 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("unable to create ImageDir: %q", err)
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if options.AudioDir != "" {
|
2024-04-25 22:47:06 +00:00
|
|
|
err := os.MkdirAll(options.AudioDir, 0750)
|
2024-03-01 15:19:53 +00:00
|
|
|
if err != nil {
|
2024-04-17 21:33:49 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("unable to create AudioDir: %q", err)
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if options.UploadDir != "" {
|
2024-04-25 22:47:06 +00:00
|
|
|
err := os.MkdirAll(options.UploadDir, 0750)
|
2024-03-01 15:19:53 +00:00
|
|
|
if err != nil {
|
2024-04-17 21:33:49 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("unable to create UploadDir: %q", err)
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-10 11:18:32 +00:00
|
|
|
if err := pkgStartup.InstallModels(options.Galleries, options.ModelLibraryURL, options.ModelPath, options.EnforcePredownloadScans, nil, options.ModelsURL...); err != nil {
|
2024-06-13 14:12:46 +00:00
|
|
|
log.Error().Err(err).Msg("error installing models")
|
|
|
|
}
|
2024-03-01 15:19:53 +00:00
|
|
|
|
2024-06-08 20:13:02 +00:00
|
|
|
cl := config.NewBackendConfigLoader(options.ModelPath)
|
2024-04-17 21:33:49 +00:00
|
|
|
ml := model.NewModelLoader(options.ModelPath)
|
2024-03-01 15:19:53 +00:00
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
configLoaderOpts := options.ToConfigLoaderOptions()
|
2024-03-22 19:55:11 +00:00
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
if err := cl.LoadBackendConfigsFromPath(options.ModelPath, configLoaderOpts...); err != nil {
|
2024-04-04 07:24:22 +00:00
|
|
|
log.Error().Err(err).Msg("error loading config files")
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.ConfigFile != "" {
|
2024-05-23 20:48:12 +00:00
|
|
|
if err := cl.LoadMultipleBackendConfigsSingleFile(options.ConfigFile, configLoaderOpts...); err != nil {
|
2024-04-04 07:24:22 +00:00
|
|
|
log.Error().Err(err).Msg("error loading config file")
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
if err := cl.Preload(options.ModelPath); err != nil {
|
2024-04-04 07:24:22 +00:00
|
|
|
log.Error().Err(err).Msg("error downloading models")
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.PreloadJSONModels != "" {
|
2024-07-10 11:18:32 +00:00
|
|
|
if err := services.ApplyGalleryFromString(options.ModelPath, options.PreloadJSONModels, options.EnforcePredownloadScans, options.Galleries); err != nil {
|
2024-04-17 21:33:49 +00:00
|
|
|
return nil, nil, nil, err
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.PreloadModelsFromPath != "" {
|
2024-07-10 11:18:32 +00:00
|
|
|
if err := services.ApplyGalleryFromFile(options.ModelPath, options.PreloadModelsFromPath, options.EnforcePredownloadScans, options.Galleries); err != nil {
|
2024-04-17 21:33:49 +00:00
|
|
|
return nil, nil, nil, err
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.Debug {
|
2024-05-23 20:48:12 +00:00
|
|
|
for _, v := range cl.GetAllBackendConfigs() {
|
|
|
|
log.Debug().Msgf("Model: %s (config: %+v)", v.Name, v)
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.AssetsDestination != "" {
|
|
|
|
// Extract files from the embedded FS
|
|
|
|
err := assets.ExtractFiles(options.BackendAssets, options.AssetsDestination)
|
|
|
|
log.Debug().Msgf("Extracting backend assets files to %s", options.AssetsDestination)
|
|
|
|
if err != nil {
|
2024-08-07 21:35:55 +00:00
|
|
|
log.Warn().Msgf("Failed extracting backend assets files: %s (might be required for some backends to work properly)", err)
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 11:58:28 +00:00
|
|
|
if options.LibPath != "" {
|
|
|
|
// If there is a lib directory, set LD_LIBRARY_PATH to include it
|
2024-06-24 06:34:36 +00:00
|
|
|
err := library.LoadExternal(options.LibPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Str("LibPath", options.LibPath).Msg("Error while loading external libraries")
|
|
|
|
}
|
2024-06-16 11:58:28 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
// turn off any process that was started by GRPC if the context is canceled
|
|
|
|
go func() {
|
|
|
|
<-options.Context.Done()
|
|
|
|
log.Debug().Msgf("Context canceled, shutting down")
|
2024-04-29 13:11:42 +00:00
|
|
|
err := ml.StopAllGRPC()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error while stopping all grpc backends")
|
|
|
|
}
|
2024-03-01 15:19:53 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
if options.WatchDog {
|
|
|
|
wd := model.NewWatchDog(
|
2024-04-17 21:33:49 +00:00
|
|
|
ml,
|
2024-03-01 15:19:53 +00:00
|
|
|
options.WatchDogBusyTimeout,
|
|
|
|
options.WatchDogIdleTimeout,
|
|
|
|
options.WatchDogBusy,
|
|
|
|
options.WatchDogIdle)
|
2024-04-17 21:33:49 +00:00
|
|
|
ml.SetWatchDog(wd)
|
2024-03-01 15:19:53 +00:00
|
|
|
go wd.Run()
|
|
|
|
go func() {
|
|
|
|
<-options.Context.Done()
|
|
|
|
log.Debug().Msgf("Context canceled, shutting down")
|
|
|
|
wd.Shutdown()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-04-18 03:21:55 +00:00
|
|
|
// Watch the configuration directory
|
2024-06-22 06:17:41 +00:00
|
|
|
startWatcher(options)
|
2024-04-18 03:21:55 +00:00
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
log.Info().Msg("core/startup process completed!")
|
2024-04-17 21:33:49 +00:00
|
|
|
return cl, ml, options, nil
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
2024-04-29 17:42:37 +00:00
|
|
|
|
2024-06-22 06:17:41 +00:00
|
|
|
func startWatcher(options *config.ApplicationConfig) {
|
|
|
|
if options.DynamicConfigsDir == "" {
|
|
|
|
// No need to start the watcher if the directory is not set
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(options.DynamicConfigsDir); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// We try to create the directory if it does not exist and was specified
|
|
|
|
if err := os.MkdirAll(options.DynamicConfigsDir, 0700); err != nil {
|
|
|
|
log.Error().Err(err).Msg("failed creating DynamicConfigsDir")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// something else happened, we log the error and don't start the watcher
|
|
|
|
log.Error().Err(err).Msg("failed to read DynamicConfigsDir, watcher will not be started")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
configHandler := newConfigFileHandler(options)
|
|
|
|
if err := configHandler.Watch(); err != nil {
|
|
|
|
log.Error().Err(err).Msg("failed creating watcher")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-29 17:42:37 +00:00
|
|
|
// In Lieu of a proper DI framework, this function wires up the Application manually.
|
|
|
|
// This is in core/startup rather than core/state.go to keep package references clean!
|
|
|
|
func createApplication(appConfig *config.ApplicationConfig) *core.Application {
|
|
|
|
app := &core.Application{
|
|
|
|
ApplicationConfig: appConfig,
|
2024-06-08 20:13:02 +00:00
|
|
|
BackendConfigLoader: config.NewBackendConfigLoader(appConfig.ModelPath),
|
2024-04-29 17:42:37 +00:00
|
|
|
ModelLoader: model.NewModelLoader(appConfig.ModelPath),
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// app.EmbeddingsBackendService = backend.NewEmbeddingsBackendService(app.ModelLoader, app.BackendConfigLoader, app.ApplicationConfig)
|
|
|
|
// app.ImageGenerationBackendService = backend.NewImageGenerationBackendService(app.ModelLoader, app.BackendConfigLoader, app.ApplicationConfig)
|
|
|
|
// app.LLMBackendService = backend.NewLLMBackendService(app.ModelLoader, app.BackendConfigLoader, app.ApplicationConfig)
|
|
|
|
// app.TranscriptionBackendService = backend.NewTranscriptionBackendService(app.ModelLoader, app.BackendConfigLoader, app.ApplicationConfig)
|
|
|
|
// app.TextToSpeechBackendService = backend.NewTextToSpeechBackendService(app.ModelLoader, app.BackendConfigLoader, app.ApplicationConfig)
|
|
|
|
|
|
|
|
app.BackendMonitorService = services.NewBackendMonitorService(app.ModelLoader, app.BackendConfigLoader, app.ApplicationConfig)
|
2024-06-05 06:45:24 +00:00
|
|
|
app.GalleryService = services.NewGalleryService(app.ApplicationConfig)
|
2024-04-29 17:42:37 +00:00
|
|
|
// app.OpenAIService = services.NewOpenAIService(app.ModelLoader, app.BackendConfigLoader, app.ApplicationConfig, app.LLMBackendService)
|
|
|
|
|
|
|
|
app.LocalAIMetricsService, err = services.NewLocalAIMetricsService()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("encountered an error initializing metrics service, startup will continue but metrics will not be tracked.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return app
|
|
|
|
}
|