Skip to content

Commit 7c30e82

Browse files
authored
fix: autoload backends when installing models from YAML files (#5859)
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
1 parent a1d061c commit 7c30e82

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

pkg/startup/model_preload.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"path"
78
"path/filepath"
89
"strings"
910

1011
"github.com/mudler/LocalAI/core/config"
1112
"github.com/mudler/LocalAI/core/gallery"
13+
"github.com/mudler/LocalAI/core/system"
1214
"github.com/mudler/LocalAI/pkg/downloader"
1315
"github.com/mudler/LocalAI/pkg/utils"
1416
"github.com/rs/zerolog/log"
17+
"gopkg.in/yaml.v2"
18+
)
19+
20+
const (
21+
YAML_EXTENSION = ".yaml"
1522
)
1623

1724
// InstallModels will preload models from the given list of URLs and galleries
@@ -21,6 +28,38 @@ func InstallModels(galleries, backendGalleries []config.Gallery, modelPath, back
2128
// create an error that groups all errors
2229
var err error
2330

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+
2463
for _, url := range models {
2564
// As a best effort, try to resolve the model from the remote library
2665
// if it's not resolved we try with the other method below
@@ -79,6 +118,13 @@ func InstallModels(galleries, backendGalleries []config.Gallery, modelPath, back
79118
err = errors.Join(err, e)
80119
}
81120
}
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+
}
82128
default:
83129
if _, e := os.Stat(url); e == nil {
84130
log.Debug().Msgf("[startup] resolved local model: %s", url)
@@ -92,11 +138,18 @@ func InstallModels(galleries, backendGalleries []config.Gallery, modelPath, back
92138
continue
93139
}
94140

95-
modelDefinitionFilePath := filepath.Join(modelPath, md5Name) + ".yaml"
141+
modelDefinitionFilePath := filepath.Join(modelPath, md5Name) + YAML_EXTENSION
96142
if e := os.WriteFile(modelDefinitionFilePath, modelYAML, 0600); e != nil {
97143
log.Error().Err(err).Str("filepath", modelDefinitionFilePath).Msg("error loading model: %s")
98144
err = errors.Join(err, e)
99145
}
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+
}
100153
} else {
101154
// Check if it's a model gallery, or print a warning
102155
e, found := installModel(galleries, backendGalleries, url, modelPath, backendBasePath, downloadStatus, enforceScan, autoloadBackendGalleries)

0 commit comments

Comments
 (0)