main.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "github.com/ollama/ollama/convert"
  8. )
  9. func main() {
  10. // Check if directory path is provided
  11. if len(os.Args) != 2 {
  12. fmt.Printf("expected one argument (directory path), got %d\n", len(os.Args)-1)
  13. os.Exit(1)
  14. }
  15. dirPath := os.Args[1]
  16. if err := convertFromDirectory(dirPath); err != nil {
  17. fmt.Fprintf(os.Stderr, "error: %v\n", err)
  18. os.Exit(1)
  19. }
  20. fmt.Println("conversion completed successfully")
  21. }
  22. func convertFromDirectory(dirPath string) error {
  23. // Verify the directory exists and is accessible
  24. info, err := os.Stat(dirPath)
  25. if err != nil {
  26. if os.IsNotExist(err) {
  27. return fmt.Errorf("directory does not exist: %s", dirPath)
  28. }
  29. if os.IsPermission(err) {
  30. return fmt.Errorf("permission denied accessing directory: %s", dirPath)
  31. }
  32. return fmt.Errorf("error accessing directory: %v", err)
  33. }
  34. if !info.IsDir() {
  35. return fmt.Errorf("%s is not a directory", dirPath)
  36. }
  37. // Get the directory where the script is located
  38. _, scriptPath, _, ok := runtime.Caller(0)
  39. if !ok {
  40. return fmt.Errorf("could not determine script location")
  41. }
  42. scriptDir := filepath.Dir(scriptPath)
  43. // Create out directory relative to the script location
  44. outDir := filepath.Join(scriptDir, "out")
  45. if err := os.MkdirAll(outDir, 0755); err != nil {
  46. return fmt.Errorf("failed to create output directory: %v", err)
  47. }
  48. // Create output file in the out directory
  49. outFile := filepath.Join(outDir, "model.fp16")
  50. fmt.Printf("writing output to: %s\n", outFile)
  51. t, err := os.Create(outFile)
  52. if err != nil {
  53. return fmt.Errorf("failed to create output file: %v", err)
  54. }
  55. defer t.Close()
  56. // Use standard os.DirFS to read from directory
  57. if err := convert.ConvertModel(os.DirFS(dirPath), t); err != nil {
  58. // Clean up the output file if conversion fails
  59. os.Remove(outFile)
  60. return fmt.Errorf("model conversion failed: %v", err)
  61. }
  62. return nil
  63. }