2023-04-27 04:18:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-05-05 20:51:20 +00:00
|
|
|
"encoding/json"
|
2023-04-27 04:18:18 +00:00
|
|
|
"fmt"
|
2023-06-01 14:20:45 +00:00
|
|
|
"io/fs"
|
2023-04-27 04:18:18 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2023-05-18 13:59:03 +00:00
|
|
|
"sync"
|
2023-04-27 04:18:18 +00:00
|
|
|
|
2023-05-05 20:51:20 +00:00
|
|
|
model "github.com/go-skynet/LocalAI/pkg/model"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/rs/zerolog/log"
|
2023-04-27 04:18:18 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2023-06-07 19:46:19 +00:00
|
|
|
OpenAIRequest `yaml:"parameters"`
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
StopWords []string `yaml:"stopwords"`
|
|
|
|
Cutstrings []string `yaml:"cutstrings"`
|
|
|
|
TrimSpace []string `yaml:"trimspace"`
|
|
|
|
ContextSize int `yaml:"context_size"`
|
|
|
|
F16 bool `yaml:"f16"`
|
|
|
|
Threads int `yaml:"threads"`
|
|
|
|
Debug bool `yaml:"debug"`
|
|
|
|
Roles map[string]string `yaml:"roles"`
|
|
|
|
Embeddings bool `yaml:"embeddings"`
|
|
|
|
Backend string `yaml:"backend"`
|
|
|
|
TemplateConfig TemplateConfig `yaml:"template"`
|
|
|
|
MirostatETA float64 `yaml:"mirostat_eta"`
|
|
|
|
MirostatTAU float64 `yaml:"mirostat_tau"`
|
|
|
|
Mirostat int `yaml:"mirostat"`
|
|
|
|
NGPULayers int `yaml:"gpu_layers"`
|
|
|
|
MMap bool `yaml:"mmap"`
|
|
|
|
MMlock bool `yaml:"mmlock"`
|
|
|
|
|
|
|
|
TensorSplit string `yaml:"tensor_split"`
|
|
|
|
MainGPU string `yaml:"main_gpu"`
|
|
|
|
ImageGenerationAssets string `yaml:"asset_dir"`
|
2023-05-27 12:29:11 +00:00
|
|
|
|
|
|
|
PromptCachePath string `yaml:"prompt_cache_path"`
|
|
|
|
PromptCacheAll bool `yaml:"prompt_cache_all"`
|
2023-06-07 19:46:19 +00:00
|
|
|
PromptCacheRO bool `yaml:"prompt_cache_ro"`
|
2023-05-27 12:29:11 +00:00
|
|
|
|
2023-05-05 13:54:59 +00:00
|
|
|
PromptStrings, InputStrings []string
|
2023-05-08 17:31:18 +00:00
|
|
|
InputToken [][]int
|
2023-04-27 04:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TemplateConfig struct {
|
|
|
|
Completion string `yaml:"completion"`
|
|
|
|
Chat string `yaml:"chat"`
|
2023-04-29 07:22:09 +00:00
|
|
|
Edit string `yaml:"edit"`
|
2023-04-27 04:18:18 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 13:59:03 +00:00
|
|
|
type ConfigMerger struct {
|
|
|
|
configs map[string]Config
|
|
|
|
sync.Mutex
|
|
|
|
}
|
2023-04-27 04:18:18 +00:00
|
|
|
|
2023-06-07 19:46:19 +00:00
|
|
|
func defaultConfig(modelFile string) *Config {
|
|
|
|
return &Config{
|
|
|
|
OpenAIRequest: defaultRequest(modelFile),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:59:03 +00:00
|
|
|
func NewConfigMerger() *ConfigMerger {
|
|
|
|
return &ConfigMerger{
|
|
|
|
configs: make(map[string]Config),
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 04:18:18 +00:00
|
|
|
func ReadConfigFile(file string) ([]*Config, error) {
|
|
|
|
c := &[]*Config{}
|
|
|
|
f, err := os.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot read config file: %w", err)
|
|
|
|
}
|
|
|
|
if err := yaml.Unmarshal(f, c); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot unmarshal config file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return *c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadConfig(file string) (*Config, error) {
|
|
|
|
c := &Config{}
|
|
|
|
f, err := os.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot read config file: %w", err)
|
|
|
|
}
|
|
|
|
if err := yaml.Unmarshal(f, c); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot unmarshal config file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm ConfigMerger) LoadConfigFile(file string) error {
|
2023-05-18 13:59:03 +00:00
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
2023-04-27 04:18:18 +00:00
|
|
|
c, err := ReadConfigFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot load config file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cc := range c {
|
2023-05-18 13:59:03 +00:00
|
|
|
cm.configs[cc.Name] = *cc
|
2023-04-27 04:18:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm ConfigMerger) LoadConfig(file string) error {
|
2023-05-18 13:59:03 +00:00
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
2023-04-27 04:18:18 +00:00
|
|
|
c, err := ReadConfig(file)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot read config file: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:59:03 +00:00
|
|
|
cm.configs[c.Name] = *c
|
2023-04-27 04:18:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:59:03 +00:00
|
|
|
func (cm ConfigMerger) GetConfig(m string) (Config, bool) {
|
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
|
|
|
v, exists := cm.configs[m]
|
|
|
|
return v, exists
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm ConfigMerger) ListConfigs() []string {
|
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
|
|
|
var res []string
|
|
|
|
for k := range cm.configs {
|
|
|
|
res = append(res, k)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-04-27 04:18:18 +00:00
|
|
|
func (cm ConfigMerger) LoadConfigs(path string) error {
|
2023-05-18 13:59:03 +00:00
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
2023-06-01 14:20:45 +00:00
|
|
|
entries, err := os.ReadDir(path)
|
2023-04-27 04:18:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-01 14:20:45 +00:00
|
|
|
files := make([]fs.FileInfo, 0, len(entries))
|
|
|
|
for _, entry := range entries {
|
|
|
|
info, err := entry.Info()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
files = append(files, info)
|
|
|
|
}
|
2023-04-27 04:18:18 +00:00
|
|
|
for _, file := range files {
|
|
|
|
// Skip templates, YAML and .keep files
|
|
|
|
if !strings.Contains(file.Name(), ".yaml") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c, err := ReadConfig(filepath.Join(path, file.Name()))
|
|
|
|
if err == nil {
|
2023-05-18 13:59:03 +00:00
|
|
|
cm.configs[c.Name] = *c
|
2023-04-27 04:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-05 20:51:20 +00:00
|
|
|
|
|
|
|
func updateConfig(config *Config, input *OpenAIRequest) {
|
|
|
|
if input.Echo {
|
|
|
|
config.Echo = input.Echo
|
|
|
|
}
|
|
|
|
if input.TopK != 0 {
|
|
|
|
config.TopK = input.TopK
|
|
|
|
}
|
|
|
|
if input.TopP != 0 {
|
|
|
|
config.TopP = input.TopP
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.Temperature != 0 {
|
|
|
|
config.Temperature = input.Temperature
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.Maxtokens != 0 {
|
|
|
|
config.Maxtokens = input.Maxtokens
|
|
|
|
}
|
|
|
|
|
|
|
|
switch stop := input.Stop.(type) {
|
|
|
|
case string:
|
|
|
|
if stop != "" {
|
|
|
|
config.StopWords = append(config.StopWords, stop)
|
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
for _, pp := range stop {
|
|
|
|
if s, ok := pp.(string); ok {
|
|
|
|
config.StopWords = append(config.StopWords, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.RepeatPenalty != 0 {
|
|
|
|
config.RepeatPenalty = input.RepeatPenalty
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.Keep != 0 {
|
|
|
|
config.Keep = input.Keep
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.Batch != 0 {
|
|
|
|
config.Batch = input.Batch
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.F16 {
|
|
|
|
config.F16 = input.F16
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.IgnoreEOS {
|
|
|
|
config.IgnoreEOS = input.IgnoreEOS
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.Seed != 0 {
|
|
|
|
config.Seed = input.Seed
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.Mirostat != 0 {
|
|
|
|
config.Mirostat = input.Mirostat
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.MirostatETA != 0 {
|
|
|
|
config.MirostatETA = input.MirostatETA
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.MirostatTAU != 0 {
|
|
|
|
config.MirostatTAU = input.MirostatTAU
|
|
|
|
}
|
|
|
|
|
2023-06-14 17:33:20 +00:00
|
|
|
if input.TypicalP != 0 {
|
|
|
|
config.TypicalP = input.TypicalP
|
|
|
|
}
|
|
|
|
|
2023-05-05 20:51:20 +00:00
|
|
|
switch inputs := input.Input.(type) {
|
|
|
|
case string:
|
|
|
|
if inputs != "" {
|
|
|
|
config.InputStrings = append(config.InputStrings, inputs)
|
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
for _, pp := range inputs {
|
2023-05-08 17:31:18 +00:00
|
|
|
switch i := pp.(type) {
|
|
|
|
case string:
|
|
|
|
config.InputStrings = append(config.InputStrings, i)
|
|
|
|
case []interface{}:
|
|
|
|
tokens := []int{}
|
|
|
|
for _, ii := range i {
|
|
|
|
tokens = append(tokens, int(ii.(float64)))
|
|
|
|
}
|
|
|
|
config.InputToken = append(config.InputToken, tokens)
|
2023-05-05 20:51:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch p := input.Prompt.(type) {
|
|
|
|
case string:
|
|
|
|
config.PromptStrings = append(config.PromptStrings, p)
|
|
|
|
case []interface{}:
|
|
|
|
for _, pp := range p {
|
|
|
|
if s, ok := pp.(string); ok {
|
|
|
|
config.PromptStrings = append(config.PromptStrings, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-16 17:32:53 +00:00
|
|
|
func readInput(c *fiber.Ctx, loader *model.ModelLoader, randomModel bool) (string, *OpenAIRequest, error) {
|
2023-05-05 20:51:20 +00:00
|
|
|
input := new(OpenAIRequest)
|
|
|
|
// Get input data from the request body
|
|
|
|
if err := c.BodyParser(input); err != nil {
|
2023-05-16 17:32:53 +00:00
|
|
|
return "", nil, err
|
2023-05-05 20:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
modelFile := input.Model
|
|
|
|
|
|
|
|
if c.Params("model") != "" {
|
|
|
|
modelFile = c.Params("model")
|
|
|
|
}
|
|
|
|
|
|
|
|
received, _ := json.Marshal(input)
|
|
|
|
|
|
|
|
log.Debug().Msgf("Request received: %s", string(received))
|
|
|
|
|
|
|
|
// Set model from bearer token, if available
|
|
|
|
bearer := strings.TrimLeft(c.Get("authorization"), "Bearer ")
|
|
|
|
bearerExists := bearer != "" && loader.ExistsInModelPath(bearer)
|
|
|
|
|
|
|
|
// If no model was specified, take the first available
|
2023-05-16 17:32:53 +00:00
|
|
|
if modelFile == "" && !bearerExists && randomModel {
|
2023-05-05 20:51:20 +00:00
|
|
|
models, _ := loader.ListModels()
|
|
|
|
if len(models) > 0 {
|
|
|
|
modelFile = models[0]
|
|
|
|
log.Debug().Msgf("No model specified, using: %s", modelFile)
|
|
|
|
} else {
|
|
|
|
log.Debug().Msgf("No model specified, returning error")
|
2023-05-16 17:32:53 +00:00
|
|
|
return "", nil, fmt.Errorf("no model specified")
|
2023-05-05 20:51:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a model is found in bearer token takes precedence
|
|
|
|
if bearerExists {
|
|
|
|
log.Debug().Msgf("Using model from bearer token: %s", bearer)
|
|
|
|
modelFile = bearer
|
|
|
|
}
|
2023-05-16 17:32:53 +00:00
|
|
|
return modelFile, input, nil
|
|
|
|
}
|
2023-05-05 20:51:20 +00:00
|
|
|
|
2023-05-18 13:59:03 +00:00
|
|
|
func readConfig(modelFile string, input *OpenAIRequest, cm *ConfigMerger, loader *model.ModelLoader, debug bool, threads, ctx int, f16 bool) (*Config, *OpenAIRequest, error) {
|
2023-05-05 20:51:20 +00:00
|
|
|
// Load a config file if present after the model name
|
|
|
|
modelConfig := filepath.Join(loader.ModelPath, modelFile+".yaml")
|
|
|
|
if _, err := os.Stat(modelConfig); err == nil {
|
|
|
|
if err := cm.LoadConfig(modelConfig); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed loading model config (%s) %s", modelConfig, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var config *Config
|
2023-05-18 13:59:03 +00:00
|
|
|
cfg, exists := cm.GetConfig(modelFile)
|
2023-05-05 20:51:20 +00:00
|
|
|
if !exists {
|
2023-06-07 19:46:19 +00:00
|
|
|
config = defaultConfig(modelFile)
|
|
|
|
config.ContextSize = ctx
|
|
|
|
config.Threads = threads
|
|
|
|
config.F16 = f16
|
|
|
|
config.Debug = debug
|
2023-05-05 20:51:20 +00:00
|
|
|
} else {
|
|
|
|
config = &cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the parameters for the language model prediction
|
|
|
|
updateConfig(config, input)
|
|
|
|
|
|
|
|
// Don't allow 0 as setting
|
|
|
|
if config.Threads == 0 {
|
|
|
|
if threads != 0 {
|
|
|
|
config.Threads = threads
|
|
|
|
} else {
|
|
|
|
config.Threads = 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 14:34:16 +00:00
|
|
|
// Enforce debug flag if passed from CLI
|
|
|
|
if debug {
|
|
|
|
config.Debug = true
|
|
|
|
}
|
|
|
|
|
2023-05-05 20:51:20 +00:00
|
|
|
return config, input, nil
|
|
|
|
}
|