modelpath.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package server
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/url"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. )
  11. type ModelPath struct {
  12. ProtocolScheme string
  13. Registry string
  14. Namespace string
  15. Repository string
  16. Tag string
  17. }
  18. const (
  19. DefaultRegistry = "registry.ollama.ai"
  20. DefaultNamespace = "library"
  21. DefaultTag = "latest"
  22. DefaultProtocolScheme = "https"
  23. )
  24. var (
  25. ErrInvalidImageFormat = errors.New("invalid image format")
  26. ErrInvalidProtocol = errors.New("invalid protocol scheme")
  27. ErrInsecureProtocol = errors.New("insecure protocol http")
  28. )
  29. func ParseModelPath(name string) ModelPath {
  30. mp := ModelPath{
  31. ProtocolScheme: DefaultProtocolScheme,
  32. Registry: DefaultRegistry,
  33. Namespace: DefaultNamespace,
  34. Repository: "",
  35. Tag: DefaultTag,
  36. }
  37. before, after, found := strings.Cut(name, "://")
  38. if found {
  39. mp.ProtocolScheme = before
  40. name = after
  41. }
  42. name = strings.ReplaceAll(name, string(os.PathSeparator), "/")
  43. parts := strings.Split(name, "/")
  44. switch len(parts) {
  45. case 3:
  46. mp.Registry = parts[0]
  47. mp.Namespace = parts[1]
  48. mp.Repository = parts[2]
  49. case 2:
  50. mp.Namespace = parts[0]
  51. mp.Repository = parts[1]
  52. case 1:
  53. mp.Repository = parts[0]
  54. }
  55. if repo, tag, found := strings.Cut(mp.Repository, ":"); found {
  56. mp.Repository = repo
  57. mp.Tag = tag
  58. }
  59. return mp
  60. }
  61. var errModelPathInvalid = errors.New("invalid model path")
  62. func (mp ModelPath) Validate() error {
  63. if mp.Repository == "" {
  64. return fmt.Errorf("%w: model repository name is required", errModelPathInvalid)
  65. }
  66. if strings.Contains(mp.Tag, ":") {
  67. return fmt.Errorf("%w: ':' (colon) is not allowed in tag names", errModelPathInvalid)
  68. }
  69. return nil
  70. }
  71. func (mp ModelPath) GetNamespaceRepository() string {
  72. return fmt.Sprintf("%s/%s", mp.Namespace, mp.Repository)
  73. }
  74. func (mp ModelPath) GetFullTagname() string {
  75. return fmt.Sprintf("%s/%s/%s:%s", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
  76. }
  77. func (mp ModelPath) GetShortTagname() string {
  78. if mp.Registry == DefaultRegistry {
  79. if mp.Namespace == DefaultNamespace {
  80. return fmt.Sprintf("%s:%s", mp.Repository, mp.Tag)
  81. }
  82. return fmt.Sprintf("%s/%s:%s", mp.Namespace, mp.Repository, mp.Tag)
  83. }
  84. return fmt.Sprintf("%s/%s/%s:%s", mp.Registry, mp.Namespace, mp.Repository, mp.Tag)
  85. }
  86. // modelsDir returns the value of the OLLAMA_MODELS environment variable or the user's home directory if OLLAMA_MODELS is not set.
  87. // The models directory is where Ollama stores its model files and manifests.
  88. func modelsDir() (string, error) {
  89. if models, exists := os.LookupEnv("OLLAMA_MODELS"); exists {
  90. return models, nil
  91. }
  92. home, err := os.UserHomeDir()
  93. if err != nil {
  94. return "", err
  95. }
  96. return filepath.Join(home, ".ollama", "models"), nil
  97. }
  98. // 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.
  99. func (mp ModelPath) GetManifestPath() (string, error) {
  100. dir, err := modelsDir()
  101. if err != nil {
  102. return "", err
  103. }
  104. return filepath.Join(dir, "manifests", mp.Registry, mp.Namespace, mp.Repository, mp.Tag), nil
  105. }
  106. func (mp ModelPath) BaseURL() *url.URL {
  107. return &url.URL{
  108. Scheme: mp.ProtocolScheme,
  109. Host: mp.Registry,
  110. }
  111. }
  112. func GetManifestPath() (string, error) {
  113. dir, err := modelsDir()
  114. if err != nil {
  115. return "", err
  116. }
  117. path := filepath.Join(dir, "manifests")
  118. if err := os.MkdirAll(path, 0o755); err != nil {
  119. return "", err
  120. }
  121. return path, nil
  122. }
  123. func GetBlobsPath(digest string) (string, error) {
  124. dir, err := modelsDir()
  125. if err != nil {
  126. return "", err
  127. }
  128. if runtime.GOOS == "windows" {
  129. digest = strings.ReplaceAll(digest, ":", "-")
  130. }
  131. path := filepath.Join(dir, "blobs", digest)
  132. dirPath := filepath.Dir(path)
  133. if digest == "" {
  134. dirPath = path
  135. }
  136. if err := os.MkdirAll(dirPath, 0o755); err != nil {
  137. return "", err
  138. }
  139. return path, nil
  140. }