Skip to content

Commit 37ea989

Browse files
committed
feat(initializer): do not specify backends to autoload
We can simply try to autoload the backends extracted in the asset dir. This will allow to build variants of the same backend (for e.g. with different instructions sets), so to have a single binary for all the variants. Signed-off-by: mudler <[email protected]>
1 parent 929a68c commit 37ea989

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

pkg/model/initializers.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package model
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"os"
78
"path/filepath"
89
"strings"
910
"time"
1011

1112
grpc "github.com/go-skynet/LocalAI/pkg/grpc"
12-
"github.com/hashicorp/go-multierror"
1313
"github.com/phayes/freeport"
1414
"github.com/rs/zerolog/log"
1515
)
@@ -39,16 +39,22 @@ const (
3939
LocalStoreBackend = "local-store"
4040
)
4141

42-
var AutoLoadBackends []string = []string{
43-
LLamaCPP,
44-
LlamaGGML,
45-
Gpt4All,
46-
BertEmbeddingsBackend,
47-
RwkvBackend,
48-
WhisperBackend,
49-
StableDiffusionBackend,
50-
TinyDreamBackend,
51-
PiperBackend,
42+
func backendPath(assetDir, backend string) string {
43+
return filepath.Join(assetDir, "backend-assets", "grpc", backend)
44+
}
45+
46+
func bakends(assetDir string) ([]string, error) {
47+
entry, err := os.ReadDir(backendPath(assetDir, ""))
48+
if err != nil {
49+
return nil, err
50+
}
51+
var backends []string
52+
for _, e := range entry {
53+
if !e.IsDir() {
54+
backends = append(backends, e.Name())
55+
}
56+
}
57+
return backends, nil
5258
}
5359

5460
// starts the grpcModelProcess for the backend, and returns a grpc client
@@ -99,7 +105,7 @@ func (ml *ModelLoader) grpcModel(backend string, o *Options) func(string, string
99105
client = ModelAddress(uri)
100106
}
101107
} else {
102-
grpcProcess := filepath.Join(o.assetDir, "backend-assets", "grpc", backend)
108+
grpcProcess := backendPath(o.assetDir, backend)
103109
// Check if the file exists
104110
if _, err := os.Stat(grpcProcess); os.IsNotExist(err) {
105111
return "", fmt.Errorf("grpc process not found: %s. some backends(stablediffusion, tts) require LocalAI compiled with GO_TAGS", grpcProcess)
@@ -243,7 +249,11 @@ func (ml *ModelLoader) GreedyLoader(opts ...Option) (grpc.Backend, error) {
243249

244250
// autoload also external backends
245251
allBackendsToAutoLoad := []string{}
246-
allBackendsToAutoLoad = append(allBackendsToAutoLoad, AutoLoadBackends...)
252+
autoLoadBackends, err := bakends(o.assetDir)
253+
if err != nil {
254+
return nil, err
255+
}
256+
allBackendsToAutoLoad = append(allBackendsToAutoLoad, autoLoadBackends...)
247257
for _, b := range o.externalBackends {
248258
allBackendsToAutoLoad = append(allBackendsToAutoLoad, b)
249259
}
@@ -271,10 +281,10 @@ func (ml *ModelLoader) GreedyLoader(opts ...Option) (grpc.Backend, error) {
271281
log.Info().Msgf("[%s] Loads OK", b)
272282
return model, nil
273283
} else if modelerr != nil {
274-
err = multierror.Append(err, modelerr)
284+
err = errors.Join(err, modelerr)
275285
log.Info().Msgf("[%s] Fails: %s", b, modelerr.Error())
276286
} else if model == nil {
277-
err = multierror.Append(err, fmt.Errorf("backend returned no usable model"))
287+
err = errors.Join(err, fmt.Errorf("backend returned no usable model"))
278288
log.Info().Msgf("[%s] Fails: %s", b, "backend returned no usable model")
279289
}
280290
}

0 commit comments

Comments
 (0)