feat: cleanups, small enhancements

Signed-off-by: mudler <mudler@localai.io>
renovate/github.com-imdario-mergo-1.x
mudler 2 years ago
parent 6d19a8bdb5
commit b722e7eb7e
  1. 3
      api/api.go
  2. 10
      api/openai.go
  3. 16
      api/options.go
  4. 2
      internal/version.go
  5. 14
      main.go
  6. 5
      pkg/gallery/gallery.go

@ -51,6 +51,9 @@ func App(opts ...AppOption) (*fiber.App, error) {
})) }))
} }
log.Info().Msgf("Starting LocalAI using %d threads, with models path: %s", options.threads, options.loader.ModelPath)
log.Info().Msgf("LocalAI version: %s", internal.PrintableVersion())
cm := NewConfigMerger() cm := NewConfigMerger()
if err := cm.LoadConfigs(options.loader.ModelPath); err != nil { if err := cm.LoadConfigs(options.loader.ModelPath); err != nil {
log.Error().Msgf("error loading config files: %s", err.Error()) log.Error().Msgf("error loading config files: %s", err.Error())

@ -214,7 +214,7 @@ func completionEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error {
if input.Stream { if input.Stream {
if len(config.PromptStrings) > 1 { if len(config.PromptStrings) > 1 {
return errors.New("cannot handle more than 1 `PromptStrings` when `Stream`ing") return errors.New("cannot handle more than 1 `PromptStrings` when Streaming")
} }
predInput := config.PromptStrings[0] predInput := config.PromptStrings[0]
@ -222,7 +222,9 @@ func completionEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error {
// A model can have a "file.bin.tmpl" file associated with a prompt template prefix // A model can have a "file.bin.tmpl" file associated with a prompt template prefix
templatedInput, err := o.loader.TemplatePrefix(templateFile, struct { templatedInput, err := o.loader.TemplatePrefix(templateFile, struct {
Input string Input string
}{Input: predInput}) }{
Input: predInput,
})
if err == nil { if err == nil {
predInput = templatedInput predInput = templatedInput
log.Debug().Msgf("Template found, input modified to: %s", predInput) log.Debug().Msgf("Template found, input modified to: %s", predInput)
@ -268,7 +270,9 @@ func completionEndpoint(cm *ConfigMerger, o *Option) func(c *fiber.Ctx) error {
// A model can have a "file.bin.tmpl" file associated with a prompt template prefix // A model can have a "file.bin.tmpl" file associated with a prompt template prefix
templatedInput, err := o.loader.TemplatePrefix(templateFile, struct { templatedInput, err := o.loader.TemplatePrefix(templateFile, struct {
Input string Input string
}{Input: i}) }{
Input: i,
})
if err == nil { if err == nil {
i = templatedInput i = templatedInput
log.Debug().Msgf("Template found, input modified to: %s", i) log.Debug().Msgf("Template found, input modified to: %s", i)

@ -3,9 +3,11 @@ package api
import ( import (
"context" "context"
"embed" "embed"
"encoding/json"
"github.com/go-skynet/LocalAI/pkg/gallery" "github.com/go-skynet/LocalAI/pkg/gallery"
model "github.com/go-skynet/LocalAI/pkg/model" model "github.com/go-skynet/LocalAI/pkg/model"
"github.com/rs/zerolog/log"
) )
type Option struct { type Option struct {
@ -69,6 +71,20 @@ func WithBackendAssets(f embed.FS) AppOption {
} }
} }
func WithStringGalleries(galls string) AppOption {
return func(o *Option) {
if galls == "" {
log.Debug().Msgf("no galleries to load")
return
}
var galleries []gallery.Gallery
if err := json.Unmarshal([]byte(galls), &galleries); err != nil {
log.Error().Msgf("failed loading galleries: %s", err.Error())
}
o.galleries = append(o.galleries, galleries...)
}
}
func WithGalleries(galleries []gallery.Gallery) AppOption { func WithGalleries(galleries []gallery.Gallery) AppOption {
return func(o *Option) { return func(o *Option) {
o.galleries = append(o.galleries, galleries...) o.galleries = append(o.galleries, galleries...)

@ -6,5 +6,5 @@ var Version = ""
var Commit = "" var Commit = ""
func PrintableVersion() string { func PrintableVersion() string {
return fmt.Sprintf("LocalAI %s (%s)", Version, Commit) return fmt.Sprintf("%s (%s)", Version, Commit)
} }

@ -1,14 +1,11 @@
package main package main
import ( import (
"encoding/json"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
api "github.com/go-skynet/LocalAI/api" api "github.com/go-skynet/LocalAI/api"
"github.com/go-skynet/LocalAI/internal" "github.com/go-skynet/LocalAI/internal"
"github.com/go-skynet/LocalAI/pkg/gallery"
model "github.com/go-skynet/LocalAI/pkg/model" model "github.com/go-skynet/LocalAI/pkg/model"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -126,19 +123,13 @@ Some of the models compatible are:
- Alpaca - Alpaca
- StableLM (ggml quantized) - StableLM (ggml quantized)
It uses llama.cpp, ggml and gpt4all as backend with golang c bindings. For a list of compatible model, check out: https://localai.io/model-compatibility/index.html
`, `,
UsageText: `local-ai [options]`, UsageText: `local-ai [options]`,
Copyright: "go-skynet authors", Copyright: "Ettore Di Giacinto",
Action: func(ctx *cli.Context) error { Action: func(ctx *cli.Context) error {
fmt.Printf("Starting LocalAI using %d threads, with models path: %s\n", ctx.Int("threads"), ctx.String("models-path"))
galls := ctx.String("galleries")
var galleries []gallery.Gallery
err := json.Unmarshal([]byte(galls), &galleries)
fmt.Println(err)
app, err := api.App( app, err := api.App(
api.WithConfigFile(ctx.String("config-file")), api.WithConfigFile(ctx.String("config-file")),
api.WithGalleries(galleries),
api.WithJSONStringPreload(ctx.String("preload-models")), api.WithJSONStringPreload(ctx.String("preload-models")),
api.WithYAMLConfigPreload(ctx.String("preload-models-config")), api.WithYAMLConfigPreload(ctx.String("preload-models-config")),
api.WithModelLoader(model.NewModelLoader(ctx.String("models-path"))), api.WithModelLoader(model.NewModelLoader(ctx.String("models-path"))),
@ -147,6 +138,7 @@ It uses llama.cpp, ggml and gpt4all as backend with golang c bindings.
api.WithImageDir(ctx.String("image-path")), api.WithImageDir(ctx.String("image-path")),
api.WithAudioDir(ctx.String("audio-path")), api.WithAudioDir(ctx.String("audio-path")),
api.WithF16(ctx.Bool("f16")), api.WithF16(ctx.Bool("f16")),
api.WithStringGalleries(ctx.String("galleries")),
api.WithDisableMessage(false), api.WithDisableMessage(false),
api.WithCors(ctx.Bool("cors")), api.WithCors(ctx.Bool("cors")),
api.WithCorsAllowOrigins(ctx.String("cors-allow-origins")), api.WithCorsAllowOrigins(ctx.String("cors-allow-origins")),

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/go-skynet/LocalAI/pkg/utils" "github.com/go-skynet/LocalAI/pkg/utils"
"github.com/imdario/mergo" "github.com/imdario/mergo"
@ -17,6 +18,10 @@ type Gallery struct {
// Installs a model from the gallery (galleryname@modelname) // Installs a model from the gallery (galleryname@modelname)
func InstallModelFromGallery(galleries []Gallery, name string, basePath string, req GalleryModel, downloadStatus func(string, string, string, float64)) error { func InstallModelFromGallery(galleries []Gallery, name string, basePath string, req GalleryModel, downloadStatus func(string, string, string, float64)) error {
// os.PathSeparator is not allowed in model names. Replace them with "__" to avoid conflicts with file paths.
name = strings.ReplaceAll(name, string(os.PathSeparator), "__")
models, err := AvailableGalleryModels(galleries, basePath) models, err := AvailableGalleryModels(galleries, basePath)
if err != nil { if err != nil {
return err return err

Loading…
Cancel
Save