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

Method ImportModel

core/services/quantization/service.go:459–593  ·  view source on GitHub ↗

ImportModel imports a quantized model into LocalAI asynchronously.

(ctx context.Context, userID, jobID string, req schema.QuantizationImportRequest)

Source from the content-addressed store, hash-verified

457
458// ImportModel imports a quantized model into LocalAI asynchronously.
459func (s *QuantizationService) ImportModel(ctx context.Context, userID, jobID string, req schema.QuantizationImportRequest) (string, error) {
460 s.mu.Lock()
461 job, ok := s.jobs.Get(jobID)
462 if !ok {
463 s.mu.Unlock()
464 return "", fmt.Errorf("job not found: %s", jobID)
465 }
466 if userID != "" && job.UserID != userID {
467 s.mu.Unlock()
468 return "", fmt.Errorf("job not found: %s", jobID)
469 }
470 if job.Status != "completed" {
471 s.mu.Unlock()
472 return "", fmt.Errorf("job %s is not completed (status: %s)", jobID, job.Status)
473 }
474 if job.ImportStatus == "importing" {
475 s.mu.Unlock()
476 return "", fmt.Errorf("import already in progress for job %s", jobID)
477 }
478 if job.OutputFile == "" {
479 s.mu.Unlock()
480 return "", fmt.Errorf("no output file for job %s", jobID)
481 }
482 s.mu.Unlock()
483
484 // Compute model name
485 modelName := req.Name
486 if modelName == "" {
487 base := sanitizeQuantModelName(job.Model)
488 if base == "" {
489 base = "model"
490 }
491 shortID := jobID
492 if len(shortID) > 8 {
493 shortID = shortID[:8]
494 }
495 modelName = base + "-" + job.QuantizationType + "-" + shortID
496 }
497
498 // Compute output path in models directory
499 modelsPath := s.appConfig.SystemState.Model.ModelsPath
500 outputPath := filepath.Join(modelsPath, modelName)
501
502 // Check for name collision
503 configPath := filepath.Join(modelsPath, modelName+".yaml")
504 if err := utils.VerifyPath(modelName+".yaml", modelsPath); err != nil {
505 return "", fmt.Errorf("invalid model name: %w", err)
506 }
507 if _, err := os.Stat(configPath); err == nil {
508 return "", fmt.Errorf("model %q already exists, choose a different name", modelName)
509 }
510
511 // Create output directory
512 if err := os.MkdirAll(outputPath, 0750); err != nil {
513 return "", fmt.Errorf("failed to create output directory: %w", err)
514 }
515
516 // Set import status

Callers 1

Calls 13

saveJobStateMethod · 0.95
setImportMessageMethod · 0.95
setImportFailedMethod · 0.95
VerifyPathFunction · 0.92
ImportLocalPathFunction · 0.92
sanitizeQuantModelNameFunction · 0.85
ToConfigLoaderOptionsMethod · 0.80
PreloadMethod · 0.80
LockMethod · 0.65
GetMethod · 0.65
UnlockMethod · 0.65

Tested by

no test coverage detected