2023-04-11 22:02:39 +00:00
|
|
|
package api
|
2023-04-11 21:43:43 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-20 16:33:02 +00:00
|
|
|
"errors"
|
2023-04-11 21:43:43 +00:00
|
|
|
|
2023-04-19 16:43:10 +00:00
|
|
|
model "github.com/go-skynet/LocalAI/pkg/model"
|
2023-04-11 21:43:43 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
2023-05-05 13:53:57 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
2023-04-11 21:43:43 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
2023-04-21 22:44:52 +00:00
|
|
|
"github.com/rs/zerolog"
|
2023-04-20 16:33:02 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-04-11 21:43:43 +00:00
|
|
|
)
|
|
|
|
|
2023-05-16 17:32:53 +00:00
|
|
|
func App(configFile string, loader *model.ModelLoader, uploadLimitMB, threads, ctxSize int, f16 bool, debug, disableMessage bool, imageDir string) *fiber.App {
|
2023-04-21 22:44:52 +00:00
|
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
|
|
|
if debug {
|
|
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:33:02 +00:00
|
|
|
// Return errors as JSON responses
|
|
|
|
app := fiber.New(fiber.Config{
|
2023-05-12 08:04:20 +00:00
|
|
|
BodyLimit: uploadLimitMB * 1024 * 1024, // this is the default limit of 4MB
|
2023-04-21 22:44:52 +00:00
|
|
|
DisableStartupMessage: disableMessage,
|
2023-04-20 16:33:02 +00:00
|
|
|
// Override default error handler
|
|
|
|
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
|
|
|
|
// Status code defaults to 500
|
|
|
|
code := fiber.StatusInternalServerError
|
|
|
|
|
|
|
|
// Retrieve the custom status code if it's a *fiber.Error
|
|
|
|
var e *fiber.Error
|
|
|
|
if errors.As(err, &e) {
|
|
|
|
code = e.Code
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send custom error page
|
2023-04-24 21:42:03 +00:00
|
|
|
return ctx.Status(code).JSON(
|
|
|
|
ErrorResponse{
|
|
|
|
Error: &APIError{Message: err.Error(), Code: code},
|
|
|
|
},
|
|
|
|
)
|
2023-04-20 16:33:02 +00:00
|
|
|
},
|
2023-04-11 21:43:43 +00:00
|
|
|
})
|
|
|
|
|
2023-05-05 13:53:57 +00:00
|
|
|
if debug {
|
|
|
|
app.Use(logger.New(logger.Config{
|
|
|
|
Format: "[${ip}]:${port} ${status} - ${method} ${path}\n",
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2023-04-27 04:18:18 +00:00
|
|
|
cm := make(ConfigMerger)
|
|
|
|
if err := cm.LoadConfigs(loader.ModelPath); err != nil {
|
|
|
|
log.Error().Msgf("error loading config files: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if configFile != "" {
|
|
|
|
if err := cm.LoadConfigFile(configFile); err != nil {
|
|
|
|
log.Error().Msgf("error loading config file: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
for k, v := range cm {
|
|
|
|
log.Debug().Msgf("Model: %s (config: %+v)", k, v)
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 16:33:02 +00:00
|
|
|
// Default middleware config
|
|
|
|
app.Use(recover.New())
|
|
|
|
app.Use(cors.New())
|
|
|
|
|
|
|
|
// openAI compatible API endpoint
|
2023-04-29 07:22:09 +00:00
|
|
|
app.Post("/v1/chat/completions", chatEndpoint(cm, debug, loader, threads, ctxSize, f16))
|
|
|
|
app.Post("/chat/completions", chatEndpoint(cm, debug, loader, threads, ctxSize, f16))
|
2023-04-20 16:33:02 +00:00
|
|
|
|
2023-04-29 07:22:09 +00:00
|
|
|
app.Post("/v1/edits", editEndpoint(cm, debug, loader, threads, ctxSize, f16))
|
|
|
|
app.Post("/edits", editEndpoint(cm, debug, loader, threads, ctxSize, f16))
|
|
|
|
|
|
|
|
app.Post("/v1/completions", completionEndpoint(cm, debug, loader, threads, ctxSize, f16))
|
|
|
|
app.Post("/completions", completionEndpoint(cm, debug, loader, threads, ctxSize, f16))
|
2023-04-20 16:33:02 +00:00
|
|
|
|
2023-05-05 09:20:06 +00:00
|
|
|
app.Post("/v1/embeddings", embeddingsEndpoint(cm, debug, loader, threads, ctxSize, f16))
|
|
|
|
app.Post("/embeddings", embeddingsEndpoint(cm, debug, loader, threads, ctxSize, f16))
|
|
|
|
|
2023-05-05 13:55:19 +00:00
|
|
|
// /v1/engines/{engine_id}/embeddings
|
|
|
|
|
|
|
|
app.Post("/v1/engines/:model/embeddings", embeddingsEndpoint(cm, debug, loader, threads, ctxSize, f16))
|
|
|
|
|
2023-05-09 09:43:50 +00:00
|
|
|
app.Post("/v1/audio/transcriptions", transcriptEndpoint(cm, debug, loader, threads, ctxSize, f16))
|
|
|
|
|
2023-05-16 17:32:53 +00:00
|
|
|
app.Post("/v1/images/generations", imageEndpoint(cm, debug, loader, imageDir))
|
|
|
|
|
|
|
|
if imageDir != "" {
|
|
|
|
app.Static("/generated-images", imageDir)
|
|
|
|
}
|
|
|
|
|
2023-04-27 04:18:18 +00:00
|
|
|
app.Get("/v1/models", listModels(loader, cm))
|
|
|
|
app.Get("/models", listModels(loader, cm))
|
2023-04-20 16:33:02 +00:00
|
|
|
|
2023-04-21 22:44:52 +00:00
|
|
|
return app
|
2023-04-11 21:43:43 +00:00
|
|
|
}
|