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-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
|
|
|
"time"
|
2023-05-11 14:34:16 +00:00
|
|
|
|
2024-01-05 17:04:46 +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/phayes/freeport"
|
2023-05-11 14:34:16 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2023-12-16 17:22:45 +00:00
|
|
|
var Aliases map[string]string = map[string]string{
|
|
|
|
"go-llama": GoLlamaBackend,
|
|
|
|
"llama": LLamaCPP,
|
|
|
|
}
|
|
|
|
|
2023-05-11 14:34:16 +00:00
|
|
|
const (
|
2023-11-18 07:18:43 +00:00
|
|
|
GoLlamaBackend = "llama"
|
|
|
|
LlamaGGML = "llama-ggml"
|
2023-10-16 19:46:29 +00:00
|
|
|
LLamaCPP = "llama-cpp"
|
2023-07-14 23:19:43 +00:00
|
|
|
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"
|
|
|
|
FalconGGMLBackend = "falcon-ggml"
|
|
|
|
|
2023-05-16 17:32:53 +00:00
|
|
|
BertEmbeddingsBackend = "bert-embeddings"
|
|
|
|
RwkvBackend = "rwkv"
|
|
|
|
WhisperBackend = "whisper"
|
|
|
|
StableDiffusionBackend = "stablediffusion"
|
2023-12-24 19:27:24 +00:00
|
|
|
TinyDreamBackend = "tinydream"
|
2023-06-22 15:53:10 +00:00
|
|
|
PiperBackend = "piper"
|
2023-06-01 10:00:06 +00:00
|
|
|
LCHuggingFaceBackend = "langchain-huggingface"
|
2023-12-08 09:01:02 +00:00
|
|
|
|
|
|
|
// External Backends that need special handling within LocalAI:
|
|
|
|
TransformersMusicGen = "transformers-musicgen"
|
2023-05-11 14:34:16 +00:00
|
|
|
)
|
|
|
|
|
2023-07-17 21:58:15 +00:00
|
|
|
var AutoLoadBackends []string = []string{
|
2023-10-16 19:46:29 +00:00
|
|
|
LLamaCPP,
|
2023-11-18 07:18:43 +00:00
|
|
|
LlamaGGML,
|
|
|
|
GoLlamaBackend,
|
2023-06-01 21:38:52 +00:00
|
|
|
Gpt4All,
|
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
|
|
|
RwkvBackend,
|
|
|
|
WhisperBackend,
|
|
|
|
StableDiffusionBackend,
|
2023-12-24 19:27:24 +00:00
|
|
|
TinyDreamBackend,
|
2023-07-19 22:40:26 +00:00
|
|
|
PiperBackend,
|
2023-05-11 18:20:07 +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
|
2024-01-05 17:04:46 +00:00
|
|
|
func (ml *ModelLoader) grpcModel(backend string, o *Options) func(string, string) (ModelAddress, error) {
|
2023-11-16 07:20:05 +00:00
|
|
|
return func(modelName, modelFile string) (ModelAddress, error) {
|
2023-11-11 12:14:59 +00:00
|
|
|
log.Debug().Msgf("Loading Model %s with gRPC (file: %s) (backend: %s): %+v", modelName, modelFile, backend, *o)
|
2023-07-14 23:19:43 +00:00
|
|
|
|
2023-11-16 07:20:05 +00:00
|
|
|
var client ModelAddress
|
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 {
|
2023-11-16 07:20:05 +00:00
|
|
|
return "", fmt.Errorf("failed allocating free ports: %s", err.Error())
|
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(uri, o.model, serverAddress); err != nil {
|
2023-11-16 07:20:05 +00:00
|
|
|
return "", err
|
2023-07-20 20:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msgf("GRPC Service Started")
|
|
|
|
|
2023-11-16 07:20:05 +00:00
|
|
|
client = ModelAddress(serverAddress)
|
2023-07-20 20:10:12 +00:00
|
|
|
} else {
|
|
|
|
// address
|
2023-11-16 07:20:05 +00:00
|
|
|
client = ModelAddress(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) {
|
2023-11-16 07:20:05 +00:00
|
|
|
return "", 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-11-16 07:20:05 +00:00
|
|
|
return "", 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-11-16 07:20:05 +00:00
|
|
|
return "", 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-11-16 07:20:05 +00:00
|
|
|
client = ModelAddress(serverAddress)
|
2023-07-20 20:10:12 +00:00
|
|
|
}
|
2023-07-14 23:19:43 +00:00
|
|
|
|
|
|
|
// Wait for the service to start up
|
|
|
|
ready := false
|
2023-08-15 23:11:32 +00:00
|
|
|
for i := 0; i < o.grpcAttempts; i++ {
|
2024-01-07 23:37:02 +00:00
|
|
|
alive, err := client.GRPC(o.parallelRequests, ml.wd).HealthCheck(context.Background())
|
|
|
|
if alive {
|
2023-07-14 23:19:43 +00:00
|
|
|
log.Debug().Msgf("GRPC Service Ready")
|
|
|
|
ready = true
|
|
|
|
break
|
|
|
|
}
|
2024-01-07 23:37:02 +00:00
|
|
|
if err != nil && i == o.grpcAttempts-1 {
|
|
|
|
log.Error().Msgf("Failed starting/connecting to the gRPC service: %s", err.Error())
|
|
|
|
}
|
2023-08-15 23:11:32 +00:00
|
|
|
time.Sleep(time.Duration(o.grpcAttemptsDelay) * time.Second)
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ready {
|
|
|
|
log.Debug().Msgf("GRPC Service NOT ready")
|
2023-11-16 07:20:05 +00:00
|
|
|
return "", fmt.Errorf("grpc service not ready")
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-11-26 17:36:23 +00:00
|
|
|
res, err := client.GRPC(o.parallelRequests, ml.wd).LoadModel(o.context, &options)
|
2023-07-14 23:19:43 +00:00
|
|
|
if err != nil {
|
2023-11-16 07:20:05 +00:00
|
|
|
return "", fmt.Errorf("could not load model: %w", err)
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
if !res.Success {
|
2023-11-16 07:20:05 +00:00
|
|
|
return "", fmt.Errorf("could not load model (no success): %s", res.Message)
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:20:05 +00:00
|
|
|
func (ml *ModelLoader) resolveAddress(addr ModelAddress, parallel bool) (*grpc.Client, error) {
|
|
|
|
if parallel {
|
2023-11-26 17:36:23 +00:00
|
|
|
return addr.GRPC(parallel, ml.wd), nil
|
2023-11-16 07:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := ml.grpcClients[string(addr)]; !ok {
|
2023-11-26 17:36:23 +00:00
|
|
|
ml.grpcClients[string(addr)] = addr.GRPC(parallel, ml.wd)
|
2023-11-16 07:20:05 +00:00
|
|
|
}
|
|
|
|
return ml.grpcClients[string(addr)], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *ModelLoader) BackendLoader(opts ...Option) (client *grpc.Client, err error) {
|
2023-07-14 23:19:43 +00:00
|
|
|
o := NewOptions(opts...)
|
|
|
|
|
2024-01-07 23:37:02 +00:00
|
|
|
if o.model != "" {
|
|
|
|
log.Info().Msgf("Loading model '%s' with backend %s", o.model, o.backendString)
|
|
|
|
} else {
|
|
|
|
log.Info().Msgf("Loading model with backend %s", o.backendString)
|
|
|
|
}
|
2023-07-14 23:19:43 +00:00
|
|
|
|
|
|
|
backend := strings.ToLower(o.backendString)
|
2023-12-16 17:22:45 +00:00
|
|
|
if realBackend, exists := Aliases[backend]; exists {
|
|
|
|
backend = realBackend
|
|
|
|
log.Debug().Msgf("%s is an alias of %s", backend, realBackend)
|
|
|
|
}
|
2023-07-20 20:10:12 +00:00
|
|
|
|
2023-08-18 23:49:33 +00:00
|
|
|
if o.singleActiveBackend {
|
|
|
|
ml.mu.Lock()
|
|
|
|
log.Debug().Msgf("Stopping all backends except '%s'", o.model)
|
|
|
|
ml.StopAllExcept(o.model)
|
|
|
|
ml.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:20:05 +00:00
|
|
|
var backendToConsume string
|
2023-07-20 20:10:12 +00:00
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
switch backend {
|
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-11-16 07:20:05 +00:00
|
|
|
backendToConsume = Gpt4All
|
2023-07-14 23:19:43 +00:00
|
|
|
case PiperBackend:
|
|
|
|
o.gRPCOptions.LibrarySearchPath = filepath.Join(o.assetDir, "backend-assets", "espeak-ng-data")
|
2023-11-16 07:20:05 +00:00
|
|
|
backendToConsume = PiperBackend
|
2023-05-11 14:34:16 +00:00
|
|
|
default:
|
2023-11-16 07:20:05 +00:00
|
|
|
backendToConsume = backend
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := ml.LoadModel(o.model, ml.grpcModel(backendToConsume, o))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-05-11 14:34:16 +00:00
|
|
|
}
|
2023-11-16 07:20:05 +00:00
|
|
|
|
|
|
|
return ml.resolveAddress(addr, o.parallelRequests)
|
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-05-11 14:34:16 +00:00
|
|
|
ml.mu.Lock()
|
2023-08-18 23:49:33 +00:00
|
|
|
// Return earlier if we have a model already loaded
|
|
|
|
// (avoid looping through all the backends)
|
2023-11-16 07:20:05 +00:00
|
|
|
if m := ml.CheckIsLoaded(o.model); m != "" {
|
2023-08-07 20:39:10 +00:00
|
|
|
log.Debug().Msgf("Model '%s' already loaded", o.model)
|
2023-05-11 14:34:16 +00:00
|
|
|
ml.mu.Unlock()
|
2023-11-16 07:20:05 +00:00
|
|
|
|
|
|
|
return ml.resolveAddress(m, o.parallelRequests)
|
2023-05-11 14:34:16 +00:00
|
|
|
}
|
2023-08-18 23:49:33 +00:00
|
|
|
// If we can have only one backend active, kill all the others (except external backends)
|
|
|
|
if o.singleActiveBackend {
|
|
|
|
log.Debug().Msgf("Stopping all backends except '%s'", o.model)
|
|
|
|
ml.StopAllExcept(o.model)
|
|
|
|
}
|
2023-05-11 14:34:16 +00:00
|
|
|
ml.mu.Unlock()
|
2023-08-18 23:49:33 +00:00
|
|
|
|
2023-05-11 14:34:16 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-01-07 23:37:02 +00:00
|
|
|
|
|
|
|
if o.model != "" {
|
|
|
|
log.Info().Msgf("Trying to load the model '%s' with 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 {
|
2024-01-01 09:31:03 +00:00
|
|
|
log.Info().Msgf("[%s] Attempting to load", b)
|
2023-07-20 20:10:12 +00:00
|
|
|
options := []Option{
|
2023-07-14 23:19:43 +00:00
|
|
|
WithBackendString(b),
|
2023-08-07 20:39:10 +00:00
|
|
|
WithModel(o.model),
|
2023-08-09 06:38:51 +00:00
|
|
|
WithLoadGRPCLoadModelOpts(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 {
|
2024-01-01 09:31:03 +00:00
|
|
|
log.Info().Msgf("[%s] Loads OK", b)
|
2023-05-11 14:34:16 +00:00
|
|
|
return model, nil
|
|
|
|
} else if modelerr != nil {
|
|
|
|
err = multierror.Append(err, modelerr)
|
2024-01-01 09:31:03 +00:00
|
|
|
log.Info().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"))
|
2024-01-01 09:31:03 +00:00
|
|
|
log.Info().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())
|
|
|
|
}
|