2023-07-14 23:19:43 +00:00
|
|
|
package openai
|
|
|
|
|
|
|
|
import (
|
2023-07-31 17:14:32 +00:00
|
|
|
"regexp"
|
|
|
|
|
2024-02-21 01:21:19 +00:00
|
|
|
config "github.com/go-skynet/LocalAI/core/config"
|
|
|
|
"github.com/go-skynet/LocalAI/core/schema"
|
2024-01-05 17:04:46 +00:00
|
|
|
model "github.com/go-skynet/LocalAI/pkg/model"
|
2023-07-14 23:19:43 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
func ListModelsEndpoint(loader *model.ModelLoader, cm *config.ConfigLoader) func(ctx *fiber.Ctx) error {
|
2023-07-14 23:19:43 +00:00
|
|
|
return func(c *fiber.Ctx) error {
|
2024-01-05 17:04:46 +00:00
|
|
|
models, err := loader.ListModels()
|
2023-07-14 23:19:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var mm map[string]interface{} = map[string]interface{}{}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
dataModels := []schema.OpenAIModel{}
|
2023-07-31 17:14:32 +00:00
|
|
|
|
|
|
|
var filterFn func(name string) bool
|
|
|
|
filter := c.Query("filter")
|
|
|
|
|
|
|
|
// If filter is not specified, do not filter the list by model name
|
|
|
|
if filter == "" {
|
|
|
|
filterFn = func(_ string) bool { return true }
|
|
|
|
} else {
|
|
|
|
// If filter _IS_ specified, we compile it to a regex which is used to create the filterFn
|
|
|
|
rxp, err := regexp.Compile(filter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
filterFn = func(name string) bool {
|
|
|
|
return rxp.MatchString(name)
|
|
|
|
}
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 17:14:32 +00:00
|
|
|
// By default, exclude any loose files that are already referenced by a configuration file.
|
|
|
|
excludeConfigured := c.QueryBool("excludeConfigured", true)
|
|
|
|
|
|
|
|
// Start with the known configurations
|
2024-01-05 17:04:46 +00:00
|
|
|
for _, c := range cm.GetAllConfigs() {
|
2023-07-31 17:14:32 +00:00
|
|
|
if excludeConfigured {
|
|
|
|
mm[c.Model] = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if filterFn(c.Name) {
|
2024-01-05 17:04:46 +00:00
|
|
|
dataModels = append(dataModels, schema.OpenAIModel{ID: c.Name, Object: "model"})
|
2023-07-31 17:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then iterate through the loose files:
|
|
|
|
for _, m := range models {
|
|
|
|
// And only adds them if they shouldn't be skipped.
|
|
|
|
if _, exists := mm[m]; !exists && filterFn(m) {
|
2024-01-05 17:04:46 +00:00
|
|
|
dataModels = append(dataModels, schema.OpenAIModel{ID: m, Object: "model"})
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(struct {
|
2023-08-20 12:04:45 +00:00
|
|
|
Object string `json:"object"`
|
|
|
|
Data []schema.OpenAIModel `json:"data"`
|
2023-07-14 23:19:43 +00:00
|
|
|
}{
|
|
|
|
Object: "list",
|
2024-01-05 17:04:46 +00:00
|
|
|
Data: dataModels,
|
2023-07-14 23:19:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|