1
- package startup
1
+ package application
2
2
3
3
import (
4
4
"fmt"
5
5
"os"
6
6
7
- "github.com/mudler/LocalAI/core"
8
7
"github.com/mudler/LocalAI/core/backend"
9
8
"github.com/mudler/LocalAI/core/config"
10
9
"github.com/mudler/LocalAI/core/services"
11
10
"github.com/mudler/LocalAI/internal"
12
11
"github.com/mudler/LocalAI/pkg/assets"
12
+
13
13
"github.com/mudler/LocalAI/pkg/library"
14
14
"github.com/mudler/LocalAI/pkg/model"
15
15
pkgStartup "github.com/mudler/LocalAI/pkg/startup"
16
16
"github.com/mudler/LocalAI/pkg/xsysinfo"
17
17
"github.com/rs/zerolog/log"
18
18
)
19
19
20
- func Startup (opts ... config.AppOption ) (* config. BackendConfigLoader , * model. ModelLoader , * config. ApplicationConfig , error ) {
20
+ func New (opts ... config.AppOption ) (* Application , error ) {
21
21
options := config .NewApplicationConfig (opts ... )
22
+ application := newApplication (options )
22
23
23
24
log .Info ().Msgf ("Starting LocalAI using %d threads, with models path: %s" , options .Threads , options .ModelPath )
24
25
log .Info ().Msgf ("LocalAI version: %s" , internal .PrintableVersion ())
@@ -36,68 +37,65 @@ func Startup(opts ...config.AppOption) (*config.BackendConfigLoader, *model.Mode
36
37
37
38
// Make sure directories exists
38
39
if options .ModelPath == "" {
39
- return nil , nil , nil , fmt .Errorf ("options.ModelPath cannot be empty" )
40
+ return nil , fmt .Errorf ("options.ModelPath cannot be empty" )
40
41
}
41
42
err = os .MkdirAll (options .ModelPath , 0750 )
42
43
if err != nil {
43
- return nil , nil , nil , fmt .Errorf ("unable to create ModelPath: %q" , err )
44
+ return nil , fmt .Errorf ("unable to create ModelPath: %q" , err )
44
45
}
45
46
if options .ImageDir != "" {
46
47
err := os .MkdirAll (options .ImageDir , 0750 )
47
48
if err != nil {
48
- return nil , nil , nil , fmt .Errorf ("unable to create ImageDir: %q" , err )
49
+ return nil , fmt .Errorf ("unable to create ImageDir: %q" , err )
49
50
}
50
51
}
51
52
if options .AudioDir != "" {
52
53
err := os .MkdirAll (options .AudioDir , 0750 )
53
54
if err != nil {
54
- return nil , nil , nil , fmt .Errorf ("unable to create AudioDir: %q" , err )
55
+ return nil , fmt .Errorf ("unable to create AudioDir: %q" , err )
55
56
}
56
57
}
57
58
if options .UploadDir != "" {
58
59
err := os .MkdirAll (options .UploadDir , 0750 )
59
60
if err != nil {
60
- return nil , nil , nil , fmt .Errorf ("unable to create UploadDir: %q" , err )
61
+ return nil , fmt .Errorf ("unable to create UploadDir: %q" , err )
61
62
}
62
63
}
63
64
64
65
if err := pkgStartup .InstallModels (options .Galleries , options .ModelLibraryURL , options .ModelPath , options .EnforcePredownloadScans , nil , options .ModelsURL ... ); err != nil {
65
66
log .Error ().Err (err ).Msg ("error installing models" )
66
67
}
67
68
68
- cl := config .NewBackendConfigLoader (options .ModelPath )
69
- ml := model .NewModelLoader (options .ModelPath )
70
-
71
69
configLoaderOpts := options .ToConfigLoaderOptions ()
72
70
73
- if err := cl .LoadBackendConfigsFromPath (options .ModelPath , configLoaderOpts ... ); err != nil {
71
+ if err := application . BackendLoader () .LoadBackendConfigsFromPath (options .ModelPath , configLoaderOpts ... ); err != nil {
74
72
log .Error ().Err (err ).Msg ("error loading config files" )
75
73
}
76
74
77
75
if options .ConfigFile != "" {
78
- if err := cl .LoadMultipleBackendConfigsSingleFile (options .ConfigFile , configLoaderOpts ... ); err != nil {
76
+ if err := application . BackendLoader () .LoadMultipleBackendConfigsSingleFile (options .ConfigFile , configLoaderOpts ... ); err != nil {
79
77
log .Error ().Err (err ).Msg ("error loading config file" )
80
78
}
81
79
}
82
80
83
- if err := cl .Preload (options .ModelPath ); err != nil {
81
+ if err := application . BackendLoader () .Preload (options .ModelPath ); err != nil {
84
82
log .Error ().Err (err ).Msg ("error downloading models" )
85
83
}
86
84
87
85
if options .PreloadJSONModels != "" {
88
86
if err := services .ApplyGalleryFromString (options .ModelPath , options .PreloadJSONModels , options .EnforcePredownloadScans , options .Galleries ); err != nil {
89
- return nil , nil , nil , err
87
+ return nil , err
90
88
}
91
89
}
92
90
93
91
if options .PreloadModelsFromPath != "" {
94
92
if err := services .ApplyGalleryFromFile (options .ModelPath , options .PreloadModelsFromPath , options .EnforcePredownloadScans , options .Galleries ); err != nil {
95
- return nil , nil , nil , err
93
+ return nil , err
96
94
}
97
95
}
98
96
99
97
if options .Debug {
100
- for _ , v := range cl .GetAllBackendConfigs () {
98
+ for _ , v := range application . BackendLoader () .GetAllBackendConfigs () {
101
99
log .Debug ().Msgf ("Model: %s (config: %+v)" , v .Name , v )
102
100
}
103
101
}
@@ -123,20 +121,20 @@ func Startup(opts ...config.AppOption) (*config.BackendConfigLoader, *model.Mode
123
121
go func () {
124
122
<- options .Context .Done ()
125
123
log .Debug ().Msgf ("Context canceled, shutting down" )
126
- err := ml .StopAllGRPC ()
124
+ err := application . ModelLoader () .StopAllGRPC ()
127
125
if err != nil {
128
126
log .Error ().Err (err ).Msg ("error while stopping all grpc backends" )
129
127
}
130
128
}()
131
129
132
130
if options .WatchDog {
133
131
wd := model .NewWatchDog (
134
- ml ,
132
+ application . ModelLoader () ,
135
133
options .WatchDogBusyTimeout ,
136
134
options .WatchDogIdleTimeout ,
137
135
options .WatchDogBusy ,
138
136
options .WatchDogIdle )
139
- ml .SetWatchDog (wd )
137
+ application . ModelLoader () .SetWatchDog (wd )
140
138
go wd .Run ()
141
139
go func () {
142
140
<- options .Context .Done ()
@@ -147,25 +145,25 @@ func Startup(opts ...config.AppOption) (*config.BackendConfigLoader, *model.Mode
147
145
148
146
if options .LoadToMemory != nil {
149
147
for _ , m := range options .LoadToMemory {
150
- cfg , err := cl .LoadBackendConfigFileByName (m , options .ModelPath ,
148
+ cfg , err := application . BackendLoader () .LoadBackendConfigFileByName (m , options .ModelPath ,
151
149
config .LoadOptionDebug (options .Debug ),
152
150
config .LoadOptionThreads (options .Threads ),
153
151
config .LoadOptionContextSize (options .ContextSize ),
154
152
config .LoadOptionF16 (options .F16 ),
155
153
config .ModelPath (options .ModelPath ),
156
154
)
157
155
if err != nil {
158
- return nil , nil , nil , err
156
+ return nil , err
159
157
}
160
158
161
159
log .Debug ().Msgf ("Auto loading model %s into memory from file: %s" , m , cfg .Model )
162
160
163
161
o := backend .ModelOptions (* cfg , options )
164
162
165
163
var backendErr error
166
- _ , backendErr = ml .Load (o ... )
164
+ _ , backendErr = application . ModelLoader () .Load (o ... )
167
165
if backendErr != nil {
168
- return nil , nil , nil , err
166
+ return nil , err
169
167
}
170
168
}
171
169
}
@@ -174,7 +172,7 @@ func Startup(opts ...config.AppOption) (*config.BackendConfigLoader, *model.Mode
174
172
startWatcher (options )
175
173
176
174
log .Info ().Msg ("core/startup process completed!" )
177
- return cl , ml , options , nil
175
+ return application , nil
178
176
}
179
177
180
178
func startWatcher (options * config.ApplicationConfig ) {
@@ -201,32 +199,3 @@ func startWatcher(options *config.ApplicationConfig) {
201
199
log .Error ().Err (err ).Msg ("failed creating watcher" )
202
200
}
203
201
}
204
-
205
- // In Lieu of a proper DI framework, this function wires up the Application manually.
206
- // This is in core/startup rather than core/state.go to keep package references clean!
207
- func createApplication (appConfig * config.ApplicationConfig ) * core.Application {
208
- app := & core.Application {
209
- ApplicationConfig : appConfig ,
210
- BackendConfigLoader : config .NewBackendConfigLoader (appConfig .ModelPath ),
211
- ModelLoader : model .NewModelLoader (appConfig .ModelPath ),
212
- }
213
-
214
- var err error
215
-
216
- // app.EmbeddingsBackendService = backend.NewEmbeddingsBackendService(app.ModelLoader, app.BackendConfigLoader, app.ApplicationConfig)
217
- // app.ImageGenerationBackendService = backend.NewImageGenerationBackendService(app.ModelLoader, app.BackendConfigLoader, app.ApplicationConfig)
218
- // app.LLMBackendService = backend.NewLLMBackendService(app.ModelLoader, app.BackendConfigLoader, app.ApplicationConfig)
219
- // app.TranscriptionBackendService = backend.NewTranscriptionBackendService(app.ModelLoader, app.BackendConfigLoader, app.ApplicationConfig)
220
- // app.TextToSpeechBackendService = backend.NewTextToSpeechBackendService(app.ModelLoader, app.BackendConfigLoader, app.ApplicationConfig)
221
-
222
- app .BackendMonitorService = services .NewBackendMonitorService (app .ModelLoader , app .BackendConfigLoader , app .ApplicationConfig )
223
- app .GalleryService = services .NewGalleryService (app .ApplicationConfig )
224
- // app.OpenAIService = services.NewOpenAIService(app.ModelLoader, app.BackendConfigLoader, app.ApplicationConfig, app.LLMBackendService)
225
-
226
- app .LocalAIMetricsService , err = services .NewLocalAIMetricsService ()
227
- if err != nil {
228
- log .Error ().Err (err ).Msg ("encountered an error initializing metrics service, startup will continue but metrics will not be tracked." )
229
- }
230
-
231
- return app
232
- }
0 commit comments