@@ -4,14 +4,21 @@ import (
4
4
"errors"
5
5
"fmt"
6
6
"os"
7
+ "path"
7
8
"path/filepath"
8
9
"strings"
9
10
10
11
"github.com/mudler/LocalAI/core/config"
11
12
"github.com/mudler/LocalAI/core/gallery"
13
+ "github.com/mudler/LocalAI/core/system"
12
14
"github.com/mudler/LocalAI/pkg/downloader"
13
15
"github.com/mudler/LocalAI/pkg/utils"
14
16
"github.com/rs/zerolog/log"
17
+ "gopkg.in/yaml.v2"
18
+ )
19
+
20
+ const (
21
+ YAML_EXTENSION = ".yaml"
15
22
)
16
23
17
24
// InstallModels will preload models from the given list of URLs and galleries
@@ -21,6 +28,38 @@ func InstallModels(galleries, backendGalleries []config.Gallery, modelPath, back
21
28
// create an error that groups all errors
22
29
var err error
23
30
31
+ installBackend := func (modelPath string ) error {
32
+ // Then load the model file, and read the backend
33
+ modelYAML , e := os .ReadFile (modelPath )
34
+ if e != nil {
35
+ log .Error ().Err (e ).Str ("filepath" , modelPath ).Msg ("error reading model definition" )
36
+ return e
37
+ }
38
+
39
+ var model config.BackendConfig
40
+ if e := yaml .Unmarshal (modelYAML , & model ); e != nil {
41
+ log .Error ().Err (e ).Str ("filepath" , modelPath ).Msg ("error unmarshalling model definition" )
42
+ return e
43
+ }
44
+
45
+ if model .Backend == "" {
46
+ log .Debug ().Str ("filepath" , modelPath ).Msg ("no backend found in model definition" )
47
+ return nil
48
+ }
49
+
50
+ systemState , err := system .GetSystemState ()
51
+ if err != nil {
52
+ return err
53
+ }
54
+
55
+ if err := gallery .InstallBackendFromGallery (backendGalleries , systemState , model .Backend , backendBasePath , downloadStatus , false ); err != nil {
56
+ log .Error ().Err (err ).Str ("backend" , model .Backend ).Msg ("error installing backend" )
57
+ return err
58
+ }
59
+
60
+ return nil
61
+ }
62
+
24
63
for _ , url := range models {
25
64
// As a best effort, try to resolve the model from the remote library
26
65
// if it's not resolved we try with the other method below
@@ -79,6 +118,13 @@ func InstallModels(galleries, backendGalleries []config.Gallery, modelPath, back
79
118
err = errors .Join (err , e )
80
119
}
81
120
}
121
+
122
+ // Check if we have the backend installed
123
+ if autoloadBackendGalleries && path .Ext (modelPath ) == YAML_EXTENSION {
124
+ if err := installBackend (modelPath ); err != nil {
125
+ log .Error ().Err (err ).Str ("filepath" , modelPath ).Msg ("error installing backend" )
126
+ }
127
+ }
82
128
default :
83
129
if _ , e := os .Stat (url ); e == nil {
84
130
log .Debug ().Msgf ("[startup] resolved local model: %s" , url )
@@ -92,11 +138,18 @@ func InstallModels(galleries, backendGalleries []config.Gallery, modelPath, back
92
138
continue
93
139
}
94
140
95
- modelDefinitionFilePath := filepath .Join (modelPath , md5Name ) + ".yaml"
141
+ modelDefinitionFilePath := filepath .Join (modelPath , md5Name ) + YAML_EXTENSION
96
142
if e := os .WriteFile (modelDefinitionFilePath , modelYAML , 0600 ); e != nil {
97
143
log .Error ().Err (err ).Str ("filepath" , modelDefinitionFilePath ).Msg ("error loading model: %s" )
98
144
err = errors .Join (err , e )
99
145
}
146
+
147
+ // Check if we have the backend installed
148
+ if autoloadBackendGalleries && path .Ext (modelDefinitionFilePath ) == YAML_EXTENSION {
149
+ if err := installBackend (modelDefinitionFilePath ); err != nil {
150
+ log .Error ().Err (err ).Str ("filepath" , modelDefinitionFilePath ).Msg ("error installing backend" )
151
+ }
152
+ }
100
153
} else {
101
154
// Check if it's a model gallery, or print a warning
102
155
e , found := installModel (galleries , backendGalleries , url , modelPath , backendBasePath , downloadStatus , enforceScan , autoloadBackendGalleries )
0 commit comments