modelpath.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package server
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "strings"
  9. )
  10. type ModelPath struct {
  11. ProtocolScheme string
  12. Registry string
  13. Namespace string
  14. Repository string
  15. Tag string
  16. }
  17. const (
  18. DefaultRegistry = "registry.ollama.ai"
  19. DefaultNamespace = "library"
  20. DefaultTag = "latest"
  21. DefaultProtocolScheme = "https"
  22. )
  23. var (
  24. ErrInvalidImageFormat = errors.New("invalid image format")
  25. ErrInvalidProtocol = errors.New("invalid protocol scheme")
  26. ErrInsecureProtocol = errors.New("insecure protocol http")
  27. )
  28. func ParseModelPath(name string, allowInsecure bool) (ModelPath, error) {
  29. mp := ModelPath{
  30. ProtocolScheme: DefaultProtocolScheme,
  31. Registry: DefaultRegistry,
  32. Namespace: DefaultNamespace,
  33. Repository: "",
  34. Tag: DefaultTag,
  35. }
  36. protocol, rest, didSplit := strings.Cut(name, "://")
  37. if didSplit {
  38. if protocol == "https" || protocol == "http" && allowInsecure {
  39. mp.ProtocolScheme = protocol
  40. name = rest
  41. } else if protocol == "http" && !allowInsecure {
  42. return ModelPath{}, ErrInsecureProtocol
  43. } else {
  44. return ModelPath{}, ErrInvalidProtocol
  45. }
  46. }
  47. slashParts := strings.Split(name, "/")
  48. switch len(slashParts) {
  49. case 3:
  50. mp.Registry = slashParts[0]
  51. mp.Namespace = slashParts[1]
  52. mp.Repository = slashParts[2]
  53. case 2:
  54. mp.Namespace = slashParts[0]
  55. mp.Repository = slashParts[1]
  56. case 1:
  57. mp.Repository = slashParts[0]
  58. default:
  59. return ModelPath{}, ErrInvalidImageFormat
  60. }
  61. if repo, tag, didSplit := strings.Cut(mp.Repository, ":"); didSplit {
  62. mp.Repository = repo
  63. mp.Tag = tag
  64. }
  65. return mp, nil
  66. }
  67. func (mp ModelPath) GetNamespaceRepository() string {
  68. return fmt.Sprintf("%s/%s", mp.Namespace, mp.Repository)
  69. }
  70. func (mp ModelPath) GetFullTagname() string {
  71. return fmt.Sprintf("%s/%s/%s:%s", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
  72. }
  73. func (mp ModelPath) GetShortTagname() string {
  74. if mp.Registry == DefaultRegistry {
  75. if mp.Namespace == DefaultNamespace {
  76. return fmt.Sprintf("%s:%s", mp.Repository, mp.Tag)
  77. }
  78. return fmt.Sprintf("%s/%s:%s", mp.Namespace, mp.Repository, mp.Tag)
  79. }
  80. return fmt.Sprintf("%s/%s/%s:%s", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
  81. }
  82. func (mp ModelPath) GetManifestPath(createDir bool) (string, error) {
  83. home, err := os.UserHomeDir()
  84. if err != nil {
  85. return "", err
  86. }
  87. path := filepath.Join(home, ".ollama", "models", "manifests", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
  88. if createDir {
  89. if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
  90. return "", err
  91. }
  92. }
  93. return path, nil
  94. }
  95. func GetManifestPath() (string, error) {
  96. home, err := os.UserHomeDir()
  97. if err != nil {
  98. return "", err
  99. }
  100. return filepath.Join(home, ".ollama", "models", "manifests"), nil
  101. }
  102. func GetBlobsPath(digest string) (string, error) {
  103. home, err := os.UserHomeDir()
  104. if err != nil {
  105. return "", err
  106. }
  107. if runtime.GOOS == "windows" {
  108. digest = strings.ReplaceAll(digest, ":", "-")
  109. }
  110. path := filepath.Join(home, ".ollama", "models", "blobs", digest)
  111. if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
  112. return "", err
  113. }
  114. return path, nil
  115. }