2023-05-11 14:34:16 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2023-07-14 23:19:43 +00:00
|
|
|
"context"
|
2023-05-11 14:34:16 +00:00
|
|
|
"fmt"
|
2023-07-14 23:19:43 +00:00
|
|
|
"os"
|
2023-07-14 23:19:43 +00:00
|
|
|
"os/signal"
|
2023-05-14 15:49:10 +00:00
|
|
|
"path/filepath"
|
2023-05-11 14:34:16 +00:00
|
|
|
"strings"
|
2023-07-14 23:19:43 +00:00
|
|
|
"syscall"
|
2023-07-14 23:19:43 +00:00
|
|
|
"time"
|
2023-05-11 14:34:16 +00:00
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
grpc "github.com/go-skynet/LocalAI/pkg/grpc"
|
2023-05-11 14:34:16 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2023-07-14 23:19:43 +00:00
|
|
|
"github.com/hpcloud/tail"
|
|
|
|
"github.com/phayes/freeport"
|
2023-05-11 14:34:16 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-07-14 23:19:43 +00:00
|
|
|
|
|
|
|
process "github.com/mudler/go-processmanager"
|
2023-05-11 14:34:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-07-14 23:19:43 +00:00
|
|
|
LlamaBackend = "llama"
|
|
|
|
BloomzBackend = "bloomz"
|
|
|
|
StarcoderBackend = "starcoder"
|
|
|
|
GPTJBackend = "gptj"
|
|
|
|
DollyBackend = "dolly"
|
|
|
|
MPTBackend = "mpt"
|
|
|
|
GPTNeoXBackend = "gptneox"
|
|
|
|
ReplitBackend = "replit"
|
|
|
|
Gpt2Backend = "gpt2"
|
|
|
|
Gpt4AllLlamaBackend = "gpt4all-llama"
|
|
|
|
Gpt4AllMptBackend = "gpt4all-mpt"
|
|
|
|
Gpt4AllJBackend = "gpt4all-j"
|
|
|
|
Gpt4All = "gpt4all"
|
|
|
|
FalconBackend = "falcon"
|
|
|
|
FalconGGMLBackend = "falcon-ggml"
|
|
|
|
|
2023-05-16 17:32:53 +00:00
|
|
|
BertEmbeddingsBackend = "bert-embeddings"
|
|
|
|
RwkvBackend = "rwkv"
|
|
|
|
WhisperBackend = "whisper"
|
|
|
|
StableDiffusionBackend = "stablediffusion"
|
2023-06-22 15:53:10 +00:00
|
|
|
PiperBackend = "piper"
|
2023-06-01 10:00:06 +00:00
|
|
|
LCHuggingFaceBackend = "langchain-huggingface"
|
2023-05-11 14:34:16 +00:00
|
|
|
)
|
|
|
|
|
2023-07-17 21:58:15 +00:00
|
|
|
var AutoLoadBackends []string = []string{
|
2023-05-11 14:34:16 +00:00
|
|
|
LlamaBackend,
|
2023-06-01 21:38:52 +00:00
|
|
|
Gpt4All,
|
2023-07-14 23:19:43 +00:00
|
|
|
FalconBackend,
|
2023-07-14 23:19:43 +00:00
|
|
|
GPTNeoXBackend,
|
2023-07-14 23:19:43 +00:00
|
|
|
BertEmbeddingsBackend,
|
|
|
|
FalconGGMLBackend,
|
2023-05-23 19:47:47 +00:00
|
|
|
GPTJBackend,
|
|
|
|
Gpt2Backend,
|
|
|
|
DollyBackend,
|
|
|
|
MPTBackend,
|
|
|
|
ReplitBackend,
|
2023-05-11 18:20:07 +00:00
|
|
|
StarcoderBackend,
|
2023-07-19 22:40:26 +00:00
|
|
|
BloomzBackend,
|
|
|
|
RwkvBackend,
|
|
|
|
WhisperBackend,
|
|
|
|
StableDiffusionBackend,
|
|
|
|
PiperBackend,
|
2023-05-11 18:20:07 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
func (ml *ModelLoader) StopGRPC() {
|
|
|
|
for _, p := range ml.grpcProcesses {
|
|
|
|
p.Stop()
|
2023-05-11 14:34:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
func (ml *ModelLoader) startProcess(grpcProcess, id string, serverAddress string) error {
|
|
|
|
// Make sure the process is executable
|
|
|
|
if err := os.Chmod(grpcProcess, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-14 23:19:43 +00:00
|
|
|
|
2023-07-24 21:10:54 +00:00
|
|
|
log.Debug().Msgf("Loading GRPC Process: %s", grpcProcess)
|
2023-07-14 23:19:43 +00:00
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
log.Debug().Msgf("GRPC Service for %s will be running at: '%s'", id, serverAddress)
|
2023-07-14 23:19:43 +00:00
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
grpcControlProcess := process.New(
|
|
|
|
process.WithTemporaryStateDir(),
|
|
|
|
process.WithName(grpcProcess),
|
2023-08-07 22:58:38 +00:00
|
|
|
process.WithArgs("--addr", serverAddress),
|
|
|
|
process.WithEnvironment(os.Environ()...),
|
|
|
|
)
|
2023-07-20 20:10:12 +00:00
|
|
|
|
|
|
|
ml.grpcProcesses[id] = grpcControlProcess
|
|
|
|
|
|
|
|
if err := grpcControlProcess.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-14 23:19:43 +00:00
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
log.Debug().Msgf("GRPC Service state dir: %s", grpcControlProcess.StateDir())
|
|
|
|
// clean up process
|
|
|
|
go func() {
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
|
|
<-c
|
|
|
|
grpcControlProcess.Stop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
t, err := tail.TailFile(grpcControlProcess.StderrPath(), tail.Config{Follow: true})
|
2023-07-14 23:19:43 +00:00
|
|
|
if err != nil {
|
2023-07-20 20:10:12 +00:00
|
|
|
log.Debug().Msgf("Could not tail stderr")
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
2023-07-20 20:10:12 +00:00
|
|
|
for line := range t.Lines {
|
|
|
|
log.Debug().Msgf("GRPC(%s): stderr %s", strings.Join([]string{id, serverAddress}, "-"), line.Text)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
t, err := tail.TailFile(grpcControlProcess.StdoutPath(), tail.Config{Follow: true})
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Msgf("Could not tail stdout")
|
|
|
|
}
|
|
|
|
for line := range t.Lines {
|
|
|
|
log.Debug().Msgf("GRPC(%s): stdout %s", strings.Join([]string{id, serverAddress}, "-"), line.Text)
|
|
|
|
}
|
|
|
|
}()
|
2023-07-14 23:19:43 +00:00
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-07-14 23:19:43 +00:00
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
// starts the grpcModelProcess for the backend, and returns a grpc client
|
|
|
|
// It also loads the model
|
2023-08-07 20:39:10 +00:00
|
|
|
func (ml *ModelLoader) grpcModel(backend string, o *Options) func(string, string) (*grpc.Client, error) {
|
|
|
|
return func(modelName, modelFile string) (*grpc.Client, error) {
|
2023-07-22 15:31:39 +00:00
|
|
|
log.Debug().Msgf("Loading GRPC Model %s: %+v", backend, *o)
|
2023-07-14 23:19:43 +00:00
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
var client *grpc.Client
|
2023-07-14 23:19:43 +00:00
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
getFreeAddress := func() (string, error) {
|
|
|
|
port, err := freeport.GetFreePort()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed allocating free ports: %s", err.Error())
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("127.0.0.1:%d", port), nil
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
// Check if the backend is provided as external
|
|
|
|
if uri, ok := o.externalBackends[backend]; ok {
|
|
|
|
log.Debug().Msgf("Loading external backend: %s", uri)
|
|
|
|
// check if uri is a file or a address
|
|
|
|
if _, err := os.Stat(uri); err == nil {
|
|
|
|
serverAddress, err := getFreeAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed allocating free ports: %s", err.Error())
|
|
|
|
}
|
|
|
|
// Make sure the process is executable
|
2023-08-07 20:39:10 +00:00
|
|
|
if err := ml.startProcess(uri, o.model, serverAddress); err != nil {
|
2023-07-20 20:10:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msgf("GRPC Service Started")
|
|
|
|
|
|
|
|
client = grpc.NewClient(serverAddress)
|
|
|
|
} else {
|
|
|
|
// address
|
|
|
|
client = grpc.NewClient(uri)
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
2023-07-20 20:10:12 +00:00
|
|
|
} else {
|
|
|
|
grpcProcess := filepath.Join(o.assetDir, "backend-assets", "grpc", backend)
|
|
|
|
// Check if the file exists
|
|
|
|
if _, err := os.Stat(grpcProcess); os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("grpc process not found: %s. some backends(stablediffusion, tts) require LocalAI compiled with GO_TAGS", grpcProcess)
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
2023-07-20 20:10:12 +00:00
|
|
|
|
|
|
|
serverAddress, err := getFreeAddress()
|
2023-07-14 23:19:43 +00:00
|
|
|
if err != nil {
|
2023-07-20 20:10:12 +00:00
|
|
|
return nil, fmt.Errorf("failed allocating free ports: %s", err.Error())
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
2023-07-20 20:10:12 +00:00
|
|
|
|
|
|
|
// Make sure the process is executable
|
2023-08-07 20:39:10 +00:00
|
|
|
if err := ml.startProcess(grpcProcess, o.model, serverAddress); err != nil {
|
2023-07-20 20:10:12 +00:00
|
|
|
return nil, err
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
log.Debug().Msgf("GRPC Service Started")
|
2023-07-14 23:19:43 +00:00
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
client = grpc.NewClient(serverAddress)
|
|
|
|
}
|
2023-07-14 23:19:43 +00:00
|
|
|
|
|
|
|
// Wait for the service to start up
|
|
|
|
ready := false
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
if client.HealthCheck(context.Background()) {
|
|
|
|
log.Debug().Msgf("GRPC Service Ready")
|
|
|
|
ready = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ready {
|
|
|
|
log.Debug().Msgf("GRPC Service NOT ready")
|
|
|
|
return nil, fmt.Errorf("grpc service not ready")
|
|
|
|
}
|
|
|
|
|
|
|
|
options := *o.gRPCOptions
|
2023-08-07 20:39:10 +00:00
|
|
|
options.Model = modelName
|
|
|
|
options.ModelFile = modelFile
|
2023-07-14 23:19:43 +00:00
|
|
|
|
|
|
|
log.Debug().Msgf("GRPC: Loading model with options: %+v", options)
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
res, err := client.LoadModel(o.context, &options)
|
2023-07-14 23:19:43 +00:00
|
|
|
if err != nil {
|
2023-07-20 22:52:43 +00:00
|
|
|
return nil, fmt.Errorf("could not load model: %w", err)
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
if !res.Success {
|
2023-07-20 22:52:43 +00:00
|
|
|
return nil, fmt.Errorf("could not load model (no success): %s", res.Message)
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
func (ml *ModelLoader) BackendLoader(opts ...Option) (model *grpc.Client, err error) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o := NewOptions(opts...)
|
|
|
|
|
2023-08-07 20:39:10 +00:00
|
|
|
log.Debug().Msgf("Loading model %s from %s", o.backendString, o.model)
|
2023-07-14 23:19:43 +00:00
|
|
|
|
|
|
|
backend := strings.ToLower(o.backendString)
|
2023-07-20 20:10:12 +00:00
|
|
|
|
|
|
|
// if an external backend is provided, use it
|
|
|
|
_, externalBackendExists := o.externalBackends[backend]
|
|
|
|
if externalBackendExists {
|
2023-08-07 20:39:10 +00:00
|
|
|
return ml.LoadModel(o.model, ml.grpcModel(backend, o))
|
2023-07-20 20:10:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
switch backend {
|
2023-07-25 17:05:27 +00:00
|
|
|
case LlamaBackend, GPTJBackend, DollyBackend,
|
2023-07-14 23:19:43 +00:00
|
|
|
MPTBackend, Gpt2Backend, FalconBackend,
|
|
|
|
GPTNeoXBackend, ReplitBackend, StarcoderBackend, BloomzBackend,
|
|
|
|
RwkvBackend, LCHuggingFaceBackend, BertEmbeddingsBackend, FalconGGMLBackend, StableDiffusionBackend, WhisperBackend:
|
2023-08-07 20:39:10 +00:00
|
|
|
return ml.LoadModel(o.model, ml.grpcModel(backend, o))
|
2023-06-01 21:38:52 +00:00
|
|
|
case Gpt4AllLlamaBackend, Gpt4AllMptBackend, Gpt4AllJBackend, Gpt4All:
|
2023-07-14 23:19:43 +00:00
|
|
|
o.gRPCOptions.LibrarySearchPath = filepath.Join(o.assetDir, "backend-assets", "gpt4all")
|
2023-08-07 20:39:10 +00:00
|
|
|
return ml.LoadModel(o.model, ml.grpcModel(Gpt4All, o))
|
2023-07-14 23:19:43 +00:00
|
|
|
case PiperBackend:
|
|
|
|
o.gRPCOptions.LibrarySearchPath = filepath.Join(o.assetDir, "backend-assets", "espeak-ng-data")
|
2023-08-07 20:39:10 +00:00
|
|
|
return ml.LoadModel(o.model, ml.grpcModel(PiperBackend, o))
|
2023-05-11 14:34:16 +00:00
|
|
|
default:
|
2023-07-14 23:19:43 +00:00
|
|
|
return nil, fmt.Errorf("backend unsupported: %s", o.backendString)
|
2023-05-11 14:34:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
func (ml *ModelLoader) GreedyLoader(opts ...Option) (*grpc.Client, error) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o := NewOptions(opts...)
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
// Is this really needed? BackendLoader already does this
|
2023-05-11 14:34:16 +00:00
|
|
|
ml.mu.Lock()
|
2023-08-07 20:39:10 +00:00
|
|
|
if m := ml.checkIsLoaded(o.model); m != nil {
|
|
|
|
log.Debug().Msgf("Model '%s' already loaded", o.model)
|
2023-05-11 14:34:16 +00:00
|
|
|
ml.mu.Unlock()
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
ml.mu.Unlock()
|
|
|
|
var err error
|
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
// autoload also external backends
|
|
|
|
allBackendsToAutoLoad := []string{}
|
|
|
|
allBackendsToAutoLoad = append(allBackendsToAutoLoad, AutoLoadBackends...)
|
|
|
|
for _, b := range o.externalBackends {
|
|
|
|
allBackendsToAutoLoad = append(allBackendsToAutoLoad, b)
|
|
|
|
}
|
2023-08-07 20:39:10 +00:00
|
|
|
log.Debug().Msgf("Loading model '%s' greedly from all the available backends: %s", o.model, strings.Join(allBackendsToAutoLoad, ", "))
|
2023-07-14 23:19:43 +00:00
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
for _, b := range allBackendsToAutoLoad {
|
|
|
|
log.Debug().Msgf("[%s] Attempting to load", b)
|
|
|
|
options := []Option{
|
2023-07-14 23:19:43 +00:00
|
|
|
WithBackendString(b),
|
2023-08-07 20:39:10 +00:00
|
|
|
WithModel(o.model),
|
2023-07-14 23:19:43 +00:00
|
|
|
WithLoadGRPCLLMModelOpts(o.gRPCOptions),
|
2023-07-14 23:19:43 +00:00
|
|
|
WithThreads(o.threads),
|
|
|
|
WithAssetDir(o.assetDir),
|
2023-07-20 20:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range o.externalBackends {
|
|
|
|
options = append(options, WithExternalBackend(k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
model, modelerr := ml.BackendLoader(options...)
|
2023-05-11 14:34:16 +00:00
|
|
|
if modelerr == nil && model != nil {
|
|
|
|
log.Debug().Msgf("[%s] Loads OK", b)
|
|
|
|
return model, nil
|
|
|
|
} else if modelerr != nil {
|
|
|
|
err = multierror.Append(err, modelerr)
|
|
|
|
log.Debug().Msgf("[%s] Fails: %s", b, modelerr.Error())
|
2023-07-17 21:58:15 +00:00
|
|
|
} else if model == nil {
|
2023-07-20 22:52:43 +00:00
|
|
|
err = multierror.Append(err, fmt.Errorf("backend returned no usable model"))
|
2023-07-17 21:58:15 +00:00
|
|
|
log.Debug().Msgf("[%s] Fails: %s", b, "backend returned no usable model")
|
2023-05-11 14:34:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("could not load model - all backends returned error: %s", err.Error())
|
|
|
|
}
|