refactor: backend/service split, channel-based llm flow (#1963)

Refactor: channel based llm flow and services split

---------

Signed-off-by: Dave Lee <dave@gray101.com>
This commit is contained in:
Dave 2024-04-13 03:45:34 -04:00 committed by GitHub
parent 1981154f49
commit eed5706994
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
52 changed files with 3064 additions and 2279 deletions

View file

@ -124,11 +124,11 @@ func (r *RunCMD) Run(ctx *Context) error {
}
if r.PreloadBackendOnly {
_, _, _, err := startup.Startup(opts...)
_, err := startup.Startup(opts...)
return err
}
cl, ml, options, err := startup.Startup(opts...)
application, err := startup.Startup(opts...)
if err != nil {
return fmt.Errorf("failed basic startup tasks with error %s", err.Error())
@ -137,7 +137,7 @@ func (r *RunCMD) Run(ctx *Context) error {
// Watch the configuration directory
// If the directory does not exist, we don't watch it
if _, err := os.Stat(r.LocalaiConfigDir); err == nil {
closeConfigWatcherFn, err := startup.WatchConfigDirectory(r.LocalaiConfigDir, options)
closeConfigWatcherFn, err := startup.WatchConfigDirectory(r.LocalaiConfigDir, application.ApplicationConfig)
defer closeConfigWatcherFn()
if err != nil {
@ -145,7 +145,7 @@ func (r *RunCMD) Run(ctx *Context) error {
}
}
appHTTP, err := http.App(cl, ml, options)
appHTTP, err := http.App(application)
if err != nil {
log.Error().Err(err).Msg("error during HTTP App construction")
return err

View file

@ -7,6 +7,7 @@ import (
"github.com/go-skynet/LocalAI/core/backend"
"github.com/go-skynet/LocalAI/core/config"
"github.com/go-skynet/LocalAI/core/schema"
"github.com/go-skynet/LocalAI/pkg/model"
)
@ -43,11 +44,21 @@ func (t *TranscriptCMD) Run(ctx *Context) error {
defer ml.StopAllGRPC()
tr, err := backend.ModelTranscription(t.Filename, t.Language, ml, c, opts)
if err != nil {
return err
tbs := backend.NewTranscriptionBackendService(ml, cl, opts)
resultChannel := tbs.Transcribe(&schema.OpenAIRequest{
PredictionOptions: schema.PredictionOptions{
Language: t.Language,
},
File: t.Filename,
})
r := <-resultChannel
if r.Error != nil {
return r.Error
}
for _, segment := range tr.Segments {
for _, segment := range r.Value.Segments {
fmt.Println(segment.Start.String(), "-", segment.Text)
}
return nil

View file

@ -9,6 +9,7 @@ import (
"github.com/go-skynet/LocalAI/core/backend"
"github.com/go-skynet/LocalAI/core/config"
"github.com/go-skynet/LocalAI/core/schema"
"github.com/go-skynet/LocalAI/pkg/model"
)
@ -42,20 +43,29 @@ func (t *TTSCMD) Run(ctx *Context) error {
defer ml.StopAllGRPC()
options := config.BackendConfig{}
options.SetDefaults()
ttsbs := backend.NewTextToSpeechBackendService(ml, config.NewBackendConfigLoader(), opts)
filePath, _, err := backend.ModelTTS(t.Backend, text, t.Model, t.Voice, ml, opts, options)
if err != nil {
return err
request := &schema.TTSRequest{
Model: t.Model,
Input: text,
Backend: t.Backend,
Voice: t.Voice,
}
resultsChannel := ttsbs.TextToAudioFile(request)
rawResult := <-resultsChannel
if rawResult.Error != nil {
return rawResult.Error
}
if outputFile != "" {
if err := os.Rename(filePath, outputFile); err != nil {
if err := os.Rename(*rawResult.Value, outputFile); err != nil {
return err
}
fmt.Printf("Generate file %s\n", outputFile)
fmt.Printf("Generated file %q\n", outputFile)
} else {
fmt.Printf("Generate file %s\n", filePath)
fmt.Printf("Generated file %q\n", *rawResult.Value)
}
return nil
}