|
| 1 | +package importers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + . "github.com/onsi/ginkgo/v2" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + |
| 11 | + "github.com/mudler/LocalAI/core/gallery/importers" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = Describe("ImportLocalPath", func() { |
| 15 | + var tmpDir string |
| 16 | + |
| 17 | + BeforeEach(func() { |
| 18 | + var err error |
| 19 | + tmpDir, err = os.MkdirTemp("", "importers-local-test") |
| 20 | + Expect(err).ToNot(HaveOccurred()) |
| 21 | + }) |
| 22 | + |
| 23 | + AfterEach(func() { |
| 24 | + os.RemoveAll(tmpDir) |
| 25 | + }) |
| 26 | + |
| 27 | + Context("GGUF detection", func() { |
| 28 | + It("detects a GGUF file in the directory", func() { |
| 29 | + modelDir := filepath.Join(tmpDir, "my-model") |
| 30 | + Expect(os.MkdirAll(modelDir, 0755)).To(Succeed()) |
| 31 | + Expect(os.WriteFile(filepath.Join(modelDir, "model-q4_k_m.gguf"), []byte("fake"), 0644)).To(Succeed()) |
| 32 | + |
| 33 | + cfg, err := importers.ImportLocalPath(modelDir, "my-model") |
| 34 | + Expect(err).ToNot(HaveOccurred()) |
| 35 | + Expect(cfg.Backend).To(Equal("llama-cpp")) |
| 36 | + Expect(cfg.Model).To(ContainSubstring(".gguf")) |
| 37 | + Expect(cfg.TemplateConfig.UseTokenizerTemplate).To(BeTrue()) |
| 38 | + Expect(cfg.KnownUsecaseStrings).To(ContainElement("chat")) |
| 39 | + Expect(cfg.Options).To(ContainElement("use_jinja:true")) |
| 40 | + }) |
| 41 | + |
| 42 | + It("detects GGUF in _gguf subdirectory", func() { |
| 43 | + modelDir := filepath.Join(tmpDir, "my-model") |
| 44 | + ggufDir := modelDir + "_gguf" |
| 45 | + Expect(os.MkdirAll(modelDir, 0755)).To(Succeed()) |
| 46 | + Expect(os.MkdirAll(ggufDir, 0755)).To(Succeed()) |
| 47 | + Expect(os.WriteFile(filepath.Join(ggufDir, "model.gguf"), []byte("fake"), 0644)).To(Succeed()) |
| 48 | + |
| 49 | + cfg, err := importers.ImportLocalPath(modelDir, "my-model") |
| 50 | + Expect(err).ToNot(HaveOccurred()) |
| 51 | + Expect(cfg.Backend).To(Equal("llama-cpp")) |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + Context("LoRA adapter detection", func() { |
| 56 | + It("detects LoRA adapter via adapter_config.json", func() { |
| 57 | + modelDir := filepath.Join(tmpDir, "lora-model") |
| 58 | + Expect(os.MkdirAll(modelDir, 0755)).To(Succeed()) |
| 59 | + |
| 60 | + adapterConfig := map[string]any{ |
| 61 | + "base_model_name_or_path": "meta-llama/Llama-2-7b-hf", |
| 62 | + "peft_type": "LORA", |
| 63 | + } |
| 64 | + data, _ := json.Marshal(adapterConfig) |
| 65 | + Expect(os.WriteFile(filepath.Join(modelDir, "adapter_config.json"), data, 0644)).To(Succeed()) |
| 66 | + |
| 67 | + cfg, err := importers.ImportLocalPath(modelDir, "lora-model") |
| 68 | + Expect(err).ToNot(HaveOccurred()) |
| 69 | + Expect(cfg.Backend).To(Equal("transformers")) |
| 70 | + Expect(cfg.Model).To(Equal("meta-llama/Llama-2-7b-hf")) |
| 71 | + Expect(cfg.LLMConfig.LoraAdapter).To(Equal(modelDir)) |
| 72 | + Expect(cfg.TemplateConfig.UseTokenizerTemplate).To(BeTrue()) |
| 73 | + }) |
| 74 | + |
| 75 | + It("reads base model from export_metadata.json as fallback", func() { |
| 76 | + modelDir := filepath.Join(tmpDir, "lora-unsloth") |
| 77 | + Expect(os.MkdirAll(modelDir, 0755)).To(Succeed()) |
| 78 | + |
| 79 | + adapterConfig := map[string]any{"peft_type": "LORA"} |
| 80 | + data, _ := json.Marshal(adapterConfig) |
| 81 | + Expect(os.WriteFile(filepath.Join(modelDir, "adapter_config.json"), data, 0644)).To(Succeed()) |
| 82 | + |
| 83 | + metadata := map[string]any{"base_model": "unsloth/tinyllama-bnb-4bit"} |
| 84 | + data, _ = json.Marshal(metadata) |
| 85 | + Expect(os.WriteFile(filepath.Join(modelDir, "export_metadata.json"), data, 0644)).To(Succeed()) |
| 86 | + |
| 87 | + cfg, err := importers.ImportLocalPath(modelDir, "lora-unsloth") |
| 88 | + Expect(err).ToNot(HaveOccurred()) |
| 89 | + Expect(cfg.Model).To(Equal("unsloth/tinyllama-bnb-4bit")) |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + Context("Merged model detection", func() { |
| 94 | + It("detects merged model with safetensors + config.json", func() { |
| 95 | + modelDir := filepath.Join(tmpDir, "merged") |
| 96 | + Expect(os.MkdirAll(modelDir, 0755)).To(Succeed()) |
| 97 | + Expect(os.WriteFile(filepath.Join(modelDir, "config.json"), []byte("{}"), 0644)).To(Succeed()) |
| 98 | + Expect(os.WriteFile(filepath.Join(modelDir, "model.safetensors"), []byte("fake"), 0644)).To(Succeed()) |
| 99 | + |
| 100 | + cfg, err := importers.ImportLocalPath(modelDir, "merged") |
| 101 | + Expect(err).ToNot(HaveOccurred()) |
| 102 | + Expect(cfg.Backend).To(Equal("transformers")) |
| 103 | + Expect(cfg.Model).To(Equal(modelDir)) |
| 104 | + Expect(cfg.TemplateConfig.UseTokenizerTemplate).To(BeTrue()) |
| 105 | + }) |
| 106 | + |
| 107 | + It("detects merged model with pytorch_model files", func() { |
| 108 | + modelDir := filepath.Join(tmpDir, "merged-pt") |
| 109 | + Expect(os.MkdirAll(modelDir, 0755)).To(Succeed()) |
| 110 | + Expect(os.WriteFile(filepath.Join(modelDir, "config.json"), []byte("{}"), 0644)).To(Succeed()) |
| 111 | + Expect(os.WriteFile(filepath.Join(modelDir, "pytorch_model-00001-of-00002.bin"), []byte("fake"), 0644)).To(Succeed()) |
| 112 | + |
| 113 | + cfg, err := importers.ImportLocalPath(modelDir, "merged-pt") |
| 114 | + Expect(err).ToNot(HaveOccurred()) |
| 115 | + Expect(cfg.Backend).To(Equal("transformers")) |
| 116 | + Expect(cfg.Model).To(Equal(modelDir)) |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + Context("fallback", func() { |
| 121 | + It("returns error for empty directory", func() { |
| 122 | + modelDir := filepath.Join(tmpDir, "empty") |
| 123 | + Expect(os.MkdirAll(modelDir, 0755)).To(Succeed()) |
| 124 | + |
| 125 | + _, err := importers.ImportLocalPath(modelDir, "empty") |
| 126 | + Expect(err).To(HaveOccurred()) |
| 127 | + Expect(err.Error()).To(ContainSubstring("could not detect model format")) |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + Context("description", func() { |
| 132 | + It("includes base model name in description", func() { |
| 133 | + modelDir := filepath.Join(tmpDir, "desc-test") |
| 134 | + Expect(os.MkdirAll(modelDir, 0755)).To(Succeed()) |
| 135 | + |
| 136 | + adapterConfig := map[string]any{ |
| 137 | + "base_model_name_or_path": "TinyLlama/TinyLlama-1.1B", |
| 138 | + } |
| 139 | + data, _ := json.Marshal(adapterConfig) |
| 140 | + Expect(os.WriteFile(filepath.Join(modelDir, "adapter_config.json"), data, 0644)).To(Succeed()) |
| 141 | + |
| 142 | + cfg, err := importers.ImportLocalPath(modelDir, "desc-test") |
| 143 | + Expect(err).ToNot(HaveOccurred()) |
| 144 | + Expect(cfg.Description).To(ContainSubstring("TinyLlama/TinyLlama-1.1B")) |
| 145 | + Expect(cfg.Description).To(ContainSubstring("Fine-tuned from")) |
| 146 | + }) |
| 147 | + }) |
| 148 | +}) |
0 commit comments