MCPcopy Index your code
hub / github.com/mudler/LocalAI / listModelFiles

Function listModelFiles

core/gallery/models.go:350–414  ·  view source on GitHub ↗
(systemState *system.SystemState, name string)

Source from the content-addressed store, hash-verified

348}
349
350func listModelFiles(systemState *system.SystemState, name string) ([]string, error) {
351
352 configFile := filepath.Join(systemState.Model.ModelsPath, fmt.Sprintf("%s.yaml", name))
353 if err := utils.VerifyPath(configFile, systemState.Model.ModelsPath); err != nil {
354 return nil, fmt.Errorf("failed to verify path %s: %w", configFile, err)
355 }
356
357 // os.PathSeparator is not allowed in model names. Replace them with "__" to avoid conflicts with file paths.
358 name = strings.ReplaceAll(name, string(os.PathSeparator), "__")
359
360 galleryFile := filepath.Join(systemState.Model.ModelsPath, galleryFileName(name))
361 if err := utils.VerifyPath(galleryFile, systemState.Model.ModelsPath); err != nil {
362 return nil, fmt.Errorf("failed to verify path %s: %w", galleryFile, err)
363 }
364
365 additionalFiles := []string{}
366 allFiles := []string{}
367
368 // Galleryname is the name of the model in this case
369 dat, err := os.ReadFile(configFile)
370 if err == nil {
371 modelConfig := &lconfig.ModelConfig{}
372
373 err = yaml.Unmarshal(dat, &modelConfig)
374 if err != nil {
375 return nil, err
376 }
377 if modelConfig.Model != "" {
378 additionalFiles = append(additionalFiles, modelConfig.ModelFileName())
379 }
380
381 if modelConfig.MMProj != "" {
382 additionalFiles = append(additionalFiles, modelConfig.MMProjFileName())
383 }
384 }
385
386 // read the model config
387 galleryconfig, err := ReadConfigFile[ModelConfig](galleryFile)
388 if err == nil && galleryconfig != nil {
389 for _, f := range galleryconfig.Files {
390 fullPath := filepath.Join(systemState.Model.ModelsPath, f.Filename)
391 if err := utils.VerifyPath(fullPath, systemState.Model.ModelsPath); err != nil {
392 return allFiles, fmt.Errorf("failed to verify path %s: %w", fullPath, err)
393 }
394 allFiles = append(allFiles, fullPath)
395 }
396 } else {
397 xlog.Error("failed to read gallery file", "error", err, "file", configFile)
398 }
399
400 for _, f := range additionalFiles {
401 fullPath := filepath.Join(filepath.Join(systemState.Model.ModelsPath, f))
402 if err := utils.VerifyPath(fullPath, systemState.Model.ModelsPath); err != nil {
403 return allFiles, fmt.Errorf("failed to verify path %s: %w", fullPath, err)
404 }
405 allFiles = append(allFiles, fullPath)
406 }
407

Callers 1

DeleteModelFromSystemFunction · 0.85

Calls 6

ModelFileNameMethod · 0.95
MMProjFileNameMethod · 0.95
VerifyPathFunction · 0.92
UniqueFunction · 0.92
galleryFileNameFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected