2023-05-18 13:59:03 +00:00
|
|
|
package gallery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-05-20 15:03:53 +00:00
|
|
|
"github.com/imdario/mergo"
|
2024-06-24 15:32:12 +00:00
|
|
|
lconfig "github.com/mudler/LocalAI/core/config"
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/pkg/downloader"
|
|
|
|
"github.com/mudler/LocalAI/pkg/utils"
|
2024-06-24 15:32:12 +00:00
|
|
|
|
2023-05-18 13:59:03 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
description: |
|
|
|
|
foo
|
|
|
|
license: ""
|
|
|
|
|
|
|
|
urls:
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
|
|
name: "bar"
|
|
|
|
|
|
|
|
config_file: |
|
|
|
|
# Note, name will be injected. or generated by the alias wanted by the user
|
|
|
|
threads: 14
|
|
|
|
|
|
|
|
files:
|
|
|
|
- filename: ""
|
|
|
|
sha: ""
|
|
|
|
uri: ""
|
|
|
|
|
|
|
|
prompt_templates:
|
|
|
|
- name: ""
|
|
|
|
content: ""
|
|
|
|
|
|
|
|
*/
|
2024-01-05 17:04:46 +00:00
|
|
|
// Config is the model configuration which contains all the model details
|
2023-06-25 20:51:02 +00:00
|
|
|
// This configuration is read from the gallery endpoint and is used to download and install the model
|
2024-05-06 23:17:07 +00:00
|
|
|
// It is the internal structure, separated from the request
|
2024-01-05 17:04:46 +00:00
|
|
|
type Config struct {
|
2023-05-18 13:59:03 +00:00
|
|
|
Description string `yaml:"description"`
|
2024-05-06 23:17:07 +00:00
|
|
|
Icon string `yaml:"icon"`
|
2023-05-18 13:59:03 +00:00
|
|
|
License string `yaml:"license"`
|
|
|
|
URLs []string `yaml:"urls"`
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
ConfigFile string `yaml:"config_file"`
|
|
|
|
Files []File `yaml:"files"`
|
|
|
|
PromptTemplates []PromptTemplate `yaml:"prompt_templates"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type File struct {
|
2023-05-19 06:31:11 +00:00
|
|
|
Filename string `yaml:"filename" json:"filename"`
|
|
|
|
SHA256 string `yaml:"sha256" json:"sha256"`
|
|
|
|
URI string `yaml:"uri" json:"uri"`
|
2023-05-18 13:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PromptTemplate struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Content string `yaml:"content"`
|
|
|
|
}
|
|
|
|
|
2024-06-04 14:32:47 +00:00
|
|
|
func GetGalleryConfigFromURL(url string, basePath string) (Config, error) {
|
2024-01-05 17:04:46 +00:00
|
|
|
var config Config
|
2024-06-22 06:17:41 +00:00
|
|
|
err := downloader.DownloadAndUnmarshal(url, basePath, func(url string, d []byte) error {
|
2023-06-26 10:25:38 +00:00
|
|
|
return yaml.Unmarshal(d, &config)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-04-04 07:24:22 +00:00
|
|
|
log.Error().Err(err).Str("url", url).Msg("failed to get gallery config for url")
|
2023-06-26 10:25:38 +00:00
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
func ReadConfigFile(filePath string) (*Config, error) {
|
2023-05-18 13:59:03 +00:00
|
|
|
// Read the YAML file
|
|
|
|
yamlFile, err := os.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read YAML file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal YAML data into a Config struct
|
2024-01-05 17:04:46 +00:00
|
|
|
var config Config
|
2023-05-18 13:59:03 +00:00
|
|
|
err = yaml.Unmarshal(yamlFile, &config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal YAML: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
func InstallModel(basePath, nameOverride string, config *Config, configOverrides map[string]interface{}, downloadStatus func(string, string, string, float64)) error {
|
2023-05-18 13:59:03 +00:00
|
|
|
// Create base path if it doesn't exist
|
2024-04-25 22:47:06 +00:00
|
|
|
err := os.MkdirAll(basePath, 0750)
|
2023-05-18 13:59:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create base path: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-05-20 15:03:53 +00:00
|
|
|
if len(configOverrides) > 0 {
|
|
|
|
log.Debug().Msgf("Config overrides %+v", configOverrides)
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:59:03 +00:00
|
|
|
// Download files and verify their SHA
|
2024-04-23 07:22:58 +00:00
|
|
|
for i, file := range config.Files {
|
2023-05-18 13:59:03 +00:00
|
|
|
log.Debug().Msgf("Checking %q exists and matches SHA", file.Filename)
|
|
|
|
|
2023-06-22 15:53:10 +00:00
|
|
|
if err := utils.VerifyPath(file.Filename, basePath); err != nil {
|
2023-05-19 06:31:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-05-18 13:59:03 +00:00
|
|
|
// Create file path
|
|
|
|
filePath := filepath.Join(basePath, file.Filename)
|
|
|
|
|
2024-04-23 07:22:58 +00:00
|
|
|
if err := downloader.DownloadFile(file.URI, filePath, file.SHA256, i, len(config.Files), downloadStatus); err != nil {
|
2023-12-18 17:58:44 +00:00
|
|
|
return err
|
2023-06-24 06:18:17 +00:00
|
|
|
}
|
2023-05-18 13:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write prompt template contents to separate files
|
|
|
|
for _, template := range config.PromptTemplates {
|
2023-06-22 15:53:10 +00:00
|
|
|
if err := utils.VerifyPath(template.Name+".tmpl", basePath); err != nil {
|
2023-05-19 06:31:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-05-18 13:59:03 +00:00
|
|
|
// Create file path
|
|
|
|
filePath := filepath.Join(basePath, template.Name+".tmpl")
|
|
|
|
|
|
|
|
// Create parent directory
|
2024-04-25 22:47:06 +00:00
|
|
|
err := os.MkdirAll(filepath.Dir(filePath), 0750)
|
2023-05-18 13:59:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create parent directory for prompt template %q: %v", template.Name, err)
|
|
|
|
}
|
|
|
|
// Create and write file content
|
2024-04-25 22:47:06 +00:00
|
|
|
err = os.WriteFile(filePath, []byte(template.Content), 0600)
|
2023-05-18 13:59:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write prompt template %q: %v", template.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msgf("Prompt template %q written", template.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := config.Name
|
|
|
|
if nameOverride != "" {
|
|
|
|
name = nameOverride
|
|
|
|
}
|
|
|
|
|
2023-06-22 15:53:10 +00:00
|
|
|
if err := utils.VerifyPath(name+".yaml", basePath); err != nil {
|
2023-05-19 06:31:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-24 06:18:17 +00:00
|
|
|
// write config file
|
|
|
|
if len(configOverrides) != 0 || len(config.ConfigFile) != 0 {
|
|
|
|
configFilePath := filepath.Join(basePath, name+".yaml")
|
2023-05-18 13:59:03 +00:00
|
|
|
|
2023-06-24 06:18:17 +00:00
|
|
|
// Read and update config file as map[string]interface{}
|
|
|
|
configMap := make(map[string]interface{})
|
|
|
|
err = yaml.Unmarshal([]byte(config.ConfigFile), &configMap)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal config YAML: %v", err)
|
|
|
|
}
|
2023-05-18 13:59:03 +00:00
|
|
|
|
2023-06-24 06:18:17 +00:00
|
|
|
configMap["name"] = name
|
2023-05-18 13:59:03 +00:00
|
|
|
|
2023-06-24 06:18:17 +00:00
|
|
|
if err := mergo.Merge(&configMap, configOverrides, mergo.WithOverride); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-20 15:03:53 +00:00
|
|
|
|
2023-06-24 06:18:17 +00:00
|
|
|
// Write updated config file
|
|
|
|
updatedConfigYAML, err := yaml.Marshal(configMap)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal updated config YAML: %v", err)
|
|
|
|
}
|
2023-05-18 13:59:03 +00:00
|
|
|
|
2024-06-24 15:32:12 +00:00
|
|
|
backendConfig := lconfig.BackendConfig{}
|
|
|
|
err = yaml.Unmarshal(updatedConfigYAML, &backendConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal updated config YAML: %v", err)
|
|
|
|
}
|
|
|
|
if !backendConfig.Validate() {
|
|
|
|
return fmt.Errorf("failed to validate updated config YAML")
|
|
|
|
}
|
|
|
|
|
2024-04-25 22:47:06 +00:00
|
|
|
err = os.WriteFile(configFilePath, updatedConfigYAML, 0600)
|
2023-06-24 06:18:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write updated config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msgf("Written config file %s", configFilePath)
|
2023-05-18 13:59:03 +00:00
|
|
|
}
|
|
|
|
|
2024-04-28 21:42:46 +00:00
|
|
|
// Save the model gallery file for further reference
|
|
|
|
modelFile := filepath.Join(basePath, galleryFileName(name))
|
|
|
|
data, err := yaml.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msgf("Written gallery file %s", modelFile)
|
|
|
|
|
|
|
|
return os.WriteFile(modelFile, data, 0600)
|
|
|
|
|
|
|
|
//return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func galleryFileName(name string) string {
|
|
|
|
return "._gallery_" + name + ".yaml"
|
2023-05-18 13:59:03 +00:00
|
|
|
}
|