-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat(llama.cpp): Totally decentralized, private, distributed, p2p inference #2343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,16 @@ | ||
package cli | ||
|
||
import "embed" | ||
|
||
type Context struct { | ||
Debug bool `env:"LOCALAI_DEBUG,DEBUG" default:"false" hidden:"" help:"DEPRECATED, use --log-level=debug instead. Enable debug logging"` | ||
LogLevel *string `env:"LOCALAI_LOG_LEVEL" enum:"error,warn,info,debug,trace" help:"Set the level of logs to output [${enum}]"` | ||
|
||
// This field is not a command line argument/flag, the struct tag excludes it from the parsed CLI | ||
BackendAssets embed.FS `kong:"-"` | ||
} | ||
import ( | ||
cliContext "github.com/go-skynet/LocalAI/core/cli/context" | ||
"github.com/go-skynet/LocalAI/core/cli/worker" | ||
) | ||
|
||
var CLI struct { | ||
Context `embed:""` | ||
cliContext.Context `embed:""` | ||
|
||
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"` | ||
Models ModelsCMD `cmd:"" help:"Manage LocalAI models and definitions"` | ||
TTS TTSCMD `cmd:"" help:"Convert text to speech"` | ||
Transcript TranscriptCMD `cmd:"" help:"Convert audio to text"` | ||
LLAMACPPWorker LLAMACPPWorkerCMD `cmd:"" help:"Run workers to distribute workload (llama.cpp-only)"` | ||
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"` | ||
Models ModelsCMD `cmd:"" help:"Manage LocalAI models and definitions"` | ||
TTS TTSCMD `cmd:"" help:"Convert text to speech"` | ||
Transcript TranscriptCMD `cmd:"" help:"Convert audio to text"` | ||
Worker worker.Worker `cmd:"" help:"Run workers to distribute workload (llama.cpp-only)"` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cliContext | ||
|
||
import "embed" | ||
|
||
type Context struct { | ||
Debug bool `env:"LOCALAI_DEBUG,DEBUG" default:"false" hidden:"" help:"DEPRECATED, use --log-level=debug instead. Enable debug logging"` | ||
LogLevel *string `env:"LOCALAI_LOG_LEVEL" enum:"error,warn,info,debug,trace" help:"Set the level of logs to output [${enum}]"` | ||
|
||
// This field is not a command line argument/flag, the struct tag excludes it from the parsed CLI | ||
BackendAssets embed.FS `kong:"-"` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package worker | ||
|
||
type WorkerFlags struct { | ||
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"` | ||
} | ||
|
||
type Worker struct { | ||
P2P P2P `cmd:"" name:"p2p-llama-cpp-rpc" help:"Starts a LocalAI llama.cpp worker in P2P mode (requires a token)"` | ||
LLamaCPP LLamaCPP `cmd:"" name:"llama-cpp-rpc" help:"Starts a llama.cpp worker in standalone mode"` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build !p2p | ||
// +build !p2p | ||
|
||
package worker | ||
|
||
import ( | ||
"fmt" | ||
|
||
cliContext "github.com/go-skynet/LocalAI/core/cli/context" | ||
) | ||
|
||
type P2P struct{} | ||
|
||
func (r *P2P) Run(ctx *cliContext.Context) error { | ||
return fmt.Errorf("p2p mode is not enabled in this build") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
//go:build p2p | ||
// +build p2p | ||
|
||
package worker | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"time" | ||
|
||
cliContext "github.com/go-skynet/LocalAI/core/cli/context" | ||
"github.com/go-skynet/LocalAI/core/p2p" | ||
"github.com/go-skynet/LocalAI/pkg/assets" | ||
"github.com/phayes/freeport" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type P2P struct { | ||
WorkerFlags `embed:""` | ||
Token string `env:"LOCALAI_TOKEN,TOKEN" help:"JSON list of galleries"` | ||
NoRunner bool `env:"LOCALAI_NO_RUNNER,NO_RUNNER" help:"Do not start the llama-cpp-rpc-server"` | ||
RunnerAddress string `env:"LOCALAI_RUNNER_ADDRESS,RUNNER_ADDRESS" help:"Address of the llama-cpp-rpc-server"` | ||
RunnerPort string `env:"LOCALAI_RUNNER_PORT,RUNNER_PORT" help:"Port of the llama-cpp-rpc-server"` | ||
ExtraLLamaCPPArgs []string `env:"LOCALAI_EXTRA_LLAMA_CPP_ARGS,EXTRA_LLAMA_CPP_ARGS" help:"Extra arguments to pass to llama-cpp-rpc-server"` | ||
} | ||
|
||
func (r *P2P) Run(ctx *cliContext.Context) error { | ||
// Extract files from the embedded FS | ||
err := assets.ExtractFiles(ctx.BackendAssets, r.BackendAssetsPath) | ||
log.Debug().Msgf("Extracting backend assets files to %s", r.BackendAssetsPath) | ||
if err != nil { | ||
log.Warn().Msgf("Failed extracting backend assets files: %s (might be required for some backends to work properly, like gpt4all)", err) | ||
} | ||
|
||
// Check if the token is set | ||
// as we always need it. | ||
if r.Token == "" { | ||
return fmt.Errorf("Token is required") | ||
} | ||
|
||
port, err := freeport.GetFreePort() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
address := "127.0.0.1" | ||
|
||
if r.NoRunner { | ||
// Let override which port and address to bind if the user | ||
// configure the llama-cpp service on its own | ||
p := fmt.Sprint(port) | ||
if r.RunnerAddress != "" { | ||
address = r.RunnerAddress | ||
} | ||
if r.RunnerPort != "" { | ||
p = r.RunnerPort | ||
} | ||
|
||
err = p2p.BindLLamaCPPWorker(context.Background(), address, p, r.Token) | ||
if err != nil { | ||
return err | ||
} | ||
log.Info().Msgf("You need to start llama-cpp-rpc-server on '%s:%s'", address, p) | ||
|
||
return nil | ||
} | ||
|
||
// Start llama.cpp directly from the version we have pre-packaged | ||
go func() { | ||
for { | ||
log.Info().Msgf("Starting llama-cpp-rpc-server on '%s:%d'", address, port) | ||
cmd := exec.Command( | ||
assets.ResolvePath( | ||
r.BackendAssetsPath, | ||
"util", | ||
"llama-cpp-rpc-server", | ||
), | ||
append([]string{"--host", address, "--port", fmt.Sprint(port)}, r.ExtraLLamaCPPArgs...)..., | ||
) | ||
|
||
cmd.Env = os.Environ() | ||
|
||
cmd.Stderr = os.Stdout | ||
cmd.Stdout = os.Stdout | ||
|
||
if err := cmd.Start(); err != nil { | ||
log.Error().Err(err).Msg("Failed to start llama-cpp-rpc-server") | ||
} | ||
|
||
cmd.Wait() | ||
} | ||
}() | ||
|
||
err = p2p.BindLLamaCPPWorker(context.Background(), address, fmt.Sprint(port), r.Token) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
time.Sleep(1 * time.Second) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.