mirror of
https://github.com/mudler/LocalAI.git
synced 2025-05-28 06:25:00 +00:00
fix: drop racy code, refactor and group API schema (#931)
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
parent
28db83e17b
commit
cc060a283d
55 changed files with 239 additions and 317 deletions
|
@ -5,34 +5,32 @@ package base
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/go-skynet/LocalAI/api/schema"
|
||||
pb "github.com/go-skynet/LocalAI/pkg/grpc/proto"
|
||||
"github.com/go-skynet/LocalAI/pkg/grpc/whisper/api"
|
||||
gopsutil "github.com/shirou/gopsutil/v3/process"
|
||||
)
|
||||
|
||||
// Base is a base class for all backends to implement
|
||||
// Note: the backends that does not support multiple requests
|
||||
// should use SingleThread instead
|
||||
type Base struct {
|
||||
backendBusy sync.Mutex
|
||||
State pb.StatusResponse_State
|
||||
}
|
||||
|
||||
func (llm *Base) Busy() bool {
|
||||
r := llm.backendBusy.TryLock()
|
||||
if r {
|
||||
llm.backendBusy.Unlock()
|
||||
}
|
||||
return r
|
||||
func (llm *Base) Locking() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (llm *Base) Lock() {
|
||||
llm.backendBusy.Lock()
|
||||
llm.State = pb.StatusResponse_BUSY
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (llm *Base) Unlock() {
|
||||
llm.State = pb.StatusResponse_READY
|
||||
llm.backendBusy.Unlock()
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (llm *Base) Busy() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (llm *Base) Load(opts *pb.ModelOptions) error {
|
||||
|
@ -55,8 +53,8 @@ func (llm *Base) GenerateImage(*pb.GenerateImageRequest) error {
|
|||
return fmt.Errorf("unimplemented")
|
||||
}
|
||||
|
||||
func (llm *Base) AudioTranscription(*pb.TranscriptRequest) (api.Result, error) {
|
||||
return api.Result{}, fmt.Errorf("unimplemented")
|
||||
func (llm *Base) AudioTranscription(*pb.TranscriptRequest) (schema.Result, error) {
|
||||
return schema.Result{}, fmt.Errorf("unimplemented")
|
||||
}
|
||||
|
||||
func (llm *Base) TTS(*pb.TTSRequest) error {
|
||||
|
@ -69,7 +67,12 @@ func (llm *Base) TokenizeString(opts *pb.PredictOptions) (pb.TokenizationRespons
|
|||
|
||||
// backends may wish to call this to capture the gopsutil info, then enhance with additional memory usage details?
|
||||
func (llm *Base) Status() (pb.StatusResponse, error) {
|
||||
return pb.StatusResponse{
|
||||
Memory: memoryUsage(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func memoryUsage() *pb.MemoryUsageData {
|
||||
mud := pb.MemoryUsageData{
|
||||
Breakdown: make(map[string]uint64),
|
||||
}
|
||||
|
@ -85,9 +88,5 @@ func (llm *Base) Status() (pb.StatusResponse, error) {
|
|||
mud.Breakdown["gopsutil-RSS"] = memInfo.RSS
|
||||
}
|
||||
}
|
||||
|
||||
return pb.StatusResponse{
|
||||
State: llm.State,
|
||||
Memory: &mud,
|
||||
}, nil
|
||||
return &mud
|
||||
}
|
||||
|
|
52
pkg/grpc/base/singlethread.go
Normal file
52
pkg/grpc/base/singlethread.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package base
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
pb "github.com/go-skynet/LocalAI/pkg/grpc/proto"
|
||||
)
|
||||
|
||||
// SingleThread are backends that does not support multiple requests.
|
||||
// There will be only one request being served at the time.
|
||||
// This is useful for models that are not thread safe and cannot run
|
||||
// multiple requests at the same time.
|
||||
type SingleThread struct {
|
||||
Base
|
||||
backendBusy sync.Mutex
|
||||
}
|
||||
|
||||
// Locking returns true if the backend needs to lock resources
|
||||
func (llm *SingleThread) Locking() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (llm *SingleThread) Lock() {
|
||||
llm.backendBusy.Lock()
|
||||
}
|
||||
|
||||
func (llm *SingleThread) Unlock() {
|
||||
llm.backendBusy.Unlock()
|
||||
}
|
||||
|
||||
func (llm *SingleThread) Busy() bool {
|
||||
r := llm.backendBusy.TryLock()
|
||||
if r {
|
||||
llm.backendBusy.Unlock()
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// backends may wish to call this to capture the gopsutil info, then enhance with additional memory usage details?
|
||||
func (llm *SingleThread) Status() (pb.StatusResponse, error) {
|
||||
mud := memoryUsage()
|
||||
|
||||
state := pb.StatusResponse_READY
|
||||
if llm.Busy() {
|
||||
state = pb.StatusResponse_BUSY
|
||||
}
|
||||
|
||||
return pb.StatusResponse{
|
||||
State: state,
|
||||
Memory: mud,
|
||||
}, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue