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

Method Preload

core/config/model_config_loader.go:336–427  ·  view source on GitHub ↗

Preload prepare models if they are not local but url or huggingface repositories

(modelPath string)

Source from the content-addressed store, hash-verified

334
335// Preload prepare models if they are not local but url or huggingface repositories
336func (bcl *ModelConfigLoader) Preload(modelPath string) error {
337 bcl.Lock()
338 defer bcl.Unlock()
339
340 status := func(fileName, current, total string, percent float64) {
341 utils.DisplayDownloadFunction(fileName, current, total, percent)
342 }
343
344 xlog.Info("Preloading models", "path", modelPath)
345
346 renderMode := "dark"
347 if os.Getenv("COLOR") != "" {
348 renderMode = os.Getenv("COLOR")
349 }
350
351 glamText := func(t string) {
352 out, err := glamour.Render(t, renderMode)
353 if err == nil && os.Getenv("NO_COLOR") == "" {
354 fmt.Println(out)
355 } else {
356 fmt.Println(t)
357 }
358 }
359
360 for i, config := range bcl.configs {
361
362 // Download files and verify their SHA
363 for i, file := range config.DownloadFiles {
364 xlog.Debug("Checking file exists and matches SHA", "filename", file.Filename)
365
366 if err := utils.VerifyPath(file.Filename, modelPath); err != nil {
367 return err
368 }
369 // Create file path
370 filePath := filepath.Join(modelPath, file.Filename)
371
372 if err := file.URI.DownloadFile(filePath, file.SHA256, i, len(config.DownloadFiles), status); err != nil {
373 return err
374 }
375 }
376
377 // If the model is an URL, expand it, and download the file
378 if config.IsModelURL() {
379 modelFileName := config.ModelFileName()
380 uri := downloader.URI(config.Model)
381 if uri.ResolveURL() != config.Model {
382 // check if file exists
383 if _, err := os.Stat(filepath.Join(modelPath, modelFileName)); errors.Is(err, os.ErrNotExist) {
384 err := uri.DownloadFile(filepath.Join(modelPath, modelFileName), "", 0, 0, status)
385 if err != nil {
386 return err
387 }
388 }
389
390 cc := bcl.configs[i]
391 c := &cc
392 c.PredictionOptions.Model = modelFileName
393 bcl.configs[i] = *c

Callers 12

ValidateSessionFunction · 0.80
ValidateAPIKeyFunction · 0.80
RegisterAuthRoutesFunction · 0.80
ReloadModelsEndpointFunction · 0.80
ImportModelEndpointFunction · 0.80
ImportModelMethod · 0.80
modelHandlerMethod · 0.80
ExportModelMethod · 0.80
PatchConfigMethod · 0.80
EditYAMLMethod · 0.80
NewFunction · 0.80
createAliasMethod · 0.80

Implementers 6

stubClientcore/http/endpoints/mcp/localai_assist
mockConfigLoadercore/services/jobs/dispatcher_test.go
ModelConfigLoadercore/config/model_config_loader.go
fakeClientpkg/mcp/localaitools/fakes_test.go
Clientpkg/mcp/localaitools/httpapi/client.go
Clientpkg/mcp/localaitools/inproc/client.go

Calls 13

DisplayDownloadFunctionFunction · 0.92
VerifyPathFunction · 0.92
URITypeAlias · 0.92
DownloadFileMethod · 0.80
IsModelURLMethod · 0.80
ModelFileNameMethod · 0.80
ResolveURLMethod · 0.80
IsMMProjURLMethod · 0.80
MMProjFileNameMethod · 0.80
LockMethod · 0.65
UnlockMethod · 0.65
RenderMethod · 0.45

Tested by

no test coverage detected