2024-03-01 15:19:53 +00:00
|
|
|
package localai
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"slices"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/google/uuid"
|
2024-06-24 15:32:12 +00:00
|
|
|
"github.com/mudler/LocalAI/core/config"
|
|
|
|
"github.com/mudler/LocalAI/core/gallery"
|
2024-07-14 03:46:42 +00:00
|
|
|
"github.com/mudler/LocalAI/core/schema"
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/core/services"
|
2024-03-01 15:19:53 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ModelGalleryEndpointService struct {
|
2024-06-24 15:32:12 +00:00
|
|
|
galleries []config.Gallery
|
2024-03-01 15:19:53 +00:00
|
|
|
modelPath string
|
|
|
|
galleryApplier *services.GalleryService
|
|
|
|
}
|
|
|
|
|
|
|
|
type GalleryModel struct {
|
2024-04-11 22:49:23 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
ConfigURL string `json:"config_url"`
|
2024-03-01 15:19:53 +00:00
|
|
|
gallery.GalleryModel
|
|
|
|
}
|
|
|
|
|
2024-06-24 15:32:12 +00:00
|
|
|
func CreateModelGalleryEndpointService(galleries []config.Gallery, modelPath string, galleryApplier *services.GalleryService) ModelGalleryEndpointService {
|
2024-03-01 15:19:53 +00:00
|
|
|
return ModelGalleryEndpointService{
|
|
|
|
galleries: galleries,
|
|
|
|
modelPath: modelPath,
|
|
|
|
galleryApplier: galleryApplier,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 09:25:21 +00:00
|
|
|
// GetOpStatusEndpoint returns the job status
|
|
|
|
// @Summary Returns the job status
|
|
|
|
// @Success 200 {object} gallery.GalleryOpStatus "Response"
|
|
|
|
// @Router /models/jobs/{uuid} [get]
|
2024-03-01 15:19:53 +00:00
|
|
|
func (mgs *ModelGalleryEndpointService) GetOpStatusEndpoint() func(c *fiber.Ctx) error {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
status := mgs.galleryApplier.GetStatus(c.Params("uuid"))
|
|
|
|
if status == nil {
|
|
|
|
return fmt.Errorf("could not find any status for ID")
|
|
|
|
}
|
|
|
|
return c.JSON(status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 09:25:21 +00:00
|
|
|
// GetAllStatusEndpoint returns all the jobs status progress
|
|
|
|
// @Summary Returns all the jobs status progress
|
|
|
|
// @Success 200 {object} map[string]gallery.GalleryOpStatus "Response"
|
|
|
|
// @Router /models/jobs [get]
|
2024-03-01 15:19:53 +00:00
|
|
|
func (mgs *ModelGalleryEndpointService) GetAllStatusEndpoint() func(c *fiber.Ctx) error {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
return c.JSON(mgs.galleryApplier.GetAllStatus())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-14 03:46:42 +00:00
|
|
|
// ApplyModelGalleryEndpoint installs a new model to a LocalAI instance from the model gallery
|
|
|
|
// @Summary Install models to LocalAI.
|
|
|
|
// @Param request body GalleryModel true "query params"
|
|
|
|
// @Success 200 {object} schema.GalleryResponse "Response"
|
|
|
|
// @Router /models/apply [post]
|
2024-03-01 15:19:53 +00:00
|
|
|
func (mgs *ModelGalleryEndpointService) ApplyModelGalleryEndpoint() func(c *fiber.Ctx) error {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
input := new(GalleryModel)
|
|
|
|
// Get input data from the request body
|
|
|
|
if err := c.BodyParser(input); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid, err := uuid.NewUUID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mgs.galleryApplier.C <- gallery.GalleryOp{
|
2024-05-06 23:17:07 +00:00
|
|
|
Req: input.GalleryModel,
|
|
|
|
Id: uuid.String(),
|
|
|
|
GalleryModelName: input.ID,
|
|
|
|
Galleries: mgs.galleries,
|
|
|
|
ConfigURL: input.ConfigURL,
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
2024-07-14 03:46:42 +00:00
|
|
|
return c.JSON(schema.GalleryResponse{ID: uuid.String(), StatusURL: c.BaseURL() + "/models/jobs/" + uuid.String()})
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-14 03:46:42 +00:00
|
|
|
// DeleteModelGalleryEndpoint lets delete models from a LocalAI instance
|
|
|
|
// @Summary delete models to LocalAI.
|
|
|
|
// @Param name path string true "Model name"
|
|
|
|
// @Success 200 {object} schema.GalleryResponse "Response"
|
|
|
|
// @Router /models/delete/{name} [post]
|
2024-04-28 21:42:46 +00:00
|
|
|
func (mgs *ModelGalleryEndpointService) DeleteModelGalleryEndpoint() func(c *fiber.Ctx) error {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
modelName := c.Params("name")
|
|
|
|
|
|
|
|
mgs.galleryApplier.C <- gallery.GalleryOp{
|
2024-05-06 23:17:07 +00:00
|
|
|
Delete: true,
|
|
|
|
GalleryModelName: modelName,
|
2024-04-28 21:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uuid, err := uuid.NewUUID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-14 03:46:42 +00:00
|
|
|
return c.JSON(schema.GalleryResponse{ID: uuid.String(), StatusURL: c.BaseURL() + "/models/jobs/" + uuid.String()})
|
2024-04-28 21:42:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-14 03:46:42 +00:00
|
|
|
// ListModelFromGalleryEndpoint list the available models for installation from the active galleries
|
|
|
|
// @Summary List installable models.
|
|
|
|
// @Success 200 {object} []gallery.GalleryModel "Response"
|
|
|
|
// @Router /models/available [get]
|
2024-03-01 15:19:53 +00:00
|
|
|
func (mgs *ModelGalleryEndpointService) ListModelFromGalleryEndpoint() func(c *fiber.Ctx) error {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
log.Debug().Msgf("Listing models from galleries: %+v", mgs.galleries)
|
|
|
|
|
|
|
|
models, err := gallery.AvailableGalleryModels(mgs.galleries, mgs.modelPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug().Msgf("Models found from galleries: %+v", models)
|
|
|
|
for _, m := range models {
|
|
|
|
log.Debug().Msgf("Model found from galleries: %+v", m)
|
|
|
|
}
|
|
|
|
dat, err := json.Marshal(models)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Send(dat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-14 03:46:42 +00:00
|
|
|
// ListModelGalleriesEndpoint list the available galleries configured in LocalAI
|
|
|
|
// @Summary List all Galleries
|
|
|
|
// @Success 200 {object} []config.Gallery "Response"
|
|
|
|
// @Router /models/galleries [get]
|
2024-03-01 15:19:53 +00:00
|
|
|
// NOTE: This is different (and much simpler!) than above! This JUST lists the model galleries that have been loaded, not their contents!
|
|
|
|
func (mgs *ModelGalleryEndpointService) ListModelGalleriesEndpoint() func(c *fiber.Ctx) error {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
log.Debug().Msgf("Listing model galleries %+v", mgs.galleries)
|
|
|
|
dat, err := json.Marshal(mgs.galleries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Send(dat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-14 03:46:42 +00:00
|
|
|
// AddModelGalleryEndpoint adds a gallery in LocalAI
|
|
|
|
// @Summary Adds a gallery in LocalAI
|
|
|
|
// @Param request body config.Gallery true "Gallery details"
|
|
|
|
// @Success 200 {object} []config.Gallery "Response"
|
|
|
|
// @Router /models/galleries [post]
|
2024-03-01 15:19:53 +00:00
|
|
|
func (mgs *ModelGalleryEndpointService) AddModelGalleryEndpoint() func(c *fiber.Ctx) error {
|
|
|
|
return func(c *fiber.Ctx) error {
|
2024-06-24 15:32:12 +00:00
|
|
|
input := new(config.Gallery)
|
2024-03-01 15:19:53 +00:00
|
|
|
// Get input data from the request body
|
|
|
|
if err := c.BodyParser(input); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-24 15:32:12 +00:00
|
|
|
if slices.ContainsFunc(mgs.galleries, func(gallery config.Gallery) bool {
|
2024-03-01 15:19:53 +00:00
|
|
|
return gallery.Name == input.Name
|
|
|
|
}) {
|
|
|
|
return fmt.Errorf("%s already exists", input.Name)
|
|
|
|
}
|
|
|
|
dat, err := json.Marshal(mgs.galleries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug().Msgf("Adding %+v to gallery list", *input)
|
|
|
|
mgs.galleries = append(mgs.galleries, *input)
|
|
|
|
return c.Send(dat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-14 03:46:42 +00:00
|
|
|
// RemoveModelGalleryEndpoint remove a gallery in LocalAI
|
|
|
|
// @Summary removes a gallery from LocalAI
|
|
|
|
// @Param request body config.Gallery true "Gallery details"
|
|
|
|
// @Success 200 {object} []config.Gallery "Response"
|
|
|
|
// @Router /models/galleries [delete]
|
2024-03-01 15:19:53 +00:00
|
|
|
func (mgs *ModelGalleryEndpointService) RemoveModelGalleryEndpoint() func(c *fiber.Ctx) error {
|
|
|
|
return func(c *fiber.Ctx) error {
|
2024-06-24 15:32:12 +00:00
|
|
|
input := new(config.Gallery)
|
2024-03-01 15:19:53 +00:00
|
|
|
// Get input data from the request body
|
|
|
|
if err := c.BodyParser(input); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-24 15:32:12 +00:00
|
|
|
if !slices.ContainsFunc(mgs.galleries, func(gallery config.Gallery) bool {
|
2024-03-01 15:19:53 +00:00
|
|
|
return gallery.Name == input.Name
|
|
|
|
}) {
|
|
|
|
return fmt.Errorf("%s is not currently registered", input.Name)
|
|
|
|
}
|
2024-06-24 15:32:12 +00:00
|
|
|
mgs.galleries = slices.DeleteFunc(mgs.galleries, func(gallery config.Gallery) bool {
|
2024-03-01 15:19:53 +00:00
|
|
|
return gallery.Name == input.Name
|
|
|
|
})
|
2024-07-14 03:46:42 +00:00
|
|
|
dat, err := json.Marshal(mgs.galleries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Send(dat)
|
2024-03-01 15:19:53 +00:00
|
|
|
}
|
|
|
|
}
|