ImportModel imports a quantized model into LocalAI asynchronously.
(ctx context.Context, userID, jobID string, req schema.QuantizationImportRequest)
| 457 | |
| 458 | // ImportModel imports a quantized model into LocalAI asynchronously. |
| 459 | func (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 |
no test coverage detected