From 302958efd6cb34395c885cea6d6b9e37103d4546 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Thu, 4 Sep 2025 21:57:39 +0200 Subject: [PATCH] fix(p2p): automatically install llama-cpp for p2p workers (#6199) Signed-off-by: Ettore Di Giacinto Signed-off-by: Ettore Di Giacinto --- core/cli/worker/worker.go | 1 + core/cli/worker/worker_llamacpp.go | 22 ++++++++++++++++++---- core/cli/worker/worker_p2p.go | 2 +- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/core/cli/worker/worker.go b/core/cli/worker/worker.go index 77bb35a1b..2f330d44a 100644 --- a/core/cli/worker/worker.go +++ b/core/cli/worker/worker.go @@ -2,6 +2,7 @@ package worker type WorkerFlags struct { BackendsPath string `env:"LOCALAI_BACKENDS_PATH,BACKENDS_PATH" type:"path" default:"${basepath}/backends" help:"Path containing backends used for inferencing" group:"backends"` + BackendGalleries string `env:"LOCALAI_BACKEND_GALLERIES,BACKEND_GALLERIES" help:"JSON list of backend galleries" group:"backends" default:"${backends}"` 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"` 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"` } diff --git a/core/cli/worker/worker_llamacpp.go b/core/cli/worker/worker_llamacpp.go index f30acfb2b..b829f5cbb 100644 --- a/core/cli/worker/worker_llamacpp.go +++ b/core/cli/worker/worker_llamacpp.go @@ -1,6 +1,7 @@ package worker import ( + "encoding/json" "errors" "fmt" "os" @@ -9,8 +10,10 @@ import ( "syscall" cliContext "github.com/mudler/LocalAI/core/cli/context" + "github.com/mudler/LocalAI/core/config" "github.com/mudler/LocalAI/core/cli/signals" "github.com/mudler/LocalAI/core/gallery" + "github.com/mudler/LocalAI/pkg/model" "github.com/mudler/LocalAI/pkg/system" "github.com/rs/zerolog/log" ) @@ -21,9 +24,10 @@ type LLamaCPP struct { const ( llamaCPPRPCBinaryName = "llama-cpp-rpc-server" + llamaCPPGalleryName = "llama-cpp" ) -func findLLamaCPPBackend(systemState *system.SystemState) (string, error) { +func findLLamaCPPBackend(galleries string, systemState *system.SystemState) (string, error) { backends, err := gallery.ListSystemBackends(systemState) if err != nil { log.Warn().Msgf("Failed listing system backends: %s", err) @@ -31,9 +35,19 @@ func findLLamaCPPBackend(systemState *system.SystemState) (string, error) { } log.Debug().Msgf("System backends: %v", backends) - backend, ok := backends.Get("llama-cpp") + backend, ok := backends.Get(llamaCPPGalleryName) if !ok { - return "", errors.New("llama-cpp backend not found, install it first") + ml := model.NewModelLoader(systemState, true) + var gals []config.Gallery + if err := json.Unmarshal([]byte(galleries), &gals); err != nil { + log.Error().Err(err).Msg("failed loading galleries") + return "", err + } + err := gallery.InstallBackendFromGallery(gals, systemState, ml, llamaCPPGalleryName, nil, true) + if err != nil { + log.Error().Err(err).Msg("llama-cpp backend not found, failed to install it") + return "", err + } } backendPath := filepath.Dir(backend.RunFile) @@ -62,7 +76,7 @@ func (r *LLamaCPP) Run(ctx *cliContext.Context) error { if err != nil { return err } - grpcProcess, err := findLLamaCPPBackend(systemState) + grpcProcess, err := findLLamaCPPBackend(r.BackendGalleries, systemState) if err != nil { return err } diff --git a/core/cli/worker/worker_p2p.go b/core/cli/worker/worker_p2p.go index 8350f05d5..3ff98efe4 100644 --- a/core/cli/worker/worker_p2p.go +++ b/core/cli/worker/worker_p2p.go @@ -70,7 +70,7 @@ func (r *P2P) Run(ctx *cliContext.Context) error { for { log.Info().Msgf("Starting llama-cpp-rpc-server on '%s:%d'", address, port) - grpcProcess, err := findLLamaCPPBackend(systemState) + grpcProcess, err := findLLamaCPPBackend(r.BackendGalleries, systemState) if err != nil { log.Error().Err(err).Msg("Failed to find llama-cpp-rpc-server") return