Skip to content

Commit 3e53f45

Browse files
committed
feat: add cmd to start rpc-server from llama.cpp
Signed-off-by: mudler <[email protected]>
1 parent 793c45d commit 3e53f45

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ ALL_GRPC_BACKENDS+=backend-assets/grpc/llama-cpp-avx2
159159
ALL_GRPC_BACKENDS+=backend-assets/grpc/llama-cpp-fallback
160160
ALL_GRPC_BACKENDS+=backend-assets/grpc/llama-ggml
161161
ALL_GRPC_BACKENDS+=backend-assets/grpc/llama-cpp-grpc
162+
ALL_GRPC_BACKENDS+=backend-assets/util/llama-cpp-rpc-server
162163
ALL_GRPC_BACKENDS+=backend-assets/grpc/gpt4all
163164
ALL_GRPC_BACKENDS+=backend-assets/grpc/rwkv
164165
ALL_GRPC_BACKENDS+=backend-assets/grpc/whisper
@@ -699,6 +700,10 @@ backend-assets/grpc/llama-cpp-grpc: backend-assets/grpc
699700
CMAKE_ARGS="$(CMAKE_ARGS) -DLLAMA_RPC=ON -DLLAMA_AVX=off -DLLAMA_AVX2=off -DLLAMA_AVX512=off -DLLAMA_FMA=off -DLLAMA_F16C=off" $(MAKE) VARIANT="llama-grpc" build-llama-cpp-grpc-server
700701
cp -rfv backend/cpp/llama-grpc/grpc-server backend-assets/grpc/llama-cpp-grpc
701702

703+
backend-assets/util/llama-cpp-rpc-server: backend-assets/grpc/llama-cpp-grpc
704+
mkdir -p backend-assets/util/
705+
cp -rf backend/cpp/llama-grpc/llama.cpp/build/bin/rpc-server backend-assets/util/llama-cpp-rpc-server
706+
702707
backend-assets/grpc/llama-ggml: sources/go-llama.cpp sources/go-llama.cpp/libbinding.a backend-assets/grpc
703708
CGO_LDFLAGS="$(CGO_LDFLAGS)" C_INCLUDE_PATH=$(CURDIR)/sources/go-llama.cpp LIBRARY_PATH=$(CURDIR)/sources/go-llama.cpp \
704709
$(GOCMD) build -ldflags "$(LD_FLAGS)" -tags "$(GO_TAGS)" -o backend-assets/grpc/llama-ggml ./backend/go/llm/llama-ggml/

core/cli/cli.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ type Context struct {
1313
var CLI struct {
1414
Context `embed:""`
1515

16-
Run RunCMD `cmd:"" help:"Run LocalAI, this the default command if no other command is specified. Run 'local-ai run --help' for more information" default:"withargs"`
17-
Models ModelsCMD `cmd:"" help:"Manage LocalAI models and definitions"`
18-
TTS TTSCMD `cmd:"" help:"Convert text to speech"`
19-
Transcript TranscriptCMD `cmd:"" help:"Convert audio to text"`
16+
Run RunCMD `cmd:"" help:"Run LocalAI, this the default command if no other command is specified. Run 'local-ai run --help' for more information" default:"withargs"`
17+
Models ModelsCMD `cmd:"" help:"Manage LocalAI models and definitions"`
18+
TTS TTSCMD `cmd:"" help:"Convert text to speech"`
19+
Transcript TranscriptCMD `cmd:"" help:"Convert audio to text"`
20+
LLAMACPPWorker LLAMACPPWorkerCMD `cmd:"" help:"Run workers to distribute workload (llama.cpp-only)"`
2021
}

core/cli/llamacppworker.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cli
2+
3+
import (
4+
"os"
5+
"syscall"
6+
7+
"github.com/go-skynet/LocalAI/pkg/assets"
8+
"github.com/rs/zerolog/log"
9+
)
10+
11+
type LLAMACPPWorkerCMD struct {
12+
Args []string `arg:"" optional:"" name:"models" help:"Worker arguments: host port"`
13+
BackendAssetsPath string `env:"LOCALAI_BACKEND_ASSETS_PATH,BACKEND_ASSETS_PATH" type:"path" default:"/tmp/localai/backend_data" help:"Path used to extract libraries that are required by some of the backends in runtime" group:"storage"`
14+
}
15+
16+
func (r *LLAMACPPWorkerCMD) Run(ctx *Context) error {
17+
// Extract files from the embedded FS
18+
err := assets.ExtractFiles(ctx.BackendAssets, r.BackendAssetsPath)
19+
log.Debug().Msgf("Extracting backend assets files to %s", r.BackendAssetsPath)
20+
if err != nil {
21+
log.Warn().Msgf("Failed extracting backend assets files: %s (might be required for some backends to work properly, like gpt4all)", err)
22+
}
23+
24+
return syscall.Exec(
25+
assets.ResolvePath(
26+
r.BackendAssetsPath,
27+
"util",
28+
"llama-cpp-rpc-server",
29+
),
30+
append([]string{
31+
assets.ResolvePath(
32+
r.BackendAssetsPath,
33+
"util",
34+
"llama-cpp-rpc-server",
35+
)}, r.Args...),
36+
os.Environ())
37+
}

pkg/assets/extract.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"path/filepath"
99
)
1010

11+
func ResolvePath(dir string, paths ...string) string {
12+
return filepath.Join(append([]string{dir, "backend-assets"}, paths...)...)
13+
}
14+
1115
func ExtractFiles(content embed.FS, extractDir string) error {
1216
// Create the target directory if it doesn't exist
1317
err := os.MkdirAll(extractDir, 0750)
@@ -39,7 +43,7 @@ func ExtractFiles(content embed.FS, extractDir string) error {
3943
}
4044

4145
// Create the file in the target directory
42-
err = os.WriteFile(targetFile, fileData, 0600)
46+
err = os.WriteFile(targetFile, fileData, 0700)
4347
if err != nil {
4448
return fmt.Errorf("failed to write file: %v", err)
4549
}

0 commit comments

Comments
 (0)