mirror of
https://github.com/mudler/LocalAI.git
synced 2024-12-19 12:47:54 +00:00
59ef426fbf
fix(model-list): be consistent, skip known files from listing This changeset does two things: - Removes the dependency of listing models from the OpenAI schema. - Tries to reduce confusion between ListModels() in model loader and in the service - now there is only one ListModels which is in services and does not depend anymore on the OpenAI schema - The OpenAI-schema functions were moved nearby the OpenAI specific endpoints that needs the schema - Drops the ListModel Service structure as there was no real need for it. Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
46 lines
1.5 KiB
Go
46 lines
1.5 KiB
Go
package fiberContext
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/mudler/LocalAI/core/config"
|
|
"github.com/mudler/LocalAI/core/services"
|
|
"github.com/mudler/LocalAI/pkg/model"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
// ModelFromContext returns the model from the context
|
|
// If no model is specified, it will take the first available
|
|
// Takes a model string as input which should be the one received from the user request.
|
|
// It returns the model name resolved from the context and an error if any.
|
|
func ModelFromContext(ctx *fiber.Ctx, cl *config.BackendConfigLoader, loader *model.ModelLoader, modelInput string, firstModel bool) (string, error) {
|
|
if ctx.Params("model") != "" {
|
|
modelInput = ctx.Params("model")
|
|
}
|
|
|
|
// Set model from bearer token, if available
|
|
bearer := strings.TrimLeft(ctx.Get("authorization"), "Bearer ")
|
|
bearerExists := bearer != "" && loader.ExistsInModelPath(bearer)
|
|
|
|
// If no model was specified, take the first available
|
|
if modelInput == "" && !bearerExists && firstModel {
|
|
models, _ := services.ListModels(cl, loader, "", true)
|
|
if len(models) > 0 {
|
|
modelInput = models[0]
|
|
log.Debug().Msgf("No model specified, using: %s", modelInput)
|
|
} else {
|
|
log.Debug().Msgf("No model specified, returning error")
|
|
return "", fmt.Errorf("no model specified")
|
|
}
|
|
}
|
|
|
|
// If a model is found in bearer token takes precedence
|
|
if bearerExists {
|
|
log.Debug().Msgf("Using model from bearer token: %s", bearer)
|
|
modelInput = bearer
|
|
}
|
|
return modelInput, nil
|
|
}
|