LocalAI/core/http/endpoints/localai/stores.go
Ettore Di Giacinto a1d5462ad0 Stores to chromem (WIP)
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
2025-01-21 10:35:01 +01:00

90 lines
1.9 KiB
Go

package localai
import (
"github.com/gofiber/fiber/v2"
"github.com/mudler/LocalAI/core/backend"
"github.com/mudler/LocalAI/core/config"
"github.com/mudler/LocalAI/core/schema"
"github.com/mudler/LocalAI/pkg/model"
"github.com/mudler/LocalAI/pkg/store"
)
func StoresSetEndpoint(sl *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
input := new(schema.StoresSet)
if err := c.BodyParser(input); err != nil {
return err
}
sb, err := backend.StoreBackend(sl, appConfig, input.Store)
if err != nil {
return err
}
vals := make([][]byte, len(input.Values))
for i, v := range input.Values {
vals[i] = []byte(v)
}
err = store.SetCols(c.Context(), sb, input.Keys, vals)
if err != nil {
return err
}
return c.Send(nil)
}
}
func StoresResetEndpoint(sl *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
input := new(schema.StoresReset)
if err := c.BodyParser(input); err != nil {
return err
}
sb, err := backend.StoreBackend(sl, appConfig, input.Store)
if err != nil {
return err
}
if _, err := sb.StoresReset(c.Context(), nil); err != nil {
return err
}
return c.Send(nil)
}
}
func StoresFindEndpoint(sl *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
input := new(schema.StoresFind)
if err := c.BodyParser(input); err != nil {
return err
}
sb, err := backend.StoreBackend(sl, appConfig, input.Store)
if err != nil {
return err
}
keys, vals, similarities, err := store.Find(c.Context(), sb, input.Key, input.Topk)
if err != nil {
return err
}
res := schema.StoresFindResponse{
Keys: keys,
Values: make([]string, len(vals)),
Similarities: similarities,
}
for i, v := range vals {
res.Values[i] = string(v)
}
return c.JSON(res)
}
}