Skip to content

Commit 302958e

Browse files
authored
fix(p2p): automatically install llama-cpp for p2p workers (#6199)
Signed-off-by: Ettore Di Giacinto <[email protected]> Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 3dc86b2 commit 302958e

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

core/cli/worker/worker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package worker
22

33
type WorkerFlags struct {
44
BackendsPath string `env:"LOCALAI_BACKENDS_PATH,BACKENDS_PATH" type:"path" default:"${basepath}/backends" help:"Path containing backends used for inferencing" group:"backends"`
5+
BackendGalleries string `env:"LOCALAI_BACKEND_GALLERIES,BACKEND_GALLERIES" help:"JSON list of backend galleries" group:"backends" default:"${backends}"`
56
BackendsSystemPath string `env:"LOCALAI_BACKENDS_SYSTEM_PATH,BACKEND_SYSTEM_PATH" type:"path" default:"/usr/share/localai/backends" help:"Path containing system backends used for inferencing" group:"backends"`
67
ExtraLLamaCPPArgs string `name:"llama-cpp-args" env:"LOCALAI_EXTRA_LLAMA_CPP_ARGS,EXTRA_LLAMA_CPP_ARGS" help:"Extra arguments to pass to llama-cpp-rpc-server"`
78
}

core/cli/worker/worker_llamacpp.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package worker
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"os"
@@ -9,8 +10,10 @@ import (
910
"syscall"
1011

1112
cliContext "github.com/mudler/LocalAI/core/cli/context"
13+
"github.com/mudler/LocalAI/core/config"
1214
"github.com/mudler/LocalAI/core/cli/signals"
1315
"github.com/mudler/LocalAI/core/gallery"
16+
"github.com/mudler/LocalAI/pkg/model"
1417
"github.com/mudler/LocalAI/pkg/system"
1518
"github.com/rs/zerolog/log"
1619
)
@@ -21,19 +24,30 @@ type LLamaCPP struct {
2124

2225
const (
2326
llamaCPPRPCBinaryName = "llama-cpp-rpc-server"
27+
llamaCPPGalleryName = "llama-cpp"
2428
)
2529

26-
func findLLamaCPPBackend(systemState *system.SystemState) (string, error) {
30+
func findLLamaCPPBackend(galleries string, systemState *system.SystemState) (string, error) {
2731
backends, err := gallery.ListSystemBackends(systemState)
2832
if err != nil {
2933
log.Warn().Msgf("Failed listing system backends: %s", err)
3034
return "", err
3135
}
3236
log.Debug().Msgf("System backends: %v", backends)
3337

34-
backend, ok := backends.Get("llama-cpp")
38+
backend, ok := backends.Get(llamaCPPGalleryName)
3539
if !ok {
36-
return "", errors.New("llama-cpp backend not found, install it first")
40+
ml := model.NewModelLoader(systemState, true)
41+
var gals []config.Gallery
42+
if err := json.Unmarshal([]byte(galleries), &gals); err != nil {
43+
log.Error().Err(err).Msg("failed loading galleries")
44+
return "", err
45+
}
46+
err := gallery.InstallBackendFromGallery(gals, systemState, ml, llamaCPPGalleryName, nil, true)
47+
if err != nil {
48+
log.Error().Err(err).Msg("llama-cpp backend not found, failed to install it")
49+
return "", err
50+
}
3751
}
3852
backendPath := filepath.Dir(backend.RunFile)
3953

@@ -62,7 +76,7 @@ func (r *LLamaCPP) Run(ctx *cliContext.Context) error {
6276
if err != nil {
6377
return err
6478
}
65-
grpcProcess, err := findLLamaCPPBackend(systemState)
79+
grpcProcess, err := findLLamaCPPBackend(r.BackendGalleries, systemState)
6680
if err != nil {
6781
return err
6882
}

core/cli/worker/worker_p2p.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (r *P2P) Run(ctx *cliContext.Context) error {
7070
for {
7171
log.Info().Msgf("Starting llama-cpp-rpc-server on '%s:%d'", address, port)
7272

73-
grpcProcess, err := findLLamaCPPBackend(systemState)
73+
grpcProcess, err := findLLamaCPPBackend(r.BackendGalleries, systemState)
7474
if err != nil {
7575
log.Error().Err(err).Msg("Failed to find llama-cpp-rpc-server")
7676
return

0 commit comments

Comments
 (0)