mirror of
https://github.com/mudler/LocalAI.git
synced 2026-07-11 16:57:40 -04:00
Importing a model from a local directory (e.g. a HuggingFace checkout or an LM Studio store) via a file:// URI produced a config whose model field kept the scheme verbatim, e.g. model: file:///Users/u/.../Qwen3-4bit. The mlx and vllm backends treat that field as a HuggingFace repo id or local path and reject the file:// form with "Repo id must be in the form 'repo_name' or 'namespace/repo_name'", so the model imported fine but failed to load (issue #7461). Add a shared LocalModelPath helper that reduces a file:// URI to the bare filesystem path it points at and leaves HuggingFace/HTTP URIs untouched, and route the mlx, vllm, transformers and diffusers importers (all of which pass details.URI straight into the model field for from_pretrained-style loading) through it. Cover the helper directly plus end-to-end file:// import specs for the mlx and vllm importers. Assisted-by: Claude:claude-opus-4-8 [Claude Code] Signed-off-by: Adira Denis Muhando <dennisadira@gmail.com>
126 lines
3.5 KiB
Go
126 lines
3.5 KiB
Go
package importers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/mudler/LocalAI/core/config"
|
|
"github.com/mudler/LocalAI/core/gallery"
|
|
"github.com/mudler/LocalAI/core/schema"
|
|
"go.yaml.in/yaml/v2"
|
|
)
|
|
|
|
var _ Importer = &MLXImporter{}
|
|
|
|
type MLXImporter struct{}
|
|
|
|
func (i *MLXImporter) Name() string { return "mlx" }
|
|
func (i *MLXImporter) Modality() string { return "text" }
|
|
func (i *MLXImporter) AutoDetects() bool { return true }
|
|
|
|
func (i *MLXImporter) Match(details Details) bool {
|
|
preferences, err := details.Preferences.MarshalJSON()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
preferencesMap := make(map[string]any)
|
|
err = json.Unmarshal(preferences, &preferencesMap)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
b, ok := preferencesMap["backend"].(string)
|
|
if ok && b == "mlx" || b == "mlx-vlm" {
|
|
return true
|
|
}
|
|
|
|
// All https://huggingface.co/mlx-community/*
|
|
if strings.Contains(details.URI, "mlx-community/") {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (i *MLXImporter) Import(details Details) (gallery.ModelConfig, error) {
|
|
preferences, err := details.Preferences.MarshalJSON()
|
|
if err != nil {
|
|
return gallery.ModelConfig{}, err
|
|
}
|
|
preferencesMap := make(map[string]any)
|
|
err = json.Unmarshal(preferences, &preferencesMap)
|
|
if err != nil {
|
|
return gallery.ModelConfig{}, err
|
|
}
|
|
|
|
name, ok := preferencesMap["name"].(string)
|
|
if !ok {
|
|
name = filepath.Base(details.URI)
|
|
}
|
|
|
|
description, ok := preferencesMap["description"].(string)
|
|
if !ok {
|
|
description = "Imported from " + details.URI
|
|
}
|
|
|
|
// Vision-language checkpoints (e.g. gemma-4 E4B) declare the
|
|
// "image-text-to-text" pipeline tag on HuggingFace. The text-only mlx-lm
|
|
// tokenizer does not carry their processor chat template, so routing them
|
|
// through the plain mlx backend yields degenerate looping output
|
|
// (issue #10269). Send them to the mlx-vlm backend, which applies the
|
|
// processor-aware chat template.
|
|
backend := "mlx"
|
|
if details.HuggingFace != nil && details.HuggingFace.PipelineTag == "image-text-to-text" {
|
|
backend = "mlx-vlm"
|
|
}
|
|
// An explicit backend preference always wins.
|
|
b, ok := preferencesMap["backend"].(string)
|
|
if ok {
|
|
backend = b
|
|
}
|
|
|
|
modelConfig := config.ModelConfig{
|
|
Name: name,
|
|
Description: description,
|
|
KnownUsecaseStrings: []string{config.UsecaseChat},
|
|
Backend: backend,
|
|
PredictionOptions: schema.PredictionOptions{
|
|
BasicModelRequest: schema.BasicModelRequest{
|
|
Model: LocalModelPath(details.URI),
|
|
},
|
|
},
|
|
TemplateConfig: config.TemplateConfig{
|
|
UseTokenizerTemplate: true,
|
|
},
|
|
}
|
|
|
|
// Apply per-model-family inference parameter defaults
|
|
config.ApplyInferenceDefaults(&modelConfig, details.URI)
|
|
|
|
// Auto-set tool_parser / reasoning_parser from parser_defaults.json so
|
|
// the generated YAML mirrors what the vllm importer produces. The mlx
|
|
// backends auto-detect parsers from the chat template at runtime and
|
|
// ignore these Options, but surfacing them in the config keeps the two
|
|
// paths consistent and gives users a single place to override.
|
|
if parsers := config.MatchParserDefaults(details.URI); parsers != nil {
|
|
if tp, ok := parsers["tool_parser"]; ok {
|
|
modelConfig.Options = append(modelConfig.Options, "tool_parser:"+tp)
|
|
}
|
|
if rp, ok := parsers["reasoning_parser"]; ok {
|
|
modelConfig.Options = append(modelConfig.Options, "reasoning_parser:"+rp)
|
|
}
|
|
}
|
|
|
|
data, err := yaml.Marshal(modelConfig)
|
|
if err != nil {
|
|
return gallery.ModelConfig{}, err
|
|
}
|
|
|
|
return gallery.ModelConfig{
|
|
Name: name,
|
|
Description: description,
|
|
ConfigFile: string(data),
|
|
}, nil
|
|
}
|