2023-05-18 13:59:03 +00:00
|
|
|
package gallery_test
|
|
|
|
|
|
|
|
import (
|
2024-04-28 21:42:46 +00:00
|
|
|
"errors"
|
2023-05-18 13:59:03 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2024-06-24 15:32:12 +00:00
|
|
|
"github.com/mudler/LocalAI/core/config"
|
|
|
|
. "github.com/mudler/LocalAI/core/gallery"
|
2023-05-18 13:59:03 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
. "github.com/onsi/gomega"
|
2023-05-20 15:03:53 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2023-05-18 13:59:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Model test", func() {
|
2024-04-28 21:42:46 +00:00
|
|
|
|
2023-05-18 13:59:03 +00:00
|
|
|
Context("Downloading", func() {
|
|
|
|
It("applies model correctly", func() {
|
|
|
|
tempdir, err := os.MkdirTemp("", "test")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tempdir)
|
2024-01-05 17:04:46 +00:00
|
|
|
c, err := ReadConfigFile(filepath.Join(os.Getenv("FIXTURES"), "gallery_simple.yaml"))
|
2023-05-18 13:59:03 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2023-06-24 06:18:17 +00:00
|
|
|
err = InstallModel(tempdir, "", c, map[string]interface{}{}, func(string, string, string, float64) {})
|
2023-05-18 13:59:03 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
for _, f := range []string{"cerebras", "cerebras-completion.tmpl", "cerebras-chat.tmpl", "cerebras.yaml"} {
|
|
|
|
_, err = os.Stat(filepath.Join(tempdir, f))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
}
|
2023-05-20 15:03:53 +00:00
|
|
|
|
|
|
|
content := map[string]interface{}{}
|
|
|
|
|
|
|
|
dat, err := os.ReadFile(filepath.Join(tempdir, "cerebras.yaml"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(dat, content)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
Expect(content["context_size"]).To(Equal(1024))
|
2023-05-18 13:59:03 +00:00
|
|
|
})
|
2023-05-20 15:03:53 +00:00
|
|
|
|
2023-06-24 06:18:17 +00:00
|
|
|
It("applies model from gallery correctly", func() {
|
|
|
|
tempdir, err := os.MkdirTemp("", "test")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tempdir)
|
|
|
|
|
|
|
|
gallery := []GalleryModel{{
|
|
|
|
Name: "bert",
|
|
|
|
URL: "https://raw.githubusercontent.com/go-skynet/model-gallery/main/bert-embeddings.yaml",
|
|
|
|
}}
|
|
|
|
out, err := yaml.Marshal(gallery)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2024-06-05 06:45:24 +00:00
|
|
|
galleryFilePath := filepath.Join(tempdir, "gallery_simple.yaml")
|
|
|
|
err = os.WriteFile(galleryFilePath, out, 0600)
|
2023-06-24 06:18:17 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2024-06-05 06:45:24 +00:00
|
|
|
Expect(filepath.IsAbs(galleryFilePath)).To(BeTrue(), galleryFilePath)
|
2024-06-24 15:32:12 +00:00
|
|
|
galleries := []config.Gallery{
|
2023-06-24 06:18:17 +00:00
|
|
|
{
|
|
|
|
Name: "test",
|
2024-06-05 06:45:24 +00:00
|
|
|
URL: "file://" + galleryFilePath,
|
2023-06-24 06:18:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
models, err := AvailableGalleryModels(galleries, tempdir)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(len(models)).To(Equal(1))
|
|
|
|
Expect(models[0].Name).To(Equal("bert"))
|
|
|
|
Expect(models[0].URL).To(Equal("https://raw.githubusercontent.com/go-skynet/model-gallery/main/bert-embeddings.yaml"))
|
|
|
|
Expect(models[0].Installed).To(BeFalse())
|
|
|
|
|
|
|
|
err = InstallModelFromGallery(galleries, "test@bert", tempdir, GalleryModel{}, func(s1, s2, s3 string, f float64) {})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
dat, err := os.ReadFile(filepath.Join(tempdir, "bert.yaml"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
content := map[string]interface{}{}
|
|
|
|
err = yaml.Unmarshal(dat, &content)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(content["backend"]).To(Equal("bert-embeddings"))
|
|
|
|
|
|
|
|
models, err = AvailableGalleryModels(galleries, tempdir)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(len(models)).To(Equal(1))
|
|
|
|
Expect(models[0].Installed).To(BeTrue())
|
2024-04-28 21:42:46 +00:00
|
|
|
|
|
|
|
// delete
|
|
|
|
err = DeleteModelFromSystem(tempdir, "bert", []string{})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
models, err = AvailableGalleryModels(galleries, tempdir)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(len(models)).To(Equal(1))
|
|
|
|
Expect(models[0].Installed).To(BeFalse())
|
|
|
|
|
|
|
|
_, err = os.Stat(filepath.Join(tempdir, "bert.yaml"))
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(errors.Is(err, os.ErrNotExist)).To(BeTrue())
|
2023-06-24 06:18:17 +00:00
|
|
|
})
|
|
|
|
|
2023-05-19 06:31:11 +00:00
|
|
|
It("renames model correctly", func() {
|
|
|
|
tempdir, err := os.MkdirTemp("", "test")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tempdir)
|
2024-01-05 17:04:46 +00:00
|
|
|
c, err := ReadConfigFile(filepath.Join(os.Getenv("FIXTURES"), "gallery_simple.yaml"))
|
2023-05-19 06:31:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2023-06-24 06:18:17 +00:00
|
|
|
err = InstallModel(tempdir, "foo", c, map[string]interface{}{}, func(string, string, string, float64) {})
|
2023-05-20 15:03:53 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
for _, f := range []string{"cerebras", "cerebras-completion.tmpl", "cerebras-chat.tmpl", "foo.yaml"} {
|
|
|
|
_, err = os.Stat(filepath.Join(tempdir, f))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("overrides parameters", func() {
|
|
|
|
tempdir, err := os.MkdirTemp("", "test")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tempdir)
|
2024-01-05 17:04:46 +00:00
|
|
|
c, err := ReadConfigFile(filepath.Join(os.Getenv("FIXTURES"), "gallery_simple.yaml"))
|
2023-05-20 15:03:53 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2023-06-24 06:18:17 +00:00
|
|
|
err = InstallModel(tempdir, "foo", c, map[string]interface{}{"backend": "foo"}, func(string, string, string, float64) {})
|
2023-05-19 06:31:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
for _, f := range []string{"cerebras", "cerebras-completion.tmpl", "cerebras-chat.tmpl", "foo.yaml"} {
|
|
|
|
_, err = os.Stat(filepath.Join(tempdir, f))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
}
|
2023-05-20 15:03:53 +00:00
|
|
|
|
|
|
|
content := map[string]interface{}{}
|
|
|
|
|
|
|
|
dat, err := os.ReadFile(filepath.Join(tempdir, "foo.yaml"))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(dat, content)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
Expect(content["backend"]).To(Equal("foo"))
|
2023-05-19 06:31:11 +00:00
|
|
|
})
|
2023-05-20 15:03:53 +00:00
|
|
|
|
2023-05-19 06:31:11 +00:00
|
|
|
It("catches path traversals", func() {
|
|
|
|
tempdir, err := os.MkdirTemp("", "test")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(tempdir)
|
2024-01-05 17:04:46 +00:00
|
|
|
c, err := ReadConfigFile(filepath.Join(os.Getenv("FIXTURES"), "gallery_simple.yaml"))
|
2023-05-19 06:31:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2023-06-24 06:18:17 +00:00
|
|
|
err = InstallModel(tempdir, "../../../foo", c, map[string]interface{}{}, func(string, string, string, float64) {})
|
2023-05-19 06:31:11 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
})
|
2023-05-18 13:59:03 +00:00
|
|
|
})
|
|
|
|
})
|