modelpath.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package server
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/url"
  6. "os"
  7. "path/filepath"
  8. "regexp"
  9. "strings"
  10. "github.com/ollama/ollama/envconfig"
  11. )
  12. type ModelPath struct {
  13. ProtocolScheme string
  14. Registry string
  15. Namespace string
  16. Repository string
  17. Tag string
  18. }
  19. const (
  20. DefaultRegistry = "registry.ollama.ai"
  21. DefaultNamespace = "library"
  22. DefaultTag = "latest"
  23. DefaultProtocolScheme = "https"
  24. )
  25. var (
  26. ErrInvalidImageFormat = errors.New("invalid image format")
  27. ErrInvalidProtocol = errors.New("invalid protocol scheme")
  28. ErrInsecureProtocol = errors.New("insecure protocol http")
  29. ErrInvalidDigestFormat = errors.New("invalid digest format")
  30. )
  31. func ParseModelPath(name string) ModelPath {
  32. mp := ModelPath{
  33. ProtocolScheme: DefaultProtocolScheme,
  34. Registry: DefaultRegistry,
  35. Namespace: DefaultNamespace,
  36. Repository: "",
  37. Tag: DefaultTag,
  38. }
  39. before, after, found := strings.Cut(name, "://")
  40. if found {
  41. mp.ProtocolScheme = before
  42. name = after
  43. }
  44. name = strings.ReplaceAll(name, string(os.PathSeparator), "/")
  45. parts := strings.Split(name, "/")
  46. switch len(parts) {
  47. case 3:
  48. mp.Registry = parts[0]
  49. mp.Namespace = parts[1]
  50. mp.Repository = parts[2]
  51. case 2:
  52. mp.Namespace = parts[0]
  53. mp.Repository = parts[1]
  54. case 1:
  55. mp.Repository = parts[0]
  56. }
  57. if repo, tag, found := strings.Cut(mp.Repository, ":"); found {
  58. mp.Repository = repo
  59. mp.Tag = tag
  60. }
  61. return mp
  62. }
  63. var errModelPathInvalid = errors.New("invalid model path")
  64. func (mp ModelPath) Validate() error {
  65. if mp.Repository == "" {
  66. return fmt.Errorf("%w: model repository name is required", errModelPathInvalid)
  67. }
  68. if strings.Contains(mp.Tag, ":") {
  69. return fmt.Errorf("%w: ':' (colon) is not allowed in tag names", errModelPathInvalid)
  70. }
  71. return nil
  72. }
  73. func (mp ModelPath) GetNamespaceRepository() string {
  74. return fmt.Sprintf("%s/%s", mp.Namespace, mp.Repository)
  75. }
  76. func (mp ModelPath) GetFullTagname() string {
  77. return fmt.Sprintf("%s/%s/%s:%s", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
  78. }
  79. func (mp ModelPath) GetShortTagname() string {
  80. if mp.Registry == DefaultRegistry {
  81. if mp.Namespace == DefaultNamespace {
  82. return fmt.Sprintf("%s:%s", mp.Repository, mp.Tag)
  83. }
  84. return fmt.Sprintf("%s/%s:%s", mp.Namespace, mp.Repository, mp.Tag)
  85. }
  86. return fmt.Sprintf("%s/%s/%s:%s", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
  87. }
  88. // GetManifestPath returns the path to the manifest file for the given model path, it is up to the caller to create the directory if it does not exist.
  89. func (mp ModelPath) GetManifestPath() (string, error) {
  90. return filepath.Join(envconfig.Models(), "manifests", mp.Registry, mp.Namespace, mp.Repository, mp.Tag), nil
  91. }
  92. func (mp ModelPath) BaseURL() *url.URL {
  93. return &url.URL{
  94. Scheme: mp.ProtocolScheme,
  95. Host: mp.Registry,
  96. }
  97. }
  98. func GetManifestPath() (string, error) {
  99. path := filepath.Join(envconfig.Models(), "manifests")
  100. if err := os.MkdirAll(path, 0o755); err != nil {
  101. return "", err
  102. }
  103. return path, nil
  104. }
  105. func GetBlobsPath(digest string) (string, error) {
  106. // only accept actual sha256 digests
  107. pattern := "^sha256[:-][0-9a-fA-F]{64}$"
  108. re := regexp.MustCompile(pattern)
  109. if digest != "" && !re.MatchString(digest) {
  110. return "", ErrInvalidDigestFormat
  111. }
  112. digest = strings.ReplaceAll(digest, ":", "-")
  113. path := filepath.Join(envconfig.Models(), "blobs", digest)
  114. dirPath := filepath.Dir(path)
  115. if digest == "" {
  116. dirPath = path
  117. }
  118. if err := os.MkdirAll(dirPath, 0o755); err != nil {
  119. return "", err
  120. }
  121. return path, nil
  122. }