Skip to content

Commit

Permalink
feat(api): list loaded models in /system
Browse files Browse the repository at this point in the history
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
  • Loading branch information
mudler committed Sep 25, 2024
1 parent 33b2d38 commit 4a4c975
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 11 deletions.
2 changes: 2 additions & 0 deletions core/http/endpoints/localai/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ func SystemInformations(ml *model.ModelLoader, appConfig *config.ApplicationConf
if err != nil {
return err
}
loadedModels := ml.ListModels()
for b := range appConfig.ExternalGRPCBackends {
availableBackends = append(availableBackends, b)
}
return c.JSON(
schema.SystemInformationResponse{
Backends: availableBackends,
Models: loadedModels,
},
)
}
Expand Down
4 changes: 3 additions & 1 deletion core/schema/localai.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package schema

import (
"github.com/mudler/LocalAI/core/p2p"
"github.com/mudler/LocalAI/pkg/model"
gopsutil "github.com/shirou/gopsutil/v3/process"
)

Expand Down Expand Up @@ -72,5 +73,6 @@ type P2PNodesResponse struct {
}

type SystemInformationResponse struct {
Backends []string `json:"backends"`
Backends []string `json:"backends"`
Models []model.Model `json:"loaded_models"`
}
7 changes: 3 additions & 4 deletions pkg/model/initializers.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,11 @@ func (ml *ModelLoader) grpcModel(backend string, o *Options) func(string, string

log.Debug().Msgf("GRPC Service Started")

client = NewModel(serverAddress)
client = NewModel(modelName, serverAddress)
} else {
log.Debug().Msg("external backend is uri")
// address
client = NewModel(uri)
client = NewModel(modelName, uri)
}
} else {
grpcProcess := backendPath(o.assetDir, backend)
Expand Down Expand Up @@ -352,7 +352,7 @@ func (ml *ModelLoader) grpcModel(backend string, o *Options) func(string, string

log.Debug().Msgf("GRPC Service Started")

client = NewModel(serverAddress)
client = NewModel(modelName, serverAddress)
}

log.Debug().Msgf("Wait for the service to start up")
Expand Down Expand Up @@ -419,7 +419,6 @@ func (ml *ModelLoader) BackendLoader(opts ...Option) (client grpc.Backend, err e
err := ml.StopGRPC(allExcept(o.model))
if err != nil {
log.Error().Err(err).Str("keptModel", o.model).Msg("error while shutting down all backends except for the keptModel")
return nil, err
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/model/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ FILE:
return models, nil
}

func (ml *ModelLoader) ListModels() []*Model {
func (ml *ModelLoader) ListModels() []Model {
ml.mu.Lock()
defer ml.mu.Unlock()

models := []*Model{}
models := []Model{}
for _, model := range ml.models {
models = append(models, model)
models = append(models, *model)
}

return models
Expand Down
4 changes: 2 additions & 2 deletions pkg/model/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var _ = Describe("ModelLoader", func() {

Context("LoadModel", func() {
It("should load a model and keep it in memory", func() {
mockModel = model.NewModel("test.model")
mockModel = model.NewModel("foo", "test.model")

mockLoader := func(modelName, modelFile string) (*model.Model, error) {
return mockModel, nil
Expand All @@ -88,7 +88,7 @@ var _ = Describe("ModelLoader", func() {

Context("ShutdownModel", func() {
It("should shutdown a loaded model", func() {
mockModel = model.NewModel("test.model")
mockModel = model.NewModel("foo", "test.model")

mockLoader := func(modelName, modelFile string) (*model.Model, error) {
return mockModel, nil
Expand Down
4 changes: 3 additions & 1 deletion pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package model
import grpc "github.com/mudler/LocalAI/pkg/grpc"

type Model struct {
ID string `json:"id"`
address string
client grpc.Backend
}

func NewModel(address string) *Model {
func NewModel(ID, address string) *Model {
return &Model{
ID: ID,
address: address,
}
}
Expand Down

0 comments on commit 4a4c975

Please sign in to comment.