MCPcopy
hub / github.com/mudler/LocalAI / InstallModels

Function InstallModels

core/startup/model_preload.go:24–83  ·  view source on GitHub ↗

InstallModels will preload models from the given list of URLs and galleries It will download the model if it is not already present in the model path It will also try to resolve if the model is an embedded model YAML configuration

(ctx context.Context, galleryService *galleryop.GalleryService, galleries, backendGalleries []config.Gallery, systemState *system.SystemState, modelLoader *model.ModelLoader, enforceScan, autoloadBackendGalleries, requireBackendIntegrity bool, downloadStatus func(string, string, string, float64), models ...string)

Source from the content-addressed store, hash-verified

22// It will download the model if it is not already present in the model path
23// It will also try to resolve if the model is an embedded model YAML configuration
24func InstallModels(ctx context.Context, galleryService *galleryop.GalleryService, galleries, backendGalleries []config.Gallery, systemState *system.SystemState, modelLoader *model.ModelLoader, enforceScan, autoloadBackendGalleries, requireBackendIntegrity bool, downloadStatus func(string, string, string, float64), models ...string) error {
25 // create an error that groups all errors
26 var err error
27 for _, url := range models {
28 // Check if it's a model gallery, or print a warning
29 e, found := installModel(ctx, galleries, backendGalleries, url, systemState, modelLoader, downloadStatus, enforceScan, autoloadBackendGalleries, requireBackendIntegrity)
30 if e != nil && found {
31 xlog.Error("[startup] failed installing model", "error", err, "model", url)
32 err = errors.Join(err, e)
33 } else if !found {
34 xlog.Debug("[startup] model not found in the gallery", "model", url)
35
36 if galleryService == nil {
37 return fmt.Errorf("cannot start autoimporter, not sure how to handle this uri")
38 }
39
40 // TODO: we should just use the discoverModelConfig here and default to this.
41 modelConfig, discoverErr := importers.DiscoverModelConfig(url, json.RawMessage{})
42 if discoverErr != nil {
43 xlog.Error("[startup] failed to discover model config", "error", discoverErr, "model", url)
44 err = errors.Join(discoverErr, fmt.Errorf("failed to discover model config: %w", err))
45 continue
46 }
47
48 uuid, uuidErr := uuid.NewUUID()
49 if uuidErr != nil {
50 err = errors.Join(uuidErr, fmt.Errorf("failed to generate UUID: %w", uuidErr))
51 continue
52 }
53
54 galleryService.ModelGalleryChannel <- galleryop.ManagementOp[gallery.GalleryModel, gallery.ModelConfig]{
55 Req: gallery.GalleryModel{
56 Overrides: map[string]any{},
57 },
58 ID: uuid.String(),
59 GalleryElementName: modelConfig.Name,
60 GalleryElement: &modelConfig,
61 BackendGalleries: backendGalleries,
62 }
63
64 var status *galleryop.OpStatus
65 // wait for op to finish
66 for {
67 status = galleryService.GetStatus(uuid.String())
68 if status != nil && status.Processed {
69 break
70 }
71 time.Sleep(1 * time.Second)
72 }
73
74 if status.Error != nil {
75 xlog.Error("[startup] failed to import model", "error", status.Error, "model", modelConfig.Name, "url", url)
76 return status.Error
77 }
78
79 xlog.Info("[startup] imported model", "model", modelConfig.Name, "url", url)
80 }
81 }

Callers 2

RunMethod · 0.92

Calls 5

DiscoverModelConfigFunction · 0.92
installModelFunction · 0.85
StringMethod · 0.65
GetStatusMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected